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.

188 lines
5.5 KiB

7 years ago
<?php
/**
* Created by Error202
* Date: 15.08.2017
*/
namespace backend\controllers;
use backend\forms\rbac\RbacEditRoleForm;
use backend\forms\rbac\RbacUpdateChildren;
use core\services\PermissionManager;
3 years ago
use DomainException;
use Exception;
7 years ago
use yii\behaviors\TimestampBehavior;
use backend\forms\rbac\RbacCreateRoleForm;
use core\services\RoleManager;
use yii\data\ArrayDataProvider;
3 years ago
use yii\rbac\Role;
7 years ago
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use Yii;
3 years ago
use yii\web\Response;
7 years ago
class RoleController extends Controller
{
3 years ago
private RoleManager $role;
private PermissionManager $permission;
7 years ago
public function __construct($id, $module, RoleManager $role, PermissionManager $permission, $config = [])
{
parent::__construct($id, $module, $config);
3 years ago
$this->role = $role;
$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
{
3 years ago
$data = $this->role->getRolesListArray();
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
/**
* @return Response|string
* @throws Exception
*/
public function actionCreate(): Response|string
7 years ago
{
$form = new RbacCreateRoleForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$this->role->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 string|Response
* @throws Exception
*/
public function actionUpdate($id): Response|string
7 years ago
{
$role = $this->findModel($id);
$form = new RbacEditRoleForm($role);
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$this->role->update($role->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
{
try {
3 years ago
$this->role->delete($id);
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
7 years ago
return $this->redirect(['index']);
}
3 years ago
/**
* @param $id
* @return string
* @throws \yii\base\Exception
*/
public function actionView($id): string
7 years ago
{
3 years ago
$currentRole = $this->role->getRole($id);
$rolesSelectArray = array_diff_assoc($this->role->getRolesSelectArray(), [$currentRole->name => $currentRole->description]);
7 years ago
$itemsForm = new RbacUpdateChildren();
if ($itemsForm->load(Yii::$app->request->post()) && $itemsForm->validate()) {
3 years ago
$this->role->saveChildren($id, $itemsForm->roles, $itemsForm->permissions);
7 years ago
Yii::$app->session->setFlash('success', Yii::t('user', 'Children roles and permissions for "{role}" is updated.', ['role' => $currentRole->description]));
}
3 years ago
$rolesSelected = $this->role->getRolesSelectArrayByRole($id);
7 years ago
3 years ago
$permissionsSelectArray = $this->permission->getPermissionsSelectArray();
$permissionsSelected = $this->permission->getPermissionsSelectArrayByRole($id);
7 years ago
$itemsForm->roles = $rolesSelected;
7 years ago
$itemsForm->permissions = $permissionsSelected;
return $this->render('view', [
'model' => $this->findModel($id),
'roles' => $rolesSelectArray,
'permissions' => $permissionsSelectArray,
'itemsForm' => $itemsForm,
7 years ago
]);
}
3 years ago
/**
* @param $id
* @return Role|null
*/
protected function findModel($id): ?Role
7 years ago
{
3 years ago
return $this->role->getRole($id);
7 years ago
}
}