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