pmtpenza

Untitled

Sep 3rd, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. <?php
  2. namespace common\models;
  3.  
  4. use Yii;
  5. use yii\base\NotSupportedException;
  6. use yii\behaviors\TimestampBehavior;
  7. use yii\db\ActiveRecord;
  8. use yii\web\IdentityInterface;
  9. use yii\filters\auth\HttpBasicAuth;
  10.  
  11. /**
  12. * User model
  13. *
  14. * @property integer $id
  15. * @property string $username
  16. * @property string $password_hash
  17. * @property string $password_reset_token
  18. * @property string $verification_token
  19. * @property string $email
  20. * @property string $auth_key
  21. * @property integer $status
  22. * @property integer $created_at
  23. * @property integer $updated_at
  24. * @property string $password write-only password
  25. */
  26. class User extends ActiveRecord implements IdentityInterface
  27. {
  28. public function getToken()
  29. {
  30. return $this->hasMany(Token::className(), ['user_id'=>'id']);
  31. }
  32. const STATUS_DELETED = 0;
  33. const STATUS_INACTIVE = 9;
  34. const STATUS_ACTIVE = 10;
  35.  
  36.  
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public static function tableName()
  41. {
  42. return '{{%user}}';
  43. }
  44.  
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function behaviors()
  49. {
  50. return [
  51. TimestampBehavior::className(),
  52. ];
  53. }
  54.  
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function rules()
  59. {
  60. return [
  61. ['status', 'default', 'value' => self::STATUS_INACTIVE],
  62. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
  63. ];
  64. }
  65.  
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public static function findIdentity($id)
  70. {
  71. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  72. }
  73.  
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public static function findIdentityByAccessToken($token, $type = null)
  78. {
  79. Token::deleteAll('expire_time < ' . time());
  80. return static::find()
  81. ->joinWith('token t')
  82. ->andWhere(['t.token' => $token])
  83. ->andWhere(['>', 't.expire_time', time()])
  84. ->one();
  85. }
  86.  
  87. /**
  88. * Finds user by username
  89. *
  90. * @param string $phone
  91. * @return static|null
  92. */
  93. public static function findByPhone($phone)
  94. {
  95. return static::findOne(['phone' => $phone, 'status' => self::STATUS_ACTIVE]);
  96. }
  97.  
  98. /**
  99. * Finds user by password reset token
  100. *
  101. * @param string $token password reset token
  102. * @return static|null
  103. */
  104. public static function findByPasswordResetToken($token)
  105. {
  106. if (!static::isPasswordResetTokenValid($token)) {
  107. return null;
  108. }
  109.  
  110. return static::findOne([
  111. 'password_reset_token' => $token,
  112. 'status' => self::STATUS_ACTIVE,
  113. ]);
  114. }
  115.  
  116. /**
  117. * Finds user by verification email token
  118. *
  119. * @param string $token verify email token
  120. * @return static|null
  121. */
  122. public static function findByVerificationToken($token) {
  123. return static::findOne([
  124. 'verification_token' => $token,
  125. 'status' => self::STATUS_INACTIVE
  126. ]);
  127. }
  128.  
  129. /**
  130. * Finds out if password reset token is valid
  131. *
  132. * @param string $token password reset token
  133. * @return bool
  134. */
  135. public static function isPasswordResetTokenValid($token)
  136. {
  137. if (empty($token)) {
  138. return false;
  139. }
  140.  
  141. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  142. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  143. return $timestamp + $expire >= time();
  144. }
  145.  
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function getId()
  150. {
  151. return $this->getPrimaryKey();
  152. }
  153.  
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function getAuthKey()
  158. {
  159. return $this->auth_key;
  160. }
  161.  
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function validateAuthKey($authKey)
  166. {
  167. return $this->getAuthKey() === $authKey;
  168. }
  169.  
  170. /**
  171. * Validates password
  172. *
  173. * @param string $password password to validate
  174. * @return bool if password provided is valid for current user
  175. */
  176. public function validatePassword($password)
  177. {
  178. return Yii::$app->security->validatePassword($password, $this->password_hash);
  179. }
  180.  
  181. /**
  182. * Generates password hash from password and sets it to the model
  183. *
  184. * @param string $password
  185. */
  186. public function setPassword($password)
  187. {
  188. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  189. }
  190.  
  191. /**
  192. * Generates "remember me" authentication key
  193. */
  194. public function generateAuthKey()
  195. {
  196. $this->auth_key = Yii::$app->security->generateRandomString();
  197. }
  198.  
  199. /**
  200. * Generates new password reset token
  201. */
  202. public function generatePasswordResetToken()
  203. {
  204. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  205. }
  206.  
  207. public function generateEmailVerificationToken()
  208. {
  209. $this->verification_token = Yii::$app->security->generateRandomString() . '_' . time();
  210. }
  211.  
  212. /**
  213. * Removes password reset token
  214. */
  215. public function removePasswordResetToken()
  216. {
  217. $this->password_reset_token = null;
  218. }
  219. public static function status()
  220. {
  221. return [
  222. self::STATUS_ACTIVE => 'Активный',
  223. self::STATUS_DELETED => 'Заблокировать',
  224. ];
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment