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