Skip to content

Commit 30fb459

Browse files
committed
Reduce sql query executions for organization token
1 parent 8ee8800 commit 30fb459

File tree

6 files changed

+22
-17
lines changed

6 files changed

+22
-17
lines changed

src/Controller/OrganizationController.php

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function overview(Organization $organization): Response
5959
{
6060
return $this->render('organization/overview.html.twig', [
6161
'organization' => $organization,
62+
'token' => $this->organizationQuery->findAnyToken($organization->id()),
6263
'tokenCount' => $this->organizationQuery->tokenCount($organization->id()),
6364
]);
6465
}

src/Query/User/Model/Organization.php

+1-9
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,16 @@ final class Organization
1919
*/
2020
private array $members;
2121

22-
private ?string $token;
23-
2422
/**
2523
* @param Member[] $members
2624
*/
27-
public function __construct(string $id, string $name, string $alias, array $members, bool $hasAnonymousAccess, ?string $token = null)
25+
public function __construct(string $id, string $name, string $alias, array $members, bool $hasAnonymousAccess)
2826
{
2927
$this->id = $id;
3028
$this->name = $name;
3129
$this->alias = $alias;
3230
$this->members = array_map(fn (Member $member) => $member, $members);
3331
$this->hasAnonymousAccess = $hasAnonymousAccess;
34-
$this->token = $token;
3532
}
3633

3734
public function id(): string
@@ -49,11 +46,6 @@ public function alias(): string
4946
return $this->alias;
5047
}
5148

52-
public function token(): ?string
53-
{
54-
return $this->token;
55-
}
56-
5749
public function isMember(string $userId): bool
5850
{
5951
foreach ($this->members as $member) {

src/Query/User/OrganizationQuery.php

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function getByInvitation(string $token, string $email): Option;
2929
*/
3030
public function findAllTokens(string $organizationId, Filter $filter): array;
3131

32+
public function findAnyToken(string $organizationId): ?string;
33+
3234
public function tokenCount(string $organizationId): int;
3335

3436
public function getInstalls(string $organizationId, int $lastDays = 30): Installs;

src/Query/User/OrganizationQuery/DbalOrganizationQuery.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ public function findAllTokens(string $organizationId, Filter $filter): array
7979
]));
8080
}
8181

82+
public function findAnyToken(string $organizationId): ?string
83+
{
84+
$token = $this->connection->fetchOne('SELECT value FROM organization_token WHERE organization_id = :id', [
85+
':id' => $organizationId,
86+
]);
87+
88+
return $token !== false ? $token : null;
89+
}
90+
8291
public function tokenCount(string $organizationId): int
8392
{
8493
return (int) $this
@@ -221,9 +230,6 @@ public function findToken(string $organizationId, string $value): Option
221230
*/
222231
private function hydrateOrganization(array $data): Organization
223232
{
224-
$token = $this->connection->fetchOne('SELECT value FROM organization_token WHERE organization_id = :id', [
225-
':id' => $data['id'],
226-
]);
227233
$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', [
228234
':id' => $data['id'],
229235
]);
@@ -234,7 +240,6 @@ private function hydrateOrganization(array $data): Organization
234240
$data['alias'],
235241
array_map(fn (array $row) => new Member($row['user_id'], $row['email'], $row['role']), $members),
236242
$data['has_anonymous_access'],
237-
$token !== false ? $token : null
238243
);
239244
}
240245

src/Service/Organization/OrganizationVoter.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,18 @@ protected function voteOnAttribute(string $attribute, $subject, TokenInterface $
5757
}
5858

5959
if ($subject instanceof Request) {
60-
$checkOrganization = $this->organizations->getByAlias($subject->get('organization'))->getOrNull();
60+
$alias = $subject->get('organization');
61+
$checkOrganization = $this->organizations->getByAlias($alias)->getOrNull();
62+
if ($checkOrganization instanceof Organization) {
63+
$subject->attributes->set('organization', $checkOrganization);
64+
}
65+
6166
if ($checkOrganization instanceof Organization && $checkOrganization->hasAnonymousAccess()) {
6267
return true;
6368
}
6469

6570
foreach ($user->organizations() as $organization) {
66-
if ($organization->alias() !== $subject->get('organization')) {
71+
if ($organization->alias() !== $alias) {
6772
continue;
6873
}
6974

templates/organization/overview.html.twig

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
{% block token %}
1414
{% if tokenCount <= 1 %}
15-
<span class="token d-none" data-type="token">{{ organization.token }}</span>
15+
<span class="token d-none" data-type="token">{{ token }}</span>
1616
<button class="btn btn-primary btn-sm show-token">Show token</button>
1717
{% else %}
1818
<a href="{{ path('organization_tokens', {organization: organization.alias}) }}" class="btn btn-primary btn-sm">Get token</a>
@@ -37,7 +37,7 @@
3737
{% block content %}
3838
<div class="markdown">
3939
{% if is_granted('ROLE_ORGANIZATION_MEMBER', organization) %}
40-
{% if organization.token %}
40+
{% if tokenCount > 0 %}
4141
<p>Configure global authentication to access this organization's packages:</p>
4242
<div class="highlight">
4343
<pre>composer config --global --auth http-basic.{{ url('organization_repo_url', {organization: organization.alias}, true) | trim('/') }} token {{ block('token') | spaceless }}</pre>

0 commit comments

Comments
 (0)