PageRenderTime 32ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/models/User.php

https://gitlab.com/Mrowa96/fluid-base-basic
PHP | 207 lines | 157 code | 32 blank | 18 comment | 3 complexity | 22a619f8db94e86b5271ee08c8598d7f MD5 | raw file
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\NotSupportedException;
  5. use yii\behaviors\TimestampBehavior;
  6. use yii\db\ActiveRecord;
  7. use yii\web\IdentityInterface;
  8. /**
  9. * This is the model class for table "{{%user}}".
  10. *
  11. * @property string username
  12. * @property string name
  13. * @property string email
  14. * @property string avatar
  15. * @property bool|string created_at
  16. * @property string role
  17. * @property string roleName
  18. * @property string language
  19. * @property mixed id
  20. * @property mixed auth_key
  21. * @property string password_reset_token
  22. * @property string password_hash
  23. * @property bool isAdmin
  24. * @property bool deletable
  25. */
  26. class User extends ActiveRecord implements IdentityInterface
  27. {
  28. const STATUS_DELETED = 0;
  29. const STATUS_ACTIVE = 10;
  30. public $isAdmin = false;
  31. public $password;
  32. public $password_repeat;
  33. public static function tableName(){
  34. return '{{%user}}';
  35. }
  36. public function behaviors(){
  37. return [
  38. TimestampBehavior::className(),
  39. ];
  40. }
  41. public function rules(){
  42. return [
  43. ['status', 'default', 'value' => self::STATUS_ACTIVE],
  44. ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
  45. ];
  46. }
  47. public function beforeSave($insert){
  48. unset($this->role);
  49. unset($this->roleName);
  50. return parent::beforeSave($insert);
  51. }
  52. public function afterFind(){
  53. $auth = Yii::$app->authManager;
  54. $roles = $auth->getRolesByUser($this->id);
  55. reset($roles);
  56. $this->created_at = date("M, Y", $this->created_at);
  57. $this->role = $roles[key($roles)]->name;
  58. $this->roleName = $roles[key($roles)]->description;
  59. if(isset($roles['admin'])){
  60. $this->isAdmin = true;
  61. }
  62. parent::afterFind();
  63. }
  64. public function attributes(){
  65. return array_merge(
  66. parent::attributes(),
  67. ['role', 'roleName']
  68. );
  69. }
  70. public function attributeLabels(){
  71. return [
  72. 'username' => Yii::t("user", "Username"),
  73. 'name' => Yii::t("user", "Name"),
  74. 'email' => Yii::t("user", "Adres email"),
  75. 'password' => Yii::t("user", "Password"),
  76. 'password_repeat' => Yii::t("user", "Password repeat"),
  77. 'role' => Yii::t("user", "Role"),
  78. 'roleName' => Yii::t("user", "Rolename"),
  79. 'language' => Yii::t("user", "Language"),
  80. 'created_at' => Yii::t("user", "Created"),
  81. 'all' => Yii::t("user", "All users"),
  82. 'add_user' => Yii::t("user", "Add user"),
  83. ];
  84. }
  85. public function getGridViewExample(){
  86. return [
  87. [
  88. 'property' => 'avatar',
  89. 'type' => 'image',
  90. 'value' => 1,
  91. ],
  92. [
  93. 'property' => 'username',
  94. ],
  95. [
  96. 'property' => 'name',
  97. 'value' => 1,
  98. ],
  99. [
  100. 'property' => 'email',
  101. ],
  102. [
  103. 'property' => 'roleName',
  104. ],
  105. [
  106. 'property' => 'created_at',
  107. ]
  108. ];
  109. }
  110. public static function findIdentity($id){
  111. return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  112. }
  113. public static function findIdentityByAccessToken($token, $type = null){
  114. throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
  115. }
  116. public static function findByUsername($username){
  117. return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
  118. }
  119. public static function findByPasswordResetToken($token){
  120. if (!static::isPasswordResetTokenValid($token)) {
  121. return null;
  122. }
  123. return static::findOne([
  124. 'password_reset_token' => $token,
  125. 'status' => self::STATUS_ACTIVE,
  126. ]);
  127. }
  128. public static function isPasswordResetTokenValid($token){
  129. if (empty($token)) {
  130. return false;
  131. }
  132. $timestamp = (int) substr($token, strrpos($token, '_') + 1);
  133. $expire = Yii::$app->params['user.passwordResetTokenExpire'];
  134. return $timestamp + $expire >= time();
  135. }
  136. public function getId(){
  137. return $this->getPrimaryKey();
  138. }
  139. public function getAuthKey(){
  140. return $this->auth_key;
  141. }
  142. public function validateAuthKey($authKey){
  143. return $this->getAuthKey() === $authKey;
  144. }
  145. public function validatePassword($password){
  146. return Yii::$app->security->validatePassword($password, $this->password_hash);
  147. }
  148. public function setPassword($password){
  149. $this->password_hash = Yii::$app->security->generatePasswordHash($password);
  150. }
  151. public function generateAuthKey(){
  152. $this->auth_key = Yii::$app->security->generateRandomString();
  153. }
  154. public function generatePasswordResetToken(){
  155. $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
  156. }
  157. public function removePasswordResetToken(){
  158. $this->password_reset_token = null;
  159. }
  160. public static function getAvatars(){
  161. $avatars = [
  162. "/images/avatars/men1.png",
  163. "/images/avatars/men2.png",
  164. "/images/avatars/men3.png",
  165. "/images/avatars/men4.png",
  166. "/images/avatars/men5.png",
  167. "/images/avatars/woman1.png",
  168. "/images/avatars/woman2.png",
  169. "/images/avatars/woman3.png",
  170. "/images/avatars/woman4.png",
  171. "/images/avatars/woman5.png",
  172. ];
  173. return $avatars;
  174. }
  175. }