2022-12-05 23:01:12 +08:00
|
|
|
<?php
|
2022-12-06 19:27:38 +08:00
|
|
|
|
2022-12-05 23:01:12 +08:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Modules\Permissions\Http\Controllers;
|
|
|
|
|
|
|
|
use Catch\Base\CatchController as Controller;
|
2022-12-23 19:47:13 +08:00
|
|
|
use Catch\Exceptions\FailedException;
|
2023-05-10 16:52:17 +08:00
|
|
|
use Illuminate\Http\Request;
|
2023-01-08 20:30:37 +08:00
|
|
|
use Modules\Permissions\Enums\DataRange;
|
2022-12-23 19:47:13 +08:00
|
|
|
use Modules\Permissions\Models\Roles;
|
2022-12-05 23:01:12 +08:00
|
|
|
use Modules\Permissions\Http\Requests\RoleRequest;
|
|
|
|
|
|
|
|
class RolesController extends Controller
|
|
|
|
{
|
2023-01-08 20:30:37 +08:00
|
|
|
/**
|
|
|
|
* @param Roles $model
|
|
|
|
*/
|
2022-12-05 23:01:12 +08:00
|
|
|
public function __construct(
|
2022-12-23 19:47:13 +08:00
|
|
|
protected readonly Roles $model
|
2022-12-06 19:27:38 +08:00
|
|
|
) {
|
|
|
|
}
|
2022-12-05 23:01:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function index(): mixed
|
|
|
|
{
|
2022-12-23 19:47:13 +08:00
|
|
|
return $this->model->setBeforeGetList(function ($query) {
|
|
|
|
return $query->with(['permissions' => function ($query) {
|
|
|
|
$query->select('id');
|
2023-01-17 18:19:25 +08:00
|
|
|
}]);
|
2022-12-16 18:30:36 +08:00
|
|
|
})->getList();
|
2022-12-05 23:01:12 +08:00
|
|
|
}
|
|
|
|
|
2023-01-08 20:30:37 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param RoleRequest $request
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-12-05 23:01:12 +08:00
|
|
|
public function store(RoleRequest $request)
|
|
|
|
{
|
2023-05-10 16:52:17 +08:00
|
|
|
$data = $request->all();
|
2023-06-03 07:42:20 +08:00
|
|
|
if ($data['data_range'] && ! DataRange::Personal_Choose->assert($data['data_range'])) {
|
2023-05-10 16:52:17 +08:00
|
|
|
$data['departments'] = [];
|
2023-01-08 20:30:37 +08:00
|
|
|
}
|
|
|
|
|
2023-05-10 16:52:17 +08:00
|
|
|
return $this->model->storeBy($data);
|
2022-12-05 23:01:12 +08:00
|
|
|
}
|
|
|
|
|
2023-01-08 20:30:37 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param $id
|
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null
|
|
|
|
*/
|
2023-05-10 16:52:17 +08:00
|
|
|
public function show($id, Request $request)
|
2022-12-05 23:01:12 +08:00
|
|
|
{
|
2022-12-16 18:30:36 +08:00
|
|
|
$role = $this->model->firstBy($id);
|
|
|
|
|
2023-05-10 16:52:17 +08:00
|
|
|
if ($request->has('from') && $request->get('from') == 'parent_role') {
|
|
|
|
$role->setAttribute('permissions', $role->permissions()->get()->toTree());
|
|
|
|
} else {
|
|
|
|
$role->setAttribute('permissions', $role->permissions()->get()->pluck('id'));
|
|
|
|
}
|
2022-12-16 18:30:36 +08:00
|
|
|
|
2023-01-08 20:30:37 +08:00
|
|
|
$role->setAttribute('departments', $role->departments()->pluck('id'));
|
2023-01-02 18:36:07 +08:00
|
|
|
|
2022-12-16 18:30:36 +08:00
|
|
|
return $role;
|
2022-12-05 23:01:12 +08:00
|
|
|
}
|
|
|
|
|
2023-01-08 20:30:37 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param $id
|
|
|
|
* @param RoleRequest $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2022-12-05 23:01:12 +08:00
|
|
|
public function update($id, RoleRequest $request)
|
|
|
|
{
|
2023-04-20 10:28:40 +08:00
|
|
|
$data = $request->all();
|
2023-06-03 07:42:20 +08:00
|
|
|
if ($data['data_range'] && ! DataRange::Personal_Choose->assert($data['data_range'])) {
|
2023-04-20 10:28:40 +08:00
|
|
|
$data['departments'] = [];
|
2023-01-08 20:30:37 +08:00
|
|
|
}
|
|
|
|
|
2023-05-10 16:52:17 +08:00
|
|
|
return $this->model->updateBy($id, $data);
|
2022-12-05 23:01:12 +08:00
|
|
|
}
|
|
|
|
|
2023-01-08 20:30:37 +08:00
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @return bool|null
|
|
|
|
*/
|
2022-12-05 23:01:12 +08:00
|
|
|
public function destroy($id)
|
|
|
|
{
|
2022-12-23 19:47:13 +08:00
|
|
|
if ($this->model->where($this->model->getParentIdColumn(), $id)->first()) {
|
2023-01-08 20:30:37 +08:00
|
|
|
throw new FailedException('请先删除子角色');
|
2022-12-23 19:47:13 +08:00
|
|
|
}
|
|
|
|
|
2022-12-05 23:01:12 +08:00
|
|
|
return $this->model->deleteBy($id);
|
|
|
|
}
|
|
|
|
}
|