2017-01-11 10 views
0

私は4つの異なるロールを持っています。これはUsersテーブルでは属性 "role_id"の外部キーです。管理者のrole_idは1です。ユーザーのすべてのユーザーをブロックしようとしていますが、ユーザーのインデックスページなどの管理ページにはアクセスできません。CakePHP 3 - 番号付きロールでの認証

次のように私のAppControllerがある:

がUserControllerで次に
class AppController extends Controller 
{ 

public function initialize() 
    { 
     parent::initialize(); 

     $this->loadComponent('RequestHandler'); 
     $this->loadComponent('Flash'); 
     $this->loadComponent('Auth',[ 
      'authorize' => 'Controller', 
     ]); 

     $this->Auth->allow(['display', 'index', 'view', 'add']); 
    } 

public function isAuthorized($user) 
    { 
     // Default deny 
     return false; 
    } 
} 

class UsersController extends AppController 
{ 

public function initialize() 
    { 
     parent::initialize(); 
     // Add logout to the allowed actions list. 
     $this->Auth->deny(['index', 'add', 'view']); 
     $this->Auth->allow(['register', 'forgetpw', 'resetpw', 'logout']); 
    } 

public function isAuthorized($user) 
    {  
     if (in_array($this->request->action,['view', 'edit', 'index', 'add'])) { 
      return (bool)($user['role_id'] === '1'); 
     } 

     return parent::isAuthorized($user); 
    } 
} 

に述べたようにすべてのユーザーが '登録'、 'forgetpw'、 'PWリセット' のビューにアクセスすることができますUsersControllerのinitialize関数で許可することができます。現在、ユーザーは管理者がアクセス可能な 'index'、 'add'、 'view'または 'edit'にアクセスすることはできません。

私は、UserControllerページの権限を修正することができれば、これを他のすべてのコントローラに適用できると考えています。

答えて

0

私はこの問題を解決したと思います。

return (bool)($user['role_id'] === '1'); 

return (bool)($user['role_id'] === 1); 
されている必要があります
関連する問題