/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Users::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $sort = $dataProvider->getSort(); $sort->attributes['officeName'] = ['asc' => ['questionlist_office.name' => SORT_ASC], 'desc' => ['questionlist_office.name' => SORT_DESC], 'label' => 'Имя офиса']; $dataProvider->setSort($sort); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); $query->joinWith(['questionlist_office']); return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); if ($this->roleName) { $this->profile_office_role = $this->roleName; } $query->andFilterWhere(['like', 'profile_id', $this->profile_id])->andFilterWhere(['like', 'profile_office_role', 'manager']); $query->joinWith(['office' => function ($q) { $q->andFilterWhere(['like', 'questionlist_office.name', $this->officeName]); }]); $query->joinWith(['region' => function ($q) { $q->andFilterWhere(['like', 'questionlist_region.name', $this->regionName]); }]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Users::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $sort = $dataProvider->getSort(); $sort->attributes['officeName'] = ['asc' => ['questionlist_office.name' => SORT_ASC], 'desc' => ['questionlist_office.name' => SORT_DESC], 'label' => 'Имя офиса']; $dataProvider->setSort($sort); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); $query->joinWith(['questionlist_office']); return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); if ($this->roleName) { $this->profile_office_role = $this->roleName; } $query->andFilterWhere(['like', 'profile_id', $this->profile_id])->andFilterWhere(['like', 'profile_office_role', $this->profile_office_role]); if ($this->scenario == 'managerSearch') { $userRoles = Users::findAll(['profile_id' => Yii::$app->user->identity->username, 'profile_office_role' => 'commercial_director']); $userRegions = array_values(ArrayHelper::map($userRoles, 'region_id', 'region_id')); $query->andFilterWhere(['like', 'profile_office_role', 'manager']); $query->andFilterWhere(['in', 'questionlist_users_offices.region_id', $userRegions]); } $query->joinWith(['office' => function ($q) { $q->andFilterWhere(['like', 'questionlist_office.name', $this->officeName]); }]); $query->joinWith(['region' => function ($q) { $q->andFilterWhere(['like', 'questionlist_region.name', $this->regionName]); }]); return $dataProvider; }
public function beforeAction($action) { $profileId = Yii::$app->user->identity->username; $userData = Users::findAll(['profile_id' => $profileId]); $roles = array_values(ArrayHelper::map($userData, 'id', 'profile_office_role')); //echo'<pre>';var_dump($action->controller->id);echo'</pre>';die; if (in_array('admin', $roles)) { $access = true; } else { switch ($action->controller->id) { //Назначать управляющих могут только роли коммерческий директор и админ case 'managers': $access = array_intersect(['commercial_director'], $roles); break; // Отвесать на опросные листы могут только менеджеры и коммерческий директор // Отвесать на опросные листы могут только менеджеры и коммерческий директор case 'write-test': $access = array_intersect(['manager', 'commercial_director'], $roles); break; //Назначать управляющих и коммерческих директоров и админов могут только роли админ //Назначать управляющих и коммерческих директоров и админов могут только роли админ case 'users-offices': $access = in_array('admin', $roles); break; // Назначать опросы отделениям могут коммерческие директора и админы // Назначать опросы отделениям могут коммерческие директора и админы case 'answer-list': $access = array_intersect(['commercial_director'], $roles); break; // Редактировать офисы и регионы может только админ // Редактировать офисы и регионы может только админ case 'office': case 'region': $access = in_array('admin', $roles); break; default: $access = true; } } return $access ? true : $this->denyAccess(); }
/** * Finds the Users model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Users the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Users::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Finds the Users model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Users the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { $model = Users::findOne($id); if ($model !== null) { return $model; } else { throw new NotFoundHttpException('Запрашиваемый ресурс не найден'); } }
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\helpers\ArrayHelper; use app\modules\unicred\questionlist\models\Region; use app\modules\unicred\questionlist\models\Users; use app\modules\unicred\questionlist\models\Office; /* @var $this yii\web\View */ /* @var $model app\modules\unicred\questionlist\models\Users */ /* @var $form yii\widgets\ActiveForm */ $userRoles = Users::findAll(['profile_id' => Yii::$app->user->identity->username, 'profile_office_role' => 'commercial_director']); $userRegions = ArrayHelper::map($userRoles, 'region_id', 'region_id'); $offices = Office::find()->where(['region_id' => array_values($userRegions)])->orderBy('region_id')->all(); $offices = ArrayHelper::map($offices, 'id', 'name'); //$regions = ArrayHelper::map(Region::findAll(array_values($userRegions)), 'id', 'name'); ?> <div class="users-offices-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'profile_id')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, "office_id")->dropDownList($offices, [$model->office_id => ['selected' => 'selected'], 'prompt' => 'Выберите ..']); ?>
/** * @param $profile_id * @return array * Возвращает массив ID отделений, где пользователь является управляющим */ protected function getOffiсeIds($profile_id) { $modelsUsers = Users::findAll(['profile_id' => $profile_id, 'profile_office_role' => 'manager']); return ArrayHelper::map($modelsUsers, 'office_id', 'office_id'); }