Skip to content

Instantly share code, notes, and snippets.

@lloy0076
Created April 25, 2017 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lloy0076/dcb0a74034cc59440395f6ff2310d1d6 to your computer and use it in GitHub Desktop.
Save lloy0076/dcb0a74034cc59440395f6ff2310d1d6 to your computer and use it in GitHub Desktop.
Backpack Permission Problem?
...
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'role' => \App\Http\Middleware\RoleMiddleware::class,
];
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
use Illuminate\Support\Facades\Log as Log;
class RoleMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $role, $permission)
{
Log::debug("Handling a request.");
if (Auth::guest()) {
return redirect(url(config('backpack.base.route_prefix').'/login'));
}
Log::debug("Role / Permission");
Log::debug($role);
Log::debug($permission);
Log::debug("User");
Log::debug($request->user());
if (! $request->user()->hasRole($role)) {
abort(403);
}
if (! $request->user()->can($permission)) {
abort(403);
}
return $next($request);
}
}
```
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'IndexController@index');
Route::get('/home', '\Backpack\Base\app\Http\Controllers\AdminController@dashboard');
Route::get('/test', 'IndexController@test');
Route::group([
'middleware' => [
'admin',
'role:admin,access_backend',
],
'prefix' => 'admin',
'namespace' => 'Admin',
],
function ()
{
CRUD::resource('city', 'CityCrudController');
});
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment