Add friendly name from the provider to ExternalUser, create a class for buttons

master
Lars Vierbergen 7 years ago
parent 4776b9075d
commit 29b35e57b0
  1. 26
      Entity/ExternalUser.php
  2. 43
      Event/LoginButtonEvent.php
  3. 10
      EventListener/LoginTemplateEventListener.php
  4. 1
      Resources/config/services.xml
  5. 42
      Resources/migrations/VersionAuthserverExternalAccount20170829111009.php
  6. 6
      Resources/views/Login/button.html.twig
  7. 7
      Resources/views/Login/buttons.html.twig
  8. 140
      ValueObject/Button.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;
}
}

@ -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;

@ -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();

@ -25,6 +25,7 @@
<services>
<service class="vierbergenlars\AuthserverExternalAccountBundle\EventListener\LoginTemplateEventListener">
<argument type="service" id="event_dispatcher" />
<argument type="service" id="router" />
<tag name="kernel.event_subscriber" />
</service>
</services>

@ -0,0 +1,42 @@
<?php
/**
* Authserver, an OAuth2-based single-signon authentication provider written in PHP.
*
* Copyright (C) $today.date Lars Vierbergen
*
* his program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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');
}
}

@ -0,0 +1,6 @@
<a class="btn btn-{{ button.style }} btn-block" href="{{ button.url }}">
{% if button.icon %}
{{ icon(button.icon) }}
{% endif %}
{{ button.label }}
</a>

@ -1,10 +1,5 @@
<div class="panel-body">
{% for button in buttons %}
<a class="btn btn-{{ button.style }} btn-block" href="{{ button.url?button.url:url(button.route) }}">
{% if button.icon %}
{{ icon(button.icon) }}
{% endif %}
{{ button.label }}
</a>
{% include 'AuthserverExternalAccountBundle:Login:button.html.twig' with {button: button} only %}
{% endfor %}
</div>

@ -0,0 +1,140 @@
<?php
/**
* Authserver, an OAuth2-based single-signon authentication provider written in PHP.
*
* Copyright (C) $today.date Lars Vierbergen
*
* his program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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;
}
}