mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-15 05:00:49 +08:00
prepare for beta8
This commit is contained in:
@@ -18,14 +18,13 @@ class Permission
|
||||
*/
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $request->user();
|
||||
$targetClass = User::CLASS_MODERATOR;
|
||||
$log = sprintf('user: %s, class: %s, target class: %s', $user->id, $user->class, $targetClass);
|
||||
if (!$user || $user->class < $targetClass) {
|
||||
do_log("$log, denied!");
|
||||
if (!$user || !$user->canAccessAdmin()) {
|
||||
do_log("denied!");
|
||||
throw new UnauthorizedException('Unauthorized!');
|
||||
}
|
||||
do_log("$log, allow!");
|
||||
do_log("allow!");
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -247,4 +247,14 @@ class User extends Authenticatable
|
||||
return $this->update($update);
|
||||
}
|
||||
|
||||
public function canAccessAdmin()
|
||||
{
|
||||
$targetClass = self::CLASS_MODERATOR;
|
||||
if (!$this->class || $this->class < $targetClass) {
|
||||
do_log(sprintf('user: %s, no class or class < %s, can not access admin.', $this->id, $targetClass));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Repositories;
|
||||
use App\Http\Resources\UserResource;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\UnauthorizedException;
|
||||
|
||||
class AuthenticateRepository extends BaseRepository
|
||||
{
|
||||
@@ -11,10 +12,13 @@ class AuthenticateRepository extends BaseRepository
|
||||
{
|
||||
$user = User::query()
|
||||
->where('username', $username)
|
||||
->first(array_merge(User::$commonFields, ['secret', 'passhash']));
|
||||
->first(array_merge(User::$commonFields, ['class', 'secret', 'passhash']));
|
||||
if (!$user || md5($user->secret . $password . $user->secret) != $user->passhash) {
|
||||
throw new \InvalidArgumentException('Username or password invalid.');
|
||||
}
|
||||
if (!$user->canAccessAdmin()) {
|
||||
throw new UnauthorizedException('Unauthorized!');
|
||||
}
|
||||
$tokenName = __METHOD__ . __LINE__;
|
||||
$token = DB::transaction(function () use ($user, $tokenName) {
|
||||
$user->tokens()->delete();
|
||||
|
||||
Reference in New Issue
Block a user