update:更新权限系统

This commit is contained in:
JaguarJack 2021-03-31 20:20:21 +08:00
parent d4020b93a3
commit 2b96f3b650
15 changed files with 582 additions and 7 deletions

View File

@ -100,6 +100,7 @@ class Permission extends CatchController
$permission = $this->permissions->findBy($id);
$params = $request->param();
// 按钮类型
if ($params['type'] == Permissions::BTN_TYPE && $permission->parent_id) {
$parentPermission = $this->permissions->findBy($permission->parent_id);

View File

@ -7,7 +7,6 @@ use catcher\base\CatchRequest as Request;
use catcher\base\CatchController;
use catcher\CatchResponse;
use catcher\exceptions\FailedException;
use catcher\Tree;
use think\response\Json;
use catchAdmin\permissions\model\Roles as RoleModel;
@ -51,7 +50,7 @@ class Role extends CatchController
$this->role->attachPermissions(array_unique($params['permissions']));
}
// 分配部门
if (count($params['departments'])) {
if (isset($params['departments']) && count($params['departments'])) {
$this->role->attachDepartments($params['departments']);
}
// 添加角色

View File

@ -35,8 +35,18 @@ class Roles extends CatchModel
public function getList()
{
return $this->catchSearch()
->with(['permissions', 'departments'])
->order('id', 'desc')
->select()
->each(function (&$item){
$permissions = $item->permissions->column('id');
unset($item['permissions']);
$item['permissions'] = $permissions;
$departments = $item->departments->column('id');
unset($item['departments']);
$item['departments'] = $departments;
})
->toTree();
}

View File

@ -5,6 +5,7 @@ use catchAdmin\permissions\model\search\UserSearch;
use catcher\base\CatchModel;
use catcher\exceptions\FailedException;
use catcher\Utils;
use think\Paginator;
class Users extends CatchModel
{
@ -47,16 +48,25 @@ class Users extends CatchModel
* 用户列表
*
* @time 2019年12月08日
* @return array|\think\Paginator
*@throws \think\db\exception\DbException
* @return \think\Paginator
*/
public function getList(): \think\Paginator
public function getList()
{
return $this->withoutField(['updated_at'], true)
$users = $this->withoutField(['updated_at', 'password', 'remember_token'], true)
->catchSearch()
->catchLeftJoin(Department::class, 'id', 'department_id', ['department_name'])
->with(['jobs', 'roles'])
->order($this->aliasField('id'), 'desc')
->paginate();
->paginate()->toArray();
foreach ($users['data'] as &$user) {
$user['roles'] = array_column($user['roles'], 'id');
$user['jobs'] = array_column($user['jobs'], 'id');
}
return Paginator::make($users['data'], $users['per_page'], $users['current_page'], $users['total']);
}
/**

View File

@ -0,0 +1,50 @@
<?php
namespace catchAdmin\permissions\tables;
use catchAdmin\permissions\tables\forms\Factory;
use catcher\CatchTable;
use catcher\library\table\Actions;
use catcher\library\table\HeaderItem;
use catcher\library\table\Search;
use catcher\library\table\Table;
class Department extends CatchTable
{
/**
* table
*
* @time 2021年03月29日
* @return array
*/
protected function table(): array
{
// TODO: Implement table() method.
return $this->getTable('user')->header([
HeaderItem::label('部门名称')->prop('department_name'),
HeaderItem::label('排序')->prop('sort')->withEditNumberComponent(),
HeaderItem::label('状态')->prop('status')->withSwitchComponent(),
HeaderItem::label('创建时间')->prop('created_at'),
HeaderItem::label('操作')->width(260)->actions([
Actions::update(''),
Actions::delete(''),
])
])->withApiRoute('departments')->withActions([
Actions::create()
])->withSearch([
Search::text('department_name', '请输入部门名称'),
Search::status()
])->withDialogWidth('35%')
->toTreeTable()->render();
}
/**
* form 方式
*
* @time 2021年03月29日
* @return array
*/
protected function form(): array
{
return Factory::create('department');
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace catchAdmin\permissions\tables;
use catcher\CatchTable;
use catchAdmin\permissions\tables\forms\Factory;
use catcher\library\table\Actions;
use catcher\library\table\HeaderItem;
use catcher\library\table\Search;
class Job extends CatchTable
{
public function table()
{
return $this->getTable('job')
->header([
HeaderItem::label('')->type('selection'),
HeaderItem::label('岗位名称')->prop('job_name'),
HeaderItem::label('编码')->prop('coding'),
HeaderItem::label('状态')->prop('status')->withSwitchComponent(),
HeaderItem::label('创建时间')->prop('created_at'),
HeaderItem::label('操作')->width(250)->actions([
Actions::update(),
Actions::delete()
])
])
->withActions([
Actions::create()
])
->withSearch([
Search::text('job_name', '岗位名称')
])
->withApiRoute('jobs')
->selectionChange()
->render();
}
public function form()
{
// TODO: Implement form() method.
return Factory::create('job');
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace catchAdmin\permissions\tables;
use catcher\CatchTable;
use catchAdmin\permissions\tables\forms\Factory;
use catcher\library\table\Actions;
use catcher\library\table\HeaderItem;
use catcher\library\table\Search;
class Permission extends CatchTable
{
public function table()
{
return $this->getTable('permission')
->header([
HeaderItem::label('菜单名称')->prop('permission_name'),
HeaderItem::label('路由Path')->prop('route'),
HeaderItem::label('权限标识')->prop('actionList')->width(250)->component('actions', 'actionList'),
HeaderItem::label('状态')->prop('hidden')->withSwitchComponent(),
HeaderItem::label('排序')->prop('sort')->withEditNumberComponent(),
HeaderItem::label('创建时间')->prop('created_at'),
HeaderItem::label('操作')->width(250)->actions([
Actions::update(),
Actions::delete()
])
])
->withActions([
Actions::create()
])
->withSearch([
Search::text('permission_name', '菜单名称')
])
->withFilterParams([
'permission_name' => '',
'actionList' => 'actionList'
])
->withDefaultQueryParams(['actionList'])
->withApiRoute('permissions')
->toTreeTable()
->render();
}
public function form()
{
// TODO: Implement form() method.
return Factory::create('permission');
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace catchAdmin\permissions\tables;
use catcher\CatchTable;
use catchAdmin\permissions\tables\forms\Factory;
use catcher\library\table\Actions;
use catcher\library\table\HeaderItem;
use catcher\library\table\Search;
class Role extends CatchTable
{
public function table()
{
// TODO: Implement table() method.
return $this->getTable('role')
->header([
HeaderItem::label('角色名称')->prop('role_name')->width(150),
HeaderItem::label('角色标识')->prop('identify')->width(150),
HeaderItem::label('角色描述')->prop('description'),
HeaderItem::label('创建时间')->prop('created_at'),
HeaderItem::label('操作')->width(250)->actions([
Actions::update(''), Actions::delete('')
])
])
->withSearch([
Search::text('role_name', '角色名称'),
])
->withApiRoute('roles')
->withActions([
Actions::create()
])->withDialogWidth('40%')
->toTreeTable()
->render();
}
protected function form()
{
// TODO: Implement form() method.
return Factory::create('role');
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace catchAdmin\permissions\tables;
use catcher\CatchTable;
use catchAdmin\permissions\tables\forms\Factory;
use catcher\library\table\Actions;
use catcher\library\table\HeaderItem;
use catcher\library\table\Search;
class User extends CatchTable
{
public function table()
{
// TODO: Implement table() method.
return $this->getTable('user')
->header([
HeaderItem::label('')->selection(),
HeaderItem::label('用户名')->prop('username'),
HeaderItem::label('邮箱')->prop('email'),
HeaderItem::label('状态')->prop('status')->component('status', 'status'),
HeaderItem::label('创建时间')->prop('created_at'),
HeaderItem::label('操作')->width(150)->actions([
Actions::update(''), Actions::delete('')
])
])
->withSearch([
Search::text('username', '用户名'),
Search::text('email', '邮箱'),
Search::status()
])
->withApiRoute('users')
->withActions([
Actions::create()
])
->withFilterParams([
'username' => '',
'email' => '',
'status' => '',
'department_id' => ''
])
->selectionChange()
->render();
}
protected function form()
{
// TODO: Implement form() method.
return Factory::create('user');
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace catchAdmin\permissions\tables\forms;
use catchAdmin\permissions\model\Department as DepartmentModel;
use catcher\library\form\Form;
class Department extends Form
{
public function fields(): array
{
return [
// TODO: Implement fields() method
Form::cascader('parent_id', '上级部门', [0])->options(
DepartmentModel::field(['id', 'parent_id', 'department_name'])->select()->toTree()
)->clearable(true)->filterable(true)->props([
'props' => [
'value' => 'id',
'label' => 'department_name',
'checkStrictly' => true
],
])->style(['width' => '100%']),
Form::input('department_name', '部门名称')->required()->placeholder('请输入部门名称'),
Form::input('principal', '部门负责人'),
Form::input('mobile', '负责人联系方式'),
Form::email('email', '邮箱'),
Form::radio('status', '状态')->value(1)->options(
Form::options()->add('启用', 1)->add('禁用', 2)->render()
),
Form::number('sort', '排序')->value(1)->min(1)->max(10000),
];
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace catchAdmin\permissions\tables\forms;
use catcher\library\form\FormFactory;
class Factory extends FormFactory
{
public static function from(): string
{
return __NAMESPACE__;
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace catchAdmin\permissions\tables\forms;
use catcher\library\form\Form;
class Job extends Form
{
public function fields(): array
{
// TODO: Implement fields() method.
return [
self::input('job_name', '岗位名称')->required(),
self::input('coding', '岗位编码'),
self::radio('status', '状态')->value(1)->options(
self::options()->add('启用', 1)->add('禁用', 2)->render()
),
self::number('sort', '排序')->value(1)->min(1)->max(10000),
];
}
}

View File

@ -0,0 +1,143 @@
<?php
namespace catchAdmin\permissions\tables\forms;
use catchAdmin\permissions\model\Permissions;
use catcher\CatchAdmin;
use catcher\library\form\Form;
class Permission extends Form
{
public function fields(): array
{
$this->getModules();
// TODO: Implement fields() method.
return [
self::cascader('parent_id', '父级菜单', [])->options(
Permissions::where('type', Permissions::MENU_TYPE)->field(['id', 'permission_name', 'parent_id'])
->select()->toTree()
)->col(12)->props(self::props('permission_name', 'id', [
'checkStrictly' => true
]))->style(['width' => '100%']),
self::radio('type', '菜单类型')
->button()
->value(1)
->options(
self::options()->add('菜单', 1)->add('按钮', 2)->render()
)->appendControl(1, [
self::input('permission_name', '菜单名称')->required()->col(12),
self::input('permission_mark', '权限标识')->required()->col(12),
self::select('module', '模块')
->required()
->style(['width' => '100%'])
->col(12)
->options($this->getModules()),
self::input('icon', '菜单图标')
->col(12)
->style(['width' => '100%'])
->clearable(true),
self::input('route', '菜单Path')->col(12),
self::cascader('component', '组件')
->col(12)
->options([])
->style(['width' => '100%'])
->showAllLevels(false),
self::input('redirect', 'Redirect')->col(12),
self::number('sort', '排序')->value(1)->col(12),
self::radio('keepalive', 'Keepalive')
->value(1)
->col(12)
->options(
self::options()->add('启用', 1)
->add('禁用', 2)
->render()
),
self::radio('hidden', 'Hidden')->value(1)->options(
self::options()->add('显示', 1)->add('隐藏', 2)->render()
)->col(12)
])
->appendControl(2, [
self::select('permission_name', '菜单名称')
->allowCreate(true)
->filterable(true)
->options(
self::options()->add('列表', '列表')
->add('创建', '创建')
->add('更新', '更新')->add('读取', '读取')
->add('删除', '删除')->add('禁用/启用', '禁用/启用')
->add('导出', '导出')->add('导入', '导入')->render()
)
->required()->style(['width' => '100%'])->col(12),
self::select('permission_mark', '权限标识')
->allowCreate(true)
->filterable(true)
->options(
self::options()->add('index', 'index')
->add('create', 'create')
->add('update', 'update')->add('read', 'read')
->add('delete', 'delete')->add('disable', 'disable')
->add('export', 'export')->add('import', 'import')->render()
)
->required()->col(12),
self::number('sort', '排序')->value(1)->col(12),
])->col(12)
];
}
/**
* 获取模块
*
* @time 2021年03月31日
* @return array
*/
protected function getModules(): array
{
$modules = [];
foreach(CatchAdmin::getModulesDirectory() as $d) {
$module = CatchAdmin::getModuleInfo($d);
if (in_array($module['alias'], ['login'])) {
continue;
}
if ($module['enable']) {
$modules[] = [
'value' => $module['alias'],
'label' => $module['name']
];
}
}
return $modules;
}
/**
* icons
*
* @time 2021年03月31日
* @return array
*/
protected function getIcons(): array
{
$icons = ['platform-eleme', 'eleme', 'delete-solid', 'delete', 's-tools', 'setting', 'user-solid', 'user', 'phone', 'phone-outline', 'more', 'more-outline', 'star-on', 'star-off', 's-goods', 'goods', 'warning', 'warning-outline', 'question', 'info', 'remove', 'circle-plus', 'success', 'error', 'zoom-in', 'zoom-out', 'remove-outline', 'circle-plus-outline', 'circle-check', 'circle-close', 's-help', 'help', 'minus', 'plus', 'check', 'close', 'picture', 'picture-outline', 'picture-outline-round', 'upload', 'upload2', 'download', 'camera-solid', 'camera', 'video-camera-solid', 'video-camera', 'message-solid', 'bell', 's-cooperation', 's-order', 's-platform', 's-fold', 's-unfold', 's-operation', 's-promotion', 's-home', 's-release', 's-ticket', 's-management', 's-open', 's-shop', 's-marketing', 's-flag', 's-comment', 's-finance', 's-claim', 's-custom', 's-opportunity', 's-data', 's-check', 's-grid', 'menu', 'share', 'd-caret', 'caret-left', 'caret-right', 'caret-bottom', 'caret-top', 'bottom-left', 'bottom-right', 'back', 'right', 'bottom', 'top', 'top-left', 'top-right', 'arrow-left', 'arrow-right', 'arrow-down', 'arrow-up', 'd-arrow-left', 'd-arrow-right', 'video-pause', 'video-play', 'refresh', 'refresh-right', 'refresh-left', 'finished', 'sort', 'sort-up', 'sort-down', 'rank', 'loading', 'view', 'c-scale-to-original', 'date', 'edit', 'edit-outline', 'folder', 'folder-opened', 'folder-add', 'folder-remove', 'folder-delete', 'folder-checked', 'tickets', 'document-remove', 'document-delete', 'document-copy', 'document-checked', 'document', 'document-add', 'printer', 'paperclip', 'takeaway-box', 'search', 'monitor', 'attract', 'mobile', 'scissors', 'umbrella', 'headset', 'brush', 'mouse', 'coordinate', 'magic-stick', 'reading', 'data-line', 'data-board', 'pie-chart', 'data-analysis', 'collection-tag', 'film', 'suitcase', 'suitcase-1', 'receiving', 'collection', 'files', 'notebook-1', 'notebook-2', 'toilet-paper', 'office-building', 'school', 'table-lamp', 'house', 'no-smoking', 'smoking', 'shopping-cart-full', 'shopping-cart-1', 'shopping-cart-2', 'shopping-bag-1', 'shopping-bag-2', 'sold-out', 'sell', 'present', 'box', 'bank-card', 'money', 'coin', 'wallet', 'discount', 'price-tag', 'news', 'guide', 'male', 'female', 'thumb', 'cpu', 'link', 'connection', 'open', 'turn-off', 'set-up', 'chat-round', 'chat-line-round', 'chat-square', 'chat-dot-round', 'chat-dot-square', 'chat-line-square', 'message', 'postcard', 'position', 'turn-off-microphone', 'microphone', 'close-notification', 'bangzhu', 'time', 'odometer', 'crop', 'aim', 'switch-button', 'full-screen', 'copy-document', 'mic', 'stopwatch', 'medal-1', 'medal', 'trophy', 'trophy-1', 'first-aid-kit', 'discover', 'place', 'location', 'location-outline', 'location-information', 'add-location', 'delete-location', 'map-location', 'alarm-clock', 'timer', 'watch-1', 'watch', 'lock', 'unlock', 'key', 'service', 'mobile-phone', 'bicycle', 'truck', 'ship', 'basketball', 'football', 'soccer', 'baseball', 'wind-power', 'light-rain', 'lightning', 'heavy-rain', 'sunrise', 'sunrise-1', 'sunset', 'sunny', 'cloudy', 'partly-cloudy', 'cloudy-and-sunny', 'moon', 'moon-night', 'dish', 'dish-1', 'food', 'chicken', 'fork-spoon', 'knife-fork', 'burger', 'tableware', 'sugar', 'dessert', 'ice-cream', 'hot-water', 'water-cup', 'coffee-cup', 'cold-drink', 'goblet', 'goblet-full', 'goblet-square', 'goblet-square-full', 'refrigerator', 'grape', 'watermelon', 'cherry', 'apple', 'pear', 'orange', 'coffee', 'ice-tea', 'ice-drink', 'milk-tea', 'potato-strips', 'lollipop', 'ice-cream-square', 'ice-cream-round'];
$options = self::options();
foreach ($icons as $icon) {
$icon = 'el-icon-' . $icon;
$options->add(htmlspecialchars(sprintf('<i class=\"%s\"></i> %s', $icon, $icon)), $icon);
}
return $options->render();
}
}

View File

@ -0,0 +1,66 @@
<?php
namespace catchAdmin\permissions\tables\forms;
use catchAdmin\permissions\model\Department as DepartmentModel;
use catchAdmin\permissions\model\Permissions;
use catchAdmin\permissions\model\Roles;
use catcher\library\form\Form;
class Role extends Form
{
public function fields(): array
{
// TODO: Implement fields() method.
return [
Form::cascader('parent_id', '上级角色', [])->options(
Roles::field(['id', 'parent_id', 'role_name'])->select()->toTree()
)->clearable(true)->filterable(true)->props([
'props' => [
'value' => 'id',
'label' => 'role_name',
'checkStrictly' => true
],
])->style(['width' => '100%']),
self::input('role_name', '角色名称')->required()
->clearable(true)->placeholder('请填写角色名称'),
self::input('identify', '角色标识')
->clearable(true)->required()
->placeholder('请填写角色标识'),
self::textarea('description', '角色描述')
->clearable(true)->placeholder('请填写角色描述'),
self::tree('permissions', '角色权限', [])
->props(self::props('permission_name', 'id', [],
Permissions::field(['id', 'parent_id', 'permission_name'])->select()->toTree()
))
->required(),
self::select('data_range', '数据权限')
->placeholder('请选择数据权限')
->options(
self::options()->add('全部数据权限', Roles::ALL_DATA)
->add('自定义数据权限', Roles::SELF_CHOOSE)
->add('仅本人数据权限', Roles::SELF_DATA)
->add('本部门数据权限', Roles::DEPARTMENT_DATA)
->add('部门以及以下数据权限', Roles::DEPARTMENT_DOWN_DATA)
->render()
)->style(['width' => '100%'])
->appendControl(Roles::SELF_CHOOSE, [
self::cascader('departments', '自定义权限')
->options(
DepartmentModel::field(['id', 'parent_id', 'department_name'])->select()->toTree()
)
->props(self::props('department_name', 'id', [
'multiple' => true,
'emitPath' => false
]))
->showAllLevels(false)
->style(['width' => '100%']),
])
];
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace catchAdmin\permissions\tables\forms;
use catchAdmin\permissions\model\Department as DepartmentModel;
use catchAdmin\permissions\model\Job;
use catchAdmin\permissions\model\Roles;
use catcher\library\form\Form;
class User extends Form
{
public function fields(): array
{
// TODO: Implement fields() method.
return [
self::input('username', '昵称')->col(self::col(12))->clearable(true)->required(),
self::cascader('department_id', '部门', [])
->col(self::col(12))
->options(
DepartmentModel::field(['id', 'parent_id', 'department_name'])->select()->toTree()
)
->props(self::props('department_name', 'id', [
'checkStrictly' => true
]))->clearable(true),
self::email('email', '邮箱')->col(self::col(12))->required()->clearable(true),
self::selectMultiple('jobs', '岗位', [])
->col(self::col(12))->options(
Job::where('status', Job::ENABLE)->field(['id as value', 'job_name as label'])->select()->toArray()
)->clearable(true)->filterable(true),
self::input('password', '密码')->col(self::col(12))
->placeholder('请输入密码')->clearable(true),
self::tree('roles', '角色', [])
->props(self::props('role_name', 'id', [], Roles::field(['id', 'parent_id', 'role_name'])->select()->toTree()))
->required()
];
}
}