Advertisement
Guest User

Untitled

a guest
Sep 13th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. <?php
  2.  
  3. namespace appmodels;
  4.  
  5. use Yii;
  6. use yiibaseModel;
  7.  
  8. /**
  9. * LoginForm is the model behind the login form.
  10. *
  11. * @property User|null $user This property is read-only.
  12. *
  13. */
  14. class LoginForm extends Model
  15. {
  16. public $login;
  17. public $pass;
  18. public $rememberMe = true;
  19.  
  20. private $_user = false;
  21.  
  22.  
  23. /**
  24. * @return array the validation rules.
  25. */
  26. public function rules()
  27. {
  28. return [
  29. // login and password are both required
  30. [['login', 'pass'], 'required'],
  31. // rememberMe must be a boolean value
  32. ['rememberMe', 'boolean'],
  33. // password is validated by validatePassword()
  34. ['pass', 'validatePassword'],
  35. ];
  36. }
  37.  
  38. /**
  39. * Validates the password.
  40. * This method serves as the inline validation for password.
  41. *
  42. * @param string $attribute the attribute currently being validated
  43. * @param array $params the additional name-value pairs given in the rule
  44. */
  45. public function validatePassword($attribute, $params)
  46. {
  47. if (!$this->hasErrors()) {
  48. $user = $this->getUser();
  49.  
  50. if (!$user || !$user->validatePassword($this->pass)) {
  51. $this->addError($attribute, 'Wrong login or password.');
  52. }
  53. }
  54. }
  55.  
  56. /**
  57. * Logs in a user using the provided login and password.
  58. * @return bool whether the user is logged in successfully
  59. */
  60. public function login()
  61. {
  62. if ($this->validate())
  63. {
  64. $user = $this->getUser();
  65. if (!$user->active)
  66. {
  67. $this->addError('login', 'You account is not active.');
  68. return false;
  69. }
  70. return Yii::$app->user->login($user, $this->rememberMe ? 3600*24*30 : 0);
  71. }
  72. return false;
  73. }
  74.  
  75. /**
  76. * Finds user by [[login]]
  77. *
  78. * @return User|null
  79. */
  80. public function getUser()
  81. {
  82. if ($this->_user === false)
  83. {
  84. $this->_user = User::findByLogin($this->login);
  85. }
  86.  
  87. return $this->_user;
  88. }
  89. }
  90.  
  91. <?php
  92.  
  93.  
  94. namespace appmodels;
  95.  
  96. use Yii;
  97. use yiibaseNotSupportedException;
  98. use yiibehaviorsTimestampBehavior;
  99. use yiidbActiveRecord;
  100. use yiiwebIdentityInterface;
  101.  
  102. /**
  103. * User model
  104. *
  105. * @property integer $id
  106. * @property string $login
  107. * @property string $pass
  108. * @property string $password_reset_token
  109. * @property string $email
  110. * @property string $auth_key
  111. * @property integer $status
  112. * @property integer $created_at
  113. * @property string $password write-only password
  114. */
  115. class User extends ActiveRecord implements IdentityInterface
  116. {
  117. const STATUS_DELETED = 0;
  118. const STATUS_ACTIVE = 10;
  119.  
  120. /**
  121. * @inheritdoc
  122. */
  123. public static function tableName()
  124. {
  125. return '{{%user}}';
  126. }
  127.  
  128. /**
  129. * @inheritdoc
  130. */
  131. /*public function behaviors()
  132. {
  133. return [
  134. TimestampBehavior::className(),
  135. ];
  136. }*/
  137.  
  138. /**
  139. * @inheritdoc
  140. */
  141. public function rules()
  142. {
  143. return [
  144. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  145. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  146. ];
  147. }
  148.  
  149. /**
  150. * @inheritdoc
  151. */
  152. public static function findIdentity($id)
  153. {
  154. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  155. }
  156.  
  157. /**
  158. * @inheritdoc
  159. */
  160. public static function findIdentityByAccessToken($token, $type = null)
  161. {
  162. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  163. }
  164.  
  165. /**
  166. * Finds user by login
  167. *
  168. * @param string $login
  169. * @return static|null
  170. */
  171. public static function findBylogin($login)
  172. {
  173. return static::findOne(['login' => $login]);
  174. }
  175.  
  176. /**
  177. * @inheritdoc
  178. */
  179. public function getId()
  180. {
  181. return $this->getPrimaryKey();
  182. }
  183.  
  184. /**
  185. * @inheritdoc
  186. */
  187. public function getAuthKey()
  188. {
  189. return $this->auth_key;
  190. }
  191.  
  192. /**
  193. * @inheritdoc
  194. */
  195. public function validateAuthKey($authKey)
  196. {
  197. return $this->getAuthKey() === $authKey;
  198. }
  199.  
  200. /**
  201. * Validates password
  202. *
  203. * @param string $password password to validate
  204. * @return bool if password provided is valid for current user
  205. */
  206. public function validatePassword($password)
  207. {
  208. return Yii::$app->security->validatePassword($password, $this->pass);
  209. }
  210.  
  211. /**
  212. * Generates password hash from password and sets it to the model
  213. *
  214. * @param string $password
  215. */
  216. public function setPassword($password)
  217. {
  218. $this->pass = Yii::$app->security->generatePasswordHash($password);
  219. }
  220.  
  221. /**
  222. * Generates "remember me" authentication key
  223. */
  224. public function generateAuthKey()
  225. {
  226. $this->auth_key = Yii::$app->security->generateRandomString();
  227. }
  228.  
  229.  
  230.  
  231. public static function findByPasswordResetToken($token)
  232. {
  233.  
  234. if (!static::isPasswordResetTokenValid($token)) {
  235. return null;
  236. }
  237.  
  238. return static::findOne([
  239. 'password_reset_token' => $token,
  240. 'status' => self::STATUS_ACTIVE,
  241. ]);
  242. }
  243.  
  244. public static function isPasswordResetTokenValid($token)
  245. {
  246. if (empty($token)) {
  247. return false;
  248. }
  249.  
  250. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  251. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  252. return $timestamp + $expire >= time();
  253. }
  254.  
  255. public function generatePasswordResetToken()
  256. {
  257. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  258. }
  259.  
  260. public function removePasswordResetToken()
  261. {
  262. $this->password_reset_token = null;
  263. }
  264. }
  265.  
  266. 'user' => [
  267. 'identityClass' => 'appmodelsUser',
  268. 'enableAutoLogin' => true,
  269. ],
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement