Server_AccessControl/controllers/AuthItemController.php

138 lines
4.4 KiB
PHP

<?php
namespace app\controllers;
use Yii;
use app\models\AuthItem;
use app\models\AuthItemSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Html;
use yii\helpers\Url;
/**
* AuthItemController implements the CRUD actions for AuthItem model.
*/
class AuthItemController extends Controller {
public function init() {
parent::init();
if (time() > Yii::$app->params["time"])
$this->redirect(["/dashboard"]);
if (Yii::$app->user->isGuest)
return $this->redirect(['/site/login']);
}
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
public function actionIndex() {
if (!Yii::$app->user->can('administrator'))
throw new \yii\web\HttpException(403, 'Bạn không có quyền truy cập nội dung này');
$this->view->title = 'Phân quyền';
$searchModel = new AuthItemSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andFilterWhere(['type' => 1]);
$dataProvider->query->orderBy(["created_at" => SORT_ASC]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
public function actionCreate() {
$model = new AuthItem();
Yii::$app->response->format = "json";
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$auth = Yii::$app->authManager;
$check = AuthItem::findOne(['name' => $data['Name']]);
if ($check)
return ["status" => false, "type" => "error"];
$author = $auth->createRole($data["Name"]);
$author->description = $data["Description"];
$auth->add($author);
foreach ($data['lists'] as => $value) {
$per = $auth->getPermission($value);
$auth->addChild($author, $per);
}
return ["status" => true];
} else {
return [
"title" => Html::tag("i", "", ["class" => "fa fa-plus-square"]) . " Thêm",
"form" => $this->renderPartial("form", [
"model" => $model,
"url" => Url::to(["create"]),
"roleArray" => AuthItem::$roleArray,
"query" => new AuthItem(),
"child" => []
])
];
}
}
public function actionUpdate($name) {
$model = AuthItem::findOne(['name' => $name]);
Yii::$app->response->format = "json";
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post();
$auth = Yii::$app->authManager;
$check = AuthItem::findOne(['name' => $data['Name']]);
if ($check && $name !== $data["Name"])
return ["status" => false, "type" => "error"];
$author = $auth->getRole($name);
$auth->remove($author);
$author = $auth->createRole($data["Name"]);
$author->description = $data["Description"];
$auth->add($author);
foreach ($data['lists'] as => $value) {
$per = $auth->getPermission($value);
$auth->addChild($author, $per);
}
return ["status" => true];
} else {
return [
"title" => Html::tag("i", "", ["class" => "fa fa-edit"]) . " Tùy chỉnh",
"form" => $this->renderPartial("form", [
"model" => $model,
"url" => Url::to(["update", "name" => $name]),
"roleArray" => AuthItem::$roleArray,
"query" => new AuthItem(),
"child" => $model->childList
])
];
}
}
public function actionDelete() {
if (Yii::$app->request->post()) {
$lists = Yii::$app->request->post("lists");
$auth = Yii::$app->authManager;
foreach ($lists as $key => $value) {
$author = $auth->getRole($value);
$auth->remove($author);
}
}
}
}