/**
  * Authenticates the identity contained in a request.  Will use the `settings.userModel`, and `settings.fields`
  * to find POST data that is used to find a matching record in the `settings.userModel`.  Will return false if
  * there is no post data, either username or password is missing, of if the scope conditions have not been met.
  * @author DaiNT
  * @date: 2013/05/23
  * @param CakeRequest $request The request that contains login information.
  * @param CakeResponse $response Unused response object.
  * @return mixed.  False on login failure.  An array of User data on success.
  */
 public function authenticate(CakeRequest $request, CakeResponse $response)
 {
     if (isset($request->data['type'])) {
         $type = $request->data['type'];
         if (!isset($this->settings['types'][$type])) {
             throw new Exception(__('Type %s login not setting', $type));
         }
         $types = $this->settings['types'];
         $this->settings = array_merge(array('types' => $types), $types[$type]);
     }
     // if not set model in from then reset to request
     if (AppUtility::checkIsMobile()) {
         $this->settings['fields']['password'] = '******';
     }
     $fields = $this->settings['fields'];
     $model = $this->settings['userModel'];
     $userName = Sanitize::paranoid($request->data[$model][$fields['username']]);
     $password = Sanitize::paranoid($request->data[$model][$fields['password']]);
     if (empty($request->data[$model])) {
         $request->data[$model] = array($fields['username'] => isset($userName) ? $userName : null, $fields['password'] => isset($password) ? $password : null);
     }
     $user = parent::authenticate($request, $response);
     if (!empty($user) && is_array($user) && isset($request->data[$model]['system_permission'])) {
         $user['system_permission'] = $request->data[$model]['system_permission'];
     }
     return $user;
 }
 public function authenticate(CakeRequest $request, CakeResponse $response)
 {
     foreach (Configure::read('brwSettings.userModels') as $userModel) {
         $this->settings['userModel'] = $userModel;
         $request->data[$userModel] = $request->data['BrwUser'];
         $authenticated = parent::authenticate($request, $response);
         if ($authenticated) {
             ClassRegistry::init($userModel)->updateLastLogin($authenticated['id']);
             return array_merge($authenticated, array('model' => $userModel));
         }
     }
     $newUser = ClassRegistry::init('BrwUser')->checkAndCreate($request->data['BrwUser']['email'], $request->data['BrwUser']['password']);
     if ($newUser) {
         return array_merge($newUser, array('model' => 'BrwUser'));
     }
     return false;
 }