PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / trunk
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution vtrunk
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 1.7.2 All 33 releases
fluent-booking / app / Services / PermissionManager.php

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

261 lines 7.3 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 public static function hasCalendarAccess($calendar)
99 {
100 $hasAccess = self::userCan('manage_all_data') || $calendar->user_id == get_current_user_id();
101
102 return apply_filters('fluent_booking/has_calendar_access', $hasAccess, $calendar);
103 }
104
105 public static function currentUserHasAnyPermission()
106 {
107 if (current_user_can('manage_options')) {
108 return true;
109 }
110
111 return !!self::getUserPermissions();
112 }
113
114 public static function userCan($permissions)
115 {
116 if (current_user_can('manage_options')) {
117 return true;
118 }
119
120 $userPermissions = self::getUserPermissions();
121
122 if (!$userPermissions) {
123 return false;
124 }
125
126 if (is_string($permissions)) {
127 return in_array($permissions, $userPermissions);
128 }
129
130 if (is_array($permissions)) {
131 foreach ($permissions as $permission) {
132 if (in_array($permission, $userPermissions)) {
133 return true;
134 }
135 }
136 }
137
138 return false;
139 }
140
141 public static function getUserPermissions($user = null, $formatted = false)
142 {
143 if ($user === null) {
144 $user = wp_get_current_user();
145 }
146
147 if (!$user || !$user->ID) {
148 return [];
149 }
150
151 $allPermissions = self::allPermissionSets();
152 if (user_can($user, 'manage_options')) {
153 $permissions = array_merge(
154 [
155 'super_admin' => 'All Access (Administrator)'
156 ],
157 $allPermissions
158 );
159
160 if ($formatted) {
161 return $permissions;
162 }
163
164 return array_keys($permissions);
165 }
166
167 // maybe restricted Access
168 $permissions = self::getMetaPermissions($user->ID);
169
170 if (!$permissions) {
171 $calendar = Calendar::where('user_id', $user->ID)->first();
172 if (!$calendar) {
173 return [];
174 }
175
176 Meta::create([
177 'object_type' => 'user_meta',
178 'object_id' => $user->ID,
179 'key' => '_access_permissions',
180 'value' => ['manage_own_calendar']
181 ]);
182
183 if ($formatted) {
184 return ['manage_own_calendar' => __('Manage only own Calendar, Events, Bookings & Availability', 'fluent-booking')];
185 }
186 return ['manage_own_calendar'];
187 }
188
189 if (!$formatted) {
190 return $permissions;
191 }
192
193 $formattedPermissions = [];
194
195 foreach ($permissions as $permission) {
196 if (isset($allPermissions[$permission])) {
197 $formattedPermissions[$permission] = $allPermissions[$permission];
198 }
199 }
200
201 return $formattedPermissions;
202 }
203
204 public static function getMetaPermissions($userId = null)
205 {
206 if ($userId === null) {
207 $userId = get_current_user_id();
208 }
209
210 if (!$userId) {
211 return [];
212 }
213
214 $meta = Meta::where('object_type', 'user_meta')
215 ->where('object_id', $userId)
216 ->where('key', '_access_permissions')
217 ->first();
218
219 if ($meta) {
220 return $meta->value;
221 }
222
223 return [];
224 }
225
226 public static function getMenuPermission()
227 {
228 if (current_user_can('manage_options')) {
229 return 'manage_options';
230 }
231
232 $userId = get_current_user_id();
233
234 // Check if the user has any calendar
235 $calendar = Calendar::where('user_id', $userId)->first();
236
237 if ($calendar) {
238 $user = wp_get_current_user();
239 $roles = array_values((array)$user->roles);
240 return Arr::get($roles, 0);
241 }
242
243 // Check Meta Permissions
244 $metaPermission = self::getMetaPermissions($userId);
245
246 if (!$metaPermission) {
247 return '';
248 }
249
250 $user = wp_get_current_user();
251 $roles = array_values((array)$user->roles);
252
253 return Arr::get($roles, 0);
254 }
255
256 public static function userCanSeeAllBookings()
257 {
258 return self::userCan(['manage_all_data', 'read_all_bookings', 'manage_all_bookings']);
259 }
260 }
261