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', 'string', 'min:3', 'max:180'],
21 'description' => ['string', 'max:180'],
22 'mfa_enforced' => ['boolean'],
23 'external_auth_id' => ['string'],
24 'permissions' => ['array'],
25 'permissions.*' => ['string'],
28 'display_name' => ['string', 'min:3', 'max:180'],
29 'description' => ['string', '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 * Permissions should be provided as an array of permission name strings.
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 role.
86 * Provides the permissions and a high-level list of the users assigned.
87 * Requires permission to manage roles.
89 public function read(string $id)
91 $user = $this->permissionsRepo->getRoleById($id);
92 $this->singleFormatter($user);
94 return response()->json($user);
98 * Update an existing role in the system.
99 * Permissions should be provided as an array of permission name strings.
100 * An empty "permissions" array would clear granted permissions.
101 * In many cases, where permissions are changed, you'll want to fetch the existing
102 * permissions and then modify before providing in your update request.
103 * Requires permission to manage roles.
105 public function update(Request $request, string $id)
107 $data = $this->validate($request, $this->rules()['update']);
108 $role = $this->permissionsRepo->updateRole($id, $data);
110 $this->singleFormatter($role);
112 return response()->json($role);
116 * Delete a role from the system.
117 * Requires permission to manage roles.
119 public function delete(string $id)
121 $this->permissionsRepo->deleteRole(intval($id));
123 return response('', 204);
127 * Format the given role model for single-result display.
129 protected function singleFormatter(Role $role)
131 $role->load('users:id,name,slug');
132 $role->unsetRelation('permissions');
133 $role->setAttribute('permissions', $role->permissions()->orderBy('name', 'asc')->pluck('name'));
134 $role->makeVisible(['users', 'permissions']);