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

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