예제 #1
0
 /**
  * Resets user's password and send it to email
  * @param UserAccount $user
  */
 public function resetPassword(UserAccount $user)
 {
     if ($user->status != UserAccount::STATUS_ACTIVE) {
         if (!$this->allowActivationOnPasswordReset) {
             throw new CException('Can\'t reset password for inactive users.');
         } else {
             $identity = Identity::model()->findByAttributes(array('user_id' => $user->id, 'type' => Identity::TYPE_EMAIL, 'status' => Identity::STATUS_NEED_CONFIRMATION));
             $identity->userIdentityConfirmation->confirm();
         }
     }
     $emailAddr = $user->getActiveEmail();
     $newPassword = $this->randomPassword();
     $user->setPassword($newPassword);
     $user->save(false, array('password'));
     $email = new YiiMailer('resetPassword', $data = array('newPassword' => $newPassword, 'description' => $description = 'Password reset'));
     $email->setSubject($description);
     $email->setTo($emailAddr);
     $email->setFrom(Yii::app()->params['noreplyAddress'], Yii::app()->name, FALSE);
     Yii::log('Sendign reset password mail to ' . $emailAddr);
     if ($email->send()) {
         Yii::log('Ok');
     } else {
         Yii::log('Failed');
         throw new CException('Failed to send the email');
     }
 }
예제 #2
0
 public function run()
 {
     $transaction = Yii::app()->db->beginTransaction();
     try {
         if ($this->data->validate()) {
             $user = new UserAccount();
             $user->setPassword($this->data->password);
             $user->status = UserAccount::STATUS_NEED_ACTIVATION;
             if (!$user->save()) {
                 throw new Exception("User registration failed. Cant save User row. Errors: " . var_export($user->getErrors(), true));
             }
             $identity = new Identity();
             $identity->user_id = $user->id;
             $identity->type = Identity::TYPE_EMAIL;
             $identity->status = Identity::STATUS_NEED_CONFIRMATION;
             $identity->identity = $this->data->email;
             if (!$identity->save()) {
                 throw new Exception("User registration failed. Can't save Identity. Errors: " . var_export($identity->getErrors(), true));
             }
             $profile = new Profile();
             $attributeNames = $profile->attributeNames();
             $attributes = $this->data->getAttributes($attributeNames);
             $profile->setAttributes($attributes, false);
             $profile->user_id = $user->id;
             if (!$profile->save()) {
                 throw new Exception("User registration failed. Can't save Profile. Errors: " . var_export($profile->getErrors(), true));
             }
             $this->afterRecordsCreated($user, $profile, $identity);
         } else {
             throw new Exception("Invalid registration data. Errors: " . var_export($this->data->getErrors(), true));
         }
     } catch (Exception $exc) {
         $transaction->rollback();
         throw $exc;
     }
     $transaction->commit();
     return true;
 }