From 30fb459d230bf5fc3873862eb85f1eb9ce73bdc6 Mon Sep 17 00:00:00 2001 From: Arkadiusz Kondas Date: Mon, 31 May 2021 13:13:55 +0200 Subject: [PATCH] Reduce sql query executions for organization token --- src/Controller/OrganizationController.php | 1 + src/Query/User/Model/Organization.php | 10 +--------- src/Query/User/OrganizationQuery.php | 2 ++ .../OrganizationQuery/DbalOrganizationQuery.php | 13 +++++++++---- src/Service/Organization/OrganizationVoter.php | 9 +++++++-- templates/organization/overview.html.twig | 4 ++-- 6 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/Controller/OrganizationController.php b/src/Controller/OrganizationController.php index 89150a0d..78cf1441 100644 --- a/src/Controller/OrganizationController.php +++ b/src/Controller/OrganizationController.php @@ -59,6 +59,7 @@ public function overview(Organization $organization): Response { return $this->render('organization/overview.html.twig', [ 'organization' => $organization, + 'token' => $this->organizationQuery->findAnyToken($organization->id()), 'tokenCount' => $this->organizationQuery->tokenCount($organization->id()), ]); } diff --git a/src/Query/User/Model/Organization.php b/src/Query/User/Model/Organization.php index 6afd2638..c361b6a3 100644 --- a/src/Query/User/Model/Organization.php +++ b/src/Query/User/Model/Organization.php @@ -19,19 +19,16 @@ final class Organization */ private array $members; - private ?string $token; - /** * @param Member[] $members */ - public function __construct(string $id, string $name, string $alias, array $members, bool $hasAnonymousAccess, ?string $token = null) + public function __construct(string $id, string $name, string $alias, array $members, bool $hasAnonymousAccess) { $this->id = $id; $this->name = $name; $this->alias = $alias; $this->members = array_map(fn (Member $member) => $member, $members); $this->hasAnonymousAccess = $hasAnonymousAccess; - $this->token = $token; } public function id(): string @@ -49,11 +46,6 @@ public function alias(): string return $this->alias; } - public function token(): ?string - { - return $this->token; - } - public function isMember(string $userId): bool { foreach ($this->members as $member) { diff --git a/src/Query/User/OrganizationQuery.php b/src/Query/User/OrganizationQuery.php index c467bc03..1600a19d 100644 --- a/src/Query/User/OrganizationQuery.php +++ b/src/Query/User/OrganizationQuery.php @@ -29,6 +29,8 @@ public function getByInvitation(string $token, string $email): Option; */ public function findAllTokens(string $organizationId, Filter $filter): array; + public function findAnyToken(string $organizationId): ?string; + public function tokenCount(string $organizationId): int; public function getInstalls(string $organizationId, int $lastDays = 30): Installs; diff --git a/src/Query/User/OrganizationQuery/DbalOrganizationQuery.php b/src/Query/User/OrganizationQuery/DbalOrganizationQuery.php index edd21e49..77e85bb3 100644 --- a/src/Query/User/OrganizationQuery/DbalOrganizationQuery.php +++ b/src/Query/User/OrganizationQuery/DbalOrganizationQuery.php @@ -79,6 +79,15 @@ public function findAllTokens(string $organizationId, Filter $filter): array ])); } + public function findAnyToken(string $organizationId): ?string + { + $token = $this->connection->fetchOne('SELECT value FROM organization_token WHERE organization_id = :id', [ + ':id' => $organizationId, + ]); + + return $token !== false ? $token : null; + } + public function tokenCount(string $organizationId): int { return (int) $this @@ -221,9 +230,6 @@ public function findToken(string $organizationId, string $value): Option */ private function hydrateOrganization(array $data): Organization { - $token = $this->connection->fetchOne('SELECT value FROM organization_token WHERE organization_id = :id', [ - ':id' => $data['id'], - ]); $members = $this->connection->fetchAllAssociative('SELECT m.user_id, m.role, u.email FROM organization_member m JOIN "user" u ON u.id = m.user_id WHERE m.organization_id = :id', [ ':id' => $data['id'], ]); @@ -234,7 +240,6 @@ private function hydrateOrganization(array $data): Organization $data['alias'], array_map(fn (array $row) => new Member($row['user_id'], $row['email'], $row['role']), $members), $data['has_anonymous_access'], - $token !== false ? $token : null ); } diff --git a/src/Service/Organization/OrganizationVoter.php b/src/Service/Organization/OrganizationVoter.php index ebf1d768..592e5676 100644 --- a/src/Service/Organization/OrganizationVoter.php +++ b/src/Service/Organization/OrganizationVoter.php @@ -57,13 +57,18 @@ protected function voteOnAttribute(string $attribute, $subject, TokenInterface $ } if ($subject instanceof Request) { - $checkOrganization = $this->organizations->getByAlias($subject->get('organization'))->getOrNull(); + $alias = $subject->get('organization'); + $checkOrganization = $this->organizations->getByAlias($alias)->getOrNull(); + if ($checkOrganization instanceof Organization) { + $subject->attributes->set('organization', $checkOrganization); + } + if ($checkOrganization instanceof Organization && $checkOrganization->hasAnonymousAccess()) { return true; } foreach ($user->organizations() as $organization) { - if ($organization->alias() !== $subject->get('organization')) { + if ($organization->alias() !== $alias) { continue; } diff --git a/templates/organization/overview.html.twig b/templates/organization/overview.html.twig index ca0f0b9f..98def7fc 100644 --- a/templates/organization/overview.html.twig +++ b/templates/organization/overview.html.twig @@ -12,7 +12,7 @@ {% block token %} {% if tokenCount <= 1 %} - {{ organization.token }} + {{ token }} {% else %} Get token @@ -37,7 +37,7 @@ {% block content %}
{% if is_granted('ROLE_ORGANIZATION_MEMBER', organization) %} - {% if organization.token %} + {% if tokenCount > 0 %}

Configure global authentication to access this organization's packages:

composer config --global --auth http-basic.{{ url('organization_repo_url', {organization: organization.alias}, true) | trim('/') }} token {{ block('token') | spaceless }}