Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\models;
  4.  
  5. use Yii;
  6. use yii\base\NotSupportedException;
  7. use yii\db\ActiveRecord;
  8. use yii\helpers\Security;
  9. use yii\web\IdentityInterface;
  10.  
  11. /**
  12. * This is the model class for table "users".
  13. *
  14. * @property string $user_id
  15. * @property string $username
  16. * @property string $password
  17. * @property string $salt
  18. */
  19. class User extends \yii\db\ActiveRecord implements IdentityInterface
  20. {
  21. /**
  22. * @inheritdoc
  23. */
  24. public static function tableName()
  25. {
  26. return 'users';
  27. }
  28.  
  29. /**
  30. * @inheritdoc
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['username', 'password'], 'required'],
  36. [['username', 'password'], 'string', 'max' => 100]
  37. ];
  38. }
  39.  
  40. /**
  41. * @inheritdoc
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'user_id' => 'User ID',
  47. 'username' => 'Username',
  48. 'password' => 'Password'
  49. ];
  50. }
  51. /** INCLUDE USER LOGIN VALIDATION FUNCTIONS**/
  52. /**
  53. * @inheritdoc
  54. */
  55. public static function findIdentity($id)
  56. {
  57. return static::findOne($id);
  58. }
  59.  
  60. /**
  61. * @inheritdoc
  62. */
  63. /* modified */
  64. public static function findIdentityByAccessToken($token, $type = null)
  65. {
  66. return static::findOne(['access_token' => $token]);
  67. }
  68.  
  69. /* removed
  70. public static function findIdentityByAccessToken($token)
  71. {
  72. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  73. }
  74. */
  75. /**
  76. * Finds user by username
  77. *
  78. * @param string $username
  79. * @return static|null
  80. */
  81. public static function findByUsername($username)
  82. {
  83. return static::findOne(['username' => $username]);
  84. }
  85.  
  86. /**
  87. * Finds user by password reset token
  88. *
  89. * @param string $token password reset token
  90. * @return static|null
  91. */
  92. public static function findByPasswordResetToken($token)
  93. {
  94. $expire = \Yii::$app->params['user.passwordResetTokenExpire'];
  95. $parts = explode('_', $token);
  96. $timestamp = (int)end($parts);
  97. if ($timestamp + $expire < time()) {
  98. // token expired
  99. return null;
  100. }
  101.  
  102. return static::findOne([
  103. 'password_reset_token' => $token
  104. ]);
  105. }
  106.  
  107. /**
  108. * @inheritdoc
  109. */
  110. public function getId()
  111. {
  112. return $this->getPrimaryKey();
  113. }
  114.  
  115. /**
  116. * @inheritdoc
  117. */
  118. public function getAuthKey()
  119. {
  120. // return $this->auth_key;
  121. }
  122.  
  123. /**
  124. * @inheritdoc
  125. */
  126. public function validateAuthKey($authKey)
  127. {
  128. return $this->getAuthKey() === $authKey;
  129. }
  130.  
  131. /**
  132. * Validates password
  133. *
  134. * @param string $password password to validate
  135. * @return boolean if password provided is valid for current user
  136. */
  137. public function validatePassword($password)
  138. {
  139. if ($this->password === md5($this->username . $password . $this->salt)) {
  140. return true;
  141. }
  142. }
  143.  
  144. /**
  145. * Generates password hash from password and sets it to the model
  146. *
  147. * @param string $password
  148. */
  149. public function setPassword($password)
  150. {
  151. $this->password_hash = Security::generatePasswordHash($password);
  152. }
  153.  
  154. /**
  155. * Generates "remember me" authentication key
  156. */
  157. public function generateAuthKey()
  158. {
  159. $this->auth_key = Security::generateRandomKey();
  160. }
  161.  
  162. /**
  163. * Generates new password reset token
  164. */
  165. public function generatePasswordResetToken()
  166. {
  167. $this->password_reset_token = Security::generateRandomKey() . '_' . time();
  168. }
  169.  
  170. /**
  171. * Removes password reset token
  172. */
  173. public function removePasswordResetToken()
  174. {
  175. $this->password_reset_token = null;
  176. }
  177.  
  178. public function beforeSave($insert)
  179. {
  180. if (parent::beforeSave($insert)) {
  181. if ($this->isNewRecord) {
  182. $this->auth_key = Yii::$app->getSecurity()->generateRandomString();
  183. }
  184. return true;
  185. }
  186. return false;
  187. }
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement