PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/models/User.php

https://gitlab.com/Georgiy.Zhegusov/museum_documents
PHP | 187 lines | 130 code | 25 blank | 32 comment | 4 complexity | 7696dff51d7bdfe7a164ac80728bd63f MD5 | raw file
  1. <?php
  2. namespace app\models;
  3. use yii;
  4. use yii\web\IdentityInterface;
  5. use yii\helpers\ArrayHelper;
  6. use app\common\components\ActiveRecord\ActiveRecord;
  7. use yii\behaviors\TimestampBehavior;
  8. /**
  9. * This is the model class for table "users".
  10. *
  11. * @property integer $id
  12. * @property string $name
  13. * @property string $surname
  14. * @property string $email
  15. * @property string $phone
  16. * @property string $authKey
  17. * @property string $password
  18. *
  19. * @property File[] $files
  20. * @property Museum[] $museums
  21. */
  22. class User extends ActiveRecord
  23. implements
  24. IdentityInterface
  25. {
  26. public function behaviors()
  27. {
  28. return [
  29. TimestampBehavior::className(),
  30. ];
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. public static function tableName()
  36. {
  37. return 'users';
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['name', 'surname', 'email', 'phone', 'groupId'], 'required','message'=>'Поле {attribute} необходимо для заполнения'],
  46. [['name', 'surname', 'authKey'], 'string', 'max' => 64,'message'=>'Длинна имени ограничена 64 символами'],
  47. [['email'], 'string', 'max' => 128,'message'=>'Длинна почты ограничена 128 символами'],
  48. [['phone'], 'string', 'max' => 20,'message'=>'Длинна телефона ограничена 20 символами'],
  49. [['email'], 'unique','message'=>'Учетная запись с такой почтой уже зарегестрирована'],
  50. [['groupId'], 'in', 'range' => ArrayHelper::getColumn(
  51. Group::find()
  52. ->where(['<=', 'level', Yii::$app->user->identity->group->level])
  53. ->all(), 'id'),'message'=>'Вы не можете устанавливать группу выше себя или данной группы не существует'],
  54. ['passwordFirst', 'compare','compareAttribute'=>'passwordSecond','operator'=>'==','message'=>'Введенные пароли должны совпадать'],
  55. ['passwordSecond', 'compare','compareAttribute'=>'passwordFirst','operator'=>'==','message'=>'Введенные пароли должны совпадать'],
  56. [['groupId'],'filter','filter'=>function($value){return $value==''?null:intval($value);}]
  57. ];
  58. }
  59. public $passwordFirst;
  60. public $passwordSecond;
  61. /**
  62. * @inheritdoc
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id' => 'ID',
  68. 'name' => 'Имя',
  69. 'surname' => 'Фамилия',
  70. 'email' => 'E-mail',
  71. 'phone' => 'Телефон',
  72. 'groupId' => 'Группа',
  73. 'groupName' => 'Группа',
  74. 'authKey' => 'Auth Key',
  75. 'password' => 'Пароль',
  76. 'passwordFirst' => 'Введите пароль( Только для его смены )',
  77. 'passwordSecond' => 'Повторите пароль',
  78. 'created_at' => 'Создан',
  79. 'updated_at' => 'Изменен',
  80. ];
  81. }
  82. /**
  83. * @return \yii\db\ActiveQuery
  84. */
  85. public function getFiles()
  86. {
  87. return $this->hasMany(File::className(), ['uploadedExpertId' => 'id']);
  88. }
  89. public function getUsername()
  90. {
  91. return $this->surname . ' ' . $this->name;
  92. }
  93. /**
  94. * @return \yii\db\ActiveQuery
  95. */
  96. public function getMuseums()
  97. {
  98. return $this->hasMany(Museum::className(), ['expertId' => 'id']);
  99. }
  100. /**
  101. * @return \yii\db\ActiveQuery
  102. */
  103. public function getGroup()
  104. {
  105. return $this->hasOne(Group::className(), ['id' => 'groupId']);
  106. }
  107. public function beforeSave($insert)
  108. {
  109. if (parent::beforeSave($insert)) {
  110. if( $insert || $this->passwordFirst!='' ) {
  111. $this->password = $this->decrypt($this->passwordFirst);
  112. $this->generateAuthKey();
  113. }
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. }
  119. public function generateAuthKey()
  120. {
  121. $this->authKey = Yii::$app->security->generateRandomString();
  122. }
  123. public function getGroupName()
  124. {
  125. return $this->group->name;
  126. }
  127. public function getFullname()
  128. {
  129. return $this->surname . ' ' . $this->name;
  130. }
  131. public static function findIdentity($id)
  132. {
  133. return static::findOne($id);
  134. }
  135. public static function findIdentityByAccessToken($token, $type = null)
  136. {
  137. return static::findOne(['access_token' => $token]);
  138. }
  139. public static function findByUsername($usename)
  140. {
  141. return static::findOne(['email' => $usename]);
  142. }
  143. public function validatePassword($pass)
  144. {
  145. return $this->decrypt($pass)==$this->password;
  146. }
  147. public function getId()
  148. {
  149. return $this->id;
  150. }
  151. public function getAuthKey()
  152. {
  153. return $this->authKey;
  154. }
  155. public function validateAuthKey($authKey)
  156. {
  157. return $this->authKey === $authKey;
  158. }
  159. private function decrypt( $string ){
  160. return crypt($string, "$5\$rounds=132000\$".$this->email);
  161. }
  162. }