public function can($permissionName, $params = [], $allowCaching = true) { if (Yii::$app->user->identity->isSuperadmin) { return true; } try { $rp = RolePermission::findOne(['roleName' => Yii::$app->user->identity->role, 'permissionName' => $permissionName]); if ($rp) { return true; } } catch (Exception $e) { } return false; }
/** * Finds the RolePermission model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $_id * @return RolePermission the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = RolePermission::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Deletes an existing Role model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $_id * @return mixed */ public function actionDelete($id) { $model = $this->findModel($id); $userModel = User::findOne(['role' => $model->name]); if (empty($userModel)) { try { $model->delete(); Yii::$app->session->setFlash('success', Yii::t('app', 'Role deleted')); try { RolePermission::deleteAll(['roleName' => $model->name]); } catch (Exception $e) { } } catch (Exception $e) { Yii::$app->session->setFlash('error', Yii::t('app', 'Role delete failed')); } } else { Yii::$app->session->setFlash('error', Yii::t('app', 'Role cannot be deleted. First delete users attached to this role.')); } return $this->redirect(['index']); }
if ($model->isNewRecord) { echo $form->field($model, 'name'); } else { echo $form->field($model, 'name')->textInput(['readonly' => true]); } ?> <?php echo $form->field($model, 'description'); ?> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'tableOptions' => ['class' => 'table table-striped'], 'columns' => [['class' => 'yii\\grid\\CheckboxColumn', 'checkboxOptions' => function ($data, $key, $index, $column) use($model) { $checked = false; if (!$model->isNewRecord) { $modelRP = RolePermission::find()->where(['roleName' => $model->name, 'permissionName' => $data->name])->one(); if (!empty($modelRP)) { $checked = true; } } return ['value' => $data->name, 'checked' => $checked]; }], 'name', 'description', 'category']]); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php
/** * Deletes an existing Permission model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $_id * @return mixed */ public function actionDelete($id) { $model = $this->findModel($id); if ($model->delete()) { Yii::$app->session->setFlash('success', Yii::t('app', 'Permission deleted')); try { RolePermission::deleteAll(['permissionName' => $model->name]); } catch (Exception $e) { } } else { Yii::$app->session->setFlash('success', Yii::t('app', 'Permission delete failed')); } return $this->redirect(['index']); }