Created
April 25, 2017 18:41
-
-
Save lloy0076/dcb0a74034cc59440395f6ff2310d1d6 to your computer and use it in GitHub Desktop.
Backpack Permission Problem?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
/** | |
* 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, | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` | |
<?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