/** * Lists all RolePermission models. * @return mixed */ public function actionIndex() { $searchModel = new PermissionSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]); }
/** * 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]); }