]> BookStack Code Mirror - bookstack/blob - app/Repos/PermissionsRepo.php
Updated remaining views to 2017 design update.
[bookstack] / app / Repos / PermissionsRepo.php
1 <?php namespace BookStack\Repos;
2
3
4 use BookStack\Exceptions\PermissionsException;
5 use BookStack\RolePermission;
6 use BookStack\Role;
7 use BookStack\Services\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 PermissionService $permissionService
24      */
25     public function __construct(RolePermission $permission, Role $role, 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 has all 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         $this->assignRolePermissions($role, $permissions);
95
96         if ($role->system_name === 'admin') {
97             $permissions = $this->permission->all()->pluck('id')->toArray();
98             $role->permissions()->sync($permissions);
99         }
100
101         $role->fill($roleData);
102         $role->save();
103         $this->permissionService->buildJointPermissionForRole($role);
104     }
105
106     /**
107      * Assign an list of permission names to an role.
108      * @param Role $role
109      * @param array $permissionNameArray
110      */
111     public function assignRolePermissions(Role $role, $permissionNameArray = [])
112     {
113         $permissions = [];
114         $permissionNameArray = array_values($permissionNameArray);
115         if ($permissionNameArray && count($permissionNameArray) > 0) {
116             $permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray();
117         }
118         $role->permissions()->sync($permissions);
119     }
120
121     /**
122      * Delete a role from the system.
123      * Check it's not an admin role or set as default before deleting.
124      * If an migration Role ID is specified the users assign to the current role
125      * will be added to the role of the specified id.
126      * @param $roleId
127      * @param $migrateRoleId
128      * @throws PermissionsException
129      */
130     public function deleteRole($roleId, $migrateRoleId)
131     {
132         $role = $this->role->findOrFail($roleId);
133
134         // Prevent deleting admin role or default registration role.
135         if ($role->system_name && in_array($role->system_name, $this->systemRoles)) {
136             throw new PermissionsException(trans('errors.role_system_cannot_be_deleted'));
137         } else if ($role->id == setting('registration-role')) {
138             throw new PermissionsException(trans('errors.role_registration_default_cannot_delete'));
139         }
140
141         if ($migrateRoleId) {
142             $newRole = $this->role->find($migrateRoleId);
143             if ($newRole) {
144                 $users = $role->users->pluck('id')->toArray();
145                 $newRole->users()->sync($users);
146             }
147         }
148
149         $this->permissionService->deleteJointPermissionsForRole($role);
150         $role->delete();
151     }
152
153 }