新增角色部门
This commit is contained in:
parent
7c09bcf0cf
commit
1b7cc3fcf5
@ -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();
|
||||
// 删除
|
||||
|
@ -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))
|
||||
|
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use think\migration\Migrator;
|
||||
use think\migration\db\Column;
|
||||
|
||||
class RoleHasDepartments extends Migrator
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* Write your reversible migrations using this method.
|
||||
*
|
||||
* More information on writing migrations is available here:
|
||||
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
|
||||
*
|
||||
* The following commands can be used in this method and Phinx will
|
||||
* automatically reverse them when rolling back:
|
||||
*
|
||||
* createTable
|
||||
* renameTable
|
||||
* addColumn
|
||||
* renameColumn
|
||||
* addIndex
|
||||
* addForeignKey
|
||||
*
|
||||
* Remember to call "create()" or "update()" and NOT "save()" when working
|
||||
* with the Table class.
|
||||
*/
|
||||
public function change()
|
||||
{
|
||||
$table = $this->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();
|
||||
}
|
||||
}
|
57
catch/permissions/model/HasDepartmentsTrait.php
Normal file
57
catch/permissions/model/HasDepartmentsTrait.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
namespace catchAdmin\permissions\model;
|
||||
|
||||
trait HasDepartmentsTrait
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @time 2019年12月08日
|
||||
* @return mixed
|
||||
*/
|
||||
public function departments()
|
||||
{
|
||||
return $this->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);
|
||||
}
|
||||
}
|
@ -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', // 更新时间
|
||||
|
Loading…
x
Reference in New Issue
Block a user