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