185 lines
5.4 KiB
PHP
Raw Normal View History

2020-04-29 17:37:45 +08:00
<?php
namespace catchAdmin\permissions\controller;
2021-07-29 17:58:22 +08:00
use catchAdmin\permissions\model\Permissions;
2020-09-08 16:25:36 +08:00
use catchAdmin\permissions\model\Roles;
2020-04-29 17:37:45 +08:00
use catcher\base\CatchRequest as Request;
use catcher\base\CatchController;
use catcher\CatchResponse;
use catcher\exceptions\FailedException;
2021-08-31 14:16:13 +08:00
use catcher\Utils;
2020-04-29 17:37:45 +08:00
use think\response\Json;
2020-10-29 08:13:20 +08:00
use catchAdmin\permissions\model\Roles as RoleModel;
2020-04-29 17:37:45 +08:00
class Role extends CatchController
{
protected $role;
2020-10-29 08:13:20 +08:00
public function __construct(RoleModel $role)
2020-04-29 17:37:45 +08:00
{
$this->role = $role;
}
/**
*
* @time 2019年12月09日
2021-04-18 07:45:02 +08:00
* @return string|Json
2020-04-29 17:37:45 +08:00
*/
public function index()
{
2020-10-21 08:12:07 +08:00
return CatchResponse::success($this->role->getList());
2020-04-29 17:37:45 +08:00
}
/**
*
* @time 2019年12月11日
* @param Request $request
* @return Json
* @throws \think\db\exception\DbException
*/
public function save(Request $request)
{
2020-09-08 16:25:36 +08:00
$params = $request->param();
2020-04-29 17:37:45 +08:00
2020-09-08 16:25:36 +08:00
if (Roles::where('identify', $params['identify'])->find()) {
throw new FailedException('角色标识 [' . $params['identify'] . ']已存在');
}
$this->role->storeBy($params);
2021-02-08 11:26:54 +08:00
// 分配权限
if (count($params['permissions'])) {
$this->role->attachPermissions(array_unique($params['permissions']));
2020-04-29 17:37:45 +08:00
}
2021-02-08 11:26:54 +08:00
// 分配部门
2021-03-31 20:20:21 +08:00
if (isset($params['departments']) && count($params['departments'])) {
2021-02-07 17:21:34 +08:00
$this->role->attachDepartments($params['departments']);
}
2020-04-29 17:37:45 +08:00
// 添加角色
return CatchResponse::success();
}
public function read($id)
{
$role = $this->role->findBy($id);
$role->permissions = $role->getPermissions();
2021-02-07 17:21:34 +08:00
$role->departments = $role->getDepartments();
2020-04-29 17:37:45 +08:00
return CatchResponse::success($role);
}
/**
*
* @time 2019年12月11日
* @param $id
* @param Request $request
* @return Json
* @throws \think\db\exception\DbException
*/
public function update($id, Request $request): Json
{
2020-09-08 16:25:36 +08:00
if (Roles::where('identify', $request->param('identify'))->where('id', '<>', $id)->find()) {
throw new FailedException('角色标识 [' . $request->param('identify') . ']已存在');
}
2020-04-29 17:37:45 +08:00
$this->role->updateBy($id, $request->param());
$role = $this->role->findBy($id);
2020-06-21 21:07:15 +08:00
$hasPermissionIds = $role->getPermissions()->column('id');
$permissionIds = $request->param('permissions');
// 已存在权限 IDS
$existedPermissionIds = [];
foreach ($hasPermissionIds as $hasPermissionId) {
if (in_array($hasPermissionId, $permissionIds)) {
$existedPermissionIds[] = $hasPermissionId;
}
2020-04-29 17:37:45 +08:00
}
2020-06-21 21:07:15 +08:00
$attachIds = array_diff($permissionIds, $existedPermissionIds);
$detachIds = array_diff($hasPermissionIds, $existedPermissionIds);
if (!empty($detachIds)) {
2020-07-24 21:40:26 +08:00
$role->detachPermissions($detachIds);
2020-04-29 17:37:45 +08:00
}
2020-06-21 21:07:15 +08:00
if (!empty($attachIds)) {
2020-07-24 21:40:26 +08:00
$role->attachPermissions(array_unique($attachIds));
2020-06-21 21:07:15 +08:00
}
2021-02-07 17:21:34 +08:00
// 更新department
$hasDepartmentIds = $role->getDepartments()->column('id');
$departmentIds = $request->param('departments',[]);
// 已存在部门 IDS
$existedDepartmentIds = [];
foreach ($hasDepartmentIds as $hasDepartmentId) {
if (in_array($hasDepartmentId, $departmentIds)) {
$existedDepartmentIds[] = $hasDepartmentId;
}
}
$attachDepartmentIds = array_diff($departmentIds, $existedDepartmentIds);
$detachDepartmentIds = array_diff($hasDepartmentIds, $existedDepartmentIds);
if (!empty($detachDepartmentIds)) {
$role->detachDepartments($detachDepartmentIds);
}
if (!empty($attachDepartmentIds)) {
$role->attachDepartments(array_unique($attachDepartmentIds));
}
2020-04-29 17:37:45 +08:00
return CatchResponse::success();
}
/**
*
* @time 2019年12月11日
* @param $id
* @throws FailedException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return Json
*/
public function delete($id): Json
{
if ($this->role->where('parent_id', $id)->find()) {
throw new FailedException('存在子角色,无法删除');
}
$role = $this->role->findBy($id);
// 删除权限
2020-07-24 21:40:26 +08:00
$role->detachPermissions();
2021-02-07 17:21:34 +08:00
// 删除部门关联
$role->detachDepartments();
2020-04-29 17:37:45 +08:00
// 删除用户关联
$role->users()->detach();
// 删除
$this->role->deleteBy($id);
return CatchResponse::success();
}
2021-07-29 17:58:22 +08:00
/**
* 获取角色权限
*
* @time 2021年07月29日
* @param $id
* @return Json
2021-08-31 14:16:13 +08:00
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
2021-07-29 17:58:22 +08:00
*/
public function getPermissions($id): Json
{
2021-09-26 20:35:57 +08:00
$permissionIds = $this->role->findBy($id)->getPermissions()->column('id');
if (! count($permissionIds)) {
$permissions = Permissions::field(['id', 'parent_id', 'permission_name'])->select()->toTree();
} else {
$permissions = Permissions::whereIn('id', $permissionIds)->field(['id', 'parent_id', 'permission_name'])->select()->toTree();
2021-08-31 14:16:13 +08:00
}
2021-09-26 20:35:57 +08:00
return CatchResponse::success($permissions);
2021-07-29 17:58:22 +08:00
}
2020-04-29 17:37:45 +08:00
}