public function actionIndex() { if (!Yii::$app->user->isGuest) { return $this->goHome(); } $model = $this->signupForm; $modelAuthRule = new AuthRule(); $modelAuthItem = new AuthItem(); $authRule = AuthRule::find()->all(); $authItem = AuthItem::find()->all(); if (Yii::$app->request->isPost) { if ($model->load(Yii::$app->request->post())) { try { $transaction = Yii::$app->db->beginTransaction(); if ($model->signup()) { $transaction->commit(); Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Pendaftaran Karyawan Berhasil')); return $this->redirect(['/user/signup']); } else { throw new \Exception("Signup validation savepoint failed"); } } catch (\Exception $e) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Pendaftaran Karyawan Gagal')); $transaction->rollback(); } } } return $this->render('index', ['model' => $model, 'modelAuthRule' => $modelAuthRule, 'authRule' => $authRule, 'modelAuthItem' => $modelAuthItem, 'authItem' => $authItem]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = AuthRule::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(['like', 'name', $this->name]); return $dataProvider; }
/** * Finds the AuthRule model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return AuthRule the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = AuthRule::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
<div class="form-group"> <?php echo Html::label('Divisi', 'division', ['class' => 'control-label']); ?> <?php $id = null; $person = []; if (!$model->isNewRecord) { $id = ArrayHelper::getValue($model, 'userTo.assignment.itemName.rule_name'); $user_id = ArrayHelper::getValue($model, 'userTo.id'); $name = ArrayHelper::getValue($model, 'userTo.person.full_name'); $person[$user_id] = $name; } ?> <?php echo Html::dropDownList('rule_name', $id, ArrayHelper::map(AuthRule::find()->all(), 'name', 'name'), ['class' => 'form-control', 'id' => 'rule_name', 'prompt' => 'Pilih']); ?> </div> <?php echo $form->field($model, 'task_to')->label('Nama Karyawan')->widget(DepDrop::className(), ['data' => $person, 'pluginOptions' => ['depends' => ['rule_name'], 'url' => Url::to(['/user/api/search-person'])]]); ?> <?php echo $form->field($model, 'task_title')->textInput(['maxlength' => 255]); ?> <?php echo $form->field($model, 'task_detail')->widget(TinyMce::className(), ['options' => ['rows' => 6], 'clientOptions' => ['plugins' => [], 'toolbar' => "bold italic | alignleft aligncenter alignright alignjustify | bullist numlist "]]); ?>
/** * Updates an existing AuthItem model. * If update is successful, the browser will be redirected to the 'view' page. * @param string $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $parent = AuthItemChild::find()->where(['child' => $model->name])->one(); if (Yii::$app->request->isPost) { $oldRole = $model->name; if ($model->load(Yii::$app->request->post()) && $model->validate()) { $role = $this->auth->getRole($oldRole); if (empty($role)) { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Data Jabatan Gagal Diubah')); } else { $role->name = $model->name; $role->description = $model->description; $role->data = Yii::$app->request->post('routers'); $role->ruleName = $model->rule_name; $this->auth->update($oldRole, $role); Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Data Jabatan Berhasil Diubah')); return $this->redirect(['index']); } } else { Yii::$app->getSession()->setFlash('error', Yii::t('app', 'Data Jabatan Gagal Diubah')); } } $routers = RouterGenerator::run(); ksort($routers); $children = AuthItem::find()->all(); $rule = AuthRule::find()->all(); return $this->render('update', ['model' => $model, 'routers' => $routers, 'registeredRouters' => $this->auth->getRole($model->name), 'children' => $children, 'rule' => $rule, 'parent' => $parent]); }
use yii\helpers\ArrayHelper; /* @var $this yii\web\View */ /* @var $searchModel app\modules\user\models\AuthItemSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = Yii::t('app', 'Management Jabatan'); $this->params['breadcrumbs'][] = $this->title; Panel::begin(['title' => $this->title, 'icon' => 'fa fa-file-text-o']); ?> <?php echo AlertBlock::widget(['type' => AlertBlock::TYPE_ALERT, 'useSessionFlash' => true]); ?> <p> <?php echo Html::a(Yii::t('app', 'Tambah Data Jabatan'), ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn', 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center']], ['attribute' => 'name', 'filter' => ArrayHelper::map(AuthItem::find()->asArray()->all(), 'name', 'name'), 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center']], ['attribute' => 'rule_name', 'filter' => ArrayHelper::map(AuthRule::find()->asArray()->all(), 'name', 'name'), 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center']], ['label' => Yii::t('app', 'Dibawahi Oleh'), 'value' => function ($data) { $str = []; foreach ($data->authItemChildren0 as $children) { $str[] = $children['parent']; } return implode(',', $str); }, 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center']], ['attribute' => 'created_at', 'filter' => false, 'format' => ['date', 'php:d F Y'], 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center']], ['attribute' => 'updated_at', 'filter' => false, 'format' => ['date', 'php:d F Y'], 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center']], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{view} {update}', 'contentOptions' => ['class' => 'text-center']]]]); ?> <?php Panel::end();
use app\components\Panel; use kartik\alert\AlertBlock; use yii\helpers\ArrayHelper; use app\modules\user\models\AuthItem; use app\modules\user\models\AuthRule; use app\components\Status; /* @var $this yii\web\View */ /* @var $searchModel app\models\PersonSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = Yii::t('app', 'Manajemen Karyawan'); $this->params['breadcrumbs'][] = $this->title; ?> <?php Panel::begin(['title' => $this->title, 'icon' => 'fa fa-file-text-o']); echo AlertBlock::widget(['type' => AlertBlock::TYPE_ALERT, 'useSessionFlash' => true]); ?> <p> <?php echo Html::a('<i class="fa fa-plus"></i> ' . Yii::t('app', 'Tambah Data Karyawan'), ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn', 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center']], ['attribute' => 'full_name', 'value' => 'full_name', 'headerOptions' => ['class' => 'text-center']], ['value' => function ($model) { return Status::getGender($model->gender); }, 'attribute' => 'gender', 'contentOptions' => ['class' => 'text-center'], 'filter' => Status::getGenderList(), 'headerOptions' => ['class' => 'text-center']], ['value' => 'user.assignment.itemName.name', 'attribute' => 'jabatan', 'filter' => ArrayHelper::map(AuthItem::find()->asArray()->all(), 'name', 'name'), 'headerOptions' => ['class' => 'text-center']], ['value' => 'user.assignment.itemName.rule_name', 'attribute' => 'divisi', 'filter' => ArrayHelper::map(AuthRule::find()->asArray()->all(), 'name', 'name'), 'headerOptions' => ['class' => 'text-center']], ['value' => 'user.email', 'attribute' => 'email', 'headerOptions' => ['class' => 'text-center']], ['class' => 'yii\\grid\\ActionColumn', 'contentOptions' => ['class' => 'text-center']]]]); ?> <?php Panel::end();
/** * 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]); }
/** * @return \yii\db\ActiveQuery */ public function getRuleName() { return $this->hasOne(AuthRule::className(), ['name' => 'rule_name']); }
use yii\helpers\Html; use yii\helpers\ArrayHelper; use app\modules\user\models\AuthRule; use yii\grid\GridView; use kartik\alert\AlertBlock; use app\components\Panel; use yii\jui\DatePicker; /* @var $this yii\web\View */ /* @var $searchModel app\modules\user\models\AuthRuleSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = Yii::t('app', 'Management Divisi'); $this->params['breadcrumbs'][] = $this->title; Panel::begin(['title' => $this->title, 'icon' => 'fa fa-file-text-o']); ?> <?php echo AlertBlock::widget(['type' => AlertBlock::TYPE_ALERT, 'useSessionFlash' => true]); ?> <p> <?php echo Html::a('<i class="fa fa-plus"></i> ' . Yii::t('app', 'Tambah Divisi'), ['create'], ['class' => 'btn btn-md btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn', 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center']], ['attribute' => 'name', 'filter' => ArrayHelper::map(AuthRule::find()->asArray()->all(), 'name', 'name'), 'headerOptions' => ['class' => 'text-center']], ['attribute' => 'created_at', 'filter' => false, 'format' => ['date', 'php:d F Y'], 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center']], ['attribute' => 'updated_at', 'filter' => false, 'format' => ['date', 'php:d F Y'], 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center']], ['class' => 'yii\\grid\\ActionColumn', 'template' => '{update}', 'headerOptions' => ['class' => 'text-center'], 'contentOptions' => ['class' => 'text-center']]]]); ?> <?php Panel::end();