ameliabooking
/
src
/
Application
/
Commands
/
Settings
/
UpdateSettingsCategoriesCommandHandler.php
FeaturesIntegrations
1 week ago
ExportDataTransferCommand.php
1 month ago
ExportDataTransferCommandHandler.php
1 month ago
GetSettingsCommand.php
1 year ago
GetSettingsCommandHandler.php
1 week ago
ProcessImportDataTransferCommand.php
1 month ago
ProcessImportDataTransferCommandHandler.php
1 month ago
StartImportDataTransferCommand.php
1 month ago
StartImportDataTransferCommandHandler.php
1 month ago
UpdateSettingsCategoriesCommand.php
7 months ago
UpdateSettingsCategoriesCommandHandler.php
1 week ago
UpdateSettingsCommand.php
1 year ago
UpdateSettingsCommandHandler.php
1 week ago
UpdateSettingsCategoriesCommandHandler.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Settings; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Domain\Entity\Entities; |
| 9 | use AmeliaBooking\Domain\Entity\User\AbstractUser; |
| 10 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 11 | use AmeliaBooking\Infrastructure\WP\UserRoles\SuperAdminRoleService; |
| 12 | use AmeliaBooking\Infrastructure\WP\UserRoles\UserRoles; |
| 13 | use Exception; |
| 14 | use Interop\Container\Exception\ContainerException; |
| 15 | use Slim\Exception\ContainerValueNotFoundException; |
| 16 | |
| 17 | /** |
| 18 | * Class UpdateSettingsCategoriesCommandHandler |
| 19 | * |
| 20 | * @package AmeliaBooking\Application\Commands\Settings |
| 21 | */ |
| 22 | class UpdateSettingsCategoriesCommandHandler extends CommandHandler |
| 23 | { |
| 24 | /** |
| 25 | * @param UpdateSettingsCategoriesCommand $command |
| 26 | * |
| 27 | * @return CommandResult |
| 28 | * @throws AccessDeniedException |
| 29 | * @throws ContainerValueNotFoundException |
| 30 | * @throws ContainerException |
| 31 | * @throws Exception |
| 32 | */ |
| 33 | public function handle(UpdateSettingsCategoriesCommand $command) |
| 34 | { |
| 35 | $result = new CommandResult(); |
| 36 | |
| 37 | if (!$this->getContainer()->getPermissionsService()->currentUserCanWrite(Entities::SETTINGS)) { |
| 38 | /** @var AbstractUser $loggedInUser */ |
| 39 | $loggedInUser = $this->container->get('logged.in.user'); |
| 40 | |
| 41 | if ( |
| 42 | !$loggedInUser || !( |
| 43 | $loggedInUser->getType() === AbstractUser::USER_ROLE_ADMIN || |
| 44 | $loggedInUser->getType() === AbstractUser::USER_ROLE_MANAGER |
| 45 | ) |
| 46 | ) { |
| 47 | throw new AccessDeniedException('You are not allowed to write settings.'); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** @var SettingsService $settingsService */ |
| 52 | $settingsService = $this->getContainer()->get('domain.settings.service'); |
| 53 | |
| 54 | $superAdminService = new SuperAdminRoleService(); |
| 55 | $pendingSuperAdminGrantUserId = null; |
| 56 | $savedActivationSettings = null; |
| 57 | |
| 58 | foreach ($command->getField('categories') as $category => $data) { |
| 59 | if ($category === 'activation' && !$superAdminService->canAccessActivationSettings()) { |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | $categorySettings = $settingsService->getCategorySettings($category); |
| 64 | |
| 65 | if ($categorySettings !== null) { |
| 66 | if ( |
| 67 | $category === 'activation' && |
| 68 | !empty($data['active']) && |
| 69 | empty($categorySettings['licenseActivatorUserId']) && |
| 70 | get_current_user_id() && |
| 71 | (current_user_can('manage_options') || is_super_admin()) |
| 72 | ) { |
| 73 | $savedActivationSettings = $categorySettings; |
| 74 | $pendingSuperAdminGrantUserId = get_current_user_id(); |
| 75 | } |
| 76 | |
| 77 | if ($category === 'general' && array_key_exists('usedLanguages', $data)) { |
| 78 | $categorySettings['usedLanguages'] = $data['usedLanguages']; |
| 79 | unset($data['usedLanguages']); |
| 80 | } |
| 81 | |
| 82 | if ($category === 'ivy') { |
| 83 | $categorySettings = !empty($data['ivy']) ? $data['ivy'] : []; |
| 84 | } |
| 85 | |
| 86 | $categorySettings = array_replace_recursive($categorySettings, $data); |
| 87 | |
| 88 | $settingsService->setCategorySettings($category, $categorySettings); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | $updatedCategories = array_keys($command->getField('categories') ?: []); |
| 93 | |
| 94 | if ( |
| 95 | in_array('whiteLabel', $updatedCategories, true) || |
| 96 | in_array('featuresIntegrations', $updatedCategories, true) |
| 97 | ) { |
| 98 | UserRoles::syncRoleLabels(); |
| 99 | } |
| 100 | |
| 101 | if (in_array('activation', $updatedCategories, true)) { |
| 102 | UserRoles::syncSuperAdminRoleAvailability(); |
| 103 | } |
| 104 | |
| 105 | if ($pendingSuperAdminGrantUserId) { |
| 106 | if (!$superAdminService->grant($pendingSuperAdminGrantUserId)) { |
| 107 | if (is_array($savedActivationSettings)) { |
| 108 | $settingsService->setCategorySettings('activation', $savedActivationSettings); |
| 109 | UserRoles::syncSuperAdminRoleAvailability(); |
| 110 | } |
| 111 | |
| 112 | $result->setResult(CommandResult::RESULT_ERROR); |
| 113 | $result->setMessage('Failed to assign Superadmin to the activating user.'); |
| 114 | $result->setData([]); |
| 115 | |
| 116 | return $result; |
| 117 | } |
| 118 | |
| 119 | $activationSettings = $settingsService->getCategorySettings('activation'); |
| 120 | $activationSettings['licenseActivatorUserId'] = $pendingSuperAdminGrantUserId; |
| 121 | $settingsService->setCategorySettings('activation', $activationSettings); |
| 122 | } |
| 123 | |
| 124 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 125 | $result->setMessage('Successfully updated settings.'); |
| 126 | $result->setData( |
| 127 | [ |
| 128 | 'isSuperAdmin' => $superAdminService->isCurrentUserSuperAdmin(), |
| 129 | 'isSuperAdminRoleAvailable' => SuperAdminRoleService::isAvailable(), |
| 130 | 'superAdminCount' => $superAdminService->countSuperAdmins(), |
| 131 | ] |
| 132 | ); |
| 133 | |
| 134 | return $result; |
| 135 | } |
| 136 | } |
| 137 |