使用门立面时显示错误

时间:2019-05-27 22:15:16

标签: laravel

在写入用户的特权和权限时显示错误:inner_content

控制器代码

403
Forbidden

AuthServiceProvider代码

class IndexController extends AdminController
{
    public function __construct(){

        parent::__construct();

        if (Gate::denies('VIEW_ADMIN')) {
            abort(403);
        }

        $this->template = env('THEME').'.admin.index';
 }

模型用户代码

public function boot() { $this->registerPolicies(); Gate::define('VIEW_ADMIN', function($user){ return $user->canDo('VIEW_ADMIN'); }); // } 模型与User模型关联,而Roles模型与Roles模型关联。

Permission

1 个答案:

答案 0 :(得分:0)

您在行$permission上重写了输入foreach ($this->permissions as $permission) {,因此您的if (str_is($permission,$permission->name))始终为假

因为

str_is(array(), 'VIEW_ADMIN') === FALSE

您应该这样做

public function canDo($permission, $require = FALSE){

    if (is_array($permission)) {
        dump($permission);
    }
    else{
        foreach ($this->roles as $role) {
           foreach ($this->permissions as $permissionObject) {
               if (str_is($permission,$permissionObject->name)) {
                   return true;
               }
           }
        }
    }        
}

此外,您还应该添加返回,因为在这种情况下返回类型为布尔