tweak middlewares

This commit is contained in:
Pig Fang 2020-01-13 09:25:07 +08:00
parent c1923cfaa1
commit c87d19f258
7 changed files with 37 additions and 69 deletions

View File

@ -59,13 +59,11 @@ class Kernel extends HttpKernel
'auth' => \App\Http\Middleware\Authenticate::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'verified' => \App\Http\Middleware\CheckUserVerified::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'admin' => \App\Http\Middleware\CheckAdministrator::class,
'super-admin' => \App\Http\Middleware\CheckSuperAdmin::class,
'player' => \App\Http\Middleware\CheckPlayerExist::class,
'role' => \App\Http\Middleware\CheckRole::class,
'setup' => \App\Http\Middleware\CheckInstallation::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \App\Http\Middleware\CheckUserVerified::class,
];
}

View File

@ -1,13 +0,0 @@
<?php
namespace App\Http\Middleware;
class CheckAdministrator
{
public function handle($request, \Closure $next)
{
abort_unless(auth()->user()->isAdmin(), 403, trans('auth.check.admin'));
return $next($request);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class CheckRole
{
protected $roles = [
'banned' => -1,
'normal' => 0,
'admin' => 1,
'super-admin' => 2,
];
public function handle(Request $request, Closure $next, $role)
{
$permission = $request->user()->permission;
abort_if($permission < $this->roles[$role], 403);
return $next($request);
}
}

View File

@ -1,16 +0,0 @@
<?php
namespace App\Http\Middleware;
use App\Models\User;
use Closure;
class CheckSuperAdmin
{
public function handle($request, Closure $next)
{
abort_if(auth()->user()->permission != User::SUPER_ADMIN, 403, trans('auth.check.super-admin'));
return $next($request);
}
}

View File

@ -118,7 +118,7 @@ Route::prefix('skinlib')->name('skinlib.')->group(function () {
Route::prefix('admin')
->name('admin.')
->middleware(['authorize', 'admin'])
->middleware(['authorize', 'role:admin'])
->group(function () {
Route::get('', 'AdminController@index');
Route::get('chart', 'AdminController@chartData');
@ -172,7 +172,7 @@ Route::prefix('admin')
});
});
Route::prefix('update')->middleware('super-admin')->group(function () {
Route::prefix('update')->middleware('role:super-admin')->group(function () {
Route::get('', 'UpdateController@showUpdatePage');
Route::post('download', 'UpdateController@download');
});

View File

@ -1,29 +0,0 @@
<?php
namespace Tests;
use App\Models\User;
class CheckAdministratorTest extends TestCase
{
public function testHandle()
{
// Without logged in
$this->get('/admin')->assertRedirect('/auth/login');
// Normal user
$this->actingAs(factory(User::class)->create())
->get('/admin')
->assertStatus(403);
// Admin
$this->actingAs(factory(User::class, 'admin')->create())
->get('/admin')
->assertSuccessful();
// Super admin
$this->actingAs(factory(User::class, 'superAdmin')->create())
->get('/admin')
->assertSuccessful();
}
}

View File

@ -4,16 +4,20 @@ namespace Tests;
use App\Models\User;
class CheckSuperAdminTest extends TestCase
class CheckRole extends TestCase
{
public function testHandle()
{
// Admin
$this->actAs(factory(User::class, 'admin')->create())
->get('/admin/update')
$this->actAs(factory(User::class)->create())
->get('/admin')
->assertForbidden();
// Super admin
$this->actAs(factory(User::class, 'admin')->create())
->get('/admin')
->assertSuccessful();
$this->get('/admin/update')->assertForbidden();
$this->actAs(factory(User::class, 'superAdmin')->create())
->get('/admin/update')
->assertSuccessful();