generated from rich-id/bundle-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #3 from rich-id/feature/t48058
Feature/t48058
- Loading branch information
Showing
7 changed files
with
187 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: Tests | ||
on: [push, pull_request] | ||
on: [pull_request] | ||
|
||
jobs: | ||
build-and-test: | ||
|
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,16 @@ | ||
== Events | ||
|
||
Various events are available to let you react to every action. Every event is prefixed with the namespace `RichId\TermsModuleBundle\Domain\Event\`. | ||
|
||
[cols="1,1"] | ||
|=== | ||
| Event | Description | ||
|
||
| TermsPublishedEvent | Triggered on publication | ||
| TermsSignedEvent | Triggered when a version is signed/not signed/skipped by a subject | ||
| TermsUnpublishedEvent | Triggered on un-publication | ||
| TermsVersionCreatedEvent | Triggered when a new version is created | ||
| TermsVersionDeletedEvent | Triggered when a draft version is deleted | ||
| TermsVersionEnabledEvent | Triggered when a version is published | ||
| TermsVersionUpdatedEvent | Triggered when a version is updated | ||
|=== |
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,108 @@ | ||
== Security | ||
|
||
=== Guards | ||
|
||
You may want to control who can sign the terms. This can be easily done using the `TermsGuardInterface` that will previously check if the subject can sign it. | ||
|
||
The following example protect the terms with the slug `your_slug` to be signed by anybody but a User. | ||
|
||
[source,php] | ||
---- | ||
final class RandomGuard implements TermsGuardInterface | ||
{ | ||
public function supports(string $slug, TermsSubjectInterface $subject): bool | ||
{ | ||
return $slug === 'your_slug'; | ||
} | ||
public function check(string $slug, TermsSubjectInterface $subject): bool | ||
{ | ||
return $subject instanceof User; | ||
} | ||
} | ||
---- | ||
|
||
|
||
=== Protecting a route | ||
|
||
Since it can be hard to create a generic voter to check if a subject has sign the terms, the best way is to create a custom voter for each case. | ||
|
||
The following example exposes a voter that checks if the User has signed a route. | ||
|
||
[source, php] | ||
---- | ||
final class UserTermsVoter extends Voter | ||
{ | ||
/** @var HasSignedLastTermsVersion **/ | ||
protected $hasSignedLastTermsVersion; | ||
/** @var Security | ||
protected $security; | ||
// ... | ||
protected function supports($attribute, $subject): bool | ||
{ | ||
$user = $this->security->getUser(); | ||
return $user instanceof User | ||
&& $user instanceof TermsSubjectInterface | ||
&& $attribute === 'HAS_USER_SIGNED_TERMS'; | ||
} | ||
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool | ||
{ | ||
return ($this->hasSignedLastTermsVersion)('your_slug', $subject); | ||
} | ||
} | ||
---- | ||
|
||
Using it in a controller is now straight forward ! | ||
|
||
[source, php] | ||
---- | ||
final class RandomRoute extends AbstractController | ||
{ | ||
/** | ||
* @IsGranted("HAS_USER_SIGNED_TERMS") | ||
*/ | ||
public function __invoke(): Response | ||
{ | ||
return new Response('Yay!'); | ||
} | ||
} | ||
---- | ||
|
||
=== Redirection after a voter | ||
|
||
Since the voter blocks the access of the route, this may be a brutal behaviour. A redirection to the signing page of the appropriate terms would be smoother. For this, a listener that catches when an `AccessDeniedException` is thrown is the best approach. | ||
|
||
The following example redirects the User if the voter denies the access. | ||
|
||
[source, php] | ||
---- | ||
final class RedirectUserWhenTermsNotSignedListener | ||
{ | ||
/** @var Security */ | ||
private $security; | ||
/** @var GenerateSigningRoute */ | ||
private $generateSigningRoute; | ||
// ... | ||
public function __invoke(ExceptionEvent $event): void | ||
{ | ||
$user = $this->security->getUserCompany(); | ||
$exception = $event->getThrowable(); | ||
if (!$exception instanceof AccessDeniedException || !user instanceof User) { | ||
return; | ||
} | ||
$route = ($this->generateSigningRoute)('your_slug', $user); | ||
$response = new RedirectResposne($route); | ||
$event->setResponse($response); | ||
} | ||
} | ||
---- |
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