PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.10.02
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.10.02
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 1.10.02, at app/Services/PermissionManager.php

260 lines 7.1 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 ];
23 }
24
25 public static function hasAllCalendarAccess($readAccess = false)
26 {
27 $hasCalendarAccess = self::userCan('manage_other_calendars');
28
29 if ($readAccess) {
30 $hasCalendarAccess = $hasCalendarAccess || self::userCan('read_other_calendars');
31 }
32
33 return apply_filters('fluent_booking/has_all_calendar_access', current_user_can('manage_options')) || $hasCalendarAccess;
34 }
35
36 public static function canReadCalendar($calendarId)
37 {
38 if (current_user_can('manage_options')) {
39 return true;
40 }
41
42 $calendar = Calendar::find($calendarId);
43
44 if (!$calendar) {
45 return false;
46 }
47
48 if ($calendar->user_id == get_current_user_id()) {
49 return true;
50 }
51
52 if (CalendarService::isSharedCalendar($calendar)) {
53 return true;
54 }
55
56 return self::userCan(['read_other_calendars', 'manage_other_calendars']);
57 }
58
59 public static function canUpdateCalendarEvent($slotId)
60 {
61 $calendarSlot = CalendarSlot::find($slotId);
62
63 if (!$calendarSlot) {
64 return false;
65 }
66
67 $userId = get_current_user_id();
68
69 $teamMembers = Arr::get($calendarSlot, 'settings.team_members', []);
70
71 if ($calendarSlot->user_id == $userId || in_array($userId, $teamMembers)) {
72 return true;
73 }
74
75 return current_user_can('manage_options') || self::userCan(['manage_other_calendars']);
76 }
77
78 public static function canWriteCalendar($calendarId)
79 {
80 if (current_user_can('manage_options')) {
81 return true;
82 }
83
84 $calendar = Calendar::find($calendarId);
85
86 if (!$calendar) {
87 return false;
88 }
89
90 if ($calendar->user_id == get_current_user_id()) {
91 return true;
92 }
93
94 return self::userCan('manage_other_calendars');
95 }
96
97 public static function hasCalendarAccess($calendar)
98 {
99 $hasAccess = current_user_can('manage_options') || $calendar->user_id == get_current_user_id();
100
101 return apply_filters('fluent_booking/has_calendar_access', $hasAccess, $calendar);
102 }
103
104 public static function currentUserHasAnyPermission()
105 {
106 if (current_user_can('manage_options')) {
107 return true;
108 }
109
110 return !!self::getUserPermissions();
111 }
112
113 public static function userCan($permissions)
114 {
115 if (current_user_can('manage_options')) {
116 return true;
117 }
118
119 $userPermissions = self::getUserPermissions();
120
121 if (!$userPermissions) {
122 return false;
123 }
124
125 if (is_string($permissions)) {
126 return in_array($permissions, $userPermissions);
127 }
128
129 if (is_array($permissions)) {
130 foreach ($permissions as $permission) {
131 if (in_array($permission, $userPermissions)) {
132 return true;
133 }
134 }
135 }
136
137 return false;
138 }
139
140 public static function getUserPermissions($user = null, $formatted = false)
141 {
142 if ($user === null) {
143 $user = wp_get_current_user();
144 }
145
146 if (!$user || !$user->ID) {
147 return [];
148 }
149
150 $allPermissions = self::allPermissionSets();
151 if (user_can($user, 'manage_options')) {
152 $permissions = array_merge(
153 [
154 'super_admin' => 'All Access (Administrator)'
155 ],
156 $allPermissions
157 );
158
159 if ($formatted) {
160 return $permissions;
161 }
162
163 return array_keys($permissions);
164 }
165
166 // maybe restricted Access
167 $permissions = self::getMetaPermissions($user->ID);
168
169 if (!$permissions) {
170 $calendar = Calendar::where('user_id', $user->ID)->first();
171 if (!$calendar) {
172 return [];
173 }
174
175 Meta::create([
176 'object_type' => 'user_meta',
177 'object_id' => $user->ID,
178 'key' => '_access_permissions',
179 'value' => ['manage_own_calendar']
180 ]);
181
182 if ($formatted) {
183 return ['manage_own_calendar' => __('Manage only own Calendar, Events, Bookings & Availability', 'fluent-booking')];
184 }
185 return ['manage_own_calendar'];
186 }
187
188 if (!$formatted) {
189 return $permissions;
190 }
191
192 $formattedPermissions = [];
193
194 foreach ($permissions as $permission) {
195 if (isset($allPermissions[$permission])) {
196 $formattedPermissions[$permission] = $allPermissions[$permission];
197 }
198 }
199
200 return $formattedPermissions;
201 }
202
203 public static function getMetaPermissions($userId = null)
204 {
205 if ($userId === null) {
206 $userId = get_current_user_id();
207 }
208
209 if (!$userId) {
210 return [];
211 }
212
213 $meta = Meta::where('object_type', 'user_meta')
214 ->where('object_id', $userId)
215 ->where('key', '_access_permissions')
216 ->first();
217
218 if ($meta) {
219 return $meta->value;
220 }
221
222 return [];
223 }
224
225 public static function getMenuPermission()
226 {
227 if (current_user_can('manage_options')) {
228 return 'manage_options';
229 }
230
231 $userId = get_current_user_id();
232
233 // Check if the user has any calendar
234 $calendar = Calendar::where('user_id', $userId)->first();
235
236 if ($calendar) {
237 $user = wp_get_current_user();
238 $roles = array_values((array)$user->roles);
239 return Arr::get($roles, 0);
240 }
241
242 // Check Meta Permissions
243 $metaPermission = self::getMetaPermissions($userId);
244
245 if (!$metaPermission) {
246 return '';
247 }
248
249 $user = wp_get_current_user();
250 $roles = array_values((array)$user->roles);
251
252 return Arr::get($roles, 0);
253 }
254
255 public static function userCanSeeAllBookings()
256 {
257 return self::userCan(['read_all_bookings', 'manage_all_bookings']);
258 }
259 }
260