示例#1
0
 public function activateUser($token)
 {
     $transaction = Yii::app()->db->beginTransaction();
     try {
         $tokenModel = $this->tokenStorage->get($token, UserToken::TYPE_ACTIVATE);
         if (null === $tokenModel) {
             return false;
         }
         $userModel = User::model()->findByPk($tokenModel->user_id);
         if (null === $userModel) {
             return false;
         }
         $userModel->status = User::STATUS_ACTIVE;
         $userModel->email_confirm = User::EMAIL_CONFIRM_YES;
         if ($this->tokenStorage->activate($tokenModel) && $userModel->save()) {
             // Автоматическая авторизация
             $identity = new UserIdentity($userModel->email, $userModel->hash);
             if ($identity->autoAuthenticate()) {
                 Yii::app()->user->login($identity);
             }
             // Записываем информацию о событии в лог-файл:
             Yii::log(Yii::t('UserModule.user', 'Account with activate_key = {activate_key} was activated!', array('{activate_key}' => $token)), CLogger::LEVEL_INFO, UserModule::$logCategory);
             $transaction->commit();
             return true;
         }
         throw new CException(Yii::t('UserModule.user', 'There was a problem with the activation of the account. Please refer to the site\'s administration.'));
     } catch (Exception $e) {
         $transaction->rollback();
         return false;
     }
 }