Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
25.81% |
8 / 31 |
CRAP | |
10.81% |
8 / 74 |
| User | |
0.00% |
0 / 1 |
|
25.81% |
8 / 31 |
955.48 | |
10.81% |
8 / 74 |
| tableName | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| behaviors | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| rules | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| attributeLabels | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 13 |
|||
| beforeDelete | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| getFullname | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| getParticipant | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| getSurveyLocks | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| getSupervisor | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| getId | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getAuthKey | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setPassword | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| validateAuthKey | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| getStatusName | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 4 |
|||
| find | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| validatePassword | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| generateAuthKey | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| generatePasswordResetToken | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| removePasswordResetToken | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| generateEmailConfirmToken | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| removeEmailConfirmToken | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| findIdentity | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| generateAccessToken | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 2 |
|||
| findIdentityByAccessToken | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| findByUsername | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| findByEmail | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| findByPasswordResetToken | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 5 |
|||
| isPasswordResetTokenValid | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 5 |
|||
| findByEmailConfirmToken | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 5 |
|||
| isEmailConfirmTokenValid | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 5 |
|||
| statusLabels | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| <?php | |
| namespace common\models; | |
| use Yii; | |
| use yii\base\NotSupportedException; | |
| use yii\behaviors\TimestampBehavior; | |
| use yii\db\ActiveRecord; | |
| use yii\web\IdentityInterface; | |
| /** | |
| * User model | |
| * | |
| * @property integer $id | |
| * @property string $username | |
| * @property string $password_hash | |
| * @property string $password_reset_token | |
| * @property string $email | |
| * @property string $email_confirm_token | |
| * @property string $firstname | |
| * @property string $lastname | |
| * @property string $auth_key | |
| * @property string $access_token | |
| * @property integer $status | |
| * @property integer $created_at | |
| * @property integer $updated_at | |
| * @property string $password write-only password | |
| * | |
| * @property Participant $participant | |
| * @property SurveyLock[] $surveyLocks | |
| * @property Supervisor $supervisor | |
| */ | |
| class User extends ActiveRecord implements IdentityInterface | |
| { | |
| const STATUS_DELETED = 0; | |
| const STATUS_INACTIVE = 1; | |
| const STATUS_ACTIVE = 10; | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public static function tableName() | |
| { | |
| return '{{%user}}'; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function behaviors() | |
| { | |
| return [ | |
| TimestampBehavior::className(), | |
| ]; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function rules() | |
| { | |
| return [ | |
| [['auth_key', 'access_token', 'password_hash', 'email', 'firstname', 'lastname'], 'required'], | |
| [['status'], 'integer'], | |
| [['username', 'password_hash', 'password_reset_token', 'email', 'email_confirm_token', 'firstname', 'lastname'], 'string', 'max' => 255], | |
| [['auth_key', 'access_token'], 'string', 'max' => 32], | |
| ['username', 'default'], | |
| ['status', 'default', 'value' => self::STATUS_INACTIVE], | |
| ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]], | |
| [['email'], 'unique'], | |
| [['username'], 'unique'] | |
| ]; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function attributeLabels() | |
| { | |
| return [ | |
| 'id' => Yii::t('app', 'ID'), | |
| 'username' => Yii::t('app', 'Username'), | |
| 'auth_key' => Yii::t('app', 'Authentication key'), | |
| 'password_hash' => Yii::t('app', 'Password hash'), | |
| 'password_reset_token' => Yii::t('app', 'Password reset token'), | |
| 'email' => Yii::t('app', 'Email'), | |
| 'email_confirm_token' => Yii::t('app', 'Email confirm token'), | |
| 'firstname' => Yii::t('app', 'Firstname'), | |
| 'lastname' => Yii::t('app', 'Lastname'), | |
| 'status' => Yii::t('app', 'Status'), | |
| 'statusName' => Yii::t('app', 'Status'), | |
| 'created_at' => Yii::t('app', 'Created at'), | |
| 'updated_at' => Yii::t('app', 'Updated at'), | |
| ]; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function beforeDelete() | |
| { | |
| SurveyLock::deleteAll(['user_id' => $this->id]); | |
| return true; | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function getFullname() | |
| { | |
| return sprintf("%s %s", $this->lastname, $this->firstname); | |
| } | |
| /** | |
| * @return \yii\db\ActiveQuery | |
| */ | |
| public function getParticipant() | |
| { | |
| return $this->hasOne(Participant::className(), ['user_id' => 'id']); | |
| } | |
| /** | |
| * @return \yii\db\ActiveQuery | |
| */ | |
| public function getSurveyLocks() | |
| { | |
| return $this->hasMany(SurveyLock::className(), ['user_id' => 'id']); | |
| } | |
| /** | |
| * @return \yii\db\ActiveQuery | |
| */ | |
| public function getSupervisor() | |
| { | |
| return $this->hasOne(Supervisor::className(), ['user_id' => 'id']); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getId() | |
| { | |
| return $this->getPrimaryKey(); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getAuthKey() | |
| { | |
| return $this->auth_key; | |
| } | |
| /** | |
| * Generates password hash from password and sets it to the model | |
| * | |
| * @param string $password | |
| */ | |
| public function setPassword($password) | |
| { | |
| $this->password_hash = Yii::$app->security->generatePasswordHash($password); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function validateAuthKey($authKey) | |
| { | |
| return $this->getAuthKey() === $authKey; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getStatusName() | |
| { | |
| $statusList = self::statusLabels(); | |
| if(array_key_exists($this->status, $statusList)) { | |
| return $statusList[$this->status]; | |
| } | |
| return null; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| * @return UserQuery | |
| */ | |
| public static function find() | |
| { | |
| return new UserQuery(get_called_class()); | |
| } | |
| /** | |
| * Validates password | |
| * | |
| * @param string $password password to validate | |
| * @return boolean if password provided is valid for current user | |
| */ | |
| public function validatePassword($password) | |
| { | |
| return Yii::$app->security->validatePassword($password, $this->password_hash); | |
| } | |
| /** | |
| * Generates "remember me" authentication key | |
| */ | |
| public function generateAuthKey() | |
| { | |
| $this->auth_key = Yii::$app->security->generateRandomString(); | |
| } | |
| /** | |
| * Generates new password reset token | |
| */ | |
| public function generatePasswordResetToken() | |
| { | |
| $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); | |
| } | |
| /** | |
| * Removes password reset token | |
| */ | |
| public function removePasswordResetToken() | |
| { | |
| $this->password_reset_token = null; | |
| } | |
| /** | |
| * Generates new email confirm token | |
| */ | |
| public function generateEmailConfirmToken() | |
| { | |
| $this->email_confirm_token = Yii::$app->security->generateRandomString() . '_' . time(); | |
| } | |
| /** | |
| * Removes email confirm token | |
| */ | |
| public function removeEmailConfirmToken() | |
| { | |
| $this->email_confirm_token = null; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public static function findIdentity($id) | |
| { | |
| return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); | |
| } | |
| /** | |
| * Generates RESTful access token | |
| */ | |
| public function generateAccessToken() | |
| { | |
| $this->access_token = Yii::$app->security->generateRandomString(); | |
| } | |
| /** | |
| * @inheritdoc | |
| */ | |
| public static function findIdentityByAccessToken($token, $type = null) | |
| { | |
| return static::findOne(['access_token' => $token]); | |
| } | |
| /** | |
| * Finds user by username | |
| * | |
| * @param string $username | |
| * @return static|null | |
| */ | |
| public static function findByUsername($username) | |
| { | |
| return static::findOne(['username' => $username, 'status' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE]]); | |
| } | |
| /** | |
| * Finds user by email | |
| * | |
| * @param string $email | |
| * @return static|null | |
| */ | |
| public static function findByEmail($email) | |
| { | |
| return static::findOne(['email' => $email, 'status' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE]]); | |
| } | |
| /** | |
| * Finds user by password reset token | |
| * | |
| * @param string $token password reset token | |
| * @return static|null | |
| */ | |
| public static function findByPasswordResetToken($token) | |
| { | |
| if (!static::isPasswordResetTokenValid($token)) { | |
| return null; | |
| } | |
| return static::findOne([ | |
| 'password_reset_token' => $token, | |
| 'status' => self::STATUS_ACTIVE, | |
| ]); | |
| } | |
| /** | |
| * Finds out if password reset token is valid | |
| * | |
| * @param string $token password reset token | |
| * @return boolean | |
| */ | |
| public static function isPasswordResetTokenValid($token) | |
| { | |
| if (empty($token)) { | |
| return false; | |
| } | |
| $timestamp = (int) substr($token, strrpos($token, '_') + 1); | |
| $expire = Yii::$app->params['user.passwordResetTokenExpire']; | |
| return $timestamp + $expire >= time(); | |
| } | |
| /** | |
| * Finds user by password reset token | |
| * | |
| * @param string $token password reset token | |
| * @return static|null | |
| */ | |
| public static function findByEmailConfirmToken($token) | |
| { | |
| if (!static::isEmailConfirmTokenValid($token)) { | |
| return null; | |
| } | |
| return static::findOne([ | |
| 'email_confirm_token' => $token, | |
| 'status' => self::STATUS_INACTIVE, | |
| ]); | |
| } | |
| /** | |
| * Finds out if password reset token is valid | |
| * | |
| * @param string $token password reset token | |
| * @return boolean | |
| */ | |
| public static function isEmailConfirmTokenValid($token) | |
| { | |
| if (empty($token)) { | |
| return false; | |
| } | |
| $timestamp = (int) substr($token, strrpos($token, '_') + 1); | |
| $expire = Yii::$app->params['user.emailConfirmTokenExpire']; | |
| return $timestamp + $expire >= time(); | |
| } | |
| public static function statusLabels(){ | |
| return [ | |
| static::STATUS_ACTIVE => Yii::t('app', 'Active'), | |
| static::STATUS_INACTIVE => Yii::t('app', 'Inactive'), | |
| static::STATUS_DELETED => Yii::t('app', 'Deleted'), | |
| ]; | |
| } | |
| } |