views:

379

answers:

1

Hi,
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 have specific fields, but I would like all of these users to be able to connect via only one form on the homepage, and being redirected after that to their specific dashboards.

I think the redirections shouldn't be a problem, and some routes added should work, but I really don't know where to look/start to ake this all possible.

Cheers,
Nicolas.

EDIT: here's the final solution (thanks to deizel)

App::import('Component', 'Auth');
class FacebrushAuthComponent 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
    }
}
+5  A: 

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.

deizel
Thank you for your detailed answer. I'll come back to you and validate your answer as soon as it is working(if not I'll post comments, but it looks promising). Cheers.
Nicolas
So I choose the second solution, and it works perfectly with your code and a line I had to add : `$this->params["data"][$model] = $this->params["data"]["User"];` . Thanks!
Nicolas
Nice one. Glad I could help.
deizel