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