/** * Cancel email change */ public function actionChangeCancel() { /** @var User $user */ $user = $this->jwtAuth->getAuthenticatedUser(); $userToken = UserToken::findByUser($user->id, UserToken::TYPE_EMAIL_CHANGE); if ($userToken) { $userToken->delete(); return ["success" => true]; } return ["error" => true]; }
/** * @return \yii\db\ActiveQuery */ public function getUserTokens() { return $this->hasMany(UserToken::className(), ['user_id' => 'id']); }
public static function generateNewTokenObject(\App\Models\User $user, $applicationId = null) { $token = hash('sha256', str_random(10), false); $userToken = \App\Models\UserToken::firstOrNew(['user_id' => $user->id, 'application_id' => $applicationId]); $userToken->user_id = $user->id; $userToken->application_id = $applicationId; $userToken->token = $token; $userToken->save(); return $userToken; }
/** * Reset */ public function actionReset($token) { /** @var User $user */ // get user token and check expiration $userToken = UserToken::findByToken($token, UserToken::TYPE_PASSWORD_RESET); if (!$userToken) { return ["error" => "Invalid token"]; } // get user and load post // return user email if user hasn't submitted yet $user = User::findOne($userToken->user_id); if (!$user->loadPost()) { return ["success" => $user->email]; } // set scenario and save new password $user->setScenario("reset"); if ($user->save(true, ["password", "newPassword", "newPasswordConfirm"])) { $userToken->delete(); return ["success" => true]; } return ["errors" => $user->errors]; }