/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->attributes = $this->attributes; $user->status = User::STATUS_NEW; $user->setPassword($this->password); $user->generateAuthKey(); if ($user->save()) { return $user; } } return null; }
/** * Hide link if user hasn't access to it * * @inheritdoc */ public static function a($text, $url = null, $options = []) { if (in_array($url, [null, '', '#'])) { return parent::a($text, $url, $options); } return User::canRoute($url) ? parent::a($text, $url, $options) : ''; }
/** * Finds user by [[username]] * * @return User|null */ public function getUser() { if ($this->_user === false) { $this->_user = User::findByEmailOrUserName($this->email); } return $this->_user; }
public function getUser() { if ($this->_user === false) { $this->_user = User::findOne(['status' => User::STATUS_ACTIVE, 'email' => $this->email]); } return $this->_user; }
/** * Check if user has access to current route * * @param Action $action the action to be executed. * * @return boolean whether the action should continue to be executed. */ public function beforeAction($action) { if ($action->id == 'captcha') { return true; } $route = '/' . $action->uniqueId; if (Route::isFreeAccess($route, $action)) { return true; } if (Yii::$app->user->isGuest) { $this->denyAccess(); } // If user has been deleted, then destroy session and redirect to home page if (!Yii::$app->user->isGuest and Yii::$app->user->identity === null) { Yii::$app->getSession()->destroy(); $this->denyAccess(); } // Superadmin owns everyone if (Yii::$app->user->identity->isSuperadmin) { return true; } if (Yii::$app->user->identity and Yii::$app->user->identity->status != User::STATUS_ACTIVE) { Yii::$app->user->logout(); Yii::$app->getResponse()->redirect(Yii::$app->getHomeUrl()); } if (User::canRoute($route)) { return true; } if (isset($this->denyCallback)) { call_user_func($this->denyCallback, null, $action); } else { $this->denyAccess(); } return false; }
protected function findFreeUsername($username, $n = '') { $exists = User::findOne(['username' => $username . $n]); if ($exists) { $n = $n == '' ? 2 : $n + 1; return $this->findFreeUsername($username, $n); } return $username . $n; }
public function search($params) { $query = User::find(); $query->with(['roles']); if (!Yii::$app->user->identity->isSuperadmin) { $query->where(['superadmin' => 0]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->request->cookies->getValue('_grid_page_size', 20)], 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } if ($this->gridRoleSearch) { $query->joinWith(['roles']); } $query->andFilterWhere(['id' => $this->id, 'superadmin' => $this->superadmin, 'status' => $this->status, Yii::$app->getModule('user')->auth_item_table . '.name' => $this->gridRoleSearch, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'email', $this->email]); return $dataProvider; }
/** * @param int $id - User ID * * @return \yii\web\Response */ public function actionSetRoles($id) { if (!Yii::$app->user->identity->isSuperadmin and Yii::$app->user->id == $id) { Yii::$app->session->setFlash('error', 'You can not change own permissions'); return $this->redirect(['set', 'id' => $id]); } $oldAssignments = array_keys(Role::getUserRoles($id)); // To be sure that user didn't attempt to assign himself some unavailable roles $newAssignments = array_intersect(Role::getAvailableRoles(Yii::$app->user->identity->isSuperAdmin, true), Yii::$app->request->post('roles', [])); $toAssign = array_diff($newAssignments, $oldAssignments); $toRevoke = array_diff($oldAssignments, $newAssignments); foreach ($toRevoke as $role) { User::revokeRole($id, $role); } foreach ($toAssign as $role) { User::assignRole($id, $role); } Yii::$app->session->setFlash('success', 'Saved'); return $this->redirect(['set', 'id' => $id]); }
public function search($params) { $query = UserVisitLog::find(); $query->joinWith(['user']); // Don't let non-superadmin view superadmin activity if (!Yii::$app->user->isSuperadmin) { $query->andWhere([User::tableName() . '.superadmin' => 0]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => Yii::$app->request->cookies->getValue('_grid_page_size', 20)], 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } if ($this->visit_time) { $tmp = explode(' - ', $this->visit_time); if (isset($tmp[0], $tmp[1])) { $query->andFilterWhere(['between', static::tableName() . '.visit_time', strtotime($tmp[0]), strtotime($tmp[1])]); } } $query->andFilterWhere([$this->tableName() . '.id' => $this->id]); $query->andFilterWhere(['like', User::tableName() . '.username', $this->user_id])->andFilterWhere(['like', static::tableName() . '.ip', $this->ip])->andFilterWhere(['like', static::tableName() . '.os', $this->os])->andFilterWhere(['like', static::tableName() . '.browser', $this->browser])->andFilterWhere(['like', static::tableName() . '.language', $this->language]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getUser() { return $this->hasOne(User::className(), ['id' => 'user_id']); }
<h1><?php echo Html::encode($this->title); ?> </h1> <p> <?php echo Yii::$app->user->can('userUpdate', ['user' => $model]) ? Html::a(Yii::t('yii', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) : ''; ?> <?php echo Yii::$app->user->can('userDelete') ? Html::a(Yii::t('yii', 'Delete'), ['delete', 'id' => $model->id], ['class' => 'btn btn-danger', 'data' => ['confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'), 'method' => 'post']]) : ''; ?> </p> <?php echo DetailView::widget(['model' => $model, 'attributes' => ['id', 'username', 'email:email', ['attribute' => 'photo', 'value' => $model->photo ? $model->photo : $assets->baseUrl . '/img/' . $model->getDefaultPhoto() . '.png', 'format' => ['image', ['width' => 200, 'height' => 200]]], ['attribute' => 'sex', 'value' => User::getSexArray()[$model->sex]], ['attribute' => 'status', 'value' => User::getStatusArray()[$model->status]], 'created_at:datetime', 'updated_at:datetime']]); ?> <h2><?php echo Yii::t('users', 'USER_PERMISSIONS'); ?> </h2> <p> <?php echo Yii::$app->user->can('userPermissions', ['user' => $model]) ? Html::a(Yii::t('yii', 'Update'), ['permissions', 'id' => $model->id], ['class' => 'btn btn-primary']) : ''; ?> </p> <?php echo PermissionsTreeWidget::widget(['user' => $model]); ?>
<h1><?php echo Html::encode($this->title); ?> </h1> <p> <?php echo Yii::$app->user->can('userCreate') ? Html::a(Yii::t('users', 'CREATE'), ['create'], ['class' => 'btn btn-success']) : ''; ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'id', 'username', 'email:email', ['attribute' => 'sex', 'value' => function ($data) { return User::getSexArray()[$data->sex]; }], ['attribute' => 'status', 'value' => function ($data) { return User::getStatusArray()[$data->status]; }], 'created_at:datetime', 'updated_at:datetime', ['class' => 'yii\\grid\\ActionColumn', 'template' => '{view} {update} {delete} {permissions}', 'buttons' => ['view' => function ($url, $model, $key) { if (!Yii::$app->user->can('userView', ['user' => $model])) { return ''; } $options = ['title' => Yii::t('yii', 'View'), 'aria-label' => Yii::t('yii', 'View'), 'data-pjax' => '0']; return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, $options); }, 'update' => function ($url, $model, $key) { if (!Yii::$app->user->can('userUpdate', ['user' => $model])) { return ''; } $options = ['title' => Yii::t('yii', 'Update'), 'aria-label' => Yii::t('yii', 'Update'), 'data-pjax' => '0']; return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options); }, 'permissions' => function ($url, $model, $key) { if (!Yii::$app->user->can('userPermissions', ['user' => $model])) { return '';
<?php echo GridPageSize::widget(['pjaxId' => 'user-grid-pjax']); ?> </div> </div> <?php Pjax::begin(['id' => 'user-grid-pjax']); ?> <?php echo GridView::widget(['id' => 'user-grid', 'dataProvider' => $dataProvider, 'pager' => ['options' => ['class' => 'pagination pagination-sm'], 'hideOnSinglePage' => true, 'lastPageLabel' => '>>', 'firstPageLabel' => '<<'], 'filterModel' => $searchModel, 'layout' => '{items}<div class="row"><div class="col-sm-8">{pager}</div><div class="col-sm-4 text-right">{summary}' . GridBulkActions::widget(['gridId' => 'user-grid', 'actions' => [Url::to(['bulk-activate', 'attribute' => 'status']) => GridBulkActions::t('app', 'Activate'), Url::to(['bulk-deactivate', 'attribute' => 'status']) => GridBulkActions::t('app', 'Deactivate'), '----' => [Url::to(['bulk-delete']) => GridBulkActions::t('app', 'Delete')]]]) . '</div></div>', 'columns' => [['class' => 'yii\\grid\\SerialColumn', 'options' => ['style' => 'width:30px']], ['class' => 'kuzmiand\\users\\components\\StatusColumn', 'attribute' => 'superadmin', 'visible' => Yii::$app->user->identity->isSuperadmin, 'options' => ['style' => 'width:100px']], ['attribute' => 'username', 'value' => function (User $model) { return Html::a($model->username, ['view', 'id' => $model->id], ['data-pjax' => 0]); }, 'format' => 'raw', 'options' => ['style' => 'width:auto']], ['attribute' => 'email', 'format' => 'raw', 'visible' => User::hasPermission('viewUserEmail'), 'options' => ['style' => 'width:auto']], ['attribute' => 'gridRoleSearch', 'filter' => ArrayHelper::map(Role::getAvailableRoles(Yii::$app->user->identity->isSuperAdmin), 'name', 'description'), 'value' => function (User $model) { return implode(', ', ArrayHelper::map($model->roles, 'name', 'description')); }, 'format' => 'raw', 'visible' => User::hasPermission('viewUserRoles')], ['attribute' => 'registration_ip', 'value' => function (User $model) { return Html::a($model->registration_ip, "http://ipinfo.io/" . $model->registration_ip, ["target" => "_blank"]); }, 'format' => 'raw', 'visible' => User::hasPermission('viewRegistrationIp'), 'options' => ['style' => 'width:100px']], ['value' => function (User $model) { return GhostHtml::a('Roles and permissions', ['/user/user-permission/set', 'id' => $model->id], ['class' => 'btn btn-sm btn-primary', 'data-pjax' => 0]); }, 'format' => 'raw', 'visible' => User::canRoute('/user/user-permission/set'), 'options' => ['width' => '165px']], ['class' => 'kuzmiand\\users\\components\\StatusColumn', 'attribute' => 'status', 'optionsArray' => [[User::STATUS_ACTIVE, 'Active', 'success'], [User::STATUS_NEW, 'Inactive', 'warning'], [User::STATUS_BLOCKED, 'Blocked', 'danger']]], ['class' => 'yii\\grid\\CheckboxColumn', 'options' => ['style' => 'width:30px']], ['class' => 'yii\\grid\\ActionColumn', 'contentOptions' => ['style' => 'width:70px; text-align:center;']]]]); ?> <?php Pjax::end(); ?> </div> </div> </div>
/** * 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 ($this->_model === false) { $this->_model = User::findOne($id); } if ($this->_model !== null) { return $this->_model; } throw new NotFoundHttpException('The requested page does not exist.'); }
<div class="row"> <div class="col-xs-12 col-md-7"> <div class="panel panel-default"> <div class="panel-heading"><?php echo Yii::t('users', 'PERSONAL_INFO'); ?> </div> <div class="panel-body"> <?php $form = ActiveForm::begin(['id' => 'form-profile']); ?> <?php echo $form->field($model, 'username'); ?> <?php echo $form->field($model, 'sex')->dropDownList(User::getSexArray()); ?> <?php echo $form->field($model, 'photo')->widget(Widget::className(), ['uploadUrl' => Url::toRoute('/user/user/uploadPhoto')]); ?> <div class="form-group"> <?php echo Html::submitButton(Yii::t('users', 'SAVE'), ['class' => 'btn btn-primary', 'name' => 'profile-button']); ?> </div> <?php ActiveForm::end(); ?> </div> </div>
public function getUser() { return User::findOne(['email' => $this->email]); }
echo $form->field($model, 'username')->textInput(['maxlength' => 255]); ?> <?php echo $form->field($model, 'email')->textInput(['maxlength' => 255]); ?> <?php echo $form->field($model, 'photo')->widget(Widget::className(), ['uploadUrl' => Url::toRoute('/user/user/uploadPhoto')]); ?> <?php echo $form->field($model, 'sex')->dropDownList(User::getSexArray()); ?> <?php echo $form->field($model, 'status')->dropDownList(User::getStatusArray()); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? Yii::t('users', 'CREATE') : Yii::t('users', 'UPDATE'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div>