[英]How can I extend the default Laravel 5 auth class to provide an admin interface which can delete and update users?
我正在構建一個包含應用程序所有元素的管理界面。 我想包括一個允許管理員編輯用戶屬性和刪除用戶的部分。
使用內置的用戶模型和身份驗證中間件,我該如何做到這一點?
如何使用Gate門面?
首先創建一個ManageUsersPolicy類
class ManageUsersPolicy {
use HandlesAuthorization;
public function authorize(User $user) {
// Whatever logic you want to authorize the current user, por example:
return $user->hasRole('Admin');
}
}
然后在您的AuthServiceProvider
類中注冊策略
public function boot(GateContract $gate) {
$gate->define('manage-users', 'App\Policies\ManageUsersPolicy@authorize');
}
然后在您的控制器中可以調用:
if(Gate::denies('manage-users') {
abort(403, 'You are nor authorized.');
} else {
// This user is authorized to manage other users.
}
查看有關政策的文檔: https : //laravel.com/docs/5.2/authorization#policies
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.