save
This commit is contained in:
parent
92a4cf22ee
commit
6e38a9c7d5
@ -19,7 +19,7 @@ class AdminRuleController extends Crud
|
|||||||
*
|
*
|
||||||
* @var string[]
|
* @var string[]
|
||||||
*/
|
*/
|
||||||
public $noNeedAuth = ['get', 'permission'];
|
public $noNeedAuth = ['get', 'permissionCodes'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var AdminRule
|
* @var AdminRule
|
||||||
@ -104,33 +104,64 @@ class AdminRuleController extends Crud
|
|||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public function permission(Request $request): Response
|
public function permissionCodes(Request $request): Response
|
||||||
{
|
{
|
||||||
$rules = $this->getRules(admin('roles'));
|
$rules = $this->getRules(admin('roles'));
|
||||||
// 超级管理员
|
// 超级管理员
|
||||||
if (in_array('*', $rules)) {
|
if (in_array('*', $rules)) {
|
||||||
return $this->json(0, 'ok', ['*']);
|
return $this->json(0, 'ok', ['*']);
|
||||||
}
|
}
|
||||||
$controller = $request->get('controller');
|
$keys = AdminRule::whereIn('id', $rules)->pluck('key');
|
||||||
if (!$controller) {
|
|
||||||
return $this->json(0, 'ok', []);
|
|
||||||
}
|
|
||||||
// 获取详细权限
|
|
||||||
$controller_search = str_replace('\\', '\\\\', $controller);
|
|
||||||
$keys = AdminRule::where('key', 'like', "$controller_search%")
|
|
||||||
->whereIn('id', $rules)->pluck('key');
|
|
||||||
$permissions = [];
|
$permissions = [];
|
||||||
$prefix_length = strlen($controller) + 1;
|
|
||||||
foreach ($keys as $key) {
|
foreach ($keys as $key) {
|
||||||
if ($key === $controller) {
|
$key = strtolower($key);
|
||||||
$permissions = ['*'];
|
$action = '';
|
||||||
break;
|
if (strpos($key, '@')) {
|
||||||
|
[$key, $action] = explode( '@', $key, 2);
|
||||||
|
}
|
||||||
|
$prefix = 'plugin';
|
||||||
|
$paths = explode('\\', $key);
|
||||||
|
if (count($paths) < 2) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$base = '';
|
||||||
|
if (strpos($key, "$prefix\\") === 0) {
|
||||||
|
if (count($paths) < 4) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
array_shift($paths);
|
||||||
|
$plugin = array_shift($paths);
|
||||||
|
$base = "app.$plugin.";
|
||||||
|
}
|
||||||
|
if ($code = $this->formatPermissionCode($paths, $action, $base)) {
|
||||||
|
$permissions[] = $code;
|
||||||
}
|
}
|
||||||
$permissions[] = substr($key, $prefix_length);
|
|
||||||
}
|
}
|
||||||
return $this->json(0, 'ok', $permissions);
|
return $this->json(0, 'ok', $permissions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $paths
|
||||||
|
* @param $action
|
||||||
|
* @param string $base
|
||||||
|
* @return false|string
|
||||||
|
*/
|
||||||
|
protected function formatPermissionCode($paths, $action, string $base = '')
|
||||||
|
{
|
||||||
|
array_shift($paths);
|
||||||
|
foreach ($paths as $index => $path) {
|
||||||
|
if ($path === 'controller') {
|
||||||
|
unset($paths[$index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$suffix = 'controller';
|
||||||
|
$code = $base . implode('.', $paths);
|
||||||
|
if (substr($code, -strlen($suffix)) === $suffix) {
|
||||||
|
$code = substr($code, 0, -strlen($suffix));
|
||||||
|
}
|
||||||
|
return $action ? "$code.$action" : $code;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据类同步规则到数据库
|
* 根据类同步规则到数据库
|
||||||
* @return void
|
* @return void
|
||||||
|
@ -150,18 +150,18 @@
|
|||||||
|
|
||||||
<!-- 表格顶部工具栏 -->
|
<!-- 表格顶部工具栏 -->
|
||||||
<script type="text/html" id="table-toolbar">
|
<script type="text/html" id="table-toolbar">
|
||||||
<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="insert">
|
<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="app.admin.user.insert">
|
||||||
<i class="layui-icon layui-icon-add-1"></i>新增
|
<i class="layui-icon layui-icon-add-1"></i>新增
|
||||||
</button>
|
</button>
|
||||||
<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" permission="delete">
|
<button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" permission="app.admin.user.delete">
|
||||||
<i class="layui-icon layui-icon-delete"></i>删除
|
<i class="layui-icon layui-icon-delete"></i>删除
|
||||||
</button>
|
</button>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- 表格行工具栏 -->
|
<!-- 表格行工具栏 -->
|
||||||
<script type="text/html" id="table-bar">
|
<script type="text/html" id="table-bar">
|
||||||
<button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="update">编辑</button>
|
<button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="app.admin.user.update">编辑</button>
|
||||||
<button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="delete">删除</button>
|
<button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.admin.user.delete">删除</button>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script src="/app/admin/component/layui/layui.js"></script>
|
<script src="/app/admin/component/layui/layui.js"></script>
|
||||||
@ -171,7 +171,7 @@
|
|||||||
|
|
||||||
// 相关常量
|
// 相关常量
|
||||||
const PRIMARY_KEY = "id";
|
const PRIMARY_KEY = "id";
|
||||||
const CONTROLLER = "plugin\\admin\\app\\controller\\UserController";
|
//const CONTROLLER = "plugin\\admin\\app\\controller\\UserController";
|
||||||
const SELECT_API = "/app/admin/user/select";
|
const SELECT_API = "/app/admin/user/select";
|
||||||
const UPDATE_API = "/app/admin/user/update";
|
const UPDATE_API = "/app/admin/user/update";
|
||||||
const DELETE_API = "/app/admin/user/delete";
|
const DELETE_API = "/app/admin/user/delete";
|
||||||
|
@ -36,22 +36,23 @@ function toggleSearchFormShow()
|
|||||||
* 获取控制器详细权限,并决定展示哪些按钮或dom元素
|
* 获取控制器详细权限,并决定展示哪些按钮或dom元素
|
||||||
*/
|
*/
|
||||||
layui.$(function () {
|
layui.$(function () {
|
||||||
if (typeof CONTROLLER === "undefined") return;
|
|
||||||
let $ = layui.$;
|
let $ = layui.$;
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "/app/admin/admin-rule/permission",
|
url: "/app/admin/admin-rule/permission-codes",
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
data: {controller: CONTROLLER},
|
|
||||||
success: function (res) {
|
success: function (res) {
|
||||||
let style = '';
|
let style = '';
|
||||||
layui.each(res.data || [], function (k, action) {
|
let codes = res.data || [];
|
||||||
if (action === '*') {
|
// codes里有*,说明是超级管理员,拥有所有权限
|
||||||
style = '*[permission]{display: initial}';
|
if (codes.indexOf('*') !== -1) {
|
||||||
|
$("head").append("<style>*[permission]{display: initial}</style>");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
style += '*[permission="'+action+'"]{display: initial}';
|
// 细分权限
|
||||||
|
layui.each(codes, function (k, code) {
|
||||||
|
codes[k] = '*[permission^="'+code+'"]';
|
||||||
});
|
});
|
||||||
$("head").append("<style>"+style+"</style>");
|
$("head").append("<style>"+codes.join(",")+"{display: initial}</style>");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user