PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.4.6
Booking for Appointments and Events Calendar – Amelia v2.4.6
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 / Command.php
ameliabooking / src / Application / Commands Last commit date
Bookable 4 weeks ago Booking 1 week ago Calendar 1 week ago Entities 1 week ago Google 4 months ago Import 4 weeks ago Mobile 1 month ago Notification 1 month ago Payment 4 weeks ago QrCode 1 week ago Settings 4 weeks ago Square 7 months ago Stash 1 week ago Stats 7 months ago Test 7 months ago User 1 week ago WhatsNew 4 months ago Command.php 1 week ago CommandHandler.php 7 years ago CommandResult.php 6 months ago SortParamsTrait.php 5 months ago
Command.php
360 lines
1 <?php
2
3 namespace AmeliaBooking\Application\Commands;
4
5 use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException;
6 use AmeliaBooking\Application\Commands\Booking\Appointment\AddBookingCommand;
7 use AmeliaBooking\Application\Commands\Booking\Appointment\DeleteBookingRemotelyCommand;
8 use AmeliaBooking\Application\Commands\Booking\Appointment\SuccessfulBookingCommand;
9 use AmeliaBooking\Application\Commands\Notification\UpdateSMSNotificationHistoryCommand;
10 use AmeliaBooking\Application\Commands\Outlook\FetchAccessTokenWithAuthCodeOutlookCommand;
11 use AmeliaBooking\Application\Commands\Payment\CalculatePaymentAmountCommand;
12 use AmeliaBooking\Application\Commands\Square\DisconnectFromSquareAccountCommand;
13 use AmeliaBooking\Application\Commands\Square\SquareRefundWebhookCommand;
14 use AmeliaBooking\Application\Services\User\UserApplicationService;
15 use AmeliaBooking\Domain\Entity\Entities;
16 use AmeliaBooking\Domain\Entity\User\AbstractUser;
17 use AmeliaBooking\Domain\Common\Exceptions\AuthorizationException;
18 use AmeliaBooking\Domain\Services\Permissions\PermissionsService;
19 use AmeliaBooking\Domain\Services\Settings\SettingsService;
20 use AmeliaBooking\Infrastructure\WP\SettingsService\SettingsStorage;
21 use AmeliaVendor\Psr\Http\Message\ServerRequestInterface as Request;
22
23 /**
24 * Class Command
25 *
26 * @package AmeliaBooking\Application\Commands
27 */
28 abstract class Command
29 {
30 protected $args;
31
32 protected $container;
33
34 private $fields = [];
35
36 public $token;
37
38 private $page;
39
40 private $cabinetType;
41
42 private $permissionService;
43
44 private $userApplicationService;
45
46 /**
47 * Command constructor.
48 *
49 * @param $args
50 */
51 public function __construct($args)
52 {
53 $this->args = $args;
54 if (isset($args['type'])) {
55 $this->setField('type', $args['type']);
56 }
57 }
58
59 /**
60 * @return mixed
61 */
62 public function getArgs()
63 {
64 return $this->args;
65 }
66
67 /**
68 * @param mixed $arg Argument to be fetched
69 *
70 * @return null|mixed
71 */
72 public function getArg($arg)
73 {
74 return isset($this->args[$arg]) ? $this->args[$arg] : null;
75 }
76
77 /**
78 * @param $fieldName
79 * @param $fieldValue
80 */
81 public function setField($fieldName, $fieldValue)
82 {
83 $this->fields[$fieldName] = $fieldValue;
84 }
85
86 /**
87 * @param $fieldName
88 */
89 public function removeField($fieldName)
90 {
91 unset($this->fields[$fieldName]);
92 }
93
94 /**
95 * Return a single field
96 *
97 * @param $fieldName
98 *
99 * @return mixed|null
100 */
101 public function getField($fieldName)
102 {
103 return isset($this->fields[$fieldName]) ? $this->fields[$fieldName] : null;
104 }
105
106 /**
107 * Return all fields
108 *
109 * @return array
110 */
111 public function getFields()
112 {
113 return $this->fields;
114 }
115
116 /**
117 * Set Token
118 *
119 * @param Request $request
120 */
121 public function setToken($request)
122 {
123 $token = null;
124
125 /** @var SettingsService $settingsService */
126 $settingsService = new SettingsService(new SettingsStorage());
127
128 $authorization = $request->getHeaderLine('Authorization');
129
130 if (
131 $authorization !== '' &&
132 ($values = explode(' ', $authorization)) &&
133 sizeof($values) === 2 &&
134 $settingsService->getSetting('roles', 'enabledHttpAuthorization')
135 ) {
136 $token = $values[1];
137 } else {
138 $cookies = $request->getCookieParams();
139 if (!empty($cookies['ameliaToken'])) {
140 $token = $cookies['ameliaToken'];
141 }
142 }
143
144 $this->token = $token;
145 }
146
147 /**
148 * Return Token
149 *
150 * @return string|null
151 */
152 public function getToken()
153 {
154 return $this->token;
155 }
156
157 /**
158 * Set page
159 *
160 * @param string $page
161 */
162 public function setPage($page)
163 {
164 $this->page = explode('-', $page)[0];
165
166 $this->cabinetType = !empty(explode('-', $page)[1]) ? explode('-', $page)[1] : null;
167 }
168
169 /**
170 * Return page
171 *
172 * @return string|null
173 */
174 public function getPage()
175 {
176 return $this->page;
177 }
178
179 /**
180 * @param $request
181 * @return int|boolean
182 */
183 public function validateNonce($request)
184 {
185 if (
186 $request->getMethod() === 'POST' &&
187 !self::getToken() &&
188 !($this instanceof CalculatePaymentAmountCommand) &&
189 !($this instanceof AddBookingCommand) &&
190 !($this instanceof DeleteBookingRemotelyCommand) &&
191 !($this instanceof SquareRefundWebhookCommand) &&
192 !($this instanceof DisconnectFromSquareAccountCommand) &&
193 !($this instanceof SuccessfulBookingCommand) &&
194 !($this instanceof FetchAccessTokenWithAuthCodeOutlookCommand) &&
195 !($this instanceof UpdateSMSNotificationHistoryCommand)
196 ) {
197 $queryParams = $request->getQueryParams();
198
199 return wp_verify_nonce(
200 !empty($queryParams['wpAmeliaNonce']) ? $queryParams['wpAmeliaNonce'] : $queryParams['ameliaNonce'],
201 'ajax-nonce'
202 );
203 }
204
205 return true;
206 }
207
208 /**
209 * Return cabinet type
210 *
211 * @return string|null
212 */
213 public function getCabinetType()
214 {
215 return $this->cabinetType;
216 }
217
218 /**
219 * @return PermissionsService
220 */
221 public function getPermissionService()
222 {
223 return $this->permissionService;
224 }
225
226 /**
227 * @param PermissionsService $permissionService
228 */
229 public function setPermissionService($permissionService)
230 {
231 $this->permissionService = $permissionService;
232 }
233
234 /**
235 * @return UserApplicationService
236 */
237 public function getUserApplicationService()
238 {
239 return $this->userApplicationService;
240 }
241
242 /**
243 * @param UserApplicationService $userApplicationService
244 */
245 public function setUserApplicationService($userApplicationService)
246 {
247 $this->userApplicationService = $userApplicationService;
248 }
249
250 /**
251 * Authorize user
252 *
253 * @return AbstractUser
254 * @throws AuthorizationException
255 * @throws AccessDeniedException
256 */
257 public function authorize($type = null): AbstractUser
258 {
259 if ($type === AbstractUser::USER_ROLE_PROVIDER || $type === AbstractUser::USER_ROLE_CUSTOMER) {
260 /** @var AbstractUser $user */
261 $user = $this->getUserApplicationService()->authorization(
262 $this->getToken(),
263 $type
264 );
265
266 // If user is admin or manager, return user
267 if (
268 $user && (
269 $user->getType() === AbstractUser::USER_ROLE_ADMIN ||
270 $user->getType() === AbstractUser::USER_ROLE_MANAGER
271 )
272 ) {
273 return $user;
274 }
275
276 // If user is not admin or manager, check if user is of the given type
277 if (!$user || $user->getType() !== $type) {
278 throw new AccessDeniedException('You are not allowed');
279 }
280
281 return $user;
282 }
283
284 return $this->getUserApplicationService()->authorization(
285 $this->getPage() === 'cabinet' ? $this->getToken() : null,
286 $this->getCabinetType()
287 );
288 }
289
290 /**
291 * Authorize provider read permission
292 *
293 * @param int $userId
294 * @param string $entity
295 *
296 * @return AbstractUser
297 *
298 * @throws AuthorizationException
299 * @throws AccessDeniedException
300 */
301 public function authorizeProviderReadPermission(int $userId, string $entity = Entities::EMPLOYEES): AbstractUser
302 {
303 // if logged in user is not WP admin, WP Amelia manager or WP Amelia provider, try to authorize as non WP Amelia provider
304 if (
305 !$this->getPermissionService()->currentUserCanRead($entity) ||
306 !$this->getPermissionService()->currentUserCanReadOthers($entity)
307 ) {
308 /** @var AbstractUser $user */
309 $user = $this->authorize(Entities::PROVIDER);
310
311 // if authorized as non WP Amelia provider, check if user ID is the same as the requested user ID
312 if (
313 $user->getType() === AbstractUser::USER_ROLE_PROVIDER &&
314 $user->getId()->getValue() !== $userId
315 ) {
316 throw new AccessDeniedException('You are not allowed');
317 }
318
319 return $user;
320 }
321
322 return $this->getUserApplicationService()->authorization(null, null);
323 }
324
325 /**
326 * Authorize provider write permission
327 *
328 * @param int $userId
329 * @param string $entity
330 *
331 * @return AbstractUser
332 *
333 * @throws AuthorizationException
334 * @throws AccessDeniedException
335 */
336 public function authorizeProviderWritePermission(int $userId, string $entity = Entities::EMPLOYEES): AbstractUser
337 {
338 // if logged in user is not WP admin, WP Amelia manager or WP Amelia provider, try to authorize as non WP Amelia provider
339 if (
340 !$this->getPermissionService()->currentUserCanWrite($entity) ||
341 !$this->getPermissionService()->currentUserCanWriteOthers($entity)
342 ) {
343 /** @var AbstractUser $user */
344 $user = $this->authorize(Entities::PROVIDER);
345
346 // if authorized as non WP Amelia provider, check if user ID is the same as the requested user ID
347 if (
348 $user->getType() === AbstractUser::USER_ROLE_PROVIDER &&
349 $user->getId()->getValue() !== $userId
350 ) {
351 throw new AccessDeniedException('You are not allowed');
352 }
353
354 return $user;
355 }
356
357 return $this->getUserApplicationService()->authorization(null, null);
358 }
359 }
360