2023-02-19 02:36:34 +08:00
|
|
|
<?php
|
|
|
|
|
2023-05-18 00:56:55 +08:00
|
|
|
namespace BookStack\Users\Controllers;
|
2023-02-19 02:36:34 +08:00
|
|
|
|
2023-05-19 03:53:39 +08:00
|
|
|
use BookStack\Http\ApiController;
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Permissions\PermissionsRepo;
|
|
|
|
use BookStack\Users\Models\Role;
|
2023-02-19 02:36:34 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
class RoleApiController extends ApiController
|
|
|
|
{
|
|
|
|
protected PermissionsRepo $permissionsRepo;
|
|
|
|
|
|
|
|
protected array $fieldsToExpose = [
|
|
|
|
'display_name', 'description', 'mfa_enforced', 'external_auth_id', 'created_at', 'updated_at',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $rules = [
|
|
|
|
'create' => [
|
2023-02-19 23:58:29 +08:00
|
|
|
'display_name' => ['required', 'string', 'min:3', 'max:180'],
|
|
|
|
'description' => ['string', 'max:180'],
|
2023-02-19 02:36:34 +08:00
|
|
|
'mfa_enforced' => ['boolean'],
|
2024-06-09 03:33:34 +08:00
|
|
|
'external_auth_id' => ['string', 'max:180'],
|
2023-02-19 02:36:34 +08:00
|
|
|
'permissions' => ['array'],
|
|
|
|
'permissions.*' => ['string'],
|
|
|
|
],
|
|
|
|
'update' => [
|
2023-02-19 23:58:29 +08:00
|
|
|
'display_name' => ['string', 'min:3', 'max:180'],
|
|
|
|
'description' => ['string', 'max:180'],
|
2023-02-19 02:36:34 +08:00
|
|
|
'mfa_enforced' => ['boolean'],
|
2024-06-09 03:33:34 +08:00
|
|
|
'external_auth_id' => ['string', 'max:180'],
|
2023-02-19 02:36:34 +08:00
|
|
|
'permissions' => ['array'],
|
|
|
|
'permissions.*' => ['string'],
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct(PermissionsRepo $permissionsRepo)
|
|
|
|
{
|
|
|
|
$this->permissionsRepo = $permissionsRepo;
|
|
|
|
|
|
|
|
// Checks for all endpoints in this controller
|
|
|
|
$this->middleware(function ($request, $next) {
|
|
|
|
$this->checkPermission('user-roles-manage');
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a listing of roles in the system.
|
|
|
|
* Requires permission to manage roles.
|
|
|
|
*/
|
|
|
|
public function list()
|
|
|
|
{
|
|
|
|
$roles = Role::query()->select(['*'])
|
|
|
|
->withCount(['users', 'permissions']);
|
|
|
|
|
|
|
|
return $this->apiListingResponse($roles, [
|
|
|
|
...$this->fieldsToExpose,
|
|
|
|
'permissions_count',
|
|
|
|
'users_count',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new role in the system.
|
2023-02-19 23:58:29 +08:00
|
|
|
* Permissions should be provided as an array of permission name strings.
|
2023-02-19 02:36:34 +08:00
|
|
|
* Requires permission to manage roles.
|
|
|
|
*/
|
|
|
|
public function create(Request $request)
|
|
|
|
{
|
|
|
|
$data = $this->validate($request, $this->rules()['create']);
|
|
|
|
|
|
|
|
$role = null;
|
|
|
|
DB::transaction(function () use ($data, &$role) {
|
|
|
|
$role = $this->permissionsRepo->saveNewRole($data);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->singleFormatter($role);
|
|
|
|
|
|
|
|
return response()->json($role);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-19 23:58:29 +08:00
|
|
|
* View the details of a single role.
|
|
|
|
* Provides the permissions and a high-level list of the users assigned.
|
2023-02-19 02:36:34 +08:00
|
|
|
* Requires permission to manage roles.
|
|
|
|
*/
|
|
|
|
public function read(string $id)
|
|
|
|
{
|
2023-03-14 20:19:19 +08:00
|
|
|
$role = $this->permissionsRepo->getRoleById($id);
|
|
|
|
$this->singleFormatter($role);
|
2023-02-19 02:36:34 +08:00
|
|
|
|
2023-03-14 20:19:19 +08:00
|
|
|
return response()->json($role);
|
2023-02-19 02:36:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing role in the system.
|
2023-02-19 23:58:29 +08:00
|
|
|
* Permissions should be provided as an array of permission name strings.
|
|
|
|
* An empty "permissions" array would clear granted permissions.
|
|
|
|
* In many cases, where permissions are changed, you'll want to fetch the existing
|
|
|
|
* permissions and then modify before providing in your update request.
|
2023-02-19 02:36:34 +08:00
|
|
|
* Requires permission to manage roles.
|
|
|
|
*/
|
|
|
|
public function update(Request $request, string $id)
|
|
|
|
{
|
|
|
|
$data = $this->validate($request, $this->rules()['update']);
|
|
|
|
$role = $this->permissionsRepo->updateRole($id, $data);
|
|
|
|
|
|
|
|
$this->singleFormatter($role);
|
|
|
|
|
|
|
|
return response()->json($role);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-19 23:58:29 +08:00
|
|
|
* Delete a role from the system.
|
2023-02-19 02:36:34 +08:00
|
|
|
* Requires permission to manage roles.
|
|
|
|
*/
|
|
|
|
public function delete(string $id)
|
|
|
|
{
|
|
|
|
$this->permissionsRepo->deleteRole(intval($id));
|
|
|
|
|
|
|
|
return response('', 204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the given role model for single-result display.
|
|
|
|
*/
|
|
|
|
protected function singleFormatter(Role $role)
|
|
|
|
{
|
|
|
|
$role->load('users:id,name,slug');
|
|
|
|
$role->unsetRelation('permissions');
|
2023-02-19 02:50:01 +08:00
|
|
|
$role->setAttribute('permissions', $role->permissions()->orderBy('name', 'asc')->pluck('name'));
|
2023-02-19 02:36:34 +08:00
|
|
|
$role->makeVisible(['users', 'permissions']);
|
|
|
|
}
|
|
|
|
}
|