make(SecurityHelper::class); if ($insert) { $this->setAttribute('auth_key', $security->generateRandomString()); if (Yii::$app instanceof Application) { $this->setAttribute('registration_ip', Yii::$app->request->getUserIP()); } } if (!empty($this->password)) { $this->setAttribute( 'password_hash', $security->generatePasswordHash($this->password, $this->getModule()->blowfishCost) ); } return parent::beforeSave($insert); } /** * @inheritdoc */ public static function tableName() { return '{{%user}}'; } /** * @inheritdoc */ public function behaviors() { return [ TimestampBehavior::className(), ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'username' => Yii::t('user', 'Username'), 'email' => Yii::t('user', 'Email'), 'registration_ip' => Yii::t('user', 'Registration ip'), 'unconfirmed_email' => Yii::t('user', 'New email'), 'password' => Yii::t('user', 'Password'), 'created_at' => Yii::t('user', 'Registration time'), 'confirmed_at' => Yii::t('user', 'Confirmation time'), ]; } /** * @inheritdoc */ public function scenarios() { return ArrayHelper::merge( parent::scenarios(), [ 'register' => ['username', 'email', 'password'], 'connect' => ['username', 'email'], 'create' => ['username', 'email', 'password'], 'update' => ['username', 'email', 'password'], 'settings' => ['username', 'email', 'password'], ] ); } /** * @inheritdoc */ public function rules() { return [ // username rules 'usernameRequired' => ['username', 'required', 'on' => ['register', 'create', 'connect', 'update']], 'usernameMatch' => ['username', 'match', 'pattern' => $this->usernameRegex], 'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255], 'usernameTrim' => ['username', 'trim'], 'usernameUnique' => [ 'username', 'unique', 'message' => Yii::t('user', 'This username has already been taken') ], // email rules 'emailRequired' => ['email', 'required', 'on' => ['register', 'connect', 'create', 'update']], 'emailPattern' => ['email', 'email'], 'emailLength' => ['email', 'string', 'max' => 255], 'emailUnique' => [ 'email', 'unique', 'message' => Yii::t('user', 'This email address has already been taken') ], 'emailTrim' => ['email', 'trim'], // password rules 'passwordRequired' => ['password', 'required', 'on' => ['register']], 'passwordLength' => ['password', 'string', 'min' => 6, 'max' => 72, 'on' => ['register', 'create']], ]; } /** * @inheritdoc */ public function validateAuthKey($authKey) { return $this->getAttribute('auth_key') === $authKey; } /** * @inheritdoc */ public function getId() { return $this->getAttribute('id'); } /** * @inheritdoc */ public function getAuthKey() { return $this->getAttribute('auth_key'); } /** * @inheritdoc */ public static function findIdentity($id) { return static::findOne($id); } /** * @return bool whether is blocked or not. */ public function getIsBlocked() { return $this->blocked_at !== null; } /** * @return bool whether the user is an admin or not */ public function getIsAdmin() { return $this->getAuth()->isAdmin($this->username); } /** * @return bool */ public function getIsConfirmed() { return $this->confirmed_at !== null; } /** * Checks whether a user has a specific role * * @param string $role * * @return bool */ public function hasRole($role) { return $this->getAuth()->hasRole($this->id, $role); } /** * @return \yii\db\ActiveQuery */ public function getProfile() { return $this->hasOne($this->getClassMap()->get(Profile::class), ['user_id' => 'id']); } /** * @return SocialNetworkAccount[] social connected accounts [ 'providerName' => socialAccountModel ] */ public function getSocialNetworkAccounts() { if ($this->connectedAccounts == null) { /** @var SocialNetworkAccount[] $accounts */ $accounts = $this->hasMany($this->getClassMap() ->get(SocialNetworkAccount::class), ['user_id' => 'id']) ->all(); foreach ($accounts as $account) { $this->connectedAccounts[$account->provider] = $account; } } return $this->connectedAccounts; } /** * @return UserQuery */ public static function find() { return new UserQuery(static::class); } /** * @inheritdoc */ public static function findIdentityByAccessToken($token, $type = null) { throw new NotSupportedException('Method "' . __CLASS__ . '::' . __METHOD__ . '" is not implemented.'); } }