每个用户的多个登录功能或所有用户的一个登录功能


Multiple login functions for each user or one for all

我正在做一个基于CakePHP的项目。

有多个具有不同视图和权限的用户。如何管理他们的登录过程?我应该在每个控制器中实现登录功能吗?还是为所有人编写一个脚本或函数?这是否意味着管理授权过程的所有用户也将有一个控制器?

一次登录。CakePHP具有一些特性,您可以使用这些特性根据登录者的状态更改其外观和可用页面和函数。

  1. 访问控制列表,或ACL,请参阅http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html ACL比其他解决方案更难设置,但如果您有一个复杂的功能列表,用户可能有或没有个人权限,这就是ACL的用途。很多时候这个功能是多余的。

  2. 在用户上有一个对应于用户级别的字段。上帝、管理员、会员等等。然后根据登录的用户切换布局。例如,假设您有一个名为role的字段,可以是"god","admin"或"user",以及与它们对应的布局"god"。ctp"、"管理。ctp"、"用户。然后你可以在AppController的beforefilter中添加:

    public function beforeFilter() 
    {
        if($this->Auth->user())
            $this->layout = $this->Auth->user('role');  
    }
    

然后,简单地使用Auth组件的isAuthorized特性来拒绝或允许不同角色的用户访问。例如,在AppController中:

public function isAuthorized($user) 
{   
    // Admin and god can access every action, including those with the admin prefix
    if (isset($user['role']) && in_array($user['role'], array('admin', 'god'))) 
    { 
        return true; 
    } 
    //allow all logged in users access to items without a prefix  
    if( !isset($this->params['prefix']))  return true;  
    // Default deny -- regular users can't access pages with the admin prefix
    return false; 
}

我建议您仔细阅读Auth组件- http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html