3 namespace BookStack\Users\Controllers;
5 use BookStack\Http\ApiController;
6 use BookStack\Permissions\PermissionsRepo;
7 use BookStack\Users\Models\Role;
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', 'string', 'min:3', 'max:180'],
22 'description' => ['string', 'max:180'],
23 'mfa_enforced' => ['boolean'],
24 'external_auth_id' => ['string'],
25 'permissions' => ['array'],
26 'permissions.*' => ['string'],
29 'display_name' => ['string', 'min:3', 'max:180'],
30 'description' => ['string', '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 * Permissions should be provided as an array of permission name strings.
69 * Requires permission to manage roles.
71 public function create(Request $request)
73 $data = $this->validate($request, $this->rules()['create']);
76 DB::transaction(function () use ($data, &$role) {
77 $role = $this->permissionsRepo->saveNewRole($data);
80 $this->singleFormatter($role);
82 return response()->json($role);
86 * View the details of a single role.
87 * Provides the permissions and a high-level list of the users assigned.
88 * Requires permission to manage roles.
90 public function read(string $id)
92 $role = $this->permissionsRepo->getRoleById($id);
93 $this->singleFormatter($role);
95 return response()->json($role);
99 * Update an existing role in the system.
100 * Permissions should be provided as an array of permission name strings.
101 * An empty "permissions" array would clear granted permissions.
102 * In many cases, where permissions are changed, you'll want to fetch the existing
103 * permissions and then modify before providing in your update request.
104 * Requires permission to manage roles.
106 public function update(Request $request, string $id)
108 $data = $this->validate($request, $this->rules()['update']);
109 $role = $this->permissionsRepo->updateRole($id, $data);
111 $this->singleFormatter($role);
113 return response()->json($role);
117 * Delete a role from the system.
118 * Requires permission to manage roles.
120 public function delete(string $id)
122 $this->permissionsRepo->deleteRole(intval($id));
124 return response('', 204);
128 * Format the given role model for single-result display.
130 protected function singleFormatter(Role $role)
132 $role->load('users:id,name,slug');
133 $role->unsetRelation('permissions');
134 $role->setAttribute('permissions', $role->permissions()->orderBy('name', 'asc')->pluck('name'));
135 $role->makeVisible(['users', 'permissions']);