From 29b35e57b076718ad7a96a4cf2604181cee993db Mon Sep 17 00:00:00 2001 From: Lars Vierbergen Date: Tue, 29 Aug 2017 13:46:46 +0200 Subject: [PATCH] Add friendly name from the provider to ExternalUser, create a class for buttons --- Entity/ExternalUser.php | 26 ++++ Event/LoginButtonEvent.php | 43 +++++- EventListener/LoginTemplateEventListener.php | 10 +- Resources/config/services.xml | 1 + ...uthserverExternalAccount20170829111009.php | 42 ++++++ Resources/views/Login/button.html.twig | 6 + Resources/views/Login/buttons.html.twig | 7 +- ValueObject/Button.php | 140 ++++++++++++++++++ 8 files changed, 260 insertions(+), 15 deletions(-) create mode 100644 Resources/migrations/VersionAuthserverExternalAccount20170829111009.php create mode 100644 Resources/views/Login/button.html.twig create mode 100644 ValueObject/Button.php diff --git a/Entity/ExternalUser.php b/Entity/ExternalUser.php index a8388cb..58f3758 100644 --- a/Entity/ExternalUser.php +++ b/Entity/ExternalUser.php @@ -60,6 +60,13 @@ class ExternalUser */ private $provider_ref; + /** + * @ORM\Column(type="string") + * @Gedmo\Versioned + * @var string + */ + private $providerFriendlyName; + /** * @return int */ @@ -124,4 +131,23 @@ class ExternalUser return $this; } + + /** + * @return string + */ + public function getProviderFriendlyName() + { + return $this->providerFriendlyName?:('Ref: '.$this->getProviderRef()); + } + + /** + * @param string $providerFriendlyName + * @return ExternalUser + */ + public function setProviderFriendlyName($providerFriendlyName) + { + $this->providerFriendlyName = $providerFriendlyName; + + return $this; + } } diff --git a/Event/LoginButtonEvent.php b/Event/LoginButtonEvent.php index 00b4c86..ebc0754 100644 --- a/Event/LoginButtonEvent.php +++ b/Event/LoginButtonEvent.php @@ -22,21 +22,50 @@ namespace vierbergenlars\AuthserverExternalAccountBundle\Event; use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use vierbergenlars\AuthserverExternalAccountBundle\ValueObject\Button; class LoginButtonEvent extends Event { + /** + * @var Button[] + */ private $buttons = []; - public function addButton($options) + /** + * @var UrlGeneratorInterface + */ + private $urlGenerator; + + /** + * LoginButtonEvent constructor. + * + * @param UrlGeneratorInterface $urlGenerator + */ + public function __construct(UrlGeneratorInterface $urlGenerator) + { + $this->urlGenerator = $urlGenerator; + } + + + /** + * @param array|Button $button + */ + public function addButton($button) { - $this->buttons[] = $options + [ - 'style' => 'default', - 'icon' => null, - 'url' => null, - 'route' => null, - ]; + if(!($button instanceof Button)) { + if(isset($button['route'])) { + $button['url'] = $this->urlGenerator->generate($button['route']); + unset($button['route']); + } + $button = new Button($button); + } + $this->buttons[] = $button; } + /** + * @return Button[] + */ public function getButtons() { return $this->buttons; diff --git a/EventListener/LoginTemplateEventListener.php b/EventListener/LoginTemplateEventListener.php index 65f44ba..efeedc7 100644 --- a/EventListener/LoginTemplateEventListener.php +++ b/EventListener/LoginTemplateEventListener.php @@ -26,6 +26,7 @@ use App\Event\TemplateEvent; use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use vierbergenlars\AuthserverExternalAccountBundle\Event\LoginButtonEvent; use vierbergenlars\AuthserverExternalAccountBundle\ExternalAccountEvents; @@ -35,11 +36,16 @@ class LoginTemplateEventListener implements EventSubscriberInterface * @var EventDispatcherInterface */ private $eventDispatcher; + /** + * @var UrlGeneratorInterface + */ + private $urlGenerator; - public function __construct(EventDispatcherInterface $eventDispatcher) + public function __construct(EventDispatcherInterface $eventDispatcher, UrlGeneratorInterface $urlGenerator) { $this->eventDispatcher = $eventDispatcher; + $this->urlGenerator = $urlGenerator; } public static function getSubscribedEvents() @@ -51,7 +57,7 @@ class LoginTemplateEventListener implements EventSubscriberInterface public function onLoginViewBody(TemplateEvent $event) { - $loginButtonEvent = new LoginButtonEvent(); + $loginButtonEvent = new LoginButtonEvent($this->urlGenerator); $this->eventDispatcher->dispatch(ExternalAccountEvents::LOGIN_BUTTON, $loginButtonEvent); $buttons = $loginButtonEvent->getButtons(); diff --git a/Resources/config/services.xml b/Resources/config/services.xml index ebed173..05b7eb7 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -25,6 +25,7 @@ + diff --git a/Resources/migrations/VersionAuthserverExternalAccount20170829111009.php b/Resources/migrations/VersionAuthserverExternalAccount20170829111009.php new file mode 100644 index 0000000..57dfbaf --- /dev/null +++ b/Resources/migrations/VersionAuthserverExternalAccount20170829111009.php @@ -0,0 +1,42 @@ +. + */ + +namespace Application\Migrations; + +use Doctrine\DBAL\Migrations\AbstractMigration; +use Doctrine\DBAL\Schema\Schema; + +class VersionAuthserverExternalAccount20170829111009 extends AbstractMigration +{ + public function up(Schema $schema) + { + $externalUser = $schema->getTable('vierbergenlars_external_account_external_user'); + + $externalUser->addColumn('providerFriendlyName', 'string')->setNotnull(false); + } + + public function down(Schema $schema) + { + $externalUser = $schema->getTable('vierbergenlars_external_account_external_user'); + + $externalUser->dropColumn('providerFriendlyName'); + } +} + diff --git a/Resources/views/Login/button.html.twig b/Resources/views/Login/button.html.twig new file mode 100644 index 0000000..0768874 --- /dev/null +++ b/Resources/views/Login/button.html.twig @@ -0,0 +1,6 @@ + + {% if button.icon %} + {{ icon(button.icon) }} + {% endif %} + {{ button.label }} + diff --git a/Resources/views/Login/buttons.html.twig b/Resources/views/Login/buttons.html.twig index 27397cf..c68a1ae 100644 --- a/Resources/views/Login/buttons.html.twig +++ b/Resources/views/Login/buttons.html.twig @@ -1,10 +1,5 @@
{% for button in buttons %} - - {% if button.icon %} - {{ icon(button.icon) }} - {% endif %} - {{ button.label }} - + {% include 'AuthserverExternalAccountBundle:Login:button.html.twig' with {button: button} only %} {% endfor %}
diff --git a/ValueObject/Button.php b/ValueObject/Button.php new file mode 100644 index 0000000..a7fb044 --- /dev/null +++ b/ValueObject/Button.php @@ -0,0 +1,140 @@ +. + */ + +namespace vierbergenlars\AuthserverExternalAccountBundle\ValueObject; + + +class Button +{ + /** + * @var string + */ + private $label; + + /** + * @var string + */ + private $url; + + /** + * @var string|null + */ + private $icon = null; + + /** + * @var string + */ + private $style = 'default'; + + public function __construct(array $config) + { + $valid = ['label', 'url', 'icon', 'style']; + foreach($config as $k => $v) + { + if(!in_array($k, $valid)) + throw new \LogicException(sprintf('Configuration key "%s" is not valid.', $k)); + $this->{$k} = $v; + } + + $required = ['label', 'url']; + + foreach($required as $k) + { + if(!$this->{$k}) + throw new \LogicException(sprintf('Configuration key "%s" is missing', $k)); + } + } + + /** + * @return string + */ + public function getLabel() + { + return $this->label; + } + + /** + * @param string $label + * @return Button + */ + public function setLabel($label) + { + $this->label = $label; + + return $this; + } + + /** + * @return string + */ + public function getUrl() + { + return $this->url; + } + + /** + * @param string $url + * @return Button + */ + public function setUrl($url) + { + $this->url = $url; + + return $this; + } + + /** + * @return null|string + */ + public function getIcon() + { + return $this->icon; + } + + /** + * @param null|string $icon + * @return Button + */ + public function setIcon($icon) + { + $this->icon = $icon; + + return $this; + } + + /** + * @return string + */ + public function getStyle() + { + return $this->style; + } + + /** + * @param string $style + * @return Button + */ + public function setStyle($style) + { + $this->style = $style; + + return $this; + } +}