PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/common/models/User.php

https://bitbucket.org/AWERD/bbq
PHP | 218 lines | 116 code | 26 blank | 76 comment | 2 complexity | 1ce8c19bd691e3289898238c35008c3e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace common\models;
  3. use backend\models\AuthAssignment;
  4. use Yii;
  5. use yii\base\NotSupportedException;
  6. use yii\behaviors\TimestampBehavior;
  7. use yii\db\ActiveRecord;
  8. use yii\web\IdentityInterface;
  9. /**
  10. * User model
  11. *
  12. * @property integer $id
  13. * @property string $username
  14. * @property string $password_hash
  15. * @property string $password_reset_token
  16. * @property string $email
  17. * @property string $auth_key
  18. * @property integer $status
  19. * @property integer $created_at
  20. * @property integer $updated_at
  21. * @property string $password write-only password
  22. */
  23. class User extends ActiveRecord implements IdentityInterface
  24. {
  25. const STATUS_DELETED = 0;
  26. const STATUS_ACTIVE = 10;
  27. /**
  28. * @inheritdoc
  29. */
  30. public static function tableName()
  31. {
  32. return '{{%user}}';
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function behaviors()
  38. {
  39. return [
  40. TimestampBehavior::className(),
  41. ];
  42. }
  43. /**
  44. * @inheritdoc
  45. */
  46. public function rules()
  47. {
  48. return [
  49. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  50. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  51. ['username', 'trim'],
  52. ['username', 'required'],
  53. ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'Этот логин уже зарегистрирован.'],
  54. ['username', 'string', 'min' => 2, 'max' => 255],
  55. ['email', 'trim'],
  56. ['email', 'required'],
  57. ['email', 'email'],
  58. ['email', 'string', 'max' => 255],
  59. ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'Этот email уже зарегистрирован.'],
  60. ];
  61. }
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'username' => 'Логин',
  66. ];
  67. }
  68. public function getProfile()
  69. {
  70. return $this->hasOne(Profile::className(), ['user_id' => 'id']);
  71. }
  72. public function getRole()
  73. {
  74. return $this->hasOne(AuthAssignment::className(), ['user_id' => 'id']);
  75. }
  76. /**
  77. * @inheritdoc
  78. */
  79. public static function findIdentity($id)
  80. {
  81. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  82. }
  83. /**
  84. * @inheritdoc
  85. */
  86. public static function findIdentityByAccessToken($token, $type = null)
  87. {
  88. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  89. }
  90. /**
  91. * Finds user by username
  92. *
  93. * @param string $username
  94. * @return static|null
  95. */
  96. public static function findByUsername($username)
  97. {
  98. return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
  99. }
  100. /**
  101. * Finds user by password reset token
  102. *
  103. * @param string $token password reset token
  104. * @return static|null
  105. */
  106. public static function findByPasswordResetToken($token)
  107. {
  108. if (!static::isPasswordResetTokenValid($token)) {
  109. return null;
  110. }
  111. return static::findOne([
  112. 'password_reset_token' => $token,
  113. 'status' => self::STATUS_ACTIVE,
  114. ]);
  115. }
  116. /**
  117. * Finds out if password reset token is valid
  118. *
  119. * @param string $token password reset token
  120. * @return bool
  121. */
  122. public static function isPasswordResetTokenValid($token)
  123. {
  124. if (empty($token)) {
  125. return false;
  126. }
  127. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  128. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  129. return $timestamp + $expire >= time();
  130. }
  131. /**
  132. * @inheritdoc
  133. */
  134. public function getId()
  135. {
  136. return $this->getPrimaryKey();
  137. }
  138. /**
  139. * @inheritdoc
  140. */
  141. public function getAuthKey()
  142. {
  143. return $this->auth_key;
  144. }
  145. /**
  146. * @inheritdoc
  147. */
  148. public function validateAuthKey($authKey)
  149. {
  150. return $this->getAuthKey() === $authKey;
  151. }
  152. /**
  153. * Validates password
  154. *
  155. * @param string $password password to validate
  156. * @return bool if password provided is valid for current user
  157. */
  158. public function validatePassword($password)
  159. {
  160. return Yii::$app->security->validatePassword($password, $this->password_hash);
  161. }
  162. /**
  163. * Generates password hash from password and sets it to the model
  164. *
  165. * @param string $password
  166. */
  167. public function setPassword($password)
  168. {
  169. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  170. }
  171. /**
  172. * Generates "remember me" authentication key
  173. */
  174. public function generateAuthKey()
  175. {
  176. $this->auth_key = Yii::$app->security->generateRandomString();
  177. }
  178. /**
  179. * Generates new password reset token
  180. */
  181. public function generatePasswordResetToken()
  182. {
  183. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  184. }
  185. /**
  186. * Removes password reset token
  187. */
  188. public function removePasswordResetToken()
  189. {
  190. $this->password_reset_token = null;
  191. }
  192. }