View source
<?php
namespace Drupal\simplesamlphp_auth\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Url;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\simplesamlphp_auth\Service\SimplesamlphpAuthManager;
use Drupal\Core\Config\ConfigFactoryInterface;
class SimplesamlphpAuthBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $simplesamlAuth;
protected $config;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('simplesamlphp_auth.manager'), $container
->get('config.factory'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, SimplesamlphpAuthManager $simplesaml_auth, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->simplesamlAuth = $simplesaml_auth;
$this->config = $config_factory
->get('simplesamlphp_auth.settings');
}
public function build() {
$content = [
'#title' => $this
->t('SimpleSAMLphp Auth Status'),
'#cache' => [
'contexts' => [
'user',
],
'tags' => $this->config
->getCacheTags(),
],
];
if ($this->simplesamlAuth
->isActivated()) {
if ($this->simplesamlAuth
->isAuthenticated()) {
$content['#markup'] = $this
->t('Logged in as %authname<br /><a href=":logout">Log out</a>', [
'%authname' => $this->simplesamlAuth
->getAuthname(),
':logout' => Url::fromRoute('user.logout')
->toString(),
]);
}
else {
$label = $this->config
->get('login_link_display_name');
$login_link = [
'#title' => $label,
'#type' => 'link',
'#url' => Url::fromRoute('simplesamlphp_auth.saml_login'),
'#attributes' => [
'title' => $label,
'class' => [
'simplesamlphp-auth-login-link',
],
],
];
$content['link'] = $login_link;
}
}
else {
$content['#markup'] = $this
->t('Warning: SimpleSAMLphp is not activated.');
}
return $content;
}
}