This commit is contained in:
2020-02-01 16:47:12 +07:00
commit 4c619ad6e6
16739 changed files with 3329179 additions and 0 deletions

View File

@@ -0,0 +1,182 @@
<?php
namespace yii2mod\rbac\controllers;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii2mod\rbac\models\AssignmentModel;
use yii2mod\rbac\models\search\AssignmentSearch;
/**
* Class AssignmentController
*
* @package yii2mod\rbac\controllers
*/
class AssignmentController extends Controller
{
/**
* @var \yii\web\IdentityInterface the class name of the [[identity]] object
*/
public $userIdentityClass;
/**
* @var string search class name for assignments search
*/
public $searchClass = [
'class' => AssignmentSearch::class,
];
/**
* @var string id column name
*/
public $idField = 'id';
/**
* @var string username column name
*/
public $usernameField = 'username';
/**
* @var array assignments GridView columns
*/
public $gridViewColumns = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->userIdentityClass === null) {
$this->userIdentityClass = Yii::$app->user->identityClass;
}
if (empty($this->gridViewColumns)) {
$this->gridViewColumns = [
$this->idField,
$this->usernameField,
];
}
}
/**
* @inheritdoc
*/
public function behaviors(): array
{
return [
'verbs' => [
'class' => 'yii\filters\VerbFilter',
'actions' => [
'index' => ['get'],
'view' => ['get'],
'assign' => ['post'],
'remove' => ['post'],
],
],
'contentNegotiator' => [
'class' => 'yii\filters\ContentNegotiator',
'only' => ['assign', 'remove'],
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
],
];
}
/**
* List of all assignments
*
* @return string
*/
public function actionIndex()
{
/* @var AssignmentSearch */
$searchModel = Yii::createObject($this->searchClass);
if ($searchModel instanceof AssignmentSearch) {
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $this->userIdentityClass, $this->idField, $this->usernameField);
} else {
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
}
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
'gridViewColumns' => $this->gridViewColumns,
]);
}
/**
* Displays a single Assignment model.
*
* @param int $id
*
* @return mixed
*/
public function actionView(int $id)
{
$model = $this->findModel($id);
return $this->render('view', [
'model' => $model,
'usernameField' => $this->usernameField,
]);
}
/**
* Assign items
*
* @param int $id
*
* @return array
*/
public function actionAssign(int $id)
{
$items = Yii::$app->getRequest()->post('items', []);
$assignmentModel = $this->findModel($id);
$assignmentModel->assign($items);
return $assignmentModel->getItems();
}
/**
* Remove items
*
* @param int $id
*
* @return array
*/
public function actionRemove(int $id)
{
$items = Yii::$app->getRequest()->post('items', []);
$assignmentModel = $this->findModel($id);
$assignmentModel->revoke($items);
return $assignmentModel->getItems();
}
/**
* Finds the Assignment model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*
* @param int $id
*
* @return AssignmentModel the loaded model
*
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel(int $id)
{
$class = $this->userIdentityClass;
if (($user = $class::findIdentity($id)) !== null) {
return new AssignmentModel($user);
}
throw new NotFoundHttpException(Yii::t('yii2mod.rbac', 'The requested page does not exist.'));
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace yii2mod\rbac\controllers;
use yii\rbac\Item;
use yii2mod\rbac\base\ItemController;
/**
* Class PermissionController
*
* @package yii2mod\rbac\controllers
*/
class PermissionController extends ItemController
{
/**
* @var int
*/
protected $type = Item::TYPE_PERMISSION;
/**
* @var array
*/
protected $labels = [
'Item' => 'Permission',
'Items' => 'Permissions',
];
}

View File

@@ -0,0 +1,27 @@
<?php
namespace yii2mod\rbac\controllers;
use yii\rbac\Item;
use yii2mod\rbac\base\ItemController;
/**
* Class RoleController
*
* @package yii2mod\rbac\controllers
*/
class RoleController extends ItemController
{
/**
* @var int
*/
protected $type = Item::TYPE_ROLE;
/**
* @var array
*/
protected $labels = [
'Item' => 'Role',
'Items' => 'Roles',
];
}

View File

