/** @inheritdoc */ public function rules() { return ['emailRequired' => ['email', 'required'], 'emailPattern' => ['email', 'email'], 'emailExist' => ['email', 'exist', 'targetClass' => User::className()], 'emailConfirmed' => ['email', function () { if ($this->user != null && $this->user->getIsConfirmed()) { $this->addError('email', Yii::t('user', 'This account has already been confirmed')); } }]]; }
/** @inheritdoc */ public function rules() { return ['requiredFields' => [['login', 'password'], 'required'], 'loginTrim' => ['login', 'trim'], 'passwordValidate' => ['password', function ($attribute) { if ($this->user === null || !Password::validate($this->password, $this->user->password_hash)) { $this->addError($attribute, Yii::t('user', 'Invalid login or password')); } }], 'confirmationValidate' => ['login', function ($attribute) { if ($this->user !== null) { $confirmationRequired = $this->module->enableConfirmation && !$this->module->enableUnconfirmedLogin; if ($confirmationRequired && !$this->user->getIsConfirmed()) { $this->addError($attribute, Yii::t('user', 'You need to confirm your email address')); } if ($this->user->getIsBlocked()) { $this->addError($attribute, Yii::t('user', 'Your account has been blocked')); } } }], 'rememberMe' => ['rememberMe', 'boolean']]; }
/** @inheritdoc */ public function rules() { return ['usernameRequired' => ['username', 'required'], 'usernameTrim' => ['username', 'filter', 'filter' => 'trim'], 'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255], 'usernamePattern' => ['username', 'match', 'pattern' => '/^[-a-zA-Z0-9_\\.@]+$/'], 'emailRequired' => ['email', 'required'], 'emailTrim' => ['email', 'filter', 'filter' => 'trim'], 'emailPattern' => ['email', 'email'], 'emailUsernameUnique' => [['email', 'username'], 'unique', 'when' => function ($model, $attribute) { return $this->user->{$attribute} != $model->{$attribute}; }, 'targetClass' => User::className()], 'newPasswordLength' => ['new_password', 'string', 'min' => 6], 'currentPasswordRequired' => ['current_password', 'required'], 'currentPasswordValidate' => ['current_password', function ($attr) { if (!Password::validate($this->{$attr}, $this->user->password_hash)) { $this->addError($attr, Yii::t('user', 'Current password is not valid')); } }]]; }
/** @inheritdoc */ public function rules() { return ['emailTrim' => ['email', 'filter', 'filter' => 'trim'], 'emailRequired' => ['email', 'required'], 'emailPattern' => ['email', 'email'], 'emailExist' => ['email', 'exist', 'targetClass' => User::className(), 'message' => Yii::t('user', 'There is no user with this email address')], 'emailUnconfirmed' => ['email', function ($attribute) { $this->user = $this->finder->findUserByEmail($this->email); if ($this->user !== null && $this->module->enableConfirmation && !$this->user->isConfirmed) { $this->addError($attribute, Yii::t('user', 'You need to confirm your email address')); } if ($this->user->isBlocked) { $this->addError($attribute, Yii::t('user', 'Your account has been blocked')); } }], 'passwordRequired' => ['password', 'required'], 'passwordLength' => ['password', 'string', 'min' => 6]]; }
/** @inheritdoc */ public function bootstrap($app) { /** @var Module $module */ /** @var \yii\db\ActiveRecord $modelName */ if ($app->hasModule('user') && ($module = $app->getModule('user')) instanceof Module) { Yii::$container->setSingleton(UserFinder::className(), ['userQuery' => \jarrus90\User\models\User::find(), 'profileQuery' => \jarrus90\User\models\Profile::find(), 'tokenQuery' => \jarrus90\User\models\Token::find(), 'accountQuery' => \jarrus90\User\models\Account::find()]); if (!isset($app->get('i18n')->translations['rbac'])) { $app->get('i18n')->translations['rbac'] = ['class' => 'yii\\i18n\\PhpMessageSource', 'basePath' => __DIR__ . '/messages', 'sourceLanguage' => 'en-US']; } if (!isset($app->get('i18n')->translations['user'])) { $app->get('i18n')->translations['user'] = ['class' => PhpMessageSource::className(), 'basePath' => __DIR__ . '/messages', 'sourceLanguage' => 'en-US']; } if (!$app instanceof ConsoleApplication) { $module->controllerNamespace = 'jarrus90\\User\\Controllers'; if (!Yii::$container->has('yii\\web\\User')) { Yii::$container->set('yii\\web\\User', ['enableAutoLogin' => true, 'loginUrl' => ['/user/security/login'], 'identityClass' => \jarrus90\User\models\User::className()]); } $configUrlRule = ['prefix' => $module->urlPrefix, 'rules' => $module->urlRules]; if ($module->urlPrefix != 'user') { $configUrlRule['routePrefix'] = 'user'; } $configUrlRule['class'] = 'yii\\web\\GroupUrlRule'; $rule = Yii::createObject($configUrlRule); $app->urlManager->addRules([$rule], false); if (!$app->has('authClientCollection')) { $app->set('authClientCollection', ['class' => Collection::className()]); } $app->params['admin']['menu']['user'] = function () use($module) { return $module->getAdminMenu(); }; $app->params['admin']['menu']['logout'] = ['label' => Yii::t('user', 'Logout'), 'icon' => '<i class="fa fa-sign-out"></i>', 'url' => '/user/security/logout']; } else { if (empty($app->controllerMap['migrate'])) { $app->controllerMap['migrate']['class'] = 'yii\\console\\controllers\\MigrateController'; } $app->controllerMap['migrate']['migrationNamespaces'][] = 'jarrus90\\User\\migrations'; } if (!$app->authManager instanceof DbManager) { $app->set('authManager', ['class' => DbManager::className(), 'cache' => $app->cache]); } Yii::$container->set('jarrus90\\User\\Mailer', $module->mailer); } }
/** * @param $params * * @return ActiveDataProvider */ public function search($params) { $query = self::find()->addSelect([User::tableName() . '.*', Profile::tableName() . '.name'])->joinWith('profile'); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => []]); $dataProvider->sort->attributes['name'] = ['asc' => [self::tableName() . '.id' => SORT_ASC], 'desc' => [self::tableName() . '.id' => SORT_DESC]]; if ($this->load($params) && $this->validate()) { if ($this->created_at !== null) { $date = strtotime($this->created_at); $query->andFilterWhere(['between', 'created_at', $date, $date + 3600 * 24]); } if (!is_null($this->name) && $this->name != '') { $clientName[] = explode(' ', $this->name); foreach ($clientName as $item) { $query->andFilterWhere(['like', Profile::tableName() . '.name', $item]); /*$query->andFilterWhere(['or', ['like', Profile::tableName() . '.name', $item], ['like', Profile::tableName() . '.surname', $item] ]);*/ } } $query->andFilterWhere(['like', 'email', $this->email]); } return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getUser() { return $this->hasOne(User::className(), ['id' => 'user_id']); }
/** * Validation rules * @return array */ public function rules() { return ['required' => [['content', 'from_id'], 'required', 'on' => ['create', 'update']], 'safeSearch' => [['content', 'from_id', 'created_at', 'parent_id', 'user_name'], 'safe', 'on' => ['search']], 'userExists' => ['from_id', 'exist', 'targetClass' => User::className(), 'targetAttribute' => 'id', 'on' => ['create', 'update']], 'parentExists' => ['parent_id', 'exist', 'targetClass' => Comment::className(), 'targetAttribute' => 'id', 'on' => ['create', 'update']]]; }
/** * Loads attributes to the user model. You should override this method if you are going to add new fields to the * registration form. You can read more in special guide. * * By default this method set all attributes of this model to the attributes of User model, so you should properly * configure safe attributes of your User model. * * @param User $user */ protected function loadAttributes(User $user) { $user->setAttributes($this->attributes); }
/** * Displays page where user can create new account that will be connected to social account. * * @param string $code * * @return string * @throws NotFoundHttpException */ public function actionConnect($code) { $account = $this->finder->findAccount()->byCode($code)->one(); if ($account === null || $account->getIsConnected()) { throw new NotFoundHttpException(); } /** @var User $user */ $user = Yii::createObject(['class' => User::className(), 'scenario' => 'connect', 'username' => $account->username, 'email' => $account->email]); $event = $this->getConnectEvent($account, $user); $this->trigger(self::EVENT_BEFORE_CONNECT, $event); if ($user->load(Yii::$app->request->post()) && $user->create()) { $account->connect($user); $this->trigger(self::EVENT_AFTER_CONNECT, $event); Yii::$app->user->login($user, $this->module->rememberFor); return $this->goBack(); } return $this->render('connect', ['model' => $user, 'account' => $account]); }
/** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'index' page. * * @return mixed */ public function actionCreate() { /** @var User $user */ $user = Yii::createObject(['class' => User::className(), 'scenario' => 'create']); $event = $this->getUserEvent($user); $this->performAjaxValidation($user); $this->trigger(self::EVENT_BEFORE_CREATE, $event); if ($user->load(Yii::$app->request->post()) && $user->create()) { Yii::$app->getSession()->setFlash('success', Yii::t('user', 'User has been created')); $this->trigger(self::EVENT_AFTER_CREATE, $event); return $this->redirect(['update', 'id' => $user->id]); } return $this->render('create', ['user' => $user]); }
public function rules() { return ['required' => [['content', 'created_by'], 'required', 'on' => ['create', 'update']], 'safeSearch' => [['content'], 'safe', 'on' => ['search']], 'postExists' => ['post_id', 'exist', 'targetClass' => Post::className(), 'targetAttribute' => 'id'], 'parentExists' => ['parent_id', 'exist', 'targetClass' => self::className(), 'targetAttribute' => 'id'], 'creatorExists' => ['created_by', 'exist', 'targetClass' => User::className(), 'targetAttribute' => 'id'], 'blockerExists' => ['blocked_by', 'exist', 'targetClass' => User::className(), 'targetAttribute' => 'id'], ['is_blocked', 'safe'], ['blocked_at', 'integer']]; }
/** * Tries to find user or create a new one. * * @param Account $account * * @return User|bool False when can't create user. */ protected static function fetchUser(Account $account) { $user = static::getUserFinder()->findUserByEmail($account->email); if (null !== $user) { return $user; } $user = \Yii::createObject(['class' => User::className(), 'scenario' => 'connect', 'username' => $account->username, 'email' => $account->email]); if (!$user->validate(['email'])) { $account->email = null; } if (!$user->validate(['username'])) { $account->username = null; } return $user->create() ? $user : false; }