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

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