add client scope

This commit is contained in:
Charles 2025-07-04 15:20:42 +02:00
parent 8a404d371e
commit 023bf29e0b
2 changed files with 51 additions and 17 deletions

View File

@ -2,7 +2,10 @@
namespace App\EventSubscriber; namespace App\EventSubscriber;
use App\Service\ClientService;
use Doctrine\ORM\EntityManagerInterface;
use League\Bundle\OAuth2ServerBundle\Event\ScopeResolveEvent; use League\Bundle\OAuth2ServerBundle\Event\ScopeResolveEvent;
use League\Bundle\OAuth2ServerBundle\Repository\ScopeRepository;
use League\Bundle\OAuth2ServerBundle\ValueObject\Scope; use League\Bundle\OAuth2ServerBundle\ValueObject\Scope;
use League\Bundle\OAuth2ServerBundle\Model\Client; use League\Bundle\OAuth2ServerBundle\Model\Client;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface; use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
@ -13,12 +16,16 @@ final class ScopeResolveListener implements EventSubscriberInterface
{ {
private ClientRepositoryInterface $clientRepository; private ClientRepositoryInterface $clientRepository;
private LoggerInterface $logger; private LoggerInterface $logger;
private ClientService $clientService;
private EntityManagerInterface $entityManager;
public function __construct(ClientRepositoryInterface $clientRepository, LoggerInterface $logger) public function __construct(ClientRepositoryInterface $clientRepository, LoggerInterface $logger, ClientService $clientService, EntityManagerInterface $entityManager)
{ {
$this->logger = $logger; $this->logger = $logger;
// Inject the client repository // Inject the client repository
$this->clientRepository = $clientRepository; $this->clientRepository = $clientRepository;
$this->clientService = $clientService;
$this->entityManager = $entityManager;
} }
public function onScopeResolve(ScopeResolveEvent $event): void public function onScopeResolve(ScopeResolveEvent $event): void
@ -38,23 +45,27 @@ final class ScopeResolveListener implements EventSubscriberInterface
foreach ($defaultScopes as $scope) { foreach ($defaultScopes as $scope) {
$finalScopes[] = new Scope($scope); $finalScopes[] = new Scope($scope);
} }
$clientEntity = $this->entityManager->getRepository(Client::class)->findOneBy(['identifier' => $clientIdentifier]);
$finalScopes[] = new Scope('apps:'. $clientEntity->getName());
// Add client-specific scopes based on client identifier or name // Add client-specific scopes based on client identifier or name
switch ($clientIdentifier) { // switch ($clientIdentifier) {
case 'a712b3caede9588372b2a83947fae53e': // case 'a712b3caede9588372b2a83947fae53e':
$finalScopes[] = new Scope('apps:easyexploit'); // $finalScopes[] = new Scope('apps:easyexploit');
break; // break;
case 'EasyAccess': // case '14bbb1b1692ac3a45159e263e3e7ec67':
$finalScopes[] = new Scope('apps:easyaccess'); // $finalScopes[] = new Scope('apps:client');
break; // break;
case 'EasyMonithor': // case 'EasyMonithor':
$finalScopes[] = new Scope('apps:easymonithor'); // $finalScopes[] = new Scope('apps:easymonithor');
break; // break;
case 'EasyCheck': // case 'EasyCheck':
$finalScopes[] = new Scope('apps:easycheck'); // $finalScopes[] = new Scope('apps:easycheck');
break; // break;
// Add more cases as needed for other applications // // Add more cases as needed for other applications
} // }
// // If the client is an admin client, add admin scopes // // If the client is an admin client, add admin scopes
// if (str_contains($client->getName(), 'Admin')) { // if (str_contains($client->getName(), 'Admin')) {

View File

@ -0,0 +1,23 @@
<?php
namespace App\Service;
use League\Bundle\OAuth2ServerBundle\Model\Client;
use Doctrine\ORM\EntityManagerInterface;
class ClientService{
/**
* Retrieves a Client entity by its identifier.
*
* @param string $identifier The identifier of the client.
* @param EntityManagerInterface $entityManager The entity manager to use for database operations.
* @return Client|null The Client entity or null if not found.
*/
public function getClientIdentifier(String $identifier, EntityManagerInterface $entityManager): Client
{
return $entityManager->getRepository(Client::class)->findOneBy(['identifier' => $identifier]);
}
}