public function actionIndex() { //get role based on user id $auth = Yii::$app->authManager; $id = Yii::$app->user->id; $app = \app\modules\user\models\AuthAssignment::find()->with(['itemName.ruleName'])->where(['user_id' => $id])->one(); $ruleName = $app->itemName->ruleName->name; $itemName = $app->itemName->name; $user = \app\modules\user\models\Person::find()->where(['user_id' => $id])->one(); $members = (new \yii\db\Query())->from('auth_item')->select(['person.first_name', 'person.last_name'])->where(['rule_name' => $ruleName])->join('INNER JOIN', 'auth_assignment', 'auth_assignment.item_name = auth_item.name')->join('INNER JOIN', 'user', 'user.id = auth_assignment.user_id')->join('INNER JOIN', 'person', 'person.user_id = user.id')->all(); return $this->render('index', ['ruleName' => $ruleName, 'itemName' => $itemName, 'members' => $members, 'name' => $user->first_name . ' ' . $user->last_name]); }
public function search($params) { $query = AuthAssignment::find()->where(['item_name' => 'client', 'uc.manager_id' => Yii::$app->user->getId()])->join('INNER JOIN', 'user u', 'u.id = auth_assignment.user_id')->join('INNER JOIN', 'user_client uc', 'uc.user_id = auth_assignment.user_id'); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => SORT_DESC]]); $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(['id' => $this->id, 'status' => $this->status]); $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'email', $this->email]); return $dataProvider; }
/** * Updates an existing Person model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $modelUser = $this->findUserModel($model); $modelAuthRule = new AuthRule(); $modelAuthItem = new AuthItem(); $authRule = AuthRule::find()->all(); $authItem = AuthItem::find()->all(); $id = $modelUser->id; $app = \app\modules\user\models\AuthAssignment::find()->with(['itemName.ruleName'])->where(['user_id' => $id])->one(); $ruleName = $app->itemName->ruleName->name; $itemName = $app->itemName->name; if (Yii::$app->request->isPost) { // do transaction if fails it will not saved $transaction = Yii::$app->db->beginTransaction(); try { if ($modelUser->load(Yii::$app->request->post()) && $modelUser->validate()) { $modelUser->generateAuthKey(); // first attempt save user record if ($modelUser->save()) { if ($model->load(Yii::$app->request->post())) { $model->user_id = $modelUser->id; // second attemp save person record if ($model->validate() && $model->save()) { if ($modelAuthItem->load(Yii::$app->request->post()) && $modelAuthItem->validate()) { $auth = Yii::$app->authManager; $role = $auth->getRole($modelAuthItem->name); $oldRole = $modelUser->assignment->itemName->name; // if role from dropdown exists in table authItem // and old role is not same as new input role from // dropdown then revoke old one then assig the new one if (!empty($role)) { // thrid attempt revoke and assign role to user if ($oldRole !== $role->name) { $oldRoleObject = $auth->getRole($oldRole); $auth->revoke($oldRoleObject, $modelUser->id); $auth->assign($role, $modelUser->id); $transaction->commit(); } else { $transaction->commit(); } Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Data Karyawan Berhasil Diubah')); return $this->redirect(['index']); } else { throw new \Exception("AuthRole search data checkpoint fail to save"); } } else { throw new \Exception("AuthItem (Role) validation checkpoint fail to save"); } } else { throw new \Exception("Person save checkpoint fail to save"); } } else { throw new \Exception("Person loaded checkpoint fail to save"); } } else { throw new \Exception("User save checkpoint fail to save"); } } else { throw new \Exception("User validation checkpoint fail to save"); } } catch (\Exception $e) { $transaction->rollback(); Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Data Karyawan Gagal Diubah')); } } return $this->render('update', ['model' => $model, 'modelUser' => $modelUser, 'modelAuthRule' => $modelAuthRule, 'authRule' => $authRule, 'modelAuthItem' => $modelAuthItem, 'authItem' => $authItem, 'ruleName' => $ruleName, 'itemName' => $itemName]); }