public function afterSave($insert, $changedAttributes) { //$oldCategorias = array("0"=>1); $actualAssignments = []; if (($actualAssignments = AuthAssignment::find()->andWhere("user_id = {$this->id}")->asArray()->all()) !== null) { $actualAssignments = ArrayHelper::getColumn($actualAssignments, 'item_name'); } //verifica se é um array $this->assignmentIds = is_array($this->assignmentIds) ? $this->assignmentIds : [$this->assignmentIds]; $inserirAssignments = array_diff($this->assignmentIds, $actualAssignments); if (!empty($inserirAssignments)) { //save the relations foreach ($inserirAssignments as $id) { $r = new AuthAssignment(); $r->user_id = $this->id; $r->item_name = $id; $r->save(); } } $delAssignments = array_diff($actualAssignments, $this->assignmentIds); if (!empty($delAssignments)) { foreach ($delAssignments as $remove) { $r = AuthAssignment::findOne(['item_name' => $remove, 'user_id' => $this->id]); $r->delete(); } } parent::afterSave($insert, $changedAttributes); //don't forget this }
public static function reassign() { $auth = \Yii::$app->authManager; $auth->removeAllAssignments(); // all assignment $query = AuthAssignment::find(); $model = $query->all(); foreach ($model as $list) { $auth->assign($auth->getRole($list->itemname), 'u:' . $list->userid); } }
public function search($params) { $query = AuthAssignment::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['created_at' => $this->created_at]); $query->andFilterWhere(['like', 'item_name', $this->item_name])->andFilterWhere(['like', 'user_id', $this->user_id]); return $dataProvider; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = AuthAssignment::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $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'); return $dataProvider; } $query->andFilterWhere(['created_at' => $this->created_at, 'user_id' => $this->user_id]); $query->andFilterWhere(['like', 'item_name', $this->item_name]); return $dataProvider; }
/** * Verifica si el usuario tiene rol indicado * @param $usuario * @param $rol * @return bool */ private static function tieneRolAsignado($usuario, $rol) { return AuthAssignment::find()->where('user_id = :usuario and item_name = :rol', [':usuario' => $usuario, ':rol' => $rol])->exists(); }
<?php echo $form->field($model, 'phone')->textInput(['maxlength' => 100]); ?> <?php echo $form->field($modelSignUp, 'email')->textInput(['maxlength' => 500]); ?> <?php if (Yii::$app->user->can("admin") || Yii::$app->user->can("hrm")) { echo $form->field($model, 'position')->textInput(['maxlength' => 2000]); if ($model->isNewRecord) { echo $form->field($model, 'manager')->dropDownList(ArrayHelper::map($model->find()->rightJoin('auth_assignment', 'user_info.user_id = auth_assignment.user_id')->where(array('item_name' => 'director'))->orWhere(array('item_name' => 'manager'))->all(), 'user_id', 'full_name'), array('prompt' => '--Select a manager--'))->label('Manager'); } else { $position = \common\models\AuthAssignment::find()->where(['user_id' => $model->user_id])->one(); //Neu la staff thi hien thi list manager. if ($position !== null && $position->item_name === "staff") { echo $form->field($model, 'manager')->dropDownList(ArrayHelper::map($model->find()->rightJoin('auth_assignment', 'user_info.user_id = auth_assignment.user_id')->where(array('item_name' => 'manager'))->all(), 'user_id', 'full_name'), array('prompt' => '--Select a manager--'))->label('Manager'); } if ($position !== null && $position->item_name === "manager" || $position->item_name === "hrm") { echo $form->field($model, 'manager')->dropDownList(ArrayHelper::map($model->find()->rightJoin('auth_assignment', 'user_info.user_id = auth_assignment.user_id')->where(array('item_name' => 'director'))->all(), 'user_id', 'full_name'), array('prompt' => '--Select a director--'))->label('Director'); } //Neu la manager thi hien thi director. } } ?> </div> </div> <div class="form-group">
public static function getPeremission($username) { $query = AuthAssignment::find(); $query->where(['userid' => $username]); $model = $query->one(); return $model; }
/** * @inheritdoc */ public function beforeSave($insert) { if ($this->birthday) { $this->birthday = CFF::formatData($this->birthday); } if ($this->password) { $this->setPassword($this->password); } /** * Защита от подмены собственных данных * Производить изменение ролей может только администратор */ $assign = AuthAssignment::find()->where(['user_id' => $this->id])->one(); if ($assign) { if ($assign->item_name != $this->role->name && !\Yii::$app->user->can('Администратор')) { $this->role = $assign->item_name; // Если Вы не администратор, оставляем роль какая была } } if (parent::beforeSave($insert)) { if ($insert) { $this->generateAuthKey(); } return true; } return false; }
/** * Verifica si un usuario ya tiene asignado un rol * @param $rol * @param $usuario * @return bool */ private function tienePermisosUsuario($rol, $usuario) { return AuthAssignment::find()->where('item_name = :rol and user_id = :usuario', [':rol' => $rol, ':usuario' => $usuario])->exists(); }