PageRenderTime 47ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/models/User.php

https://gitlab.com/kursat/conews
PHP | 234 lines | 121 code | 28 blank | 85 comment | 3 complexity | d78abe1802ec59d3c75d343ff6a90124 MD5 | raw file
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\NotSupportedException;
  5. use yii\behaviors\BlameableBehavior;
  6. use yii\behaviors\TimestampBehavior;
  7. use yii\db\ActiveQuery;
  8. use yii\db\ActiveRecord;
  9. use yii\web\IdentityInterface;
  10. /**
  11. * User model
  12. *
  13. * @property integer $id
  14. * @property string $password_hash
  15. * @property string $password_reset_token
  16. * @property string $email
  17. * @property string $auth_key
  18. * @property integer $status
  19. * @property integer $created_at
  20. * @property integer $updated_at
  21. * @property string $password write-only password
  22. */
  23. class User extends ActiveRecord implements IdentityInterface {
  24. public $form_password;
  25. const STATUS_PASSIVE = 0;
  26. const STATUS_ACTIVE = 10;
  27. /**
  28. * @inheritdoc
  29. */
  30. public function behaviors() {
  31. return [
  32. TimestampBehavior::className(),
  33. BlameableBehavior::className()
  34. ];
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public static function tableName() {
  40. return '{{%user}}';
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function rules() {
  46. return [
  47. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  48. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_PASSIVE]],
  49. [['auth_key', 'password_hash', 'email'], 'required'],
  50. [['status', 'created_at', 'updated_at', 'created_by', 'updated_by'], 'integer'],
  51. [['password_hash', 'password_reset_token', 'firstname', 'lastname'], 'string', 'max' => 255],
  52. [['auth_key'], 'string', 'max' => 32],
  53. [['email'], 'string', 'min' => 5, 'max' => 254],
  54. [['email'], 'unique'],
  55. [['email'], 'email'],
  56. [['email'], 'trim'],
  57. [['password_reset_token'], 'unique'],
  58. ];
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function attributeLabels() {
  64. return [
  65. 'id' => Yii::t('user', 'ID'),
  66. 'firstname' => Yii::t('user', 'Name'),
  67. 'lastname' => Yii::t('user', 'Surname'),
  68. 'form_password' => Yii::t('user', 'Password'),
  69. 'auth_key' => Yii::t('user', 'Auth Key'),
  70. 'password_hash' => Yii::t('user', 'Password Hash'),
  71. 'password_reset_token' => Yii::t('user', 'Password Reset Token'),
  72. 'email' => Yii::t('user', 'Email'),
  73. 'status' => Yii::t('app', 'Status'),
  74. 'created_at' => Yii::t('app', 'Created At'),
  75. 'updated_at' => Yii::t('app', 'Updated At'),
  76. 'created_by' => Yii::t('app', 'Created By'),
  77. 'updated_by' => Yii::t('app', 'Updated By'),
  78. 'authItems' => Yii::t('user', 'Auth Items'),
  79. ];
  80. }
  81. /**
  82. * @inheritdoc
  83. */
  84. public static function findIdentity($id) {
  85. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  86. }
  87. /**
  88. * @inheritdoc
  89. */
  90. public static function findIdentityByAccessToken($token, $type = null) {
  91. return static::findOne(['auth_key' => $token, 'status' => self::STATUS_ACTIVE]);
  92. }
  93. /**
  94. * Finds user by email
  95. *
  96. * @param string $email
  97. * @return static|null
  98. */
  99. public static function findByEmail($email) {
  100. return static::findOne(['email' => $email, 'status' => self::STATUS_ACTIVE]);
  101. }
  102. /**
  103. * Finds user by password reset token
  104. *
  105. * @param string $token password reset token
  106. * @return static|null
  107. */
  108. public static function findByPasswordResetToken($token) {
  109. if (!static::isPasswordResetTokenValid($token)) {
  110. return null;
  111. }
  112. return static::findOne([
  113. 'password_reset_token' => $token,
  114. 'status' => self::STATUS_ACTIVE,
  115. ]);
  116. }
  117. /**
  118. * Finds out if password reset token is valid
  119. *
  120. * @param string $token password reset token
  121. * @return boolean
  122. */
  123. public static function isPasswordResetTokenValid($token) {
  124. if (empty($token)) {
  125. return false;
  126. }
  127. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  128. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  129. return $timestamp + $expire >= time();
  130. }
  131. /**
  132. * @inheritdoc
  133. */
  134. public function getId() {
  135. return $this->getPrimaryKey();
  136. }
  137. /**
  138. * @inheritdoc
  139. */
  140. public function getAuthKey() {
  141. return $this->auth_key;
  142. }
  143. /**
  144. * @inheritdoc
  145. */
  146. public function validateAuthKey($authKey) {
  147. return $this->getAuthKey() === $authKey;
  148. }
  149. /**
  150. * Validates password
  151. *
  152. * @param string $password password to validate
  153. * @return boolean if password provided is valid for current user
  154. */
  155. public function validatePassword($password) {
  156. return Yii::$app->security->validatePassword($password, $this->password_hash);
  157. }
  158. /**
  159. * Generates password hash from password and sets it to the model
  160. *
  161. * @param string $password
  162. */
  163. public function setPassword($password) {
  164. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  165. }
  166. /**
  167. * Generates "remember me" authentication key
  168. */
  169. public function generateAuthKey() {
  170. $this->auth_key = Yii::$app->security->generateRandomString();
  171. }
  172. /**
  173. * Generates new password reset token
  174. */
  175. public function generatePasswordResetToken() {
  176. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  177. }
  178. /**
  179. * Removes password reset token
  180. */
  181. public function removePasswordResetToken() {
  182. $this->password_reset_token = null;
  183. }
  184. /**
  185. * @return ActiveQuery
  186. */
  187. public function getAuthItems() {
  188. return $this->hasMany(AuthItem::className(), ['name' => 'item_name'])
  189. ->via('authItemLinks');
  190. }
  191. public function getAuthItemLinks() {
  192. return $this->hasMany(AuthAssignment::className(), ['user_id' => 'id']);
  193. }
  194. /**
  195. *
  196. * @return string fullname
  197. */
  198. public function getFullname() {
  199. $parts = [$this->firstname, $this->lastname];
  200. foreach ($parts as $key => $value) {
  201. if (!$value)
  202. unset($parts[$key]);
  203. }
  204. return join(' ', $parts);
  205. }
  206. }