PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.5.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.5.0
2.5.0 2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 All 34 releases
fluent-booking / app / Services / PermissionManager.php

PermissionManager.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 2.5.0, at app/Services/PermissionManager.php

292 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBooking\App\Services;
4
5 use FluentBooking\App\Models\Meta;
6 use FluentBooking\App\Models\Calendar;
7 use FluentBooking\App\Models\CalendarSlot;
8 use FluentBooking\Framework\Support\Arr;
9
10 class PermissionManager
11 {
12 public static function allPermissionSets()
13 {
14 return [
15 'manage_own_calendar' => __('Manage only own Calendar, Events, Bookings & Availability', 'fluent-booking'),
16 'read_all_bookings' => __('Read Access to All Bookings', 'fluent-booking'),
17 'manage_all_bookings' => __('Read & Write Access to All Bookings', 'fluent-booking'),
18 'read_other_calendars' => __('Read Access of Other Users Calendars', 'fluent-booking'),
19 'manage_other_calendars' => __('Manage Other Users Calendars', 'fluent-booking'),
20 'read_and_use_other_availabilities' => __('Read & Use Access of All Availabilities', 'fluent-booking'),
21 'manage_other_availabilities' => __('Manage All Availabilities', 'fluent-booking'),
22 'manage_all_data' => __('Manage All Data and Settings', 'fluent-booking')
23 ];
24 }
25
26 public static function hasAllCalendarAccess($readAccess = false)
27 {
28 $hasCalendarAccess = self::userCan(['manage_all_data', 'manage_other_calendars']);
29
30 if ($readAccess) {
31 $hasCalendarAccess = $hasCalendarAccess || self::userCan('read_other_calendars');
32 }
33
34 return apply_filters('fluent_booking/has_all_calendar_access', current_user_can('manage_options')) || $hasCalendarAccess;
35 }
36
37 public static function canReadCalendar($calendarId)
38 {
39 if (current_user_can('manage_options')) {
40 return true;
41 }
42
43 $calendar = Calendar::find($calendarId);
44
45 if (!$calendar) {
46 return false;
47 }
48
49 if ($calendar->user_id == get_current_user_id()) {
50 return true;
51 }
52
53 if (CalendarService::isSharedCalendar($calendar)) {
54 return true;
55 }
56
57 return self::userCan(['manage_all_data', 'read_other_calendars', 'manage_other_calendars']);
58 }
59
60 public static function canUpdateCalendarEvent($slotId)
61 {
62 $calendarSlot = CalendarSlot::find($slotId);
63
64 if (!$calendarSlot) {
65 return false;
66 }
67
68 $userId = get_current_user_id();
69
70 $teamMembers = Arr::get($calendarSlot, 'settings.team_members', []);
71
72 if ($calendarSlot->user_id == $userId || in_array($userId, $teamMembers)) {
73 return true;
74 }
75
76 return self::userCan(['manage_all_data', 'manage_other_calendars']);
77 }
78
79 public static function canWriteCalendar($calendarId)
80 {
81 if (current_user_can('manage_options')) {
82 return true;
83 }
84
85 $calendar = Calendar::find($calendarId);
86
87 if (!$calendar) {
88 return false;
89 }
90
91 if ($calendar->user_id == get_current_user_id()) {
92 return true;
93 }
94
95 return self::userCan(['manage_all_data', 'manage_other_calendars']);
96 }
97
98 /**
99 * Whether the current user may make $memberIds hosts. Anyone outside $allowedIds
100 * is someone new being assigned, which is the same act as creating a calendar
101 * for another user.
102 *
103 * @param int[] $memberIds
104 * @param int[] $allowedIds
105 * @return bool
106 */
107 public static function canAssignHosts($memberIds, $allowedIds)
108 {
109 $memberIds = array_map('intval', (array) $memberIds);
110 $allowedIds = array_map('intval', (array) $allowedIds);
111
112 if (!array_diff($memberIds, $allowedIds)) {
113 return true;
114 }
115
116 return self::canManageOtherHosts();
117 }
118
119 public static function hasCalendarAccess($calendar)
120 {
121 $hasAccess = self::userCan('manage_all_data') || $calendar->user_id == get_current_user_id();
122
123 return apply_filters('fluent_booking/has_calendar_access', $hasAccess, $calendar);
124 }
125
126 public static function currentUserHasAnyPermission()
127 {
128 if (current_user_can('manage_options')) {
129 return true;
130 }
131
132 return !!self::getUserPermissions();
133 }
134
135 public static function userCan($permissions)
136 {
137 if (current_user_can('manage_options')) {
138 return true;
139 }
140
141 $userPermissions = self::getUserPermissions();
142
143 if (!$userPermissions) {
144 return false;
145 }
146
147 if (is_string($permissions)) {
148 return in_array($permissions, $userPermissions);
149 }
150
151 if (is_array($permissions)) {
152 foreach ($permissions as $permission) {
153 if (in_array($permission, $userPermissions)) {
154 return true;
155 }
156 }
157 }
158
159 return false;
160 }
161
162 /**
163 * Whether the current user may act for, assign or see the details of other hosts.
164 *
165 * @return bool
166 */
167 public static function canManageOtherHosts()
168 {
169 return self::userCan(['manage_all_data', 'invite_team_members']);
170 }
171
172 public static function getUserPermissions($user = null, $formatted = false)
173 {
174 if ($user === null) {
175 $user = wp_get_current_user();
176 }
177
178 if (!$user || !$user->ID) {
179 return [];
180 }
181
182 $allPermissions = self::allPermissionSets();
183 if (user_can($user, 'manage_options')) {
184 $permissions = array_merge(
185 [
186 'super_admin' => 'All Access (Administrator)'
187 ],
188 $allPermissions
189 );
190
191 if ($formatted) {
192 return $permissions;
193 }
194
195 return array_keys($permissions);
196 }
197
198 // maybe restricted Access
199 $permissions = self::getMetaPermissions($user->ID);
200
201 if (!$permissions) {
202 $calendar = Calendar::where('user_id', $user->ID)->first();
203 if (!$calendar) {
204 return [];
205 }
206
207 Meta::create([
208 'object_type' => 'user_meta',
209 'object_id' => $user->ID,
210 'key' => '_access_permissions',
211 'value' => ['manage_own_calendar']
212 ]);
213
214 if ($formatted) {
215 return ['manage_own_calendar' => __('Manage only own Calendar, Events, Bookings & Availability', 'fluent-booking')];
216 }
217 return ['manage_own_calendar'];
218 }
219
220 if (!$formatted) {
221 return $permissions;
222 }
223
224 $formattedPermissions = [];
225
226 foreach ($permissions as $permission) {
227 if (isset($allPermissions[$permission])) {
228 $formattedPermissions[$permission] = $allPermissions[$permission];
229 }
230 }
231
232 return $formattedPermissions;
233 }
234
235 public static function getMetaPermissions($userId = null)
236 {
237 if ($userId === null) {
238 $userId = get_current_user_id();
239 }
240
241 if (!$userId) {
242 return [];
243 }
244
245 $meta = Meta::where('object_type', 'user_meta')
246 ->where('object_id', $userId)
247 ->where('key', '_access_permissions')
248 ->first();
249
250 if ($meta) {
251 return $meta->value;
252 }
253
254 return [];
255 }
256
257 public static function getMenuPermission()
258 {
259 if (current_user_can('manage_options')) {
260 return 'manage_options';
261 }
262
263 $userId = get_current_user_id();
264
265 // Check if the user has any calendar
266 $calendar = Calendar::where('user_id', $userId)->first();
267
268 if ($calendar) {
269 $user = wp_get_current_user();
270 $roles = array_values((array)$user->roles);
271 return Arr::get($roles, 0);
272 }
273
274 // Check Meta Permissions
275 $metaPermission = self::getMetaPermissions($userId);
276
277 if (!$metaPermission) {
278 return '';
279 }
280
281 $user = wp_get_current_user();
282 $roles = array_values((array)$user->roles);
283
284 return Arr::get($roles, 0);
285 }
286
287 public static function userCanSeeAllBookings()
288 {
289 return self::userCan(['manage_all_data', 'read_all_bookings', 'manage_all_bookings']);
290 }
291 }
292