Token.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace common\modules\user\models;
  3. use common\modules\user\traits\ModuleTrait;
  4. use Yii;
  5. use yii\db\ActiveRecord;
  6. use yii\helpers\Url;
  7. /**
  8. * Token Active Record model.
  9. *
  10. * @property integer $user_id
  11. * @property string $code
  12. * @property integer $created_at
  13. * @property integer $type
  14. * @property string $url
  15. * @property bool $isExpired
  16. * @property User $user
  17. *
  18. */
  19. class Token extends ActiveRecord
  20. {
  21. /** @var int The time before a confirmation token becomes invalid. */
  22. public $confirmWithin = 86400; // 24 hours
  23. /** @var int The time before a recovery token becomes invalid. */
  24. public $recoverWithin = 21600; // 6 hours
  25. const TYPE_CONFIRMATION = 0;
  26. const TYPE_RECOVERY = 1;
  27. const TYPE_CONFIRM_NEW_EMAIL = 2;
  28. const TYPE_CONFIRM_OLD_EMAIL = 3;
  29. /**
  30. * @return \yii\db\ActiveQuery
  31. */
  32. public function getUser()
  33. {
  34. return $this->hasOne(User::className(), ['id' => 'user_id']);
  35. }
  36. /**
  37. * @return string
  38. */
  39. public function getUrl()
  40. {
  41. switch ($this->type) {
  42. case self::TYPE_CONFIRMATION:
  43. $route = '/user/registration/confirm';
  44. break;
  45. case self::TYPE_RECOVERY:
  46. $route = '/user/recovery/reset';
  47. break;
  48. case self::TYPE_CONFIRM_NEW_EMAIL:
  49. case self::TYPE_CONFIRM_OLD_EMAIL:
  50. $route = '/user/settings/confirm';
  51. break;
  52. default:
  53. throw new \RuntimeException();
  54. }
  55. return Url::to([$route, 'id' => $this->user_id, 'code' => $this->code], true);
  56. }
  57. /**
  58. * @return bool Whether token has expired.
  59. */
  60. public function getIsExpired()
  61. {
  62. switch ($this->type) {
  63. case self::TYPE_CONFIRMATION:
  64. case self::TYPE_CONFIRM_NEW_EMAIL:
  65. case self::TYPE_CONFIRM_OLD_EMAIL:
  66. $expirationTime = $this->confirmWithin;
  67. break;
  68. case self::TYPE_RECOVERY:
  69. $expirationTime = $this->recoverWithin;
  70. break;
  71. default:
  72. throw new \RuntimeException();
  73. }
  74. return ($this->created_at + $expirationTime) < time();
  75. }
  76. /** @inheritdoc */
  77. public function beforeSave($insert)
  78. {
  79. if ($insert) {
  80. static::deleteAll(['user_id' => $this->user_id, 'type' => $this->type]);
  81. $this->setAttribute('created_at', time());
  82. $this->setAttribute('code', Yii::$app->security->generateRandomString());
  83. }
  84. return parent::beforeSave($insert);
  85. }
  86. /** @inheritdoc */
  87. public static function tableName()
  88. {
  89. return '{{%token}}';
  90. }
  91. /** @inheritdoc */
  92. public static function primaryKey()
  93. {
  94. return ['user_id', 'code', 'type'];
  95. }
  96. }