开发者

CakePHP Auth ACL

I started with CakePHP and just want to work with Access Control List (ACL)

I found an tutorial, but I can't work with this.

I would like two groups with separate permissions.

Admins can register new users. Users can only login and see their profiles.

I think it's really simple, but I can not get the logic..

I hope that you can help me out, it's really frustrating .. :P

Database users id - primary username - unique password group_id groups id - primary name - unique

Model class USER

<?php  
class User extends AppModel { 
    var $name = 'User'; 
    var $displayField = 'username'; 
    var $belongsTo = array( 
        'Group' => array( 
            'className' => 'Group', 
            'foreignKey' => 'group_id' 
        ) 
    ); 
} 
?>

Model class GROUP

<?php  
class Group extends AppModel { 
    var $name = 'Group'; 
    var $displayField = 'name'; 

    var $hasMany = array( 
        'User' => array( 
            'className' => 'User', 
            'foreignKey' => 'group_id', 
            'dependent' => false 
        ) 
    ); 

} 
?>

Controller class AppController

<?php  
class AppController extends Controller { 

    var $components = array('Auth', 'RequestHandler');  
    var $permissions = array(); 

    function beforeFilter() { 
        $this->Auth->fields  = array( 
            'username'=>'username', //The field the user logs in with (eg. username) 
            'password' =>'password' //The password field 
        ); 
        $this->Auth->authorize = 'controller'; 
        $this->Auth->autoRedirect = false; 
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login'); 
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login'); 
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'welcome'); 

    } 

    function isAuthorized(){ 
        if($this->Auth->user('group') == 'admin') return true; //Remove this line if you don't want admins to have access to everything by default
        if(!empty($this->permissions[$this->action])){ 
            if($this->permissions[$this->action] == '*') return true; 
            if(in_array($this->Auth->user('gro开发者_JS百科up'), $this->permissions[$this->action])) return true; 
        } 
        return false; 

    } 

} 
?>

Controller class UsersController

<?php  
class UsersController extends AppController { 

    var $name = 'Users'; 
    var $helpers = array('Html', 'Form'); 
    var $permissions = array( 
        'logout' => '*', 
        'welcome' => '*' 
    ); 

    function welcome(){ 
    } 

    function login(){ 
        if($this->Auth->user()){ 
            $this->Session->write('Auth.User.group', $this->User->Group->field('name',array('id' => $this->Auth->user('group_id')))); 
            $this->redirect($this->Auth->redirect()); 
        } 
    } 

    function logout(){ 
        $this->redirect($this->Auth->logout()); 
    } 

    function registerusers(){
   //  Only for administrators.. how can I set this permission?
    }

    // Add whatever user logic methods you'd like here as well (eg. add/edit/delete users) 
?>


anddevelop,

from a quick review, I would tend to say you mixed things up, e.g. the $actsAs is missing, and some other stuff added. You should go over the official tutorial here as this delivers, what you are asking for.

Words of encouragement: If you step over the tutorial carefully, you will succeed. Just try it from a fresh CakePHP environment.


Did you create the ACL tables (aros and acos) as per the instructions in the CakePHP Manual ?

If you follow that tutorial you'll see what you need to set up the right structure to determine which action can be executed by which users (or groups).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