Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Jul 14, 2023
1 parent 0b4d0d4 commit 7e89b78
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 26 deletions.
1 change: 1 addition & 0 deletions app/Domains/Contact/Dav/Jobs/UpdateVCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function permissions(): array
'author_must_belong_to_account',
'vault_must_belong_to_account',
'author_must_be_in_vault',
'author_must_be_vault_editor',
];
}

Expand Down
103 changes: 77 additions & 26 deletions app/Services/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ abstract class BaseService
*/
public Contact $contact;

/**
* Dependencies between permissions.
*
* @var array<string,array<string>>
*/
private static array $dependencies = [
'author_must_belong_to_account' => [],
'author_must_be_account_administrator' => [
'author_must_belong_to_account',
],
'vault_must_belong_to_account' => [],
'author_must_be_vault_manager' => [
'vault_must_belong_to_account',
'author_must_belong_to_account',
],
'author_must_be_vault_editor' => [
'vault_must_belong_to_account',
'author_must_belong_to_account',
],
'author_must_be_in_vault' => [
'vault_must_belong_to_account',
'author_must_belong_to_account',
],
'contact_must_belong_to_vault' => [
'vault_must_belong_to_account',
'author_must_belong_to_account',
],
];

/**
* Get the validation rules that apply to the service.
*/
Expand Down Expand Up @@ -57,38 +86,60 @@ public function validateRules(array $data): bool
{
Validator::make($data, $this->rules())->validate();

foreach ($this->permissions() as $permission) {
switch ($permission) {
case 'author_must_belong_to_account':
$this->validateAuthorBelongsToAccount($data);
break;
case 'author_must_be_account_administrator':
$this->validateAuthorIsAccountAdministrator();
break;
case 'vault_must_belong_to_account':
$this->validateVaultExists($data);
break;
case 'author_must_be_vault_manager':
$this->validateUserPermissionInVault(Vault::PERMISSION_MANAGE);
break;
case 'author_must_be_vault_editor':
$this->validateUserPermissionInVault(Vault::PERMISSION_EDIT);
break;
case 'author_must_be_in_vault':
$this->validateUserPermissionInVault(Vault::PERMISSION_VIEW);
break;
case 'contact_must_belong_to_vault':
$this->validateContactBelongsToVault($data);
break;
default:
throw new \Exception("Unknown permission: $permission");
break;
$permissions = collect($this->permissions());

foreach (static::$dependencies as $key => $value) {

Check failure on line 91 in app/Services/BaseService.php

View workflow job for this annotation

GitHub Actions / phpstan

Unsafe access to private property App\Services\BaseService::$dependencies through static::.
if ($permissions->contains($key)) {
collect($value)->each(function ($v) use ($permissions, $key) {
if (! $permissions->contains($v)) {
throw new \Exception("$key requires $v");
}
});

$this->validatePermission($key, $data);
}
}

if (($e = $permissions->diff(collect(static::$dependencies)->keys()))->isNotEmpty()) {

Check failure on line 103 in app/Services/BaseService.php

View workflow job for this annotation

GitHub Actions / phpstan

Unsafe access to private property App\Services\BaseService::$dependencies through static::.
throw new \Exception('Unknown permission: '.$e->first());
}

return true;
}

/**
* Validate a permission.
*/
private function validatePermission(string $permission, array $data): void
{
switch ($permission) {
case 'author_must_belong_to_account':
$this->validateAuthorBelongsToAccount($data);
break;
case 'author_must_be_account_administrator':
$this->validateAuthorIsAccountAdministrator();
break;
case 'vault_must_belong_to_account':
$this->validateVaultExists($data);
break;
case 'author_must_be_vault_manager':
$this->validateUserPermissionInVault(Vault::PERMISSION_MANAGE);
break;
case 'author_must_be_vault_editor':
$this->validateUserPermissionInVault(Vault::PERMISSION_EDIT);
break;
case 'author_must_be_in_vault':
$this->validateUserPermissionInVault(Vault::PERMISSION_VIEW);
break;
case 'contact_must_belong_to_vault':
$this->validateContactBelongsToVault($data);
break;
default:
throw new \Exception("Unknown permission: $permission");
break;

Check failure on line 139 in app/Services/BaseService.php

View workflow job for this annotation

GitHub Actions / phpstan

Unreachable statement - code above always terminates.
}
}

/**
* Validate that the author of the action belongs to the account.
*/
Expand Down

0 comments on commit 7e89b78

Please sign in to comment.