Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHS-5811: Add "logout" button to sites #21

Merged
merged 8 commits into from
Dec 3, 2024
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "su-sws/stanford_samlauth",
"name": "fourkitchens/stanford_samlauth",
"type": "drupal-custom-module",
"license": "GPL-2.0-or-later",
"minimum-stability": "dev",
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin/Block/SamlLoginBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* @Block(
* id = "stanford_samlauth_login_block",
codechefmarc marked this conversation as resolved.
Show resolved Hide resolved
* admin_label = @Translation("SAML SUNetID Block")
* admin_label = @Translation("SAML SUNetID Login Block")
* )
*/
class SamlLoginBlock extends BlockBase implements ContainerFactoryPluginInterface {
Expand Down
129 changes: 129 additions & 0 deletions src/Plugin/Block/SamlLogoutBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace Drupal\stanford_samlauth\Plugin\Block;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Provides a 'Saml Logout Block' block.
*
* @Block(
* id = "stanford_samlauth_logout_block",
* admin_label = @Translation("SAML SUNetID Logout Block")
* )
*/
class SamlLogoutBlock extends BlockBase implements ContainerFactoryPluginInterface {
codechefmarc marked this conversation as resolved.
Show resolved Hide resolved

/**
* Current uri the block is displayed on.
*
* @var string
*/
protected $currentUri;

/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('request_stack')
);
}

/**
* Block constructor.
*
* @param array $configuration
* Configuration settings.
* @param string $plugin_id
* Block machine name.
* @param array $plugin_definition
* Plugin definition.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* Current request stack object.
*/
public function __construct(array $configuration, string $plugin_id, array $plugin_definition, RequestStack $requestStack) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUri = $requestStack->getCurrentRequest()?->getPathInfo();
}

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return ['link_text' => 'SUNetID Logout'] + parent::defaultConfiguration();
}

/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['link_text'] = [
'#type' => 'textfield',
'#title' => $this->t('SUNetID log-out link text'),
'#description' => $this->t('Add text to show a link for authenticated users.'),
'#default_value' => $this->configuration['link_text'],
'#required' => TRUE,
];
return $form;
}

/**
* {@inheritDoc}
*/
public function getCacheContexts() {
$context = parent::getCacheContexts();
// Make the block cache different for each page since the login link has a
// destination parameter.
return Cache::mergeContexts($context, ['url.path']);
}

/**
* {@inheritdoc}
*/
public function access(AccountInterface $account, $return_as_object = FALSE) {
$access = AccessResult::allowedIf($account->isAuthenticated());
return $return_as_object ? $access : $access->isAllowed();
}

/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['link_text'] = $form_state->getValue('link_text');
}

/**
* {@inheritdoc}
*/
public function build() {
$url = Url::fromRoute('user.logout', ['destination' => $this->currentUri]);
$build = [];
$build['logout'] = [
'#type' => 'html_tag',
'#tag' => 'a',
'#value' => $this->configuration['link_text'],
'#attributes' => [
'rel' => 'nofollow',
'href' => $url->toString(),
'class' => [
'su-button',
'decanter-button',
],
],
];
return $build;
}

}