]> BookStack Code Mirror - bookstack/blob - app/Repos/PermissionsRepo.php
ab265a45f58ede14f1f3a36f18ba821ac8feaa92
[bookstack] / app / Repos / PermissionsRepo.php
1 <?php namespace BookStack\Repos;
2
3
4 use BookStack\Exceptions\PermissionsException;
5 use BookStack\Permission;
6 use BookStack\Role;
7 use BookStack\Services\RestrictionService;
8 use Setting;
9
10 class PermissionsRepo
11 {
12
13     protected $permission;
14     protected $role;
15     protected $restrictionService;
16
17     /**
18      * PermissionsRepo constructor.
19      * @param Permission $permission
20      * @param Role $role
21      * @param RestrictionService $restrictionService
22      */
23     public function __construct(Permission $permission, Role $role, RestrictionService $restrictionService)
24     {
25         $this->permission = $permission;
26         $this->role = $role;
27         $this->restrictionService = $restrictionService;
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->restrictionService->buildEntityPermissionForRole($role);
77         return $role;
78     }
79
80     /**
81      * Updates an existing role.
82      * Ensure Admin role always has all permissions.
83      * @param $roleId
84      * @param $roleData
85      */
86     public function updateRole($roleId, $roleData)
87     {
88         $role = $this->role->findOrFail($roleId);
89         $permissions = isset($roleData['permissions']) ? array_keys($roleData['permissions']) : [];
90         $this->assignRolePermissions($role, $permissions);
91
92         if ($role->name === 'admin') {
93             $permissions = $this->permission->all()->pluck('id')->toArray();
94             $role->permissions()->sync($permissions);
95         }
96
97         $role->fill($roleData);
98         $role->save();
99         $this->restrictionService->buildEntityPermissionForRole($role);
100     }
101
102     /**
103      * Assign an list of permission names to an role.
104      * @param Role $role
105      * @param array $permissionNameArray
106      */
107     public function assignRolePermissions(Role $role, $permissionNameArray = [])
108     {
109         $permissions = [];
110         $permissionNameArray = array_values($permissionNameArray);
111         if ($permissionNameArray && count($permissionNameArray) > 0) {
112             $permissions = $this->permission->whereIn('name', $permissionNameArray)->pluck('id')->toArray();
113         }
114         $role->permissions()->sync($permissions);
115     }
116
117     /**
118      * Delete a role from the system.
119      * Check it's not an admin role or set as default before deleting.
120      * If an migration Role ID is specified the users assign to the current role
121      * will be added to the role of the specified id.
122      * @param $roleId
123      * @param $migrateRoleId
124      * @throws PermissionsException
125      */
126     public function deleteRole($roleId, $migrateRoleId)
127     {
128         $role = $this->role->findOrFail($roleId);
129
130         // Prevent deleting admin role or default registration role.
131         if ($role->name === 'admin') {
132             throw new PermissionsException('The admin role cannot be deleted');
133         } else if ($role->id == setting('registration-role')) {
134             throw new PermissionsException('This role cannot be deleted while set as the default registration role.');
135         }
136
137         if ($migrateRoleId) {
138             $newRole = $this->role->find($migrateRoleId);
139             if ($newRole) {
140                 $users = $role->users->pluck('id')->toArray();
141                 $newRole->users()->sync($users);
142             }
143         }
144
145         $this->restrictionService->deleteEntityPermissionsForRole($role);
146         $role->delete();
147     }
148
149 }