'/user/registration/confirm', self::TYPE_RECOVERY => '/usr/recovery/reset', self::TYPE_CONFIRM_NEW_EMAIL => '/user/settings/confirm', self::TYPE_CONFIRM_OLD_EMAIL => '/usr/settings/confirm', ]; /** * {@inheritdoc} */ public function beforeSave($insert) { if ($insert) { $this->setAttribute('code', $this->make(SecurityHelper::class)->generateRandomString()); static::deleteAll(['user_id' => $this->user_id, 'type' => $this->type]); $this->setAttribute('created_at', time()); } return parent::beforeSave($insert); } /** * {@inheritdoc} */ public static function tableName() { return '{{%token}}'; } /** * {@inheritdoc} */ public static function primaryKey() { return ['user_id', 'code', 'type']; } /** * @return \yii\db\ActiveQuery */ public function getUser() { return $this->hasOne($this->getClassMap()->get(User::class), ['id' => 'user_id']); } /** * @return string */ public function getUrl() { return Url::to([$this->routes[$this->type], 'id' => $this->user_id, 'code' => $this->code], true); } /** * @return bool Whether token has expired */ public function getIsExpired() { if ($this->type == static::TYPE_RECOVERY) { $expirationTime = $this->getModule()->tokenRecoveryLifespan; } elseif ($this->type >= static::TYPE_CONFIRMATION && $this->type <= static::TYPE_CONFIRM_OLD_EMAIL) { $expirationTime = $this->getModule()->tokenConfirmationLifespan; } else { throw new RuntimeException(); } return ($this->created_at + $expirationTime) < time(); } /** * @return TokenQuery */ public static function find() { return new TokenQuery(static::class); } }