Exemplo n.º 1
0
 /**
  * {@inheritDoc}
  * @see CUserIdentity::authenticate()
  */
 public function authenticate()
 {
     $dbconn = Yii::app()->getModule('user')->getDatabaseConnection();
     if ($dbconn === null) {
         return false;
     }
     $users = UserUser::model()->findAllByAttributes(array('hash_email' => sha1($this->username)));
     if ($users === array()) {
         $criteria = new CDbCriteria();
         $criteria->with = array('userProfile' => array('alias' => 'p'));
         $criteria->compare('p.pseudo', $this->username);
         $users = UserUser::model()->findAll($criteria);
     }
     if (count($users) === 0) {
         $this->errorCode = self::ERROR_USERNAME_INVALID;
     } else {
         $this->errorCode = self::ERROR_PASSWORD_INVALID;
     }
     foreach ($users as $user) {
         /* @var $user UserUser */
         $hash = UserRegistrationForm::hash($this->password, $user->hash_salt);
         if (!strcmp($hash, $user->hash_password)) {
             $this->errorCode = self::ERROR_NONE;
             $this->user = $user;
         }
     }
     if ($this->errorCode === self::ERROR_NONE) {
         $profile = $this->user->userProfile;
         $this->id = $this->user->hash_email;
         if ($profile !== null) {
             $this->username = $profile->getName();
         } else {
             $this->username = $this->user->email;
         }
         /* @var $session CHttpSession */
         $session = Yii::app()->session;
         $session->regenerateID();
         $this->setState('ring_value.current', IPluggableAccessRole::RING_AUTHENTICATED);
     }
     return !$this->errorCode;
 }
Exemplo n.º 2
0
 /**
  * Saves the form to a new user.
  * @return boolean
  */
 public function saveToUser()
 {
     if ($this->validate()) {
         $transaction = UserProfile::model()->getDbConnection()->beginTransaction();
         try {
             $profile = new UserProfile();
             $profile->user_status_id = 'ACT';
             $profile->pseudo = $this->pseudo;
             $profile->hash = sha1($this->pseudo);
             $profile->created = date('Y-m-d H:i:s');
             if ($profile->save()) {
                 $user = new UserUser();
                 $user->user_profile_id = $profile->user_profile_id;
                 $user->userProfile = $profile;
                 $user->hash_salt = self::genSalt();
                 $user->hash_password = self::hash($this->password, $user->hash_salt);
                 $user->email = $this->usermail;
                 $user->hash_email = sha1($this->usermail);
                 $user->registered = date('Y-m-d H:i:s');
                 $user->last_login = date('Y-m-d H:i:s');
                 if ($user->save()) {
                     $this->user = $user;
                     $transaction->commit();
                     return true;
                 } else {
                     $transaction->rollback();
                     foreach ($user->getErrors() as $errorlist) {
                         foreach ($errorlist as $error) {
                             $this->addError('pseudo', $error);
                         }
                     }
                     return false;
                 }
             } else {
                 foreach ($profile->getErrors() as $errorlist) {
                     foreach ($errorlist as $error) {
                         $this->addError('pseudo', $error);
                     }
                 }
             }
         } catch (CDbException $e) {
             $this->addError('pseudo', $e->getMessage());
             try {
                 $transaction->rollback();
             } catch (CDbException $e2) {
                 // nothing to do
             }
             return false;
         }
     }
     return false;
 }