@@ -0,0 +1,105 @@
<?php
namespace yii2mod\rbac\controllers;
use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\web\Response;
use yii2mod\rbac\models\RouteModel;
/**
* Class RouteController
*
* @package yii2mod\rbac\controllers
*/
class RouteController extends Controller
{
/**
* @var array route model class
*/
public $modelClass = [
'class' => RouteModel::class,
];
/**
* Returns a list of behaviors that this component should behave as.
*
* @return array
*/
public function behaviors(): array
{
return [
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'index' => ['get', 'post'],
'create' => ['post'],
'assign' => ['post'],
'remove' => ['post'],
'refresh' => ['post'],
],
],
'contentNegotiator' => [
'class' => 'yii\filters\ContentNegotiator',
'only' => ['assign', 'remove', 'refresh'],
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
],
];
}
/**
* Lists all Route models.
*
* @return mixed
*/
public function actionIndex()
{
$model = Yii::createObject($this->modelClass);
return $this->render('index', ['routes' => $model->getAvailableAndAssignedRoutes()]);
}
/**
* Assign routes
*
* @return array
*/
public function actionAssign(): array
{
$routes = Yii::$app->getRequest()->post('routes', []);
$model = Yii::createObject($this->modelClass);
$model->addNew($routes);
return $model->getAvailableAndAssignedRoutes();
}
/**
* Remove routes
*
* @return array
*/
public function actionRemove(): array
{
$routes = Yii::$app->getRequest()->post('routes', []);
$model = Yii::createObject($this->modelClass);
$model->remove($routes);
return $model->getAvailableAndAssignedRoutes();
}
/**
* Refresh cache of routes
*
* @return array
*/
public function actionRefresh(): array
{
$model = Yii::createObject($this->modelClass);
$model->invalidate();
return $model->getAvailableAndAssignedRoutes();
}
}

View File

@@ -0,0 +1,158 @@
<?php
namespace yii2mod\rbac\controllers;
use Yii;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii2mod\rbac\models\BizRuleModel;
use yii2mod\rbac\models\search\BizRuleSearch;
/**
* Class RuleController
*
* @package yii2mod\rbac\controllers
*/
class RuleController extends Controller
{
/**
* @var string search class name for rules search
*/
public $searchClass = [
'class' => BizRuleSearch::class,
];
/**
* Returns a list of behaviors that this component should behave as.
*
* @return array
*/
public function behaviors(): array
{
return [
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'index' => ['get'],
'view' => ['get'],
'create' => ['get', 'post'],
'update' => ['get', 'post'],
'delete' => ['post'],
],
],
];
}
/**
* List of all rules
*
* @return mixed
*/
public function actionIndex()
{
$searchModel = Yii::createObject($this->searchClass);
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
return $this->render('index', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]);
}
/**
* Displays a single Rule item.
*
* @param string $id
*
* @return mixed
*/
public function actionView(string $id)
{
$model = $this->findModel($id);
return $this->render('view', ['model' => $model]);
}
/**
* Creates a new Rule item.
*
* If creation is successful, the browser will be redirected to the 'view' page.
*
* @return mixed
*/
public function actionCreate()
{
$model = new BizRuleModel();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been saved.'));
return $this->redirect(['view', 'id' => $model->name]);
}
return $this->render('create', ['model' => $model]);
}
/**
* Updates an existing Rule item.
*
* If update is successful, the browser will be redirected to the 'view' page.
*
* @param string $id
*
* @return mixed
*/
public function actionUpdate(string $id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been saved.'));
return $this->redirect(['view', 'id' => $model->name]);
}
return $this->render('update', ['model' => $model]);
}
/**
* Deletes an existing Rule item.
*
* If deletion is successful, the browser will be redirected to the 'index' page.
*
* @param string $id
*
* @return mixed
*/
public function actionDelete(string $id)
{
$model = $this->findModel($id);
Yii::$app->authManager->remove($model->item);
Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been deleted.'));
return $this->redirect(['index']);
}
/**
* Finds the BizRuleModel based on its primary key value.
*
* If the model is not found, a 404 HTTP exception will be thrown.
*
* @param string $id
*
* @return BizRuleModel the loaded model
*
* @throws \yii\web\NotFoundHttpException
*/
protected function findModel(string $id)
{
$item = Yii::$app->authManager->getRule($id);
if (!empty($item)) {
return new BizRuleModel($item);
}
throw new NotFoundHttpException(Yii::t('yii2mod.rbac', 'The requested page does not exist.'));
}
}