Skip to content

Commit

Permalink
Add permission access civiremote case
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Tubach committed Aug 8, 2024
1 parent 6388aa9 commit 5efb90c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
2 changes: 2 additions & 0 deletions modules/civiremote_case/civiremote_case.permissions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
access civiremote case:
title: 'Access Cases in CiviCRM'
4 changes: 2 additions & 2 deletions modules/civiremote_case/civiremote_case.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand All @@ -22,4 +22,4 @@ civiremote_case.update_form:
profile:
type: string
requirements:
_user_is_logged_in: 'TRUE'
_permission: 'access civiremote case'
32 changes: 24 additions & 8 deletions modules/civiremote_entity/src/Access/RemoteContactIdProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

namespace Drupal\civiremote_entity\Access;

use Assert\Assertion;
use Drupal\Core\Session\AccountProxyInterface;

final class RemoteContactIdProvider implements RemoteContactIdProviderInterface {
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
12 changes: 6 additions & 6 deletions modules/civiremote_entity/src/Api/AbstractEntityApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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']);
Expand All @@ -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']);
Expand All @@ -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']);
Expand All @@ -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']);
Expand All @@ -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']);
Expand Down

0 comments on commit 5efb90c

Please sign in to comment.