public function actionConfirm($code) { Yii::info('User is entering the confirmation page', __CLASS__); $token = Token::findByCode($code, Token::TYPE_CONFIRMATION); if ($token->user->confirm($token)) { Yii::info("User [{$token->user->email}] successfuly confirmed", __CLASS__); Yii::$app->session->setFlash('success', Yii::t(Module::I18N_CATEGORY, 'Your account was successfuly confirmed!')); } else { Yii::error("Error while confirming user [{$token->user->email}]", __CLASS__); Yii::$app->session->setFlash('warning', Yii::t(Module::I18N_CATEGORY, 'Error while confirming your account!')); } return $this->render($this->module->confirmView); }
public function testResetPassword(FunctionalTester $I) { $user = Commons::createUnconfirmedUser(); $token = Commons::createTokenForUser($user->id); $I->amGoingTo('test that the reset password functionality is working properly'); $I->amGoingTo('to confirm the email for the user'); PasswordResetPage::openBy($I, ['code' => $token->code]); $I->expectTo('see successful reset'); $I->dontSeeRecord(Token::className(), ['user_id' => $user->id]); $I->expectTo('see the user is sent to the change password form'); $I->see('Change password'); $I->seeElement('#changepasswordform-newpassword'); $I->seeElement('#changepasswordform-newpasswordrepeat'); }
/** * Confirms the account for the user. * * @param type $email The email of the user that is to be confirmed. */ public function actionConfirm($email = null) { if (($email = $this->promptEmail($email, $model)) == null) { return; } try { $token = Token::findByUserEmail($email, Token::TYPE_CONFIRMATION); if ($token->user->confirm($token)) { $this->stdout(Yii::t(Module::I18N_CATEGORY, 'The user is successfuly confirmed!'), Console::FG_GREEN); } else { $this->stderr(Yii::t(Module::I18N_CATEGORY, 'Error while trying to confirm the user!'), Console::FG_RED); } } catch (NotFoundHttpException $ex) { $this->stdout($ex->getMessage(), Console::FG_RED); } }
public static function createTokenForUser($userId, $type = Token::TYPE_RECOVERY) { $token = Yii::createObject(['class' => Token::className(), 'user_id' => $userId, 'type' => $type]); $token->save(false); return $token; }
public function down() { $this->alterColumn(Token::tableName(), 'expires_on', $this->integer()->notNull()); }
public function resetPassword($tokenCode, $newPassword) { Yii::info("Fetching token", __CLASS__); $token = Token::findByCode($tokenCode); Yii::info("Setting new password", __CLASS__); $token->user->setPassword($newPassword); Yii::info("Trying to save user [{$token->user->email}] after password change", __CLASS__); if ($token->user->save(false) && $token->delete()) { Yii::info("Password of user [{$token->user->email}] successfuly changed", __CLASS__); } Yii::info("Logging in user [{$token->user->email}] after a password change", __CLASS__); return Yii::$app->user->login($token->user); }
public function getTokens() { return $this->hasMany(Token::className(), ['user_id' => 'id']); }
/** * Finds a token with user by the user's email. * * @param string $email The user's email * @param integer $type The token's type. By default Token::TYPE_CONFIRMATION * @return Token The token if found * @throws NotFoundHttpException If the token is not found */ public static function findByUserEmail($email, $type = self::TYPE_CONFIRMATION) { $token = Token::find()->select('*')->leftJoin(User::tableName(), 'user.id = token.user_id')->where(['user.email' => $email, 'user.status' => User::STATUS_ACTIVE, 'type' => $type])->one(); if (empty($token)) { throw new NotFoundHttpException(Yii::t(Module::I18N_CATEGORY, 'Token not found!')); } return $token; }