Move and refactoring ScopeResolveEvent

This commit is contained in:
Artem Vasilev 2024-03-14 16:18:22 +03:00
parent f67a9b7701
commit 8cf69e1968
3 changed files with 142 additions and 92 deletions

View File

@ -1,87 +0,0 @@
<?php
namespace Webmasterskaya\Component\OauthServer\Administrator\Event\Scope;
use Joomla\CMS\Event\AbstractEvent;
use Webmasterskaya\Component\OauthServer\Site\Entity\Scope;
class ScopeResolveEvent extends AbstractEvent
{
private bool $is_constructed;
public function __construct(array $scopes, string $grant, object $client, ?int $user_id)
{
$arguments = [
'scopes' => $scopes,
'grant' => $grant,
'client' => $client,
'user_id' => $user_id
];
$this->is_constructed = false;
parent::__construct('onOauthServerScopeResolve', $arguments);
$this->is_constructed = true;
}
protected function onSetScopes(array $scopes): array
{
foreach ($scopes as &$scope) {
if (!($scope instanceof Scope)) {
throw new \InvalidArgumentException(sprintf('Argument "scopes" must be array of "%s" in class "%s". "%s" given.',
Scope::class,
get_class($this),
get_debug_type($scope)
));
}
}
return $scopes;
}
protected function onSetGrant(string $grant)
{
if ($this->is_constructed) {
$grant = $this->getArgument('grant');
}
return $grant;
}
protected function onSetClient(object $client)
{
if ($this->is_constructed) {
$client = $this->getArgument('client');
}
return $client;
}
protected function onSetUser_id(?int $user_id)
{
if ($this->is_constructed) {
$user_id = $this->getArgument('user_id');
}
return $user_id;
}
public function removeArgument($name)
{
throw new \BadMethodCallException(
sprintf(
'Cannot remove the argument %s of the event %s.',
$name,
$this->name
)
);
}
public function clearArguments()
{
throw new \BadMethodCallException(
sprintf(
'Cannot clear arguments of the event %s.',
$this->name
)
);
}
}

View File

@ -0,0 +1,134 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_oauthserver
*
* @copyright (c) 2024. Webmasterskaya. <https://webmasterskaya.xyz>
* @license MIT; see LICENSE.txt
**/
namespace Webmasterskaya\Component\OauthServer\Administrator\Event;
use Joomla\CMS\Event\AbstractEvent;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use Webmasterskaya\Component\OauthServer\Site\Entity\Scope;
class ScopeResolveEvent extends AbstractEvent
{
private bool $constructed = false;
public function __construct(string $name, array $arguments = [])
{
if (!array_key_exists('scopes', $arguments))
{
throw new \BadMethodCallException("Argument 'scopes' is required for event $name");
}
if (!array_key_exists('grant', $arguments))
{
throw new \BadMethodCallException("Argument 'grant' is required for event $name");
}
if (!array_key_exists('client', $arguments))
{
throw new \BadMethodCallException("Argument 'client' is required for event $name");
}
parent::__construct('onScopeResolve', $arguments);
$this->constructed = true;
}
protected function onSetScopes(array $scopes): array
{
foreach ($scopes as &$scope)
{
if (!($scope instanceof Scope))
{
throw new \InvalidArgumentException(sprintf("Argument 'scopes' must be array of '%s' in class '%s'. '%s' given.",
Scope::class,
get_class($this),
get_debug_type($scope)
));
}
}
return $scopes;
}
protected function onSetGrant(string $grant): string
{
if (!$this->constructed)
{
return $grant;
}
throw new \BadMethodCallException(
sprintf(
"Cannot set the argument 'grant' of the event %s after initialize.",
$this->name
)
);
}
protected function onSetClient(ClientEntityInterface $client): ClientEntityInterface
{
if (!$this->constructed)
{
return $client;
}
throw new \BadMethodCallException(
sprintf(
"Cannot set the argument 'client' of the event %s after initialize.",
$this->name
)
);
}
protected function onSetUserId(?int $userId): ?int
{
if (!$this->constructed)
{
return $userId;
}
throw new \BadMethodCallException(
sprintf(
"Cannot set the argument 'userId' of the event %s after initialize.",
$this->name
)
);
}
public function removeArgument($name)
{
if (!$this->constructed)
{
return parent::removeArgument($name);
}
throw new \BadMethodCallException(
sprintf(
'Cannot remove the argument %s of the immutable event %s.',
$name,
$this->name
)
);
}
public function clearArguments(): array
{
if (!$this->constructed)
{
return parent::clearArguments();
}
throw new \BadMethodCallException(
sprintf(
'Cannot clear arguments of the immutable event %s.',
$this->name
)
);
}
}

View File

@ -15,7 +15,7 @@ use Joomla\Event\DispatcherAwareTrait;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use Webmasterskaya\Component\OauthServer\Administrator\Event\Scope\ScopeResolveEvent;
use Webmasterskaya\Component\OauthServer\Administrator\Event\ScopeResolveEvent;
use Webmasterskaya\Component\OauthServer\Administrator\Model\ClientModel;
use Webmasterskaya\Component\OauthServer\Site\Entity\Scope;
@ -66,10 +66,13 @@ class ScopeRepository implements ScopeRepositoryInterface, DispatcherAwareInterf
PluginHelper::importPlugin('oauthserver');
$event = new ScopeResolveEvent(
$scopes,
$grantType,
$client,
$userIdentifier
'onScopeResolve',
[
'scopes' => $scopes,
'grant' => $grantType,
'client' => $client,
'userId' => $userIdentifier
]
);
return $this->getDispatcher()