Cakephp Auth with multiple “Users” tables

后端 未结 4 1262
一生所求
一生所求 2020-12-02 12:47

I would like to know how to deal with only ONE authentification process and \"users\" in multiple tables. I have 4 Users table: users, admins, artists, teamadmins which all

相关标签:
4条回答
  • 2020-12-02 13:09

    this is also a possibility

    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->authenticate = array(
            AuthComponent::ALL => array('userModel' => 'AnotherModel'),
            'Form',
            'Basic'
        );
    }
    
    0 讨论(0)
  • 2020-12-02 13:14

    While annoying, I think the best solution is probably using Cake's built in ACL support (see http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html).

    If you do authentication the way you're talking about, you have to keep track of permissions in your controller code, checking to see what the userModel is. If you use an access control list, the permission tree will already exist in the database, which should simplify your code a great deal, and make it more modular.

    It also means restructuring your data model to have a single users table and groups table instead of entity classes for each type of user.

    I just went through the process of doing this myself... :(

    0 讨论(0)
  • 2020-12-02 13:17

    CakePHP's AuthComponent only supports authentication against a single "User" model at a time. The model is chosen by setting the Auth::userModel property, but it only accepts a string and not an array of models.

    You can switch the userModel on the fly with the following code, but this requires you to know in advance which model to switch to (eg. your users have to choose their account type from a dropdown):

    public function beforeFilter() {
        if (isset($this->data['User']['model'])) {
            $this->Auth->userModel = $this->data['User']['model'];
        }
    }
    

    You can likely extend the core AuthComponent to add the functionality you want by overwriting the AuthComponent::identify() method so it loops over and attempts authentication with each model:

    App::import('Component', 'AuthComponent');
    class AppAuthComponent extends AuthComponent {
    
        function identify($user = null, $conditions = null) {
            $models = array('User', 'Admin', 'Artist', 'TeamAdmin');
            foreach ($models as $model) {
                $this->userModel = $model; // switch model
                $result = parent::identify($user, $conditions); // let cake do it's thing
                if ($result) {
                    return $result; // login success
                }
            }
            return null; // login failure
        }
    }
    

    You will have to replace occurrences of Auth in your application with AppAuth to use your extended AuthComponent, unless you use this trick.

    0 讨论(0)
  • 2020-12-02 13:19

    Here is the final solution as suggested by deizel and modified by Nicolas:

    App::import('Component', 'Auth');
    class SiteAuthComponent extends AuthComponent {
    
        function identify($user = null, $conditions = null) {
            $models = array('User', 'Admin', 'Artist');
            foreach ($models as $model) {
                $this->userModel = $model; // switch model
                $this->params["data"][$model] = $this->params["data"]["User"]; // switch model in params/data too
                $result = parent::identify($this->params["data"][$model], $conditions); // let cake do its thing
                if ($result) {
                    return $result; // login success
                }
            }
            return null; // login failure
        }
    }
    
    0 讨论(0)
提交回复
热议问题