]> BookStack Code Mirror - bookstack/blob - app/Auth/Permissions/PermissionsRepo.php
Re-structured the app code to be feature based rather than code type based
[bookstack] / app / Auth / Permissions / PermissionsRepo.php
1 <?php namespace BookStack\Auth\Permissions;
2
3 use BookStack\Auth\Permissions;
4 use BookStack\Exceptions\PermissionsException;
5 use BookStack\Auth\Permissions\RolePermission;
6 use BookStack\Auth\Role;
7 use BookStack\Auth\Permissions\PermissionService;
8 use Setting;
9
10 class PermissionsRepo
11 {
12
13     protected $permission;
14     protected $role;
15     protected $permissionService;
16
17     protected $systemRoles = ['admin', 'public'];
18
19     /**
20      * PermissionsRepo constructor.
21      * @param RolePermission $permission
22      * @param Role $role
23      * @param \BookStack\Auth\Permissions\PermissionService $permissionService
24      */
25     public function __construct(RolePermission $permission, Role $role, Permissions\PermissionService $permissionService)
26     {
27         $this->permission = $permission;
28         $this->role = $role;
29         $this->permissionService = $permissionService;
30     }
31
32     /**
33      * Get all the user roles from the system.
34      * @return \Illuminate\Database\Eloquent\Collection|static[]
35      */
36     public function getAllRoles()
37     {
38         return $this->role->all();
39     }
40
41     /**
42      * Get all the roles except for the provided one.
43      * @param Role $role
44      * @return mixed
45      */
46     public function getAllRolesExcept(Role $role)
47     {
48         return $this->role->where('id', '!=', $role->id)->get();
49     }
50
51     /**
52      * Get a role via its ID.
53      * @param $id
54      * @return mixed
55      */
56     public function getRoleById($id)
57     {
58         return $this->role->findOrFail($id);
59     }
60
61     /**
62      * Save a new role into the system.
63      * @param array $roleData
64      * @return Role
65      */
66     public function saveNewRole($roleData)
67     {
68         $role = $this->role->newInstance($roleData);
69         $role->name = str_replace(' ', '-', strtolower($roleData['display_name']));
70         // Prevent duplicate names
71         while ($this->role->where('name', '=', $role->name)->count() > 0) {
72             $role->name .= strtolower(str_random(2));
73         }
74         $role->save();
75
76         $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
77         $this->assignRolePermissions($role, $permissions);
78         $this->permissionService->buildJointPermissionForRole($role);
79         return $role;
80     }
81
82     /**
83      * Updates an existing role.
84      * Ensure Admin role always have core permissions.
85      * @param $roleId
86      * @param $roleData
87      * @throws PermissionsException
88      */
89     public function updateRole($roleId, $roleData)
90     {
91         $role = $this->role->findOrFail($roleId);
92
93         $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
94         if ($role->system_name === 'admin') {
95             $permissions = array_merge($permissions, [
96                 'users-manage',
97                 'user-roles-manage',
98                 'restrictions-manage-all',
99                 'restrictions-manage-own',
100                 'settings-manage',
101             ]);
102         }
103
104         $this->assignRolePermissions($role, $permissions);
105
106         $role->fill($roleData);
107         $role->save();
108         $this->permissionService->buildJointPermissionForRole($role);
109     }
110
111     /**
112      * Assign an list of permission names to an role.
113      * @param Role $role
114      * @param array $permissionNameArray
115      */
116     public function assignRolePermissions(Role $role, $permissionNameArray = [])
117     {
118         $permissions = [];
119         $permissionNameArray = array_values($permissionNameArray);
120         if ($permissionNameArray && count($permissionNameArray) > 0) {
121             $permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray();
122         }
123         $role->permissions()->sync($permissions);
124     }
125
126     /**
127      * Delete a role from the system.
128      * Check it's not an admin role or set as default before deleting.
129      * If an migration Role ID is specified the users assign to the current role
130      * will be added to the role of the specified id.
131      * @param $roleId
132      * @param $migrateRoleId
133      * @throws PermissionsException
134      */
135     public function deleteRole($roleId, $migrateRoleId)
136     {
137         $role = $this->role->findOrFail($roleId);
138
139         // Prevent deleting admin role or default registration role.
140         if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
141             throw new PermissionsException(trans('errors.role_system_cannot_be_deleted'));
142         } else if ($role->id == setting('registration-role')) {
143             throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
144         }
145
146         if ($migrateRoleId) {
147             $newRole = $this->role->find($migrateRoleId);
148             if ($newRole) {
149                 $users = $role->users->pluck('id')->toArray();
150                 $newRole->users()->sync($users);
151             }
152         }
153
154         $this->permissionService->deleteJointPermissionsForRole($role);
155         $role->delete();
156     }
157 }