svyatoslav-kubakh /
postfix-admin
| 1 | <?php |
||
| 2 | namespace common\models; |
||
| 3 | |||
| 4 | use Yii; |
||
| 5 | use yii\base\NotSupportedException; |
||
| 6 | use yii\behaviors\TimestampBehavior; |
||
| 7 | use yii\db\ActiveRecord; |
||
| 8 | use yii\web\IdentityInterface; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * User model |
||
| 12 | * |
||
| 13 | * @property integer $id |
||
| 14 | * @property string $username |
||
| 15 | * @property string $password_hash |
||
| 16 | * @property string $auth_key |
||
| 17 | * @property integer $status |
||
| 18 | * @property integer $created_at |
||
| 19 | * @property integer $updated_at |
||
| 20 | * @property string $password write-only password |
||
| 21 | */ |
||
| 22 | class User extends ActiveRecord implements IdentityInterface |
||
| 23 | { |
||
| 24 | const STATUS_DELETED = 0; |
||
| 25 | const STATUS_ACTIVE = 10; |
||
| 26 | |||
| 27 | |||
| 28 | /** |
||
| 29 | * @inheritdoc |
||
| 30 | */ |
||
| 31 | public static function tableName() |
||
| 32 | { |
||
| 33 | return '{{%users}}'; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @inheritdoc |
||
| 38 | */ |
||
| 39 | public function behaviors() |
||
| 40 | { |
||
| 41 | return [ |
||
| 42 | TimestampBehavior::class, |
||
| 43 | ]; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @inheritdoc |
||
| 48 | */ |
||
| 49 | public function rules() |
||
| 50 | { |
||
| 51 | return [ |
||
| 52 | [['status', 'username'], 'required'], |
||
| 53 | [['status'], 'default', 'value' => self::STATUS_ACTIVE], |
||
| 54 | [['status'], 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]], |
||
| 55 | [['username'], 'string'], |
||
| 56 | [['username'], 'unique'], |
||
| 57 | ]; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @inheritdoc |
||
| 62 | */ |
||
| 63 | public static function findIdentity($id) |
||
| 64 | { |
||
| 65 | return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @inheritdoc |
||
| 70 | */ |
||
| 71 | public static function findIdentityByAccessToken($token, $type = null) |
||
| 72 | { |
||
| 73 | throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Finds user by username |
||
| 78 | * |
||
| 79 | * @param string $username |
||
| 80 | * @return static|null |
||
| 81 | */ |
||
| 82 | public static function findByUsername($username) |
||
| 83 | { |
||
| 84 | return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @inheritdoc |
||
| 89 | */ |
||
| 90 | public function getId() |
||
| 91 | { |
||
| 92 | return $this->getPrimaryKey(); |
||
|
0 ignored issues
–
show
|
|||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @inheritdoc |
||
| 97 | */ |
||
| 98 | public function getAuthKey() |
||
| 99 | { |
||
| 100 | return $this->auth_key; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @inheritdoc |
||
| 105 | */ |
||
| 106 | public function validateAuthKey($authKey) |
||
| 107 | { |
||
| 108 | return $this->getAuthKey() === $authKey; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Validates password |
||
| 113 | * |
||
| 114 | * @param string $password password to validate |
||
| 115 | * @return bool if password provided is valid for current user |
||
| 116 | */ |
||
| 117 | public function validatePassword($password) |
||
| 118 | { |
||
| 119 | return Yii::$app->security->validatePassword($password, $this->password_hash); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Generates password hash from password and sets it to the model |
||
| 124 | * @param string $password |
||
| 125 | */ |
||
| 126 | public function setPassword($password) |
||
| 127 | { |
||
| 128 | if (!$password) { |
||
| 129 | return; |
||
| 130 | } |
||
| 131 | $this->password_hash = Yii::$app->security->generatePasswordHash($password); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Generates "remember me" authentication key |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | public function generateAuthKey() |
||
| 139 | { |
||
| 140 | return Yii::$app->security->generateRandomString(); |
||
| 141 | } |
||
| 142 | } |
||
| 143 |