CakePHP Auth Component Using 2 Tables

后端 未结 1 872
暗喜
暗喜 2020-12-16 20:15

CakePHP Version 1.2.5

I would like a single user to have multiple email addresses.
I would like a single user to have a single password.
I would like users t

相关标签:
1条回答
  • 2020-12-16 20:52

    AuthComponent::identify() takes two parameters, $user and $conditions

    if ($conditions === false) {
            $conditions = null;
    } elseif (is_array($conditions)) {
            $conditions = array_merge((array)$this->userScope, $conditions);
    } else {
            $conditions = $this->userScope;
    }
    

    Looking at the above snippet, if you pass false as the $conditions, the method will execute with no model conditions.

    Also, looking at the rest of the code, if you pass a $user value of type string, it won't execute most of the user-related code until it gets here:

    } elseif (!empty($user) && is_string($user)) {
            $model =& $this->getModel();
            $data = $model->find(array_merge(array($model->escapeField() => $user), $conditions));
    
            if (empty($data) || empty($data[$this->userModel])) {
                    return null;
            }
    }
    

    Here it runs Model::escapeField(), with no parameters, which returns an escaped version of User.id (by default) and maps this field to the string that was passed in. It then merges this with the $conditions array and performs a Model::find().

    It should be safe to say that if the string is the user's ID and there are no conditions it will find the person with that ID every time.

    As such, you should be able to extend AuthComponent to do what you want like so:

    // app/controllers/components/app_auth.php
    <?php
    App::import('Component', 'Auth');
    class AppAuthComponent extends AuthComponent {
    /**
     * Custom user identification
     */
        function identify($user=null, $conditions=null) {
            // get the model AuthComponent is configured to use
            $model =& $this->getModel(); // default is User
            // do a query that will find a User record when given successful login data
            $user = $model->find('first', array('conditions' => array(
                'EmailAddress.' . $this->fields['username'] => $user[$this->userModel][$this->fields['username']],
                'User.' . $this->fields['password'] => $user[$this->userModel][$this->fields['password']],
            ));
            // return null if user invalid
            if (!$user) {
                return null; // this is what AuthComponent::identify would return on failure
            }
            // call original AuthComponent::identify with string for $user and false for $conditions
            return parent::identify($user[$this->userModel][$model->primaryKey], false);
        }
    }
    ?>
    

    You will have to replace all references to Auth with AppAuth in your application unless you follow this handy tip (the approach in the comments is nice).

    0 讨论(0)
提交回复
热议问题