Customer.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\models\mysqldb;
  10. use Yii;
  11. use yii\db\ActiveRecord;
  12. use yii\web\IdentityInterface;
  13. /**
  14. * User model.
  15. *
  16. * @property int $id
  17. * @property string $username
  18. * @property string $password_hash
  19. * @property string $password_reset_token
  20. * @property string $email
  21. * @property string $auth_key
  22. * @property int $status
  23. * @property int $created_at
  24. * @property int $updated_at
  25. * @property string $password write-only password
  26. * @property int $access_token_created_at
  27. *
  28. * @author Terry Zhao <2358269014@qq.com>
  29. * @since 1.0
  30. */
  31. class Customer extends ActiveRecord implements IdentityInterface
  32. {
  33. const STATUS_DELETED = 10;
  34. const STATUS_ACTIVE = 1;
  35. const STATUS_REGISTER_DISABLE = 2;
  36. public static function tableName()
  37. {
  38. return '{{%customer}}';
  39. }
  40. public function rules()
  41. {
  42. return [
  43. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  44. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_REGISTER_DISABLE, self::STATUS_DELETED]],
  45. ];
  46. }
  47. /**
  48. * @param $id | Int , 用户id
  49. * 通过id 找到identity(状态有效)
  50. */
  51. public static function findIdentity($id)
  52. {
  53. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  54. }
  55. /**
  56. * @param $token | String
  57. * 通过access_token 找到identity
  58. */
  59. public static function findIdentityByAccessToken($token, $type = null)
  60. {
  61. return static::findOne(['access_token' => $token, 'status' => self::STATUS_ACTIVE]);
  62. }
  63. /**
  64. * 生成access_token
  65. */
  66. public function generateAccessToken()
  67. {
  68. $this->access_token = Yii::$app->security->generateRandomString();
  69. }
  70. /**
  71. * Finds user by username.
  72. *
  73. * @param string $username
  74. * @return static|null
  75. */
  76. public static function findByEmail($email)
  77. {
  78. return static::findOne(['email' => $email, 'status' => self::STATUS_ACTIVE]);
  79. }
  80. public static function findAvailableByEmail($email)
  81. {
  82. return static::find()->where(['email' => $email])->andWhere(['in', 'status', [
  83. self::STATUS_ACTIVE,
  84. self::STATUS_REGISTER_DISABLE
  85. ]])->one();
  86. }
  87. /**
  88. * Finds user by password reset token.
  89. *
  90. * @param string $token password reset token
  91. * @return static|null
  92. */
  93. public static function findByPasswordResetToken($token)
  94. {
  95. if (!static::isPasswordResetTokenValid($token)) {
  96. return null;
  97. }
  98. return static::findOne([
  99. 'password_reset_token' => $token,
  100. 'status' => self::STATUS_ACTIVE,
  101. ]);
  102. }
  103. /**
  104. * Finds user by password reset token.
  105. *
  106. * @param string $token password reset token
  107. * @return static|null
  108. */
  109. public static function findByRegisterEnableToken($token)
  110. {
  111. if (!static::isRegisterEnableTokenValid($token)) {
  112. return null;
  113. }
  114. return static::findOne([
  115. 'register_enable_token' => $token,
  116. 'status' => self::STATUS_REGISTER_DISABLE,
  117. ]);
  118. }
  119. /**
  120. * Finds out if password reset token is valid.
  121. *
  122. * @param string $token password reset token
  123. * @return bool
  124. */
  125. public static function isRegisterEnableTokenValid($token)
  126. {
  127. if (empty($token)) {
  128. return false;
  129. }
  130. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  131. //$expire = Yii::$app->params['user.passwordResetTokenExpire'];
  132. $expire = Yii::$service->email->customer->getRegisterEnableTokenExpire();
  133. return $timestamp + $expire >= time();
  134. }
  135. /**
  136. * Finds out if password reset token is valid.
  137. *
  138. * @param string $token password reset token
  139. * @return bool
  140. */
  141. public static function isPasswordResetTokenValid($token)
  142. {
  143. if (empty($token)) {
  144. return false;
  145. }
  146. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  147. //$expire = Yii::$app->params['user.passwordResetTokenExpire'];
  148. $expire = Yii::$service->email->customer->getPasswordResetTokenExpire();
  149. return $timestamp + $expire >= time();
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getId()
  155. {
  156. return $this->getPrimaryKey();
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function getAuthKey()
  162. {
  163. return $this->auth_key;
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function validateAuthKey($authKey)
  169. {
  170. return $this->getAuthKey() === $authKey;
  171. }
  172. /**
  173. * Validates password.
  174. *
  175. * @param string $password password to validate
  176. * @return bool if password provided is valid for current user
  177. */
  178. public function validatePassword($password)
  179. {
  180. return Yii::$app->security->validatePassword($password, $this->password_hash);
  181. }
  182. /**
  183. * Generates password hash from password and sets it to the model.
  184. *
  185. * @param string $password
  186. */
  187. public function setPassword($password)
  188. {
  189. $this->password_hash = Yii::$app->security->generatePasswordHash($password, 6);
  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 password reset token.
  200. */
  201. public function generatePasswordResetToken()
  202. {
  203. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  204. }
  205. /**
  206. * Removes password reset token.
  207. */
  208. public function removePasswordResetToken()
  209. {
  210. $this->password_reset_token = null;
  211. }
  212. /**
  213. * Generates new password reset token.
  214. */
  215. public function generateRegisterEnableToken()
  216. {
  217. $this->register_enable_token = Yii::$app->security->generateRandomString() . '_' . time();
  218. }
  219. /**
  220. * Removes password reset token.
  221. */
  222. public function removeRegisterEnableToken()
  223. {
  224. $this->register_enable_token = null;
  225. }
  226. }