From 1b7cc3fcf588c4c339ea8acff36e2053a90efc0c Mon Sep 17 00:00:00 2001 From: yanwenwu Date: Sun, 12 Jan 2020 12:54:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=A7=92=E8=89=B2=E9=83=A8?= =?UTF-8?q?=E9=97=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/permissions/controller/Role.php | 11 +++- .../migrations/20191208125722_roles.php | 1 + .../20200112031437_role_has_departments.php | 36 ++++++++++++ .../permissions/model/HasDepartmentsTrait.php | 57 +++++++++++++++++++ catch/permissions/model/Roles.php | 5 +- 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 catch/permissions/database/migrations/20200112031437_role_has_departments.php create mode 100644 catch/permissions/model/HasDepartmentsTrait.php diff --git a/catch/permissions/controller/Role.php b/catch/permissions/controller/Role.php index 532a29d..854a9b9 100644 --- a/catch/permissions/controller/Role.php +++ b/catch/permissions/controller/Role.php @@ -51,6 +51,9 @@ class Role extends CatchController if (!empty($request->param('permissions'))) { $this->role->attach($request->param('permissions')); } + if (!empty($request->param('departments'))) { + $this->role->attachDepartments($request->param('departments')); + } // 添加角色 return CatchResponse::success(); } @@ -59,6 +62,7 @@ class Role extends CatchController { $role = $this->role->findBy($id); $role->permissions = $role->getPermissions(); + $role->departments = $role->getDepartments(); return CatchResponse::success($role); } @@ -91,7 +95,10 @@ class Role extends CatchController if (!empty($request->param('permissions'))) { $role->attach($request->param('permissions')); } - + if (!empty($request->param('departments'))) { + $role->detachDepartments(); + $role->attach($request->param('departments')); + } return CatchResponse::success(); } @@ -113,6 +120,8 @@ class Role extends CatchController $role = $this->role->findBy($id); // 删除权限 $role->detach(); + // 删除部门关联 + $role->detachDepartments(); // 删除用户关联 $role->users()->detach(); // 删除 diff --git a/catch/permissions/database/migrations/20191208125722_roles.php b/catch/permissions/database/migrations/20191208125722_roles.php index 7bc4e01..0609091 100644 --- a/catch/permissions/database/migrations/20191208125722_roles.php +++ b/catch/permissions/database/migrations/20191208125722_roles.php @@ -32,6 +32,7 @@ class Roles extends Migrator $table->addColumn('role_name', 'string',['limit' => 15,'default'=>'','comment'=>'角色名']) ->addColumn('parent_id', 'integer',['default'=>0,'comment'=>'父级ID', 'signed' => false]) ->addColumn('description', 'string',['default'=> '','comment'=>'角色备注']) + ->addColumn('data_range', 'integer',['limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_TINY,'default'=> 0,'comment'=>'1 全部数据 2 自定义数据 3 仅本人数据 4 部门数据 4 部门及以下数据']) ->addColumn('creator_id', 'integer',['default' => 0, 'comment'=>'创建人ID']) ->addColumn('created_at', 'integer', array('default'=>0,'comment'=>'创建时间', 'signed' => false )) ->addColumn('updated_at', 'integer', array('default'=>0,'comment'=>'更新时间', 'signed' => false)) diff --git a/catch/permissions/database/migrations/20200112031437_role_has_departments.php b/catch/permissions/database/migrations/20200112031437_role_has_departments.php new file mode 100644 index 0000000..a569c02 --- /dev/null +++ b/catch/permissions/database/migrations/20200112031437_role_has_departments.php @@ -0,0 +1,36 @@ +table('role_has_departments',['engine'=>'Innodb', 'comment' => '角色部门表', 'signed' => false]); + $table->addColumn('role_id', 'integer',['comment'=>'角色ID', 'signed' => false]) + ->addColumn('department_id', 'integer', ['comment'=>'部门ID', 'signed' => false]) + ->create(); + } +} diff --git a/catch/permissions/model/HasDepartmentsTrait.php b/catch/permissions/model/HasDepartmentsTrait.php new file mode 100644 index 0000000..55ea036 --- /dev/null +++ b/catch/permissions/model/HasDepartmentsTrait.php @@ -0,0 +1,57 @@ +belongsToMany(Department::class, 'role_has_departments', 'department_id', 'role_id'); + } + + /** + * + * @time 2019年12月08日 + * @return mixed + */ + public function getDepartments() + { + return $this->departments()->select(); + } + + /** + * + * @time 2019年12月08日 + * @param array $departments + * @return mixed + */ + public function attachDepartments(array $departments) + { + if (empty($departments)) { + return true; + } + + sort($departments); + + return $this->departments()->attach($departments); + } + + /** + * + * @time 2019年12月08日 + * @param array $departments + * @return mixed + */ + public function detachDepartments(array $departments = []) + { + if (empty($departments)) { + return $this->departments()->detach(); + } + + return $this->departments()->detach($departments); + } +} diff --git a/catch/permissions/model/Roles.php b/catch/permissions/model/Roles.php index 024018e..711f164 100644 --- a/catch/permissions/model/Roles.php +++ b/catch/permissions/model/Roles.php @@ -6,13 +6,16 @@ use catcher\base\CatchModel; class Roles extends CatchModel { + use HasDepartmentsTrait; + protected $name = 'roles'; protected $field = [ 'id', // 'role_name', // 角色名 'parent_id', // 父级ID - 'creator_id', + 'creator_id', + 'data_range', 'description', // 角色备注 'created_at', // 创建时间 'updated_at', // 更新时间