PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.8
Booking for Appointments and Events Calendar – Amelia v2.4.8
2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Application / Commands / Settings / UpdateSettingsCategoriesCommandHandler.php
ameliabooking / src / Application / Commands / Settings Last commit date
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