CakePHP 身份验证 ACL

发布于 2024-10-31 03:31:53 字数 3282 浏览 1 评论 0原文

我开始使用 CakePHP,只想使用访问控制列表 (ACL)

我找到了一个教程,但我无法使用它。
我想要两个具有不同权限的组。

管理员可以注册新用户。 用户只能登录并查看他们的个人资料。

我认为这很简单,但我无法理解其中的逻辑..
我希望你能帮助我,这真的很令人沮丧..:P

数据库 用户 id - 主要 用户名 - 唯一 密码 组ID 团体 id - 主要 名称 - 唯一

模型类 USER

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

模型类 GROUP

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

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

} 
?>

控制器类 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('group'), $this->permissions[$this->action])) return true; 
        } 
        return false; 

    } 

} 
?>

控制器类 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) 
?>

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('group'), $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) 
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饭团 2024-11-07 03:31:53

anddevelop,

从快速回顾来看,我倾向于说你把事情搞混了,例如 $actsAs 丢失了,并且添加了一些其他东西。您应该在此处查看官方教程,如下所示提供您所要求的。

鼓励的话:只要你仔细地跨过教程,你就会成功。只需在新的 CakePHP 环境中尝试一下即可。

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.

听风吹 2024-11-07 03:31:53

您是否按照 CakePHP 手册 ?

如果您按照该教程进行操作,您将了解如何设置正确的结构来确定哪些用户(或组)可以执行哪些操作。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文