diff --git a/com_oauthserver/administrator/src/Event/Scope/ScopeResolveEvent.php b/com_oauthserver/administrator/src/Event/Scope/ScopeResolveEvent.php deleted file mode 100644 index 50ae660..0000000 --- a/com_oauthserver/administrator/src/Event/Scope/ScopeResolveEvent.php +++ /dev/null @@ -1,87 +0,0 @@ - $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 - ) - ); - } -} \ No newline at end of file diff --git a/com_oauthserver/administrator/src/Event/ScopeResolveEvent.php b/com_oauthserver/administrator/src/Event/ScopeResolveEvent.php new file mode 100644 index 0000000..9d4b30a --- /dev/null +++ b/com_oauthserver/administrator/src/Event/ScopeResolveEvent.php @@ -0,0 +1,134 @@ + + * @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 + ) + ); + } +} diff --git a/com_oauthserver/site/src/Repository/ScopeRepository.php b/com_oauthserver/site/src/Repository/ScopeRepository.php index bf191f5..35fd56d 100644 --- a/com_oauthserver/site/src/Repository/ScopeRepository.php +++ b/com_oauthserver/site/src/Repository/ScopeRepository.php @@ -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()