/** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = User::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Creates a form model given a token. * * @param string $token * @param array $config name-value pairs that will be used to initialize the object properties * @throws \yii\base\InvalidParamException if token is empty or not valid */ public function __construct($token, $config = []) { if (empty($token) || !is_string($token)) { throw new InvalidParamException(Yii::t('maddoger/user', 'Password reset token cannot be blank.')); } $this->_user = User::findByPasswordResetToken($token); if (!$this->_user) { throw new InvalidParamException(Yii::t('maddoger/user', 'Wrong password reset token.')); } parent::__construct($config); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = User::find(); $query->joinWith('profile'); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->sort->attributes['name'] = ['asc' => [UserProfile::tableName() . '.[[first_name]]' => SORT_ASC, UserProfile::tableName() . '.[[last_name]]' => SORT_ASC, UserProfile::tableName() . '.[[patronymic]]' => SORT_ASC], 'desc' => [UserProfile::tableName() . '.[[first_name]]' => SORT_DESC, UserProfile::tableName() . '.[[last_name]]' => SORT_DESC, UserProfile::tableName() . '.[[patronymic]]' => SORT_DESC], 'default' => SORT_ASC]; $dataProvider->sort->defaultOrder = ['username' => SORT_ASC]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, User::tableName() . '.[[role]]' => $this->role, User::tableName() . '.[[status]]' => $this->status, User::tableName() . '.[[last_visit_at]]' => $this->last_visit_at]); $query->andFilterWhere(['like', User::tableName() . '.[[username]]', $this->username])->andFilterWhere(['like', User::tableName() . '.[[email]]', $this->email]); $query->andFilterWhere(['or', ['like', UserProfile::tableName() . '.[[first_name]]', $this->name], ['like', UserProfile::tableName() . '.[[last_name]]', $this->name], ['like', UserProfile::tableName() . '.[[patronymic]]', $this->name]]); return $dataProvider; }
/** * Sends an email with a link, for resetting the password. * * @return boolean whether the email was send */ public function sendEmail() { /* @var $user User */ $user = User::findOne(['status' => User::STATUS_ACTIVE, 'email' => $this->email]); if ($user) { if (!User::isPasswordResetTokenValid($user->password_reset_token)) { $user->generatePasswordResetToken(); } if ($user->save()) { $resetUrl = $this->resetUrl; $resetUrl['token'] = $user->password_reset_token; return Yii::$app->mailer->compose($this->mailView, ['user' => $user, 'resetUrl' => $resetUrl])->setTo($this->email)->setSubject(Yii::t('maddoger/user', 'Password reset for {app}', ['app' => \Yii::$app->name]))->send(); } } return false; }
/** * @return \yii\db\ActiveQuery */ public function getUser() { return $this->hasOne(User::className(), ['id' => 'user_id']); }
<?php use maddoger\user\common\models\User; use yii\grid\GridView; use yii\helpers\Html; /* @var $this yii\web\View */ /* @var $searchModel maddoger\user\common\models\search\UserSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = Yii::t('maddoger/user', 'Users'); $this->params['breadcrumbs'][] = $this->title; ?> <div class="user-index"> <div class="panel panel-default"> <div class="panel-body"> <p> <?php echo Html::a('<i class="glyphicon glyphicon-plus"></i> ' . Yii::t('maddoger/user', 'Create user'), ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'username', 'email:email', ['attribute' => 'name', 'value' => 'profile.name'], ['attribute' => 'role', 'filter' => User::getRoles(), 'value' => 'roleDescription'], ['attribute' => 'status', 'filter' => User::getStatuses(), 'value' => 'statusDescription'], 'last_visit_at:datetime', ['class' => 'yii\\grid\\ActionColumn']]]); ?> </div> </div> </div>
?> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <div class="panel-title"><?php echo Yii::t('maddoger/user', 'Status'); ?> </div> </div> <div class="panel-body"> <?php echo $form->field($userModel, 'role')->dropDownList(User::getRoles()); ?> <?php echo $form->field($userModel, 'status')->dropDownList(User::getStatuses()); ?> </div> </div> </div> </div> <div class="form-group"> <div class="btn-group"> <?php echo Html::submitButton(Yii::t('maddoger/user', 'Save'), ['class' => $userModel->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> <?php echo Html::submitButton(Yii::t('maddoger/user', 'Save and exit'), ['name' => 'redirect', 'value' => 'exit', 'class' => 'btn btn-default']); ?> <?php