简体   繁体   中英

Checking admin roles in laravel 5.1

I'm doing my project using Laravel framework. Everything seems to be perfect. However, in my login part, I want to do something like when a user login into the system, it will check the "is_admin" column. If the "is_admin" column is equal to 1, it will go straight to admin page. I've been looking on the Internet but I can't find the answer for this. I also did use Middleware but it didn't work. I don't know why. Can somebody help me with this one. Thanks

Laravel 5.1 allows authorization, you can add something like this in your AuthServiceProvider

public function boot(GateContract $gate){
    $gate->define('admin', function($user){
        return $user->is_admin;
    });
}

Then use the Gate facade in your controllers like

$user=Auth::user();
if(Gate::allows('admin', $user)){
    //user is admin
}

Take a look at this part of Laravel's documentation http://laravel.com/docs/5.1/authorization

You can write this in your AuthController.php

if(Auth::user()->is_admin == 1){
    protected $redirectTo = 'admin';
}else{
    protected $redirectTo = 'member';
}

The avobe code is only for redirection. You can use middleware to protect admin panel from member.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM