Пример #1
0
 /**
  * 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];
 }
Пример #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getUserTokens()
 {
     return $this->hasMany(UserToken::className(), ['user_id' => 'id']);
 }
Пример #3
0
 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;
 }
Пример #4
0
 /**
  * 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];
 }