PluginProbe
The Innovative Form Builder – IvyForms / 1.5.1
The Innovative Form Builder – IvyForms v1.5.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / Confirmation / ConfirmationAccessPolicy.php

ConfirmationAccessPolicy.php in The Innovative Form Builder – IvyForms 1.5.1, at backend/src/Services/Confirmation/ConfirmationAccessPolicy.php

145 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\Confirmation;
4
5 use IvyForms\Common\Exceptions\ForbiddenException;
6 use IvyForms\Entity\Confirmation\Confirmation;
7 use IvyForms\Repository\Confirmation\ConfirmationRepositoryInterface;
8 use IvyForms\Services\Translations\BackendStrings;
9
10 /**
11 * License-aware confirmation management rules (Lite downgrade + Pro filters).
12 */
13 class ConfirmationAccessPolicy
14 {
15 private ConfirmationRepositoryInterface $confirmationRepository;
16
17 public function __construct(ConfirmationRepositoryInterface $confirmationRepository)
18 {
19 $this->confirmationRepository = $confirmationRepository;
20 }
21
22 /**
23 * @param int $formId
24 * @param callable $createDefault fn(int $formId): int
25 * @return int
26 */
27 public function ensureDefaultExists(int $formId, callable $createDefault): int
28 {
29 if ($this->confirmationRepository->countByFormId($formId) === 0) {
30 return (int) $createDefault($formId);
31 }
32
33 $confirmations = $this->confirmationRepository->getAllById($formId);
34 foreach ($confirmations as $confirmation) {
35 if ($confirmation->isDefault()) {
36 return $confirmation->getId();
37 }
38 }
39
40 return $confirmations[0]->getId();
41 }
42
43 /**
44 * @param array<Confirmation> $confirmations
45 */
46 public function resolvePrimaryEditableId(array $confirmations): int
47 {
48 foreach ($confirmations as $confirmation) {
49 if ($confirmation->isEnabled()) {
50 return $confirmation->getId();
51 }
52 }
53
54 return $confirmations[0]->getId();
55 }
56
57 /**
58 * @param int $confirmationId
59 * @param int $formId
60 * @param string $action
61 * @param int $primaryEditableId
62 * @return bool
63 */
64 public function canManage(int $confirmationId, int $formId, string $action, int $primaryEditableId): bool
65 {
66 $default = $this->resolveDefaultManagePermission($confirmationId, $formId, $action, $primaryEditableId);
67
68 /**
69 * @param bool $default
70 * @param int $confirmationId
71 * @param int $formId
72 * @param string $action
73 */
74 return (bool) apply_filters(
75 'ivyforms/can_manage_confirmation',
76 $default,
77 $confirmationId,
78 $formId,
79 $action
80 );
81 }
82
83 /**
84 * @param int $confirmationId
85 * @param int $formId
86 * @param string $action
87 * @param int $primaryEditableId
88 * @return bool
89 */
90 private function resolveDefaultManagePermission(
91 int $confirmationId,
92 int $formId,
93 string $action,
94 int $primaryEditableId
95 ): bool {
96 if (apply_filters('ivyforms/can_create_multiple_confirmations', false, $formId)) {
97 return true;
98 }
99
100 if ($this->confirmationRepository->countByFormId($formId) <= 1) {
101 return $action === 'edit';
102 }
103
104 if ($action === 'edit') {
105 return $confirmationId === $primaryEditableId;
106 }
107
108 return false;
109 }
110
111 /**
112 * @param int $confirmationId
113 * @param int $formId
114 * @param string $action
115 * @return void
116 * @throws ForbiddenException
117 */
118 public function assertCanManage(int $confirmationId, int $formId, string $action): void
119 {
120 $confirmations = $this->confirmationRepository->getAllById($formId);
121 $primaryId = $this->resolvePrimaryEditableId($confirmations);
122
123 if (!$this->canManage($confirmationId, $formId, $action, $primaryId)) {
124 throw new ForbiddenException(
125 BackendStrings::getSettingsFormBuilderStrings()['confirmation_locked_pro_required']
126 );
127 }
128 }
129
130 /**
131 * @param int $formId
132 * @return void
133 * @throws ForbiddenException
134 */
135 public function assertCanAdd(int $formId): void
136 {
137 $count = $this->confirmationRepository->countByFormId($formId);
138 if ($count >= 1 && !apply_filters('ivyforms/can_create_multiple_confirmations', false, $formId)) {
139 throw new ForbiddenException(
140 BackendStrings::getSettingsFormBuilderStrings()['multiple_confirmations_pro_required']
141 );
142 }
143 }
144 }
145