/** * Creates a new RolePermission model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new RolePermission(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => (string) $model->_id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Updates an existing Role 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); $searchModel = new PermissionSearch(); $dataProvider = $searchModel->search([]); if ($model->load(Yii::$app->request->post())) { $postData = Yii::$app->request->post(); $model->updatedAt = Yii::$app->util->getUtcDateTime(); $model->updatedById = Yii::$app->user->identity->id; if (!empty($postData['selection'])) { if ($model->validate()) { try { if ($model->save()) { Yii::$app->session->setFlash('success', Yii::t('app', 'Role updated')); RolePermission::deleteAll(['roleName' => $model->name]); foreach ($postData['selection'] as $permission) { $modelRolePermission = new RolePermission(); $modelRolePermission->roleName = $model->name; $modelRolePermission->permissionName = $permission; try { $modelRolePermission->save(); } catch (Exception $e) { } } return $this->redirect(['index']); } else { Yii::$app->session->setFlash('success', Yii::t('app', 'Role update failed')); } } catch (Exception $e) { Yii::$app->session->setFlash('success', Yii::t('app', 'Role update failed')); } } } else { Yii::$app->session->setFlash('error', Yii::t('app', 'Please select atleast one permission')); } } return $this->render('update', ['model' => $model, 'dataProvider' => $dataProvider]); }