mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2025-01-09 04:27:47 +08:00
tweak middlewares
This commit is contained in:
parent
c1923cfaa1
commit
c87d19f258
@ -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,
|
||||
];
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
24
app/Http/Middleware/CheckRole.php
Normal file
24
app/Http/Middleware/CheckRole.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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');
|
||||
});
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -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();
|
Loading…
Reference in New Issue
Block a user