Skip to content
Snippets Groups Projects
Commit 423f51c1 authored by Atul Ghate's avatar Atul Ghate Committed by Stephan Huber
Browse files

Issue #3331408 by SolimanHarkas, atul ghate, Akram Khan: Fix the issues reported by phpcs

parent efdac644
No related branches found
No related tags found
No related merge requests found
# Externalauth Gitlab OAuth2 connector
Externalauth Gitlab OAuth2 connector will allow users of your site to authenticate against a running gitlab instance via OAuth2. The module will not register new users, just map existing users via the email-address.
Externalauth Gitlab OAuth2 connector will allow users of your site to
authenticate against a running gitlab instance via OAuth2. The module will not
register new users, just map existing users via the email-address.
## Dependencies:
......@@ -17,5 +19,5 @@ Externalauth Gitlab OAuth2 connector will allow users of your site to authentica
## Usage
* The module will add a new local task at `/user`
* When visiting that link the user will gets redirected to the gitlab instance, if the user authenticates successfully, it will get authenticated in Drupal.
* When visiting that link the user will gets redirected to the gitlab instance,
if the user authenticates successfully, it will get authenticated in Drupal.
<?php /** @noinspection PhpDocSignatureInspection */
<?php
namespace Drupal\externalauth_gitlab\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\PageCache\ResponsePolicy\KillSwitch;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
......@@ -15,7 +17,7 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
/**
* Class LoginController.
* {@inheritdoc}
*/
class LoginController extends ControllerBase {
......@@ -35,17 +37,35 @@ class LoginController extends ControllerBase {
*/
private $tempstore;
/**
* The config factory object.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The page cache disabling policy.
*
* @var \Drupal\Core\PageCache\ResponsePolicy\KillSwitch
*/
protected $pageCacheKillSwitch;
/**
* Constructs a new LoginController object.
*/
public function __construct(
ExternalAuthInterface $external_auth,
PrivateTempStoreFactory $temp_store_factory,
EntityTypeManagerInterface $entity_type_manager
EntityTypeManagerInterface $entity_type_manager,
ConfigFactoryInterface $config_factory,
KillSwitch $killer_switch
) {
$this->externalauth = $external_auth;
$this->tempstore = $temp_store_factory->get('externalauth_gitlab');
$this->entityTypeManager = $entity_type_manager;
$this->configFactory = $config_factory;
$this->pageCacheKillSwitch = $killer_switch;
}
/**
......@@ -55,7 +75,9 @@ class LoginController extends ControllerBase {
return new static(
$container->get('externalauth.externalauth'),
$container->get('tempstore.private'),
$container->get('entity_type.manager')
$container->get('entity_type.manager'),
$container->get('config.factory'),
$container->get('page_cache_kill_switch'),
);
}
......@@ -63,13 +85,13 @@ class LoginController extends ControllerBase {
* Try to login via gitlab.
*/
public function login(Request $request) {
$config = $config = \Drupal::config('externalauth_gitlab.settings');
$config = $this->configFactory->get('externalauth_gitlab.settings');
if (empty($config->get('client_id')) || empty($config->get('client_secret')) || empty($config->get('domain'))) {
throw new \InvalidArgumentException($this->t('Please finish setup of module first, missing config!'));
throw new \InvalidArgumentException('Please finish setup of module first, missing config!');
}
// Drupal caches redirects, which is not safe!
\Drupal::service('page_cache_kill_switch')->trigger();
$this->pageCacheKillSwitch->trigger();
$provider = new Gitlab([
'clientId' => $config->get('client_id'),
......@@ -97,7 +119,6 @@ class LoginController extends ControllerBase {
}
elseif (!$state || ($state !== $this->tempstore->get(self::OAUTH_2_STATE))) {
$saved_state = $this->tempstore->get(self::OAUTH_2_STATE);
$this->tempstore->delete(self::OAUTH_2_STATE);
throw new \RuntimeException('Invalid state');
}
......@@ -168,9 +189,7 @@ class LoginController extends ControllerBase {
);
}
else {
throw new \RuntimeException($this->t(
'Could not find gitlab user! Please ask your admin for help!'
));
throw new \RuntimeException('Could not find gitlab user! Please ask your admin for help!');
}
}
......
......@@ -2,14 +2,52 @@
namespace Drupal\externalauth_gitlab\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBaseTrait;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class SettingsForm.
* {@inheritdoc}
*/
class SettingsForm extends FormBase {
use ConfigFormBaseTrait;
/**
* Config settings.
*
* @var string
*/
const SETTINGS = 'externalauth_gitlab.settings';
/**
* The config factory object.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a SettingsForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* A configuration factory instance.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
......@@ -17,11 +55,20 @@ class SettingsForm extends FormBase {
return 'settings_form';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
static::SETTINGS,
];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = \Drupal::config('externalauth_gitlab.settings');
$config = $this->config(static::SETTINGS);
$form['client_id'] = [
'#type' => 'textfield',
......@@ -48,19 +95,12 @@ class SettingsForm extends FormBase {
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Display result.
$config = \Drupal::service('config.factory')->getEditable('externalauth_gitlab.settings');
$config = $this->configFactory->getEditable('externalauth_gitlab.settings');
$keys = ['client_id', 'client_secret', 'domain'];
foreach ($keys as $key) {
$config->set($key, $form_state->getValue($key));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment