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