Skip to content

Commit

Permalink
use cacheable security voters
Browse files Browse the repository at this point in the history
  • Loading branch information
4rthem committed Sep 27, 2023
1 parent 09c8178 commit dd05c82
Show file tree
Hide file tree
Showing 17 changed files with 107 additions and 4 deletions.
10 changes: 9 additions & 1 deletion databox/api/src/Elasticsearch/AbstractSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ protected function createACLBoolQuery(?string $userId, array $groupIds): ?Query\

$publicWorkspaceIds = $this->getPublicWorkspaceIds();
if (null !== $userId) {
$allowedWorkspaceIds = $this->getAllowedWorkspaceIds($userId, $groupIds);

$aclBoolQuery->addMust(new Query\Terms(
'workspaceId',
array_values(array_unique(array_merge($allowedWorkspaceIds, $publicWorkspaceIds)))
));

if (!empty($publicWorkspaceIds)) {
$publicWorkspaceBoolQuery = new Query\BoolQuery();
$publicWorkspaceBoolQuery->addMust(new Query\Range('privacy', [
Expand All @@ -37,7 +44,6 @@ protected function createACLBoolQuery(?string $userId, array $groupIds): ?Query\
$shoulds[] = $publicWorkspaceBoolQuery;
}

$allowedWorkspaceIds = $this->getAllowedWorkspaceIds($userId, $groupIds);
if (!empty($allowedWorkspaceIds)) {
$workspaceBoolQuery = new Query\BoolQuery();

Expand All @@ -55,6 +61,8 @@ protected function createACLBoolQuery(?string $userId, array $groupIds): ?Query\
$shoulds[] = new Query\Terms('groups', $groupIds);
}
} else {
$aclBoolQuery->addMust(new Query\Terms('workspaceId', $publicWorkspaceIds));

if (!empty($publicWorkspaceIds)) {
$publicWorkspaceBoolQuery = new Query\BoolQuery();
$publicWorkspaceBoolQuery->addMust(new Query\Range('privacy', [
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/AssetDataTemplateVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof AssetDataTemplate;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, AssetDataTemplate::class, true);
}

/**
* @param AssetDataTemplate $subject
*/
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/AssetFileVersionVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof AssetFileVersion;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, AssetFileVersion::class, true);
}

/**
* @param AssetFileVersion $subject
*/
Expand Down
10 changes: 10 additions & 0 deletions databox/api/src/Security/Voter/AssetVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof Asset;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, Asset::class, true);
}

/**
* @param Asset $subject
*/
Expand All @@ -31,6 +36,11 @@ protected function voteOnAttribute(string $attribute, $subject, TokenInterface $
$userId = $user instanceof JwtUser ? $user->getId() : false;
$isOwner = fn (): bool => $userId && $subject->getOwnerId() === $userId;

$workspace = $subject->getWorkspace();
if (!$this->security->isGranted(AbstractVoter::READ, $workspace)) {
return false;
}

switch ($attribute) {
case self::CREATE:
if (null !== $collection = $subject->getReferenceCollection()) {
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/AttributeClassVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof AttributeClass;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, AttributeClass::class, true);
}

/**
* @param AttributeClass $subject
*/
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/AttributeDefinitionVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof AttributeDefinition;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, AttributeDefinition::class, true);
}

/**
* @param AttributeDefinition $subject
*/
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/AttributeVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof Attribute;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, Attribute::class, true);
}

/**
* @param Attribute $subject
*/
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/ChuckNorrisVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ protected function supports(string $attribute, $subject): bool
return self::ROLE !== $attribute;
}

public function supportsAttribute(string $attribute): bool
{
return self::ROLE !== $attribute;
}

protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
return $this->security->isGranted(self::ROLE);
Expand Down
16 changes: 13 additions & 3 deletions databox/api/src/Security/Voter/CollectionVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof Collection;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, Collection::class, true);
}

/**
* @param Collection $subject
*/
Expand All @@ -35,19 +40,24 @@ private function doVote(string $attribute, Collection $subject, TokenInterface $
$userId = $user instanceof JwtUser ? $user->getId() : false;
$isOwner = fn (): bool => $userId && $subject->getOwnerId() === $userId;

$workspace = $subject->getWorkspace();
if (!$this->security->isGranted(AbstractVoter::READ, $workspace)) {
return false;
}

return match ($attribute) {
self::CREATE => $subject->getParent() ? $this->security->isGranted(AbstractVoter::EDIT, $subject->getParent())
: $this->security->isGranted(AbstractVoter::EDIT, $subject->getWorkspace()),
: $this->security->isGranted(AbstractVoter::EDIT, $workspace),
self::LIST => $isOwner()
|| $subject->getPrivacy() >= WorkspaceItemPrivacyInterface::PUBLIC
|| ($userId && $subject->getPrivacy() >= WorkspaceItemPrivacyInterface::PRIVATE)
|| ($this->security->isGranted(AbstractVoter::READ, $subject->getWorkspace()) && $subject->getPrivacy() >= WorkspaceItemPrivacyInterface::PRIVATE_IN_WORKSPACE)
|| ($this->security->isGranted(AbstractVoter::READ, $workspace) && $subject->getPrivacy() >= WorkspaceItemPrivacyInterface::PRIVATE_IN_WORKSPACE)
|| $this->hasAcl(PermissionInterface::VIEW, $subject, $token)
|| (null !== $subject->getParent() && $this->security->isGranted($attribute, $subject->getParent())),
self::READ => $isOwner()
|| $subject->getPrivacy() >= WorkspaceItemPrivacyInterface::PUBLIC
|| ($userId && $subject->getPrivacy() >= WorkspaceItemPrivacyInterface::PUBLIC_FOR_USERS)
|| ($this->security->isGranted(AbstractVoter::READ, $subject->getWorkspace()) && $subject->getPrivacy() >= WorkspaceItemPrivacyInterface::PUBLIC_IN_WORKSPACE)
|| ($this->security->isGranted(AbstractVoter::READ, $workspace) && $subject->getPrivacy() >= WorkspaceItemPrivacyInterface::PUBLIC_IN_WORKSPACE)
|| $this->hasAcl(PermissionInterface::VIEW, $subject, $token)
|| (null !== $subject->getParent() && $this->security->isGranted($attribute, $subject->getParent())),
self::EDIT => $isOwner()
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/FileVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof File;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, File::class, true);
}

/**
* @param File $subject
*/
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/RenditionClassVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof RenditionClass;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, RenditionClass::class, true);
}

/**
* @param RenditionClass $subject
*/
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/RenditionDefinitionVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof RenditionDefinition;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, RenditionDefinition::class, true);
}

/**
* @param RenditionDefinition $subject
*/
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/RenditionVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof AssetRendition;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, AssetRendition::class, true);
}

/**
* @param AssetRendition $subject
*/
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/TagFilterRuleVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof TagFilterRule;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, TagFilterRule::class, true);
}

/**
* @param TagFilterRule $subject
*/
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/TemplateAttributeVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof TemplateAttribute;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, TemplateAttribute::class, true);
}

/**
* @param TemplateAttribute $subject
*/
Expand Down
5 changes: 5 additions & 0 deletions databox/api/src/Security/Voter/WorkflowStateVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof WorkflowState;
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, WorkflowState::class, true);
}

/**
* @param WorkflowState $subject
*/
Expand Down
10 changes: 10 additions & 0 deletions databox/api/src/Security/Voter/WorkspaceVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ protected function supports(string $attribute, $subject): bool
return $subject instanceof Workspace && !is_numeric($attribute);
}

public function supportsAttribute(string $attribute): bool
{
return !is_numeric($attribute);
}

public function supportsType(string $subjectType): bool
{
return is_a($subjectType, Workspace::class, true);
}

/**
* @param Workspace $subject
*/
Expand Down

0 comments on commit dd05c82

Please sign in to comment.