新增角色部门

This commit is contained in:
yanwenwu 2020-01-12 12:54:59 +08:00
parent 7c09bcf0cf
commit 1b7cc3fcf5
5 changed files with 108 additions and 2 deletions

View File

@ -51,6 +51,9 @@ class Role extends CatchController
if (!empty($request->param('permissions'))) { if (!empty($request->param('permissions'))) {
$this->role->attach($request->param('permissions')); $this->role->attach($request->param('permissions'));
} }
if (!empty($request->param('departments'))) {
$this->role->attachDepartments($request->param('departments'));
}
// 添加角色 // 添加角色
return CatchResponse::success(); return CatchResponse::success();
} }
@ -59,6 +62,7 @@ class Role extends CatchController
{ {
$role = $this->role->findBy($id); $role = $this->role->findBy($id);
$role->permissions = $role->getPermissions(); $role->permissions = $role->getPermissions();
$role->departments = $role->getDepartments();
return CatchResponse::success($role); return CatchResponse::success($role);
} }
@ -91,7 +95,10 @@ class Role extends CatchController
if (!empty($request->param('permissions'))) { if (!empty($request->param('permissions'))) {
$role->attach($request->param('permissions')); $role->attach($request->param('permissions'));
} }
if (!empty($request->param('departments'))) {
$role->detachDepartments();
$role->attach($request->param('departments'));
}
return CatchResponse::success(); return CatchResponse::success();
} }
@ -113,6 +120,8 @@ class Role extends CatchController
$role = $this->role->findBy($id); $role = $this->role->findBy($id);
// 删除权限 // 删除权限
$role->detach(); $role->detach();
// 删除部门关联
$role->detachDepartments();
// 删除用户关联 // 删除用户关联
$role->users()->detach(); $role->users()->detach();
// 删除 // 删除

View File

@ -32,6 +32,7 @@ class Roles extends Migrator
$table->addColumn('role_name', 'string',['limit' => 15,'default'=>'','comment'=>'角色名']) $table->addColumn('role_name', 'string',['limit' => 15,'default'=>'','comment'=>'角色名'])
->addColumn('parent_id', 'integer',['default'=>0,'comment'=>'父级ID', 'signed' => false]) ->addColumn('parent_id', 'integer',['default'=>0,'comment'=>'父级ID', 'signed' => false])
->addColumn('description', 'string',['default'=> '','comment'=>'角色备注']) ->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('creator_id', 'integer',['default' => 0, 'comment'=>'创建人ID'])
->addColumn('created_at', 'integer', array('default'=>0,'comment'=>'创建时间', 'signed' => false )) ->addColumn('created_at', 'integer', array('default'=>0,'comment'=>'创建时间', 'signed' => false ))
->addColumn('updated_at', 'integer', array('default'=>0,'comment'=>'更新时间', 'signed' => false)) ->addColumn('updated_at', 'integer', array('default'=>0,'comment'=>'更新时间', 'signed' => false))

View File

@ -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();
}
}

View 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);
}
}

View File

@ -6,6 +6,8 @@ use catcher\base\CatchModel;
class Roles extends CatchModel class Roles extends CatchModel
{ {
use HasDepartmentsTrait;
protected $name = 'roles'; protected $name = 'roles';
protected $field = [ protected $field = [
@ -13,6 +15,7 @@ class Roles extends CatchModel
'role_name', // 角色名 'role_name', // 角色名
'parent_id', // 父级ID 'parent_id', // 父级ID
'creator_id', 'creator_id',
'data_range',
'description', // 角色备注 'description', // 角色备注
'created_at', // 创建时间 'created_at', // 创建时间
'updated_at', // 更新时间 'updated_at', // 更新时间