-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from moufmouf/inject_user
Adding @InjectUser annotation
- Loading branch information
Showing
13 changed files
with
269 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Annotations; | ||
|
||
use BadMethodCallException; | ||
use function ltrim; | ||
|
||
/** | ||
* Use this annotation to tell GraphQLite to inject the current logged user as an input parameter. | ||
* If the parameter is not nullable, the user MUST be logged to access the resource. | ||
* | ||
* @Annotation | ||
* @Target({"METHOD", "ANNOTATION"}) | ||
* @Attributes({ | ||
* @Attribute("for", type = "string") | ||
* }) | ||
*/ | ||
class InjectUser implements ParameterAnnotationInterface | ||
{ | ||
/** @var string */ | ||
private $for; | ||
|
||
/** | ||
* @param array<string, mixed> $values | ||
*/ | ||
public function __construct(array $values) | ||
{ | ||
if (! isset($values['for'])) { | ||
throw new BadMethodCallException('The @InjectUser annotation must be passed a target. For instance: "@InjectUser(for="$user")"'); | ||
} | ||
$this->for = ltrim($values['for'], '$'); | ||
} | ||
|
||
public function getTarget(): string | ||
{ | ||
return $this->for; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Mappers\Parameters; | ||
|
||
use phpDocumentor\Reflection\DocBlock; | ||
use phpDocumentor\Reflection\Type; | ||
use ReflectionParameter; | ||
use TheCodingMachine\GraphQLite\Annotations\InjectUser; | ||
use TheCodingMachine\GraphQLite\Annotations\ParameterAnnotations; | ||
use TheCodingMachine\GraphQLite\Parameters\InjectUserParameter; | ||
use TheCodingMachine\GraphQLite\Parameters\ParameterInterface; | ||
use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface; | ||
|
||
/** | ||
* Maps the current user to a parameter targetted by the \@InjectUser annotation. | ||
*/ | ||
class InjectUserParameterHandler implements ParameterMiddlewareInterface | ||
{ | ||
/** @var AuthenticationServiceInterface */ | ||
private $authenticationService; | ||
|
||
public function __construct(AuthenticationServiceInterface $authenticationService) | ||
{ | ||
$this->authenticationService = $authenticationService; | ||
} | ||
|
||
public function mapParameter(ReflectionParameter $parameter, DocBlock $docBlock, ?Type $paramTagType, ParameterAnnotations $parameterAnnotations, ParameterHandlerInterface $next): ParameterInterface | ||
{ | ||
$injectUser = $parameterAnnotations->getAnnotationByType(InjectUser::class); | ||
|
||
if ($injectUser === null) { | ||
return $next->mapParameter($parameter, $docBlock, $paramTagType, $parameterAnnotations); | ||
} | ||
|
||
return new InjectUserParameter($this->authenticationService); | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TheCodingMachine\GraphQLite\Parameters; | ||
|
||
use GraphQL\Type\Definition\ResolveInfo; | ||
use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface; | ||
|
||
/** | ||
* A parameter filled from the current user. | ||
*/ | ||
class InjectUserParameter implements ParameterInterface | ||
{ | ||
/** @var AuthenticationServiceInterface */ | ||
private $authenticationService; | ||
|
||
public function __construct(AuthenticationServiceInterface $authenticationService) | ||
{ | ||
$this->authenticationService = $authenticationService; | ||
} | ||
|
||
/** | ||
* @param array<string, mixed> $args | ||
* @param mixed $context | ||
* | ||
* @return mixed | ||
*/ | ||
public function resolve(?object $source, array $args, $context, ResolveInfo $info) | ||
{ | ||
return $this->authenticationService->getUser(); | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace TheCodingMachine\GraphQLite\Annotations; | ||
|
||
use BadMethodCallException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class HideParameterTest extends TestCase | ||
{ | ||
public function testException(): void | ||
{ | ||
$this->expectException(BadMethodCallException::class); | ||
new HideParameter([]); | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace TheCodingMachine\GraphQLite\Annotations; | ||
|
||
use BadMethodCallException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class InjectUserTest extends TestCase | ||
{ | ||
public function testException(): void | ||
{ | ||
$this->expectException(BadMethodCallException::class); | ||
new InjectUser([]); | ||
} | ||
} |
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