Files
ulthon_admin/extend/base/admin/controller/system/AuthBase.php
2025-03-20 16:57:05 +08:00

99 lines
3.0 KiB
PHP

<?php
namespace base\admin\controller\system;
use app\admin\model\SystemAuth;
use app\admin\model\SystemAuthNode;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use app\admin\service\NodeService;
use app\admin\service\TriggerService;
use app\common\controller\AdminController;
use think\App;
/**
* @ControllerAnnotation(title="角色权限管理")
* Class Auth
*/
class AuthBase extends AdminController
{
use \app\admin\traits\Curd;
protected $sort = [
'sort' => 'desc',
'id' => 'desc',
];
public function __construct(App $app)
{
parent::__construct($app);
$this->model = new SystemAuth();
}
/**
* @NodeAnotation(title="授权")
*/
public function authorize($id)
{
$row = $this->model->find($id);
empty($row) && $this->error('数据不存在');
$checkNodeList = (new SystemAuthNode())
->where('auth_id', $id)
->column('node');
$module_list = (new NodeService())->getNodeTree();
foreach ($module_list as $module_key => $module) {
foreach ($module['children'] as $controllerKey => $controller) {
foreach ($controller['children'] as $actionKey => $action) {
$checked = in_array($action['node'], $checkNodeList);
$checked_string = $checked? 'checked' : '';
$disabled_string = '';
if(!$action['auth']){
$checked_string = 'checked';
$disabled_string = 'disabled';
}
$module_list[$module_key]['children'][$controllerKey]['children'][$actionKey]['checked'] = $checked_string;
$module_list[$module_key]['children'][$controllerKey]['children'][$actionKey]['disabled'] = $disabled_string;
}
}
}
$this->assign('row', $row);
$this->assign('checked_node_list', $checkNodeList);
$this->assign('module_list', $module_list);
return $this->fetch();
}
/**
* @NodeAnotation(title="授权保存")
*/
public function saveAuthorize()
{
$this->checkPostRequest();
$id = $this->request->post('id');
$node = $this->request->post('node', []);
$row = $this->model->find($id);
empty($row) && $this->error('数据不存在');
try {
$authNode = new SystemAuthNode();
$authNode->where('auth_id', $id)->delete();
if (!empty($node)) {
$saveAll = [];
foreach ($node as $vo) {
$saveAll[] = [
'auth_id' => $id,
'node' => $vo,
];
}
$authNode->saveAll($saveAll);
}
TriggerService::updateMenu();
} catch (\Exception $e) {
$this->error('保存失败');
}
$this->success('保存成功');
}
}