| @@ -1,10 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentBooking\App\Services; |
| 4 | 4 | |
| 5 | +use FluentBooking\App\Models\Meta; | |
| 5 | 6 | use FluentBooking\App\Models\Calendar; |
| 6 | -use FluentBooking\App\Models\Meta; | |
| 7 | +use FluentBooking\App\Models\CalendarSlot; | |
| 7 | 8 | use FluentBooking\Framework\Support\Arr; |
| 8 | 9 | |
| 9 | 10 | class PermissionManager |
| 10 | 11 | { |
| @@ -16,20 +17,21 @@ | ||
| 16 | 17 | 'manage_all_bookings' => __('Read & Write Access to All Bookings', 'fluent-booking'), |
| 17 | 18 | 'read_other_calendars' => __('Read Access of Other Users Calendars', 'fluent-booking'), |
| 18 | 19 | 'manage_other_calendars' => __('Manage Other Users Calendars', 'fluent-booking'), |
| 19 | 20 | 'read_and_use_other_availabilities' => __('Read & Use Access of All Availabilities', 'fluent-booking'), |
| 20 | - 'manage_other_availabilities' => __('Manage All Availabilities', 'fluent-booking') | |
| 21 | + 'manage_other_availabilities' => __('Manage All Availabilities', 'fluent-booking'), | |
| 22 | + 'manage_all_data' => __('Manage All Data and Settings', 'fluent-booking') | |
| 21 | 23 | ]; |
| 22 | 24 | } |
| 23 | 25 | |
| 24 | 26 | public static function hasAllCalendarAccess($readAccess = false) |
| 25 | 27 | { |
| 26 | - $hasCalendarAccess = self::userCan('manage_other_calendars'); | |
| 28 | + $hasCalendarAccess = self::userCan(['manage_all_data', 'manage_other_calendars']); | |
| 27 | 29 | |
| 28 | 30 | if ($readAccess) { |
| 29 | 31 | $hasCalendarAccess = $hasCalendarAccess || self::userCan('read_other_calendars'); |
| 30 | 32 | } |
| 31 | - | |
| 33 | + | |
| 32 | 34 | return apply_filters('fluent_booking/has_all_calendar_access', current_user_can('manage_options')) || $hasCalendarAccess; |
| 33 | 35 | } |
| 34 | 36 | |
| 35 | 37 | public static function canReadCalendar($calendarId) |
| @@ -47,10 +49,32 @@ | ||
| 47 | 49 | if ($calendar->user_id == get_current_user_id()) { |
| 48 | 50 | return true; |
| 49 | 51 | } |
| 50 | 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 | + } | |
| 51 | 59 | |
| 52 | - return self::userCan(['read_other_calendars', 'manage_other_calendars']); | |
| 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']); | |
| 53 | 77 | } |
| 54 | 78 | |
| 55 | 79 | public static function canWriteCalendar($calendarId) |
| 56 | 80 | { |
| @@ -67,17 +91,40 @@ | ||
| 67 | 91 | if ($calendar->user_id == get_current_user_id()) { |
| 68 | 92 | return true; |
| 69 | 93 | } |
| 70 | 94 | |
| 71 | - return self::userCan('manage_other_calendars'); | |
| 95 | + return self::userCan(['manage_all_data', 'manage_other_calendars']); | |
| 72 | 96 | } |
| 73 | 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 | + | |
| 74 | 119 | public static function hasCalendarAccess($calendar) |
| 75 | 120 | { |
| 76 | - return current_user_can('manage_options') || $calendar->user_id == get_current_user_id(); | |
| 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); | |
| 77 | 124 | } |
| 78 | 125 | |
| 79 | - public static function currentUserHasAnyPemrmission() | |
| 126 | + public static function currentUserHasAnyPermission() | |
| 80 | 127 | { |
| 81 | 128 | if (current_user_can('manage_options')) { |
| 82 | 129 | return true; |
| 83 | 130 | } |
| @@ -111,8 +158,18 @@ | ||
| 111 | 158 | |
| 112 | 159 | return false; |
| 113 | 160 | } |
| 114 | 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 | + | |
| 115 | 172 | public static function getUserPermissions($user = null, $formatted = false) |
| 116 | 173 | { |
| 117 | 174 | if ($user === null) { |
| 118 | 175 | $user = wp_get_current_user(); |
| @@ -176,9 +233,8 @@ | ||
| 176 | 233 | } |
| 177 | 234 | |
| 178 | 235 | public static function getMetaPermissions($userId = null) |
| 179 | 236 | { |
| 180 | - | |
| 181 | 237 | if ($userId === null) { |
| 182 | 238 | $userId = get_current_user_id(); |
| 183 | 239 | } |
| 184 | 240 | |
| @@ -229,7 +285,7 @@ | ||
| 229 | 285 | } |
| 230 | 286 | |
| 231 | 287 | public static function userCanSeeAllBookings() |
| 232 | 288 | { |
| 233 | - return self::userCan(['read_all_bookings', 'manage_all_bookings']); | |
| 289 | + return self::userCan(['manage_all_data', 'read_all_bookings', 'manage_all_bookings']); | |
| 234 | 290 | } |
| 235 | 291 | } |