]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/RoleApiController.php
Aded roles API controller methods
[bookstack] / app / Http / Controllers / Api / RoleApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
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;
10
11 class RoleApiController extends ApiController
12 {
13     protected PermissionsRepo $permissionsRepo;
14
15     protected array $fieldsToExpose = [
16         'display_name', 'description', 'mfa_enforced', 'external_auth_id', 'created_at', 'updated_at',
17     ];
18
19     protected $rules = [
20         'create' => [
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'],
27         ],
28         'update' => [
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'],
35         ]
36     ];
37
38     public function __construct(PermissionsRepo $permissionsRepo)
39     {
40         $this->permissionsRepo = $permissionsRepo;
41
42         // Checks for all endpoints in this controller
43         $this->middleware(function ($request, $next) {
44             $this->checkPermission('user-roles-manage');
45
46             return $next($request);
47         });
48     }
49
50     /**
51      * Get a listing of roles in the system.
52      * Requires permission to manage roles.
53      */
54     public function list()
55     {
56         $roles = Role::query()->select(['*'])
57             ->withCount(['users', 'permissions']);
58
59         return $this->apiListingResponse($roles, [
60             ...$this->fieldsToExpose,
61             'permissions_count',
62             'users_count',
63         ]);
64     }
65
66     /**
67      * Create a new role in the system.
68      * Requires permission to manage roles.
69      */
70     public function create(Request $request)
71     {
72         $data = $this->validate($request, $this->rules()['create']);
73
74         $role = null;
75         DB::transaction(function () use ($data, &$role) {
76             $role = $this->permissionsRepo->saveNewRole($data);
77         });
78
79         $this->singleFormatter($role);
80
81         return response()->json($role);
82     }
83
84     /**
85      * View the details of a single user.
86      * Requires permission to manage roles.
87      */
88     public function read(string $id)
89     {
90         $user = $this->permissionsRepo->getRoleById($id);
91         $this->singleFormatter($user);
92
93         return response()->json($user);
94     }
95
96     /**
97      * Update an existing role in the system.
98      * Requires permission to manage roles.
99      */
100     public function update(Request $request, string $id)
101     {
102         $data = $this->validate($request, $this->rules()['update']);
103         $role = $this->permissionsRepo->updateRole($id, $data);
104
105         $this->singleFormatter($role);
106
107         return response()->json($role);
108     }
109
110     /**
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.
115      */
116     public function delete(string $id)
117     {
118         $this->permissionsRepo->deleteRole(intval($id));
119
120         return response('', 204);
121     }
122
123     /**
124      * Format the given role model for single-result display.
125      */
126     protected function singleFormatter(Role $role)
127     {
128         $role->load('users:id,name,slug');
129         $role->unsetRelation('permissions');
130         $role->setAttribute('permissions', $role->permissions()->pluck('name'));
131         $role->makeVisible(['users', 'permissions']);
132     }
133 }