Skip to content
This repository was archived by the owner on Nov 23, 2020. It is now read-only.

[Feature][0.9][Merged] Custom admin middleware #258

Merged
merged 9 commits into from
Mar 22, 2018
17 changes: 14 additions & 3 deletions src/BaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function boot(\Illuminate\Routing\Router $router)
__DIR__.'/config/backpack/base.php', 'backpack.base'
);

$this->registerAdminMiddleware($this->app->router);
$this->registerMiddlewareGroup($this->app->router);
$this->setupRoutes($this->app->router);
$this->publishFiles();
$this->loadHelpers();
Expand Down Expand Up @@ -117,9 +117,20 @@ public function register()
$this->commands($this->commands);
}

public function registerAdminMiddleware(Router $router)
public function registerMiddlewareGroup(Router $router)
{
Route::aliasMiddleware('admin', \Backpack\Base\app\Http\Middleware\Admin::class);
$middleware_key = config('backpack.base.middleware_key');
$middleware_class = config('backpack.base.middleware_class');

if (!is_array($middleware_class)) {
$router->pushMiddlewareToGroup($middleware_key, $middleware_class);

return;
}

foreach ($middleware_class as $middleware_class) {
$router->pushMiddlewareToGroup($middleware_key, $middleware_class);
}
}

public function publishFiles()
Expand Down
2 changes: 1 addition & 1 deletion src/app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AdminController extends Controller
*/
public function __construct()
{
$this->middleware('admin');
$this->middleware(backpack_middleware());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/app/Http/Controllers/Auth/MyAccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MyAccountController extends Controller

public function __construct()
{
$this->middleware('admin');
$this->middleware(backpack_middleware());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Closure;
use Illuminate\Support\Facades\Auth;

class Admin
class CheckIfAdmin
{
/**
* Handle an incoming request.
Expand Down
10 changes: 10 additions & 0 deletions src/config/backpack/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@
// Fully qualified namespace of the User model
'user_model_fqn' => '\App\User',

// The classes for the middleware to check if the visitor is an admin
// Can be a single class or an array of clases
'middleware_class' => [
\Backpack\Base\app\Http\Middleware\CheckIfAdmin::class,
],

// Alias for that middleware
'middleware_key' => 'admin',
// Note: It's recommended to use the backpack_middleware() helper everywhere, which pulls this key for you.

// What kind of avatar will you like to show to the user?
// Default: gravatar (automatically use the gravatar for his email)
// Other options:
Expand Down
15 changes: 15 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ function backpack_avatar_url($user)
}
}
}

if (!function_exists('backpack_middleware')) {
/**
* Return the key of the middleware used across Backpack.
* That middleware checks if the visitor is an admin.
*
* @param $path
*
* @return string
*/
function backpack_middleware()
{
return config('backpack.base.middleware_key', 'admin');
}
}