diff --git a/modules/civiremote_case/civiremote_case.permissions.yml b/modules/civiremote_case/civiremote_case.permissions.yml new file mode 100644 index 0000000..42042a4 --- /dev/null +++ b/modules/civiremote_case/civiremote_case.permissions.yml @@ -0,0 +1,2 @@ +access civiremote case: + title: 'Access Cases in CiviCRM' diff --git a/modules/civiremote_case/civiremote_case.routing.yml b/modules/civiremote_case/civiremote_case.routing.yml index 8bbcb72..5965d75 100644 --- a/modules/civiremote_case/civiremote_case.routing.yml +++ b/modules/civiremote_case/civiremote_case.routing.yml @@ -8,7 +8,7 @@ civiremote_case.create_form: profile: type: string requirements: - _user_is_logged_in: 'TRUE' + _permission: 'access civiremote case' civiremote_case.update_form: path: '/civiremote/case/{id}/update/{profile}' @@ -22,4 +22,4 @@ civiremote_case.update_form: profile: type: string requirements: - _user_is_logged_in: 'TRUE' + _permission: 'access civiremote case' diff --git a/modules/civiremote_entity/src/Access/RemoteContactIdProvider.php b/modules/civiremote_entity/src/Access/RemoteContactIdProvider.php index bf734d7..d3f24f4 100644 --- a/modules/civiremote_entity/src/Access/RemoteContactIdProvider.php +++ b/modules/civiremote_entity/src/Access/RemoteContactIdProvider.php @@ -20,7 +20,6 @@ namespace Drupal\civiremote_entity\Access; -use Assert\Assertion; use Drupal\Core\Session\AccountProxyInterface; final class RemoteContactIdProvider implements RemoteContactIdProviderInterface { @@ -31,16 +30,33 @@ public function __construct(AccountProxyInterface $currentUser) { $this->currentUser = $currentUser; } + /** + * @{inheritDoc} + */ public function getRemoteContactId(): string { - $account = $this->currentUser->getAccount(); - Assertion::propertyExists($account, 'civiremote_id'); - // @phpstan-ignore-next-line - $remoteContactId = $account->civiremote_id; + if (!$this->hasRemoteContactId()) { + throw new \RuntimeException(sprintf('User "%s" has no remote contact ID', $this->currentUser->getAccountName())); + } + + // @phpstan-ignore property.notFound + return $this->currentUser->getAccount()->civiremote_id; + } - Assertion::string($remoteContactId); - Assertion::notEmpty($remoteContactId); + /** + * @{inheritDoc} + */ + public function getRemoteContactIdOrNull(): ?string { + return $this->hasRemoteContactId() ? $this->getRemoteContactId() : NULL; + } + + /** + * @{inheritDoc} + */ + public function hasRemoteContactId(): bool { + $account = $this->currentUser->getAccount(); + $remoteContactId = $account->civiremote_id ?? NULL; - return $remoteContactId; + return is_string($remoteContactId) && '' !== $remoteContactId; } } diff --git a/modules/civiremote_entity/src/Access/RemoteContactIdProviderInterface.php b/modules/civiremote_entity/src/Access/RemoteContactIdProviderInterface.php index 787c421..a952878 100644 --- a/modules/civiremote_entity/src/Access/RemoteContactIdProviderInterface.php +++ b/modules/civiremote_entity/src/Access/RemoteContactIdProviderInterface.php @@ -22,6 +22,19 @@ interface RemoteContactIdProviderInterface { + /** + * @throws \RuntimeException + * If current user has no remote contact ID. + */ public function getRemoteContactId(): string; + /** + * @return string|null + * The user's remote contact ID, or NULL if the user has no remote contact + * ID. + */ + public function getRemoteContactIdOrNull(): ?string; + + public function hasRemoteContactId(): bool; + } diff --git a/modules/civiremote_entity/src/Api/AbstractEntityApi.php b/modules/civiremote_entity/src/Api/AbstractEntityApi.php index 9cd5505..20dc99f 100644 --- a/modules/civiremote_entity/src/Api/AbstractEntityApi.php +++ b/modules/civiremote_entity/src/Api/AbstractEntityApi.php @@ -46,7 +46,7 @@ public function getCreateForm(string $profile, array $arguments = []): EntityFor $result = $this->client->executeV4($this->getRemoteEntityName(), 'getCreateForm', [ 'profile' => $profile, 'arguments' => $arguments, - 'remoteContactId' => $this->remoteContactIdProvider->getRemoteContactId(), + 'remoteContactId' => $this->remoteContactIdProvider->getRemoteContactIdOrNull(), ]); return EntityForm::fromApiResultValue($result['values']); @@ -61,7 +61,7 @@ public function submitCreateForm(string $profile, array $data, array $arguments 'profile' => $profile, 'data' => $data, 'arguments' => $arguments, - 'remoteContactId' => $this->remoteContactIdProvider->getRemoteContactId(), + 'remoteContactId' => $this->remoteContactIdProvider->getRemoteContactIdOrNull(), ]); return FormSubmitResponse::fromApiResultValue($result['values']); @@ -76,7 +76,7 @@ public function validateCreateForm(string $profile, array $data, array $argument 'profile' => $profile, 'data' => $data, 'arguments' => $arguments, - 'remoteContactId' => $this->remoteContactIdProvider->getRemoteContactId(), + 'remoteContactId' => $this->remoteContactIdProvider->getRemoteContactIdOrNull(), ]); return FormValidationResponse::fromApiResultValue($result['values']); @@ -86,7 +86,7 @@ public function getUpdateForm(string $profile, int $id): EntityForm { $result = $this->client->executeV4($this->getRemoteEntityName(), 'getUpdateForm', [ 'profile' => $profile, 'id' => $id, - 'remoteContactId' => $this->remoteContactIdProvider->getRemoteContactId(), + 'remoteContactId' => $this->remoteContactIdProvider->getRemoteContactIdOrNull(), ]); return EntityForm::fromApiResultValue($result['values']); @@ -100,7 +100,7 @@ public function submitUpdateForm(string $profile, int $id, array $data): FormSub 'profile' => $profile, 'id' => $id, 'data' => $data, - 'remoteContactId' => $this->remoteContactIdProvider->getRemoteContactId(), + 'remoteContactId' => $this->remoteContactIdProvider->getRemoteContactIdOrNull(), ]); return FormSubmitResponse::fromApiResultValue($result['values']); @@ -114,7 +114,7 @@ public function validateUpdateForm(string $profile, int $id, array $data): FormV 'profile' => $profile, 'id' => $id, 'data' => $data, - 'remoteContactId' => $this->remoteContactIdProvider->getRemoteContactId(), + 'remoteContactId' => $this->remoteContactIdProvider->getRemoteContactIdOrNull(), ]); return FormValidationResponse::fromApiResultValue($result['values']);