2011-12-20 2 views
5

나는 Yii를 배우며 RBAC를 개발하려고합니다. 이제는 역할을 만들었고, 쉘을 통해 스크립트를 실행했는데 데이터베이스 테이블이 제 위치에 있고 그 역할과 모든 것이 채워집니다. 지금은 내가 너무에 대한 관리자 또는 리더 또는 관리자와는 즉 사용자 유형에 따라 다른 메뉴를 표시하면된다하려고 180cm 정도지만Yii가 유효한 checkAccess 결과를 얻지 못했습니다.

if(Yii::app()->user->checkAccess('admin')) 
     echo 'Admin'; 
else 
    echo 'No Admin'; 

항상 없음 관리자 돌려주지 않는 이유를 알고이 donot. 그러나 이것은 실패합니다.

나는 여기 내 역할 할당을 부착하고

<?php 
class RbacCommand extends CConsoleCommand 
{ 
    private $_authManager; 

    public function getHelp() 
    {return <<<EOD 
     USAGE 
      rbac 
      DESCRIPTION 
      This command generates an initial RBAC authorization hierarchy. 
EOD; 
    } 

    /** 
    * Execute the action. 
    * @param array command line parameters specific for this command 
    */ 
    public function run($args) 
    { 
     echo "SHELLLLLLLLLL.\n"; 
     //ensure that an authManager is defined as this is mandatory for creating an auth heirarchy 
     if(($this->_authManager=Yii::app()->authManager)===null) 
     { 
      echo "Error: an authorization manager, named 'authManager' 
must be configured to use this command.\n"; 
      echo "If you already added 'authManager' component in 
application configuration,\n"; 
      echo "please quit and re-enter the yiic shell.\n"; 
      return; 
     }   
//provide the oportunity for the use to abort the request 
     echo "This command will create three roles: Admin, Manager, and Reader and the following premissions:\n"; 
     echo "create, read, update and delete Hotels\n"; 
     echo "create, read, update and delete Items\n"; 
     echo "create, read, update and delete Users\n"; 
     echo "create, read, update and delete Category\n"; 
     echo "Would you like to continue? [Yes|No] "; 

//check the input from the user and continue if they indicated yes to the above question 
     if(!strncasecmp(trim(fgets(STDIN)),'y',1)) 
     { 
      //first we need to remove all operations, roles, child relationship and assignments 
      $this->_authManager->clearAll(); 
      //create the lowest level operations for users 
      $this->_authManager->createOperation("createUser","create a new user"); 
      $this->_authManager->createOperation("readUser","read user profile information"); 
      $this->_authManager->createOperation("updateUser","update a users information"); 
      $this->_authManager->createOperation("deleteUser","remove a user from a Hotel"); 
      ////create the lowest level operations for projects 
      $this->_authManager->createOperation("createHotel","create a new Hotel"); 
      $this->_authManager->createOperation("readHotel","read Hotel information"); 
       $this->_authManager->createOperation("updateHotel","update Hotel information"); 
      $this->_authManager->createOperation("deleteHotel","delete a Hotel"); 
      ////create the lowest level operations for Category 
      $this->_authManager->createOperation("createCategory","create a new Item"); 
      $this->_authManager->createOperation("readCategory","read Item information"); 
      $this->_authManager->createOperation("updateCategory","update Item information"); 
      $this->_authManager->createOperation("deleteCategory","delete an Item from a Hotel");  
      ////create the lowest level operations for issues 
      $this->_authManager->createOperation("createItem","create a new Item"); 
      $this->_authManager->createOperation("readItem","read Item information"); 
      $this->_authManager->createOperation("updateItem","update Item information"); 
      $this->_authManager->createOperation("deleteItem","delete an Item from a Category");  
      ////create the reader role and add the appropriate permissions as children to this role 
      $role=$this->_authManager->createRole("reader"); 
      $role->addChild("readUser"); 
      $role->addChild("readHotel"); 
      $role->addChild("readCategory"); 
      $role->addChild("readItem"); 
      $role->addChild("createUser"); 

      ////create the member role, and add the appropriate permissions, as well as the reader role itself, as children 
      $role=$this->_authManager->createRole("manager"); 
      $role->addChild("readUser"); 
      $role->addChild("readHotel"); 
      $role->addChild("readCategory"); 
      $role->addChild("readItem"); 

      $role->addChild("createHotel"); 
      $role->addChild("createCategory"); 
      $role->addChild("createItem"); 

      $role->addChild("updateHotel"); 
      $role->addChild("updateCategory"); 
      $role->addChild("updateItem"); 

      $role->addChild("deleteHotel"); 
      $role->addChild("deleteCategory"); 
      $role->addChild("deleteItem"); 
      ////create the owner role, and add the appropriate permissions, as well as both the reader and member roles as children 
      $role=$this->_authManager->createRole("admin"); 
      $role->addChild("reader"); 
      $role->addChild("manager");  
      $role->addChild("createUser"); 
      $role->addChild("updateUser"); 
      $role->addChild("deleteUser"); 


      echo 'Making Afnan admin'; 
      $this->_authManager->assign('admin','3'); 
      echo 'Making Riaz Manager';    
      $this->_authManager->assign('manager','2'); 
      echo 'Sucess'; 
      //provide a message indicating success 
      echo "Authorization hierarchy successfully generated."; 
     } 
    } 
} 
?> 
+0

관계()에 대해 아무 것도해야합니까? – Symfony

답변

7

때문에 사용자 ID를 기반으로 당신이 당신의 사용자 ID에 getId() 함수가이 donot이라면 그냥 자리에 이름을 반환합니다 체크 액세스 방법을 확인 이드와 그게 거짓을 보내는 이유가 될 것입니다.

1

RBAC 시스템을 구현하려고했지만 모든 것이 작동하는 것처럼 보였습니다.하지만 문제는 그 checkAccess가 작동하지 않았기 때문입니다. 그런 다음 내 UserIdentity 클래스를 다음과 같이 변경하여 작동하기 시작했습니다.

class UserIdentity extends CUserIdentity 
{ 
    private $_id; 

    public function authenticate() 
    { 
     $record=User::model()->findByAttributes(array('username'=>$this->username)); 
     if($record===null) 
      $this->errorCode=self::ERROR_USERNAME_INVALID; 
     else if($record->password!==md5($this->password)) 
      $this->errorCode=self::ERROR_PASSWORD_INVALID; 
     else 
     { 
      $this->_id=$record->id; 
      $this->setState('title', $record->username); 
      $this->errorCode=self::ERROR_NONE; 
     } 
     return !$this->errorCode; 
    } 

    public function getId() 
    { 
     return $this->_id; 
    } 
} 
관련 문제