2017-08-06 5 views
0

ユーザテーブルcakephp 3でユーザとロールテーブルに参加するユーザロールの賢明なアクセス制御を作成する方法は?

enter image description here

ロールテーブル

enter image description here

私はちょうどロールテーブルへのアクセス制御を許可するように設定します。ctrl_view = 1は、この役割は任意のコントローラのビューを表示することができます意味。

異なる役割で異なる行動を設定するにはどうすればよいですか?

答えて

0

conventionsに従うと、user_role_idにはrole_id、role_idには "id"、user_nameには "username"を指定してください。Auth configuration接続フォームのデフォルトフィールド名を変更してください。

public function initialize() 
    { 
//... 
    $this->loadComponent('Auth', [ 
       'loginRedirect' => [ 
       'controller' => 'Pages', 
       'action' => 'welcome', 
       'prefix' => 'admin' 
       ], 
       'logoutRedirect' => [ 
       'controller' => 'Users', 
       'action' => 'login', 
       'prefix' => false 
       ], 
       'authError' => 'Unauthorized access...', 
       'authenticate' => [ 
       'Form' => [ 
        'fields' => ['username' => 'user_name', 'password' => 'password'] 
       ] 
       ], 
       'authorize' => 'Controller', 
       'unauthorizedRedirect' => [ 
        'controller' => 'Pages', 
        'action' => 'unauthorized' 
       ], 
      ]); 
// ... 
} 

とあなたのAppController内部

このような
public function isAuthorized($user) 
     { 

      if(!is_null($this->Auth->user())): // if user is logged 

      $action = $this->request->getParam('action'); // get name action 

      $this->loadModel('Roles'); // load your model Roles 
      $query = $this->Authorizations->find() // find inside Roles 
      ->where([ 
      'Roles.role_id IN' => $user['user_role_id'], // where role_id is like user_role_id of current user 
      'Roles.ctl_'.$action => 1 // and where ctl_[action] is set to 1 
      ])->toArray(); 

      if (!empty($query)): // if we find an occurence, we allow the action 
       return true; 
      else: // else we don't authorize 
       return false, 
      endif; 

      /* previous lines can be change with this ----> return (!empty($query)); */ 
      else: // if user is not connected we don't allow action 
      return false 
      endif; 
    } 

をsomtehing作成し、完了するために、私は私が許可していないプレフィックスがする(プレフィックスでuはあなたの承認プロセスを簡素化することができ、それは「接頭辞」を使用する方が良いと思います、私は私の役割の表をご確認のプレフィックス)と、これのためにあなたは、単にあなたのisAuthorized機能のbegininにこれらの行を追加する必要があります。

if (!$this->request->getParam('prefix')) { 
    return true; 
} 

は、それが

0123を役に立てば幸い
関連する問題