-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement anonymous access to organization (#201)
* Implement anonymous access to organization * Code review fixes
- Loading branch information
Showing
28 changed files
with
622 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buddy\Repman\Form\Type\Organization; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | ||
use Symfony\Component\Form\Extension\Core\Type\SubmitType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
|
||
class ChangeAnonymousAccessType extends AbstractType | ||
{ | ||
public function getBlockPrefix(): string | ||
{ | ||
return ''; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $options | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add('hasAnonymousAccess', CheckboxType::class, [ | ||
'label' => 'Allow anonymous users', | ||
'required' => false, | ||
]) | ||
->add('changeAnonymousAccess', SubmitType::class, ['label' => 'Change']) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buddy\Repman\Message\Organization; | ||
|
||
final class ChangeAnonymousAccess | ||
{ | ||
private string $organizationId; | ||
private bool $hasAnonymousAccess; | ||
|
||
public function __construct(string $organizationId, bool $hasAnonymousAccess) | ||
{ | ||
$this->organizationId = $organizationId; | ||
$this->hasAnonymousAccess = $hasAnonymousAccess; | ||
} | ||
|
||
public function organizationId(): string | ||
{ | ||
return $this->organizationId; | ||
} | ||
|
||
public function hasAnonymousAccess(): bool | ||
{ | ||
return $this->hasAnonymousAccess; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/MessageHandler/Organization/ChangeAnonymousAccessHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buddy\Repman\MessageHandler\Organization; | ||
|
||
use Buddy\Repman\Message\Organization\ChangeAnonymousAccess; | ||
use Buddy\Repman\Repository\OrganizationRepository; | ||
use Ramsey\Uuid\Uuid; | ||
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; | ||
|
||
final class ChangeAnonymousAccessHandler implements MessageHandlerInterface | ||
{ | ||
private OrganizationRepository $repositories; | ||
|
||
public function __construct(OrganizationRepository $repositories) | ||
{ | ||
$this->repositories = $repositories; | ||
} | ||
|
||
public function __invoke(ChangeAnonymousAccess $message): void | ||
{ | ||
$this->repositories | ||
->getById(Uuid::fromString($message->organizationId())) | ||
->changeAnonymousAccess($message->hasAnonymousAccess()) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buddy\Repman\Migrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
/** | ||
* Auto-generated Migration: Please modify to your needs! | ||
*/ | ||
final class Version20200615181216 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return ''; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
// this up() migration is auto-generated, please modify it to your needs | ||
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.'); | ||
|
||
$this->addSql('ALTER TABLE organization ADD has_anonymous_access BOOLEAN NOT NULL DEFAULT false'); | ||
$this->addSql('ALTER TABLE "user" ALTER email_scan_result DROP DEFAULT'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
// this down() migration is auto-generated, please modify it to your needs | ||
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.'); | ||
|
||
$this->addSql('ALTER TABLE organization DROP has_anonymous_access'); | ||
$this->addSql('ALTER TABLE "user" ALTER email_scan_result SET DEFAULT \'true\''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buddy\Repman\Security; | ||
|
||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Core\Exception\BadCredentialsException; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Core\User\UserProviderInterface; | ||
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; | ||
|
||
final class AnonymousOrganizationUserAuthenticator extends AbstractGuardAuthenticator | ||
{ | ||
/** | ||
* @codeCoverageIgnore | ||
* | ||
* @return Response | ||
*/ | ||
public function start(Request $request, AuthenticationException $authException = null) | ||
{ | ||
return new JsonResponse([ | ||
'message' => 'Authentication Required', | ||
], Response::HTTP_UNAUTHORIZED); | ||
} | ||
|
||
public function supports(Request $request) | ||
{ | ||
return $request->get('_route') !== 'repo_package_downloads' | ||
&& !$request->headers->has('PHP_AUTH_USER') | ||
&& !$request->headers->has('PHP_AUTH_PW'); | ||
} | ||
|
||
public function getCredentials(Request $request) | ||
{ | ||
$organizationAlias = $request->get('organization'); | ||
if ($organizationAlias === null) { | ||
throw new BadCredentialsException(); | ||
} | ||
|
||
return $organizationAlias; | ||
} | ||
|
||
public function getUser($credentials, UserProviderInterface $userProvider) | ||
{ | ||
if (!$userProvider instanceof OrganizationProvider) { | ||
throw new \InvalidArgumentException(); | ||
} | ||
|
||
return $userProvider->loadUserByAlias($credentials); | ||
} | ||
|
||
public function checkCredentials($credentials, UserInterface $user) | ||
{ | ||
return true; | ||
} | ||
|
||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response | ||
{ | ||
return new JsonResponse([ | ||
'message' => strtr($exception->getMessageKey(), $exception->getMessageData()), | ||
], Response::HTTP_FORBIDDEN); | ||
} | ||
|
||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey) | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function supportsRememberMe(): bool | ||
{ | ||
return false; | ||
} | ||
} |
Oops, something went wrong.