2012-03-14 3 views
2

역할 클래스 모델 패턴을 내 웹 사이트 사용자 접근 메커니즘 (PHP로 작성)에 구현하려고했습니다. 나는 약간의 의혹이있다. 나는 약간 여기 약점 참조역할 클래스 모델 올바른 구현

class User 
{ 
     public $role; 
     public $uid;   
     public function setRole($role) 
     { 
      $this->role = $role; 
     } 
} 
// role classes responsible for restricted actions 
class BaseRole {} 
class AdminRole extends BaseRole 
{ 
     // do something adminish + log action into database (with User ID) 
     public function SomethingAdminish($admin_id) { } 
} 
$user = new User(); 
$user->setRole(new AdminRole()); 

// pass Admin ID (User ID) into method 
$user->rola->SomethingAdminish($user->uid); 

: 여기에 관련 코드의 단순화 된 버전입니다 다른 전달

  1. 은 $ 사용자 -> "SomethingAdminish"방법에 uid를 내 로그에 로그 잘못된 정보는 것입니다 나는 위의 방법으로 다른 사용자의 정보를 기록하기로 결정하는 경우 시스템 (잘못된 사용자 ID)
  2. , 는 기본적으로 나는과 같이, 인자로 를 전체 사용자 개체를 전달해야합니다 :

    $ user-> rola-> SomethingAdminish ($ user);

여기에는 필수적인 것이 빠져 있습니다. 주제에 대해 좀 밝혀 주시겠습니까?

답변

0

개인적으로 ACL (Access Control List) 패턴을 설정하고 사용합니다.

는 "자원은 제어 액세스되는 객체입니다.

역할이 자원에 대한 액세스를 요청할 수있는 객체이다. 간단히 말하면

, 역할이 자원에 대한 액세스를 요청합니다. 예를 들어 주차 담당자가 자동차에 대한 액세스를 요청하면 차량에 대한 액세스 권한이 모든 사람에게 부여되지 않기 때문에 주차 담당자가 요청한 역할이며 자동차가 리소스입니다. "

다음은 ACL 흐름이 어떻게 표시되는지에 대한 기본 예입니다 (위의 코드 사용). 여기

// Create an ACL object to store roles and resources. The ACL also grants 
// and denys access to resources. 
$acl = new Acl(); 

// Create 2 roles. 
$adminRole = new Acl_Role('admin'); 
$editorRole = new Acl_Role('editor'); 

// Add the Roles to the ACL. 
$acl->addRole($adminRole) 
    ->addRole($editorRole); 

// Create an example Resource. A somethingAdminish() function in this case. 
$exampleResource = new Acl_Resource('somethingAdminish'); 

// Add the Resource to the ACL. 
$acl->add($exampleResource); 

// Define the rules. admins can are allowed access to the somethingAdminish 
// resource, editors are denied access to the somethingAdminish resource. 
$acl->allow('admin', 'somethingAdminish'); 
$acl->deny('editor', 'somethingAdminish'); 

는 사용자 개체가 ACL

// Load the User 
$userID = 7; 
$user = User::load($userID); 

// Set the User's Role. admin in this case. 
$user->setRole($adminRole); 

// Query the ACL to see if this User can access the somethingAdminish resource. 
if ($acl->isAllowed($user, 'somethingAdminish')){ 

    // Call the somethingAdminish function. Eg: 
    somethingAdminish(); 

    // Log the action and pass the User object in so you can take any information 
    // you require from the User data. 
    $acl->logAction('somethingAdminish', $user) 

}else{ 
    die('You dont have permission to perform the somethingAdminish action.') 
} 
와 상호 작용하는 것이 어떻게