Esempio n. 1
0
 /**
  * Creates a new AuthItem model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new AuthItem();
     if (Yii::$app->request->isPost) {
         if ($model->load(Yii::$app->request->post()) && $model->validate()) {
             $auth = Yii::$app->authManager;
             $role = $auth->createRole($model->name);
             $role->description = $model->description;
             $role->data = Yii::$app->request->post('routers');
             $role->ruleName = $model->rule_name;
             $auth->add($role);
             if (!empty($model->authItemParent)) {
                 $parentRole = $auth->getRole($model->authItemParent);
                 $auth->addChild($parentRole, $role);
             }
             Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Data Jabatan Berhasil Disimpan'));
             return $this->redirect(['index']);
         } else {
             Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Data Jabatan Gagal Disimpan'));
         }
     }
     $routers = RouterGenerator::run();
     ksort($routers);
     $children = AuthItem::find()->all();
     $rule = AuthRule::find()->all();
     return $this->render('create', ['model' => $model, 'routers' => $routers, 'children' => $children, 'rule' => $rule]);
 }
Esempio n. 2
0
 /**
  * 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]);
 }