# 使用说明
权限类型除常规的权限外,还可设置侧边栏菜单、顶部菜单等。
# 侧边栏菜单
# 描述:
可通过设置侧边栏菜单权限的方式来管理侧边栏菜单。
# 示例:
为不同角色分配不同的侧边栏菜单查看权限。
结果示例:
角色账户1 | 角色账户2 |
---|---|
![]() | ![]() |
步骤一:前提!增加路由请求项配置如下:
步骤二:增加侧边栏菜单权限配置如下,并对应配置步骤一中增加的路由请求项
步骤三:为不同角色分配权限
角色1 | 角色2 |
---|---|
![]() | ![]() |
# 顶部菜单
# 描述:
可通过设置顶部菜单权限的方式来管理顶部菜单。
# 示例:
为不同角色分配不同的顶部菜单查看权限。
结果示例:
角色账户1 | 角色账户2 |
---|---|
![]() | ![]() |
步骤一:前提!增加路由请求项配置如下:
注意!即使请求地址和侧边栏菜单一样,也需要设置新的路由请求项来提供给权限进行配置。因为请求日志呈现时需要进行区分。
其他步骤,参考侧边栏菜单配置...
# 表格构建器
# 描述:
可通过设置表格构建的筛选表单字段、表头字段、工具栏、操作栏等的权限配置项来控制为不同角色分配不同的列表数据查看、操作等权限。
# 示例:
为不同角色分配不同的demo用户列表查看、操作权限。
结果示例:
角色账户1 | 角色账户2 |
---|---|
![]() | ![]() |
代码示例: 其中的auth
键值配置未权限key即可,详见表格构建器各个配置项的描述。
namespace yunj\app\demo\controller\user;
use yunj\app\demo\enum\State;
use yunj\core\builder\YunjTable;
use yunj\app\demo\controller\Controller;
class IndexController extends Controller {
public function lists() {
$builder = YT('demo', [
...,
"toolbar" => function (YunjTable $builder, $state) {
$toolbar = ['add' => [
'type' => 'openTab',
'title' => '添加',
'class' => 'layui-icon-add-circle',
'url' => build_url('yunjDemoUserAdd'),
'auth' => 'yunj_demo_user_add',
]];
switch ($state) {
case State::NORMAL:
$toolbar += [
State::RECYLE_BIN => ['type' => 'asyncEvent', 'title' => '移入回收站', 'dropdown' => true, 'auth' => 'yunj_demo_user_recyle_bin',],
];
break;
case State::RECYLE_BIN:
$toolbar += [
State::NORMAL => ['type' => 'asyncEvent', 'title' => '恢复正常', 'dropdown' => true, 'auth' => 'yunj_demo_user_normal'],
State::DELETED => ['type' => 'asyncEvent', 'title' => '永久删除', 'dropdown' => true, 'auth' => 'yunj_demo_user_deleted'],
];
break;
}
return $toolbar;
},
"defaultToolbar" => function (YunjTable $builder, $state) {
return ['filter', 'print',
'export' => [
'auth' => 'yunj_demo_user_export'
],
'import' => [
'url' => build_url("yunjDemoUserImport"),
'auth' => 'yunj_demo_user_import'
],
'custom' => [
'title' => '自定义前端事件',
'class' => 'yunj-icon yunj-icon-pen',
]
];
},
"cols" => function (YunjTable $builder, $state) {
$cols = [
'id' => [
'type' => 'checkbox',
'fixed' => "left"
],
'avatar' => [
'title' => '头像',
"templet" => "img",
"align" => "center",
"auth" => "yunj_demo_user_list_view_normal"
],
'name' => [
'title' => '姓名',
"auth" => "yunj_demo_user_list_view_normal"
],
'sex' => [
'title' => '性别',
"templet" => "enum",
"options" => Sex::getTitleMap(),
"align" => "center",
"auth" => "yunj_demo_user_list_view_normal"
],
'age' => [
'title' => '年龄',
"align" => "center",
"auth" => "yunj_demo_user_list_view_normal"
],
'grade' => [
'title' => '年级',
"templet" => "enum",
"options" => Grade::getTitleMap(),
"align" => "center",
"auth" => "yunj_demo_user_list_view_normal"
],
'color' => [
'title' => '喜欢的颜色',
"templet" => "color",
"auth" => "yunj_demo_user_list_view_normal"
],
"album" => [
'title' => '相册',
"templet" => "imgs",
"auth" => "yunj_demo_user_list_view_album"
],
"hobby" => [
'title' => '爱好',
"templet" => "enum",
"options" => Hobby::getTitleMap(),
"auth" => "yunj_demo_user_list_view_hobby"
],
"interest_cate_ids" => [
'title' => '兴趣分类',
"templet" => "enum",
"options" => $this->getInterestCateIdNameOptions(),
"auth" => "yunj_demo_user_list_view_interest_cate_ids"
],
'area' => [
'title' => '地址',
'templet' => 'area',
"auth" => "yunj_demo_user_list_view_area"
],
'document' => [
'title' => '个人文档',
'templet' => 'file',
"auth" => "yunj_demo_user_list_view_document"
],
'other_document' => [
'title' => '其他文档',
'templet' => 'files',
"auth" => "yunj_demo_user_list_view_other_document"
],
'test' => [
'title' => '自定义表头',
'align' => 'center',
'hide' => 'yes',
"auth" => "yunj_demo_user_list_view_test",
'templet' => function () {
return "<div>自定义表头:{{ d.create_time}}</div>";
}
],
'create_time' => [
'title' => '添加时间',
'align' => 'center',
'templet' => 'datetime',
'hide' => 'yes',
'sort' => true,
"auth" => "yunj_demo_user_list_view_normal"
],
'last_update_time' => [
'title' => '上次更新时间',
'align' => 'center',
'templet' => 'datetime',
'sort' => true,
"auth" => "yunj_demo_user_list_view_normal"
],
'action' => [
'title' => '操作',
'fixed' => "right",
'templet' => 'action',
'options' => [],
]
];
$actionOptions = [
'edit' => ['type' => 'openPopup', 'title' => '详情', 'icon' => 'layui-icon-survey', 'url' => build_url('yunjDemoUserEdit'), 'auth' => 'yunj_demo_user_edit_detail'],
];
switch ($state) {
case State::NORMAL:
$actionOptions += [
State::RECYLE_BIN => ['type' => 'asyncEvent', 'title' => '移入回收站', 'dropdown' => true, 'auth' => 'yunj_demo_user_recyle_bin'],
];
break;
case State::RECYLE_BIN:
$actionOptions += [
State::NORMAL => ['type' => 'asyncEvent', 'title' => '恢复正常', 'dropdown' => true, 'auth' => 'yunj_demo_user_normal'],
State::DELETED => ['type' => 'asyncEvent', 'title' => '永久删除', 'dropdown' => true, 'auth' => 'yunj_demo_user_deleted']
];
break;
}
$cols['action']['options'] = $actionOptions;
return $cols;
}
]);
$builder->assign();
return $this->view("user/index/lists");
}
}
其他步骤,参考侧边栏菜单配置...
# 表单构建器
# 描述:
可通过设置表单构建的字段、按钮的权限配置项来控制为不同角色分配不同的表单数据查看、操作等权限。
# 示例:
结果示例:
角色账户1 | 角色账户2 |
---|---|
![]() | ![]() |
代码示例: 其中的auth
键值配置未权限key即可,详见表单构建器各个配置项的描述。
namespace yunj\app\demo\controller\user;
use yunj\core\builder\YunjForm;
use yunj\app\demo\controller\Controller;
class IndexController extends Controller {
public function edit() {
$builder = YF('demo', [
...,
"field" => function (YunjForm $builder, $tab) {
$field = [];
switch ($tab) {
case "base":
$field = [
"id" => [
"title" => "用户",
"type" => "hidden",
"verify" => "require|positiveInt",
],
"avatar" => [
"title" => "头像",
"type" => "img",
"auth" => "yunj_demo_user_edit_detail_normal",
],
"name" => [
"title" => "姓名",
"type" => "text",
"default" => "某某人",
"verify" => "require|chsDash",
"desc" => '必须,允许键入汉字/字母/数字/下划线/破折号-',
"auth" => "yunj_demo_user_edit_detail_normal",
],
"sex" => [
"title" => "性别",
"type" => "radio",
"options" => Sex::getTitleMap(),
"verify" => "require",
"auth" => "yunj_demo_user_edit_detail_normal",
],
"age" => [
"title" => "年龄",
"default" => 18,
"verify" => "require|positiveInt",
"auth" => "yunj_demo_user_edit_detail_normal",
],
"grade" => [
"title" => "年级",
"type" => "select",
"options" => Grade::getTitleMap(),
"auth" => "yunj_demo_user_edit_detail_normal",
],
"hobby" => [
"title" => "爱好",
"type" => "checkbox",
"options" => Hobby::getTitleMap(),
"auth" => "yunj_demo_user_edit_detail_hobby",
],
"area" => [
"title" => "地址",
"type" => "area",
"verify" => "require",
"auth" => "yunj_demo_user_edit_detail_area",
],
"intro" => [
"title" => "个人介绍",
"type" => "textarea",
"default" => "某某人的个人介绍",
"verify" => "max:200",
"desc" => '限制200字符',
"auth" => "yunj_demo_user_edit_detail_normal",
],
'test' => [
/** @var string $formId 当前表单构建器id */
'type' => function (string $formId) {
return "<label class='layui-form-label'>自定义字段标题</label>
<div class='layui-input-inline yunj-form-item-control'>自定义字段内容:<span class='value'></span></div>
<div class='yunj-form-item-desc'>自定义字段描述</div>
<script type='application/javascript'>
layui.use(['jquery'],function() {
let $ = layui.jquery;
// 设置字段值。事件名:yunj_form_{当前表单构建器id}_{字段key}_set_value
$(document).bind('yunj_form_{$formId}_test_set_value',function(e,field,value) {
// field.boxEl 当前字段外部父元素的jQuery对象
// value 当前字段的值
field.boxEl.find('.value').html(value);
});
// 获取字段值。事件名:yunj_form_{当前表单构建器id}_{字段key}_get_value
$(document).bind('yunj_form_{$formId}_test_get_value',function(e,field,val) {
// field.boxEl 当前字段外部父元素的jQuery对象
// val.value 当前字段的值,默认为空字符串''
val.value = field.boxEl.find('.value').html();
});
});
</script>";
},
"auth" => "yunj_demo_user_edit_detail_test",
]
];
break;
case "other":
$field = [
"interest_cate_ids" => [
"title" => "感兴趣的分类",
'type' => 'tree',
'nodes' => $this->getAllInterestCateItems(),
"auth" => "yunj_demo_user_edit_detail_interest_cate_ids",
],
"friend_ids" => [
"title" => "朋友",
'type' => 'dropdownSearch',
'verify' => 'arrayPositiveInt',
'url' => build_url('yunjDemoUserDropdownSearchOptions'),
"auth" => "yunj_demo_user_edit_detail_friend_ids",
],
"color" => [
"title" => "喜欢的颜色",
"type" => "color",
"auth" => "yunj_demo_user_edit_detail_color",
],
"album" => [
"title" => "相册",
"type" => "imgs",
"auth" => "yunj_demo_user_edit_detail_album",
],
'document' => [
'title' => '个人文档',
'type' => 'file',
"auth" => "yunj_demo_user_edit_detail_document",
],
'other_document' => [
'title' => '其他文档',
'type' => 'files',
"auth" => "yunj_demo_user_edit_detail_other_document",
],
];
break;
}
if (!admin_member_auth_check('yunj_demo_user_edit_submit')) {
// 没有编辑提交权限,全部字段改为只读
foreach ($field as &$v) {
$v['readonly'] = true;
}
}
return $field;
},
'button' => function () {
return admin_member_auth_check('yunj_demo_user_edit_submit')
? ['custom' => ['desc' => '自定义按钮'], 'back', 'reload', 'reset', 'submit'] : [];
}
]);
$builder->assign();
return $this->view("user/index/edit");
}
}
其他步骤,参考侧边栏菜单配置...
# 单独使用
# 描述:
在业务中需要对用户是否具有某个指定权限进行独立的判断,来决定是否对当前用户进行数据的展示/处理等。
可使用方法admin_member_auth_check('权限key')来判断当前用户是否具有指定权限。
# 示例:
为不同的角色赋予右上角全局搜索的权限
结果示例:
角色账户1 | 角色账户2 |
---|---|
![]() | ![]() |
代码示例:
{if admin_member_auth_check('yunj_top_right_action_global_search')}
<li class="top-menu-item">
<div class="global-search" data-member-id="{$memberId}">
<div class="input-box">
<i class="layui-icon layui-icon-search"></i>
<input type="text" placeholder="全局搜索..." name="keywords">
</div>
<div class="result">
<div class='item' data-key="no_result">无搜索结果</div>
{:admin_global_search_result_html_layout()}
</div>
<div class="result-mask"></div>
</div>
</li>
{/if}
# 系统权限重置/初始化
如果遇到某些情况下系统菜单/权限出现了异常;
此时可以通过以下命令方式进行重置:
# 云静初始化系统权限
php think yunj:init-system-auths
或进入权限管理页面执行列表右上角系统权限同步功能: