You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.2 KiB

7 years ago
<?php
/**
* Created by Error202
* Date: 15.08.2017
*/
namespace backend\controllers;
use backend\forms\rbac\RbacCreatePermissionForm;
use backend\forms\rbac\RbacEditPermissionForm;
use core\services\PermissionManager;
3 years ago
use DomainException;
use Exception;
7 years ago
use yii\behaviors\TimestampBehavior;
use yii\data\ArrayDataProvider;
use yii\rbac\Permission;
use yii\web\Controller;
use yii\filters\VerbFilter;
use Yii;
use yii\filters\AccessControl;
3 years ago
use yii\web\Response;
7 years ago
class PermissionController extends Controller
{
3 years ago
private PermissionManager $permission;
7 years ago
public function __construct($id, $module, PermissionManager $permission, $config = [])
{
parent::__construct($id, $module, $config);
3 years ago
$this->permission = $permission;
7 years ago
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
7 years ago
],
'access' => [
'class' => AccessControl::class,
7 years ago
'rules' => [
[
'actions' => ['create', 'view', 'index', 'update', 'delete'],
'allow' => true,
'roles' => ['UserManagement'],
7 years ago
],
[ // all the action are accessible to admin
'allow' => true,
'roles' => ['admin'],
],
],
],
'verbs' => [
'class' => VerbFilter::class,
7 years ago
'actions' => [
'delete' => ['POST'],
],
],
];
}
3 years ago
public function actionIndex(): string
7 years ago
{
$data = array_map(function (Permission $permission) {
7 years ago
return [
'name' => $permission->name,
7 years ago
'description' => $permission->description,
];
3 years ago
}, $this->permission->getPermissions());
7 years ago
$dataProvider = new ArrayDataProvider([
'allModels' => $data,
7 years ago
'pagination' => [
'pageSize' => 20,
],
'sort' => [
7 years ago
'attributes' => ['name', 'description'],
],
]);
return $this->render('index', ['dataProvider' => $dataProvider]);
}
3 years ago
public function actionCreate(): Response|string
7 years ago
{
$form = new RbacCreatePermissionForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$this->permission->create($form->name, $form->description, $form->rule_name, $form->data);
7 years ago
return $this->redirect(['view', 'id' => $form->name]);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
7 years ago
return $this->render('create', [
'model' => $form,
]);
}
3 years ago
/**
* @param $id
* @return Response|string
* @throws Exception
*/
public function actionUpdate($id): Response|string
7 years ago
{
$permission = $this->findModel($id);
$form = new RbacEditPermissionForm($permission);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$this->permission->update($permission->name, $form->name, $form->description, $form->rule_name, $form->data);
7 years ago
return $this->redirect(['view', 'id' => $form->name]);
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
7 years ago
return $this->render('update', [
'model' => $form,
]);
}
3 years ago
public function actionDelete($id): Response
7 years ago
{
3 years ago
$this->permission->delete($id);
7 years ago
return $this->redirect(['index']);
}
3 years ago
public function actionView($id): string
7 years ago
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
3 years ago
protected function findModel($id): ?Permission
7 years ago
{
3 years ago
return $this->permission->getPermission($id);
7 years ago
}
}