3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Auth\Permissions\PermissionsRepo;
6 use BookStack\Auth\Role;
7 use Illuminate\Http\Request;
8 use Illuminate\Support\Facades\DB;
10 class RoleApiController extends ApiController
12 protected PermissionsRepo $permissionsRepo;
14 protected array $fieldsToExpose = [
15 'display_name', 'description', 'mfa_enforced', 'external_auth_id', 'created_at', 'updated_at',
20 'display_name' => ['required', 'min:3', 'max:180'],
21 'description' => ['max:180'],
22 'mfa_enforced' => ['boolean'],
23 'external_auth_id' => ['string'],
24 'permissions' => ['array'],
25 'permissions.*' => ['string'],
28 'display_name' => ['min:3', 'max:180'],
29 'description' => ['max:180'],
30 'mfa_enforced' => ['boolean'],
31 'external_auth_id' => ['string'],
32 'permissions' => ['array'],
33 'permissions.*' => ['string'],
37 public function __construct(PermissionsRepo $permissionsRepo)
39 $this->permissionsRepo = $permissionsRepo;
41 // Checks for all endpoints in this controller
42 $this->middleware(function ($request, $next) {
43 $this->checkPermission('user-roles-manage');
45 return $next($request);
50 * Get a listing of roles in the system.
51 * Requires permission to manage roles.
53 public function list()
55 $roles = Role::query()->select(['*'])
56 ->withCount(['users', 'permissions']);
58 return $this->apiListingResponse($roles, [
59 ...$this->fieldsToExpose,
66 * Create a new role in the system.
67 * Requires permission to manage roles.
69 public function create(Request $request)
71 $data = $this->validate($request, $this->rules()['create']);
74 DB::transaction(function () use ($data, &$role) {
75 $role = $this->permissionsRepo->saveNewRole($data);
78 $this->singleFormatter($role);
80 return response()->json($role);
84 * View the details of a single user.
85 * Requires permission to manage roles.
87 public function read(string $id)
89 $user = $this->permissionsRepo->getRoleById($id);
90 $this->singleFormatter($user);
92 return response()->json($user);
96 * Update an existing role in the system.
97 * Requires permission to manage roles.
99 public function update(Request $request, string $id)
101 $data = $this->validate($request, $this->rules()['update']);
102 $role = $this->permissionsRepo->updateRole($id, $data);
104 $this->singleFormatter($role);
106 return response()->json($role);
110 * Delete a user from the system.
111 * Can optionally accept a user id via `migrate_ownership_id` to indicate
112 * who should be the new owner of their related content.
113 * Requires permission to manage roles.
115 public function delete(string $id)
117 $this->permissionsRepo->deleteRole(intval($id));
119 return response('', 204);
123 * Format the given role model for single-result display.
125 protected function singleFormatter(Role $role)
127 $role->load('users:id,name,slug');
128 $role->unsetRelation('permissions');
129 $role->setAttribute('permissions', $role->permissions()->orderBy('name', 'asc')->pluck('name'));
130 $role->makeVisible(['users', 'permissions']);