2015-09-20 61 views
2

所以我想了解Laravel 5.1中的用戶授權。Laravel 5.1調用未定義的方法Illuminate Database Query Builder :: isSuperAdmin()

基礎上docs我已經設置了AuthServiceProvider引導方法如下:

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; 
     } 
    }); 
} 

在我的控制,我有:

if (Gate::denies('view-dashboard')) { 
     return view('auth.login'); 
    } 

    return view('admin.home'); 

當我沒有登錄我得到的auth.login視圖。但是,一旦我登錄,我得到了以下錯誤:

BadMethodCallException in Builder.php line 2025: Call to undefined method Illuminate\Database\Query\Builder::isSuperAdmin()

首先,因爲我把這些線直出的文檔的,我不知道我爲什麼會得到這個錯誤。有任何想法嗎?其次,文檔似乎沒有解釋如何去指定給定用戶作爲超級管理員,或者如何給予用戶特定的能力(例如我的示例中的「視圖 - 儀表板」能力)。我該怎麼做呢?

UPDATE:這是我的用戶模型:

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']; 
} 

回答

0

它看起來像超級管理員middlewhere可以不定義。

app/Http/Kernel.php之內您會看到一個名爲routeMiddleware的指令。在數組中添加以下行。

'superadmin' => 'App\Http\Middleware\SuperAdminMiddleware'

您還沒有提供您的User模型。請仔細檢查User模型是否實現了至少以下接口和特性。

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; 
} 
+0

嗯,我的config/app.php中沒有routeMiddleware。我確實在app \ Http \ Kernel.php中找到了$ routeMiddleware。 爲了增加'superadmin'=> \ App \ Http \ Middleware \ SuperAdminMiddleware :: class 但它仍然拋出相同的錯誤 –

+0

更新了我的答案。讓我知道 – verheesj

+0

是的,我的用戶模型實現了所有這些(我已經更新了我的問題以包含用戶模型) –

4

在模型中用戶你沒有定義的方法isSuperAdmin

public function isSuperAdmin() 
{ 
    // your logic here 
    return true; // or false 
} 
4

檢查AuthServiceProvider爲線:

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; 

默認我的文件有:

use Illuminate\Support\ServiceProvider; 

替換後全部工作正常

相關問題