Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce sql query executions for organization token #474

Merged
merged 1 commit into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Controller/OrganizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
]);
}
Expand Down
10 changes: 1 addition & 9 deletions src/Query/User/Model/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/Query/User/OrganizationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 9 additions & 4 deletions src/Query/User/OrganizationQuery/DbalOrganizationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'],
]);
Expand All @@ -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
);
}

Expand Down
9 changes: 7 additions & 2 deletions src/Service/Organization/OrganizationVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions templates/organization/overview.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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