2019-12-09 09:58:52 +08:00
|
|
|
<?php
|
2019-12-09 16:22:00 +08:00
|
|
|
namespace catchAdmin\permissions\controller;
|
|
|
|
|
2020-01-09 08:21:44 +08:00
|
|
|
use catcher\base\CatchRequest as Request;
|
2019-12-11 21:00:14 +08:00
|
|
|
use catcher\base\CatchController;
|
2019-12-09 16:22:00 +08:00
|
|
|
use catcher\CatchResponse;
|
2019-12-11 21:00:14 +08:00
|
|
|
use catcher\exceptions\FailedException;
|
|
|
|
use catcher\Tree;
|
2019-12-12 09:13:29 +08:00
|
|
|
use think\response\Json;
|
2019-12-09 16:22:00 +08:00
|
|
|
|
2019-12-12 18:52:33 +08:00
|
|
|
class Role extends CatchController
|
2019-12-09 16:22:00 +08:00
|
|
|
{
|
|
|
|
protected $role;
|
|
|
|
|
|
|
|
public function __construct(\catchAdmin\permissions\model\Roles $role)
|
|
|
|
{
|
|
|
|
$this->role = $role;
|
|
|
|
}
|
|
|
|
|
2019-12-26 09:03:09 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月09日
|
|
|
|
* @param Request $request
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-01-13 21:23:24 +08:00
|
|
|
public function index()
|
2019-12-09 16:22:00 +08:00
|
|
|
{
|
2020-01-13 21:23:24 +08:00
|
|
|
return CatchResponse::success(Tree::done($this->role->getList()));
|
2019-12-09 16:22:00 +08:00
|
|
|
}
|
|
|
|
|
2019-12-11 21:00:14 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月11日
|
|
|
|
* @throws \Exception
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-09 16:22:00 +08:00
|
|
|
public function create()
|
2019-12-26 09:03:09 +08:00
|
|
|
{}
|
2019-12-09 16:22:00 +08:00
|
|
|
|
2019-12-11 21:00:14 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月11日
|
|
|
|
* @param Request $request
|
2019-12-12 09:13:29 +08:00
|
|
|
* @return Json
|
|
|
|
* @throws \think\db\exception\DbException
|
2019-12-11 21:00:14 +08:00
|
|
|
*/
|
|
|
|
public function save(Request $request)
|
|
|
|
{
|
2019-12-12 09:13:29 +08:00
|
|
|
$this->role->storeBy($request->param());
|
|
|
|
|
2019-12-27 09:52:03 +08:00
|
|
|
if (!empty($request->param('permissions'))) {
|
|
|
|
$this->role->attach($request->param('permissions'));
|
2019-12-12 09:13:29 +08:00
|
|
|
}
|
2020-01-12 12:54:59 +08:00
|
|
|
if (!empty($request->param('departments'))) {
|
|
|
|
$this->role->attachDepartments($request->param('departments'));
|
|
|
|
}
|
2019-12-11 21:00:14 +08:00
|
|
|
// 添加角色
|
2019-12-12 09:13:29 +08:00
|
|
|
return CatchResponse::success();
|
2019-12-11 21:00:14 +08:00
|
|
|
}
|
2019-12-09 16:22:00 +08:00
|
|
|
|
|
|
|
public function read($id)
|
|
|
|
{
|
2019-12-27 09:52:03 +08:00
|
|
|
$role = $this->role->findBy($id);
|
|
|
|
$role->permissions = $role->getPermissions();
|
2020-01-12 12:54:59 +08:00
|
|
|
$role->departments = $role->getDepartments();
|
2019-12-27 09:52:03 +08:00
|
|
|
return CatchResponse::success($role);
|
2019-12-09 16:22:00 +08:00
|
|
|
}
|
|
|
|
|
2019-12-11 21:00:14 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月11日
|
|
|
|
* @param $id
|
|
|
|
* @throws \Exception
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-12-09 16:22:00 +08:00
|
|
|
public function edit($id)
|
2019-12-27 09:52:03 +08:00
|
|
|
{}
|
2019-12-09 16:22:00 +08:00
|
|
|
|
2019-12-11 21:00:14 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月11日
|
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
2019-12-12 09:13:29 +08:00
|
|
|
* @return Json
|
|
|
|
* @throws \think\db\exception\DbException
|
2019-12-11 21:00:14 +08:00
|
|
|
*/
|
|
|
|
public function update($id, Request $request)
|
2019-12-09 16:22:00 +08:00
|
|
|
{
|
2019-12-12 09:13:29 +08:00
|
|
|
$this->role->updateBy($id, $request->param());
|
|
|
|
|
|
|
|
$role = $this->role->findBy($id);
|
|
|
|
|
|
|
|
$role->detach();
|
|
|
|
|
2019-12-27 09:52:03 +08:00
|
|
|
if (!empty($request->param('permissions'))) {
|
|
|
|
$role->attach($request->param('permissions'));
|
2019-12-12 09:13:29 +08:00
|
|
|
}
|
2020-01-12 12:54:59 +08:00
|
|
|
if (!empty($request->param('departments'))) {
|
|
|
|
$role->detachDepartments();
|
|
|
|
$role->attach($request->param('departments'));
|
|
|
|
}
|
2019-12-12 09:13:29 +08:00
|
|
|
return CatchResponse::success();
|
2019-12-09 16:22:00 +08:00
|
|
|
}
|
|
|
|
|
2019-12-11 21:00:14 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月11日
|
|
|
|
* @param $id
|
|
|
|
* @throws FailedException
|
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
|
* @throws \think\db\exception\DbException
|
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
2019-12-12 09:13:29 +08:00
|
|
|
* @return Json
|
2019-12-11 21:00:14 +08:00
|
|
|
*/
|
2019-12-09 16:22:00 +08:00
|
|
|
public function delete($id)
|
|
|
|
{
|
2019-12-11 21:00:14 +08:00
|
|
|
if ($this->role->where('parent_id', $id)->find()) {
|
|
|
|
throw new FailedException('存在子角色,无法删除');
|
|
|
|
}
|
2019-12-12 09:13:29 +08:00
|
|
|
$role = $this->role->findBy($id);
|
2019-12-11 21:00:14 +08:00
|
|
|
// 删除权限
|
2019-12-12 09:13:29 +08:00
|
|
|
$role->detach();
|
2020-01-12 12:54:59 +08:00
|
|
|
// 删除部门关联
|
|
|
|
$role->detachDepartments();
|
2019-12-12 09:13:29 +08:00
|
|
|
// 删除用户关联
|
|
|
|
$role->users()->detach();
|
|
|
|
// 删除
|
|
|
|
$this->role->deleteBy($id);
|
|
|
|
|
|
|
|
return CatchResponse::success();
|
2019-12-09 16:22:00 +08:00
|
|
|
}
|
|
|
|
|
2019-12-12 09:13:29 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月11日
|
|
|
|
* @param Request $request
|
|
|
|
* @param \catchAdmin\permissions\model\Permissions $permission
|
|
|
|
* @return Json
|
|
|
|
*/
|
|
|
|
public function getPermissions(Request $request, \catchAdmin\permissions\model\Permissions $permission): Json
|
|
|
|
{
|
2019-12-12 18:52:11 +08:00
|
|
|
$parentRoleHasPermissionIds = null;
|
|
|
|
if ($request->param('parent_id')) {
|
|
|
|
$permissions = $this->role->findBy($request->param('parent_id'))->getPermissions();
|
|
|
|
foreach ($permissions as $_permission) {
|
|
|
|
$parentRoleHasPermissionIds[] = $_permission->pivot->permission_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$permissions = Tree::done($permission->getList([
|
|
|
|
'permission_ids' => $parentRoleHasPermissionIds
|
|
|
|
]));
|
2019-12-12 09:13:29 +08:00
|
|
|
|
|
|
|
$permissionIds = [];
|
|
|
|
if ($request->param('role_id')) {
|
|
|
|
$roleHasPermissions = $this->role->findBy($request->param('role_id'))->getPermissions();
|
|
|
|
foreach ($roleHasPermissions as $_permission) {
|
|
|
|
$permissionIds[] = $_permission->pivot->permission_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CatchResponse::success([
|
|
|
|
'permissions' => $permissions,
|
|
|
|
'hasPermissions' => $permissionIds,
|
|
|
|
]);
|
|
|
|
}
|
2019-12-26 09:03:09 +08:00
|
|
|
}
|