Advertisement
Guest User

Untitled

a guest
Mar 14th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. <?php
  2.  
  3. namespace appmodels;
  4.  
  5. use Yii;
  6. use yiibaseException;
  7. use yiibaseNotSupportedException;
  8. use yiibaseSecurity;
  9. use yiiwebIdentityInterface;
  10.  
  11. /**
  12. * This is the model class for table "users".
  13. *
  14. * @property int $id
  15. * @property string $email
  16. * @property string $password
  17. * @property string $username
  18. * @property string $authKey
  19. */
  20. class Blogger extends yiidbActiveRecord implements yiiwebIdentityInterface
  21. {
  22. /**
  23. * @inheritdoc
  24. */
  25. public static function tableName()
  26. {
  27. return 'users';
  28. }
  29.  
  30. /**
  31. * @inheritdoc
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['email', 'password', 'username'], 'required'],
  37. [['email', 'password', 'username'], 'string', 'max' => 50],
  38. [['authKey'], 'string', 'max' => 50],
  39. [['authKey'], 'unique'],
  40. ];
  41. }
  42.  
  43. /**
  44. * @inheritdoc
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'email' => 'Email',
  51. 'password' => 'Password',
  52. 'username' => 'Username',
  53. ];
  54. }
  55.  
  56. public function getId()
  57. {
  58. return $this->id;
  59. }
  60.  
  61. public static function findIdentity($id)
  62. {
  63. return self::findOne($id);
  64. }
  65.  
  66. public static function findByUsername($username)
  67. {
  68. return self::findOne(['username' => $username]);
  69. }
  70.  
  71. public function validatePassword($password)
  72. {
  73. return $this->password === $password;
  74. }
  75.  
  76. public static function findIdentityByAccessToken($token, $type = null)
  77. {
  78. throw new NotSupportedException();
  79. }
  80.  
  81. public function getAuthKey()
  82. {
  83. return $this->authKey;
  84. }
  85.  
  86. public function validateAuthKey($authKey)
  87. {
  88. return $this->authKey === $authKey;
  89. }
  90. }
  91.  
  92. namespace appmodels;
  93.  
  94. use Yii;
  95. use yiibaseModel;
  96.  
  97. /**
  98. * LoginForm is the model behind the login form.
  99. *
  100. * @property User|null $user This property is read-only.
  101. *
  102. */
  103. class LoginForm extends Model
  104. {
  105. public $username;
  106. public $password;
  107. public $rememberMe = true;
  108.  
  109. private $_user = false;
  110.  
  111.  
  112. /**
  113. * @return array the validation rules.
  114. */
  115. public function rules()
  116. {
  117. return [
  118. // username and password are both required
  119. [['username', 'password'], 'required'],
  120. // rememberMe must be a boolean value
  121. ['rememberMe', 'boolean'],
  122. // password is validated by validatePassword()
  123. ['password', 'validatePassword'],
  124. ];
  125. }
  126.  
  127. /**
  128. * Validates the password.
  129. * This method serves as the inline validation for password.
  130. *
  131. * @param string $attribute the attribute currently being validated
  132. * @param array $params the additional name-value pairs given in the rule
  133. */
  134. public function validatePassword($attribute, $params)
  135. {
  136. if (!$this->hasErrors()) {
  137. $user = $this->getUser();
  138.  
  139. if (!$user || !$user->validatePassword($this->password)) {
  140. $this->addError($attribute, 'Incorrect username or password.');
  141. }
  142. }
  143. }
  144.  
  145. /**
  146. * Logs in a user using the provided username and password.
  147. * @return bool whether the user is logged in successfully
  148. */
  149. public function login()
  150. {
  151. if ($this->validate()) {
  152. return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
  153. }
  154. return false;
  155. }
  156.  
  157. /**
  158. * Finds user by [[username]]
  159. *
  160. * @return Blogger|null
  161. */
  162. public function getUser()
  163. {
  164. if ($this->_user === false) {
  165. $this->_user = Blogger::findByUsername($this->username);
  166. }
  167.  
  168. return $this->_user;
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement