User.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\base\NotSupportedException;
  5. use yii\behaviors\TimestampBehavior;
  6. use yii\db\ActiveRecord;
  7. use yii\web\IdentityInterface;
  8. /**
  9. * User model
  10. *
  11. * @property integer $id
  12. * @property string $username
  13. * @property string $password_hash
  14. * @property string $password_reset_token
  15. * @property string $email
  16. * @property string $auth_key
  17. * @property integer $status
  18. * @property integer $created_at
  19. * @property integer $updated_at
  20. * @property string $password write-only password
  21. * @property integer $login_count
  22. */
  23. class User extends ActiveRecord implements IdentityInterface
  24. {
  25. const STATUS_DELETED = 0;
  26. const STATUS_ACTIVE = 10;
  27. //用户默认头像
  28. const DEFAULT_FACE = '../images/defaultface.png';
  29. /**
  30. * 用户审核状态
  31. */
  32. const USER_STATE_WAIT = 0; //待审核
  33. const USER_STATE_THROUGH = 10; //审核通过
  34. const USER_STATE_REJECT = 20; //审核不通过
  35. /**
  36. * 用户角色
  37. */
  38. const USER_ROLE_COMMON = '0'; //普通用户(微信用户)
  39. const USER_ROLE_DECORATE = '1'; //装修公司
  40. const USER_ROLE_MATER = '2'; //建材商
  41. /**
  42. * @inheritdoc
  43. */
  44. public static function tableName()
  45. {
  46. return '{{%user}}';
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function behaviors()
  52. {
  53. return [
  54. TimestampBehavior::className(),
  55. ];
  56. }
  57. /**
  58. * @inheritdoc
  59. */
  60. public function rules()
  61. {
  62. return [
  63. [['username', 'auth_key', 'password_hash', 'status', 'created_at', 'updated_at'], 'required'],
  64. [[ 'status', 'created_at', 'updated_at','state','role'], 'integer'],
  65. [['username', 'auth_key', 'email','tel'], 'string', 'max' => 32],
  66. [['password_hash', 'password_reset_token', 'access_token'], 'string', 'max' => 255],
  67. [['username'], 'unique'],
  68. // [['email'], 'unique'],
  69. [['password_reset_token'], 'unique'],
  70. ];
  71. // return [
  72. // ['status', 'default','state', 'value' => self::STATUS_ACTIVE],
  73. // [['role'], 'string'],
  74. // ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  75. // ];
  76. }
  77. /**
  78. * @inheritdoc
  79. */
  80. public static function findIdentity($id)
  81. {
  82. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  83. }
  84. /**
  85. * API access_token 认证
  86. * @inheritdoc
  87. */
  88. public static function findIdentityByAccessToken($token, $type = null)
  89. {
  90. return static::findOne(['access_token' => $token, 'status' => self::STATUS_ACTIVE]);
  91. // $username = Yii::$app->request->post('username');
  92. // $model = static::findOne(['username'=>$username,'status'=>self::STATUS_ACTIVE]);
  93. // if($model === null){
  94. // return null;
  95. // }
  96. //
  97. // return $model->access_token === $token?$model:null;
  98. // throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  99. }
  100. /**
  101. * Finds user by username
  102. *
  103. * @param string $username
  104. * @return static|null
  105. */
  106. public static function findByUsername($username)
  107. {
  108. return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
  109. }
  110. /**
  111. * Finds user by password reset token
  112. *
  113. * @param string $token password reset token
  114. * @return static|null
  115. */
  116. public static function findByPasswordResetToken($token)
  117. {
  118. if (!static::isPasswordResetTokenValid($token)) {
  119. return null;
  120. }
  121. return static::findOne([
  122. 'password_reset_token' => $token,
  123. 'status' => self::STATUS_ACTIVE,
  124. ]);
  125. }
  126. /**
  127. * Finds out if password reset token is valid
  128. *
  129. * @param string $token password reset token
  130. * @return bool
  131. */
  132. public static function isPasswordResetTokenValid($token)
  133. {
  134. if (empty($token)) {
  135. return false;
  136. }
  137. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  138. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  139. return $timestamp + $expire >= time();
  140. }
  141. /**
  142. * @inheritdoc
  143. */
  144. public function getId()
  145. {
  146. return $this->getPrimaryKey();
  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 bool 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_hash = Yii::$app->security->generatePasswordHash($password);
  180. }
  181. /**
  182. * Generates "remember me" authentication key
  183. */
  184. public function generateAuthKey()
  185. {
  186. $this->auth_key = Yii::$app->security->generateRandomString();
  187. }
  188. /**
  189. * Generates new password reset token
  190. */
  191. public function generatePasswordResetToken()
  192. {
  193. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  194. }
  195. /**
  196. * Removes password reset token
  197. */
  198. public function removePasswordResetToken()
  199. {
  200. $this->password_reset_token = null;
  201. }
  202. public function getUsercompany(){
  203. return $this->hasOne(UserCompany::className(),['uid'=>'id']);
  204. }
  205. public function getUserinfo(){
  206. return $this->hasOne(UserInfo::className(),['uid'=>'id']);
  207. }
  208. public function getMater(){
  209. return $this->hasOne(UserMater::className(),['uid'=>'id']);
  210. }
  211. }