2012-03-27 2 views
0

로그인하지 않은 사용자의 특정보기를 숨기고 특정 사용자 역할이 수정/삭제하도록 허용하고 싶습니다.색인의보기를 제한하고 CakePHP에서 편집을 허용 하시겠습니까?

하지만 Auth-> allow와 isAuthorized를 사용하면 혼란 스럽습니다. . 다음을 단순화 할 수있는 방법이 있습니까?

특정 역할 (코치 및 관리자)이 색인 및보기를보고 다른 사람으로부터 완전히 숨길 수있게하고 싶습니다.

public function beforeFilter() { 
    parent::beforeFilter(); 
    $this->Auth->allow('index', 'view'); 
} 

public function isAuthorized($user) { 
    if (in_array($this->action, array('edit', 'delete'))) { 
     if ($user['id'] != $this->request->params['pass'][0]) { 
      return false; 
     } 
    } 
    return true; 
} 
+0

ACL 아마 길을 가야하는 것입니다 자세한 내용은 http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#using-controllerauthorize를 참조하십시오 http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application /simple-acl-controlled-application.html – nIcO

답변

0
$this->Auth->allow('index', 'view'); 

()는 사람이 케이크를 알려줍니다 로그인 여부에 상관없이 색인 &보기 동작을 볼 수 있습니다.

isAuthorized()에서 테스트를 수행하고 사용자가 작업을 수행 할 수 있는지 여부를 테스트해야합니다. 액션 ($ this-> action)이 현재 사용자에 의해 수행 될 수 있다면 true를 반환하고 그렇지 않으면 false를 반환합니다.

public isAuthorized($user = null) { 

    switch($this->action) { 

    case "index": 
    case "view": 

     if ($user['role'] == 'admin') { 

     return true; 

     } 

     break; 

    case "edit": 
    case "delete": 

     if ($user['id'] == $this->request->params['pass'][0]) { 
     return true; 
     } 

     break; 

    } 

    return false; 

} 

0

당신은보기에 약간의 부분적인 요소를 렌더링하는 세션

에서

현재 사용자를 얻을 수 있습니다 (* .ctp)으로, beforeFilter에

<?php 
$user = $this->session->read('Auth.User') 

if(!$user){ 
    echo $this->element('logmein'); 
}else{ 
    echo $this->element('logmeout') 
?> 
<h2>Here is member section</h2> 
<?php 
//... do some thing for member 
} 
?> 
관련 문제