3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Auth\Permissions\PermissionsRepo;
6 use BookStack\Auth\Role;
7 use BookStack\Exceptions\UserUpdateException;
8 use Illuminate\Http\Request;
9 use Illuminate\Support\Facades\DB;
11 class RoleApiController extends ApiController
13 protected PermissionsRepo $permissionsRepo;
15 protected array $fieldsToExpose = [
16 'display_name', 'description', 'mfa_enforced', 'external_auth_id', 'created_at', 'updated_at',
21 'display_name' => ['required', 'min:3', 'max:180'],
22 'description' => ['max:180'],
23 'mfa_enforced' => ['boolean'],
24 'external_auth_id' => ['string'],
25 'permissions' => ['array'],
26 'permissions.*' => ['string'],
29 'display_name' => ['min:3', 'max:180'],
30 'description' => ['max:180'],
31 'mfa_enforced' => ['boolean'],
32 'external_auth_id' => ['string'],
33 'permissions' => ['array'],
34 'permissions.*' => ['string'],
38 public function __construct(PermissionsRepo $permissionsRepo)
40 $this->permissionsRepo = $permissionsRepo;
42 // Checks for all endpoints in this controller
43 $this->middleware(function ($request, $next) {
44 $this->checkPermission('user-roles-manage');
46 return $next($request);
51 * Get a listing of roles in the system.
52 * Requires permission to manage roles.
54 public function list()
56 $roles = Role::query()->select(['*'])
57 ->withCount(['users', 'permissions']);
59 return $this->apiListingResponse($roles, [
60 ...$this->fieldsToExpose,
67 * Create a new role in the system.
68 * Requires permission to manage roles.
70 public function create(Request $request)
72 $data = $this->validate($request, $this->rules()['create']);
75 DB::transaction(function () use ($data, &$role) {
76 $role = $this->permissionsRepo->saveNewRole($data);
79 $this->singleFormatter($role);
81 return response()->json($role);
85 * View the details of a single user.
86 * Requires permission to manage roles.
88 public function read(string $id)
90 $user = $this->permissionsRepo->getRoleById($id);
91 $this->singleFormatter($user);
93 return response()->json($user);
97 * Update an existing role in the system.
98 * Requires permission to manage roles.
100 public function update(Request $request, string $id)
102 $data = $this->validate($request, $this->rules()['update']);
103 $role = $this->permissionsRepo->updateRole($id, $data);
105 $this->singleFormatter($role);
107 return response()->json($role);
111 * Delete a user from the system.
112 * Can optionally accept a user id via `migrate_ownership_id` to indicate
113 * who should be the new owner of their related content.
114 * Requires permission to manage roles.
116 public function delete(string $id)
118 $this->permissionsRepo->deleteRole(intval($id));
120 return response('', 204);
124 * Format the given role model for single-result display.
126 protected function singleFormatter(Role $role)
128 $role->load('users:id,name,slug');
129 $role->unsetRelation('permissions');
130 $role->setAttribute('permissions', $role->permissions()->pluck('name'));
131 $role->makeVisible(['users', 'permissions']);