User.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\NotSupportedException;
  5. use yii\db\ActiveRecord;
  6. use yii\web\IdentityInterface;
  7. use app\models\behaviors\TimestampBehavior;
  8. use app\models\queries\UserQuery;
  9. /**
  10. * User model
  11. *
  12. * @property integer $id
  13. * @property string $username
  14. * @property string $is_email_verified
  15. * @property string $password_hash
  16. * @property string $password_reset_token
  17. * @property string $email_confirmation_token
  18. * @property string $email
  19. * @property string $auth_key
  20. * @property integer $role
  21. * @property integer $status
  22. * @property string $created_at
  23. * @property string $updated_at
  24. * @property string $password write-only password
  25. */
  26. class User extends ActiveRecord implements IdentityInterface
  27. {
  28. const STATUS_DELETED = 0;
  29. const STATUS_ACTIVE = 10;
  30. const ROLE_USER = 10;
  31. /**
  32. * 管理员
  33. */
  34. const ROLE_ADMIN = 1;
  35. /**
  36. * 开发者
  37. */
  38. const ROLE_DEV = 2;
  39. /**
  40. * @var string|null the current password value from form input
  41. */
  42. protected $_password;
  43. /**
  44. * @return UserQuery custom query class with user scopes
  45. */
  46. public static function find()
  47. {
  48. return new UserQuery(get_called_class());
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function scenarios()
  54. {
  55. return array_merge(parent::scenarios(), [
  56. 'signup' => ['username','email','password','role'],
  57. ]);
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'username' => '用户名',
  66. 'email' => '邮箱',
  67. 'password' => '密码',
  68. 'role' => '角色',
  69. ];
  70. }
  71. /**
  72. * @inheritdoc
  73. */
  74. public function rules()
  75. {
  76. return [
  77. [['username','email','password','role'], 'required', 'on'=>'signup'],
  78. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  79. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  80. ['role', 'default', 'value' => self::ROLE_DEV],
  81. ['role', 'in', 'range' => [self::ROLE_USER, self::ROLE_DEV, self::ROLE_ADMIN]],
  82. ['username', 'filter', 'filter' => 'trim'],
  83. ['username', 'unique'],
  84. ['username', 'string', 'min' => 2, 'max' => 255],
  85. ['email', 'filter', 'filter' => 'trim'],
  86. ['email', 'validateEmail'],
  87. ['email', 'email'],
  88. ['email', 'unique'],
  89. ];
  90. }
  91. public function validateEmail($attribute, $params) {
  92. if (!preg_match('/.*@social-touch.com$/', $this->$attribute)) {
  93. $this->addError($attribute, '我猜你丫是外星人,没有st邮箱不可注册:)');
  94. }
  95. }
  96. /**
  97. * @inheritdoc
  98. */
  99. public function behaviors()
  100. {
  101. return [
  102. 'timestamp' => [
  103. 'class' => TimestampBehavior::className(),
  104. ],
  105. ];
  106. }
  107. /**
  108. * @inheritdoc
  109. */
  110. public function beforeSave($insert)
  111. {
  112. if ($this->isNewRecord) {
  113. $this->generateAuthKey();
  114. $this->generateEmailConfirmationToken();
  115. }
  116. $this->realname = $this->username;
  117. $this->username = $this->email;
  118. return parent::beforeSave($insert);
  119. }
  120. /**
  121. * @inheritdoc
  122. */
  123. public static function findIdentity($id)
  124. {
  125. return static::findOne($id);
  126. }
  127. /**
  128. * @inheritdoc
  129. */
  130. public static function findIdentityByAccessToken($token, $type = null)
  131. {
  132. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  133. }
  134. /**
  135. * @inheritdoc
  136. */
  137. public function getId()
  138. {
  139. return $this->getPrimaryKey();
  140. }
  141. /**
  142. * @inheritdoc
  143. */
  144. public function getName()
  145. {
  146. return $this->realname;
  147. }
  148. /**
  149. * @inheritdoc
  150. */
  151. public function getAuthKey()
  152. {
  153. return $this->auth_key;
  154. }
  155. /**
  156. * @inheritdoc
  157. */
  158. public function validateAuthKey($authKey)
  159. {
  160. return $this->getAuthKey() === $authKey;
  161. }
  162. /**
  163. * Validates password
  164. *
  165. * @param string $password password to validate
  166. * @return boolean if password provided is valid for current user
  167. */
  168. public function validatePassword($password)
  169. {
  170. return Yii::$app->security->validatePassword($password, $this->password_hash);
  171. }
  172. /**
  173. * Generates password hash from password and sets it to the model
  174. *
  175. * @param string $password
  176. */
  177. public function setPassword($password)
  178. {
  179. $this->_password = $password;
  180. if (!empty($password)) {
  181. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  182. }
  183. }
  184. /**
  185. * @return string|null the current password value, if set from form. Null otherwise.
  186. */
  187. public function getPassword()
  188. {
  189. return $this->_password;
  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. * Generates new email confirmation token
  200. */
  201. public function generateEmailConfirmationToken()
  202. {
  203. $this->email_confirmation_token = Yii::$app->security->generateRandomString() . '_' . time();
  204. }
  205. /**
  206. * Generates new password reset token
  207. */
  208. public function generatePasswordResetToken()
  209. {
  210. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  211. }
  212. /**
  213. * Removes password reset token
  214. * @param bool $save whether to save the record. Default is `false`.
  215. * @return bool|null whether the save was successful or null if $save was false.
  216. */
  217. public function removePasswordResetToken($save = false)
  218. {
  219. $this->password_reset_token = null;
  220. if ($save) {
  221. return $this->save();
  222. }
  223. }
  224. /**
  225. * Removes email confirmation token and sets is_email_verified to true
  226. * @param bool $save whether to save the record. Default is `false`.
  227. * @return bool|null whether the save was successful or null if $save was false.
  228. */
  229. public function removeEmailConfirmationToken($save = false)
  230. {
  231. $this->email_confirmation_token = null;
  232. $this->is_email_verified = 1;
  233. if ($save) {
  234. return $this->save();
  235. }
  236. }
  237. }