UserQuery.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace app\models\queries;
  3. use yii\db\ActiveQuery;
  4. use app\models\User;
  5. class UserQuery extends ActiveQuery
  6. {
  7. /**
  8. * @return UserQuery the query with conditions for users that can login applied
  9. */
  10. public function canLogin()
  11. {
  12. return $this->andWhere([
  13. 'status' => User::STATUS_ACTIVE,
  14. 'is_email_verified' => 1,
  15. ]);
  16. }
  17. /**
  18. * @return UserQuery the query with condition for given email applied
  19. */
  20. public function email($email)
  21. {
  22. return $this->andWhere(['email' => $email]);
  23. }
  24. /**
  25. * @return UserQuery the query with condition for given username applied
  26. */
  27. public function username($username)
  28. {
  29. return $this->andWhere(['username' => $username]);
  30. }
  31. /**
  32. * @param string $token the password reset token
  33. * @return UserQuery the query with conditions for valid password reset token applied
  34. */
  35. public function passwordResetToken($token)
  36. {
  37. $expire = \Yii::$app->params['user.passwordResetTokenExpire'];
  38. $parts = explode('_', $token);
  39. $timestamp = (int) end($parts);
  40. if ($timestamp + $expire < time()) {
  41. // token expired
  42. return $this->andWhere('FALSE');
  43. }
  44. return $this->andWhere(['password_reset_token' => $token]);
  45. }
  46. /**
  47. * @param string $token the email confirmation token
  48. * @return UserQuery the query with conditions for valid email confirmation token applied
  49. */
  50. public function emailConfirmationToken($token)
  51. {
  52. $expire = \Yii::$app->params['user.emailConfirmationTokenExpire'];
  53. $parts = explode('_', $token);
  54. $timestamp = (int) end($parts);
  55. if ($timestamp + $expire < time()) {
  56. // token expired
  57. return $this->andWhere('FALSE');
  58. }
  59. return $this->andWhere(['email_confirmation_token' => $token]);
  60. }
  61. }