Reputation: 453
So I'm trying to understand user authorization in Laravel 5.1.
Based on the docs I've set up the AuthServiceProvider boot method as follows:
public function boot(GateContract $gate)
{
parent::registerPolicies($gate);
$gate->define('view-dashboard', function ($user, $post) {
return $user->id === $post->user_id;
});
$gate->before(function ($user, $ability) {
if($user->isSuperAdmin()) {
return true;
}
});
}
In my controller I have:
if (Gate::denies('view-dashboard')) {
return view('auth.login');
}
return view('admin.home');
When I'm not logged in I get the auth.login view. However, once I log in I get the following error:
BadMethodCallException in Builder.php line 2025: Call to undefined method Illuminate\Database\Query\Builder::isSuperAdmin()
First, since I took those lines straight out of the docs, I'm not sure why I would get that error. Any ideas?
Second, the docs don't seem to explain how to go about designating a given user as a Super Admin, or how to give a user specific abilities (such as the "view-dashboard" ability in my example). How do I do this?
UPDATE: Here is my user model:
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
}
Upvotes: 2
Views: 5450
Reputation: 113
Check AuthServiceProvider for the line:
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
by default my file have:
use Illuminate\Support\ServiceProvider;
after replace all work fine
Upvotes: 4
Reputation: 51
In your model User you didn't defined the method isSuperAdmin
public function isSuperAdmin()
{
// your logic here
return true; // or false
}
Upvotes: 5
Reputation: 1458
It looks like Super Admin middlewhere may not be defined.
Within app/Http/Kernel.php
you will see a directive named routeMiddleware
. Add the following line within the array.
'superadmin' => 'App\Http\Middleware\SuperAdminMiddleware'
You also didn't supply your User
model. Please can you double check that the User
model implements at least the following interfaces and traits.
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
}
Upvotes: 0