Skip to content

Commit

Permalink
Merge pull request #489 from jhedstrom/488-backend-authentication
Browse files Browse the repository at this point in the history
Authenticate user in the backend bootstrap process on login.
  • Loading branch information
jhedstrom authored Dec 31, 2019
2 parents c981fbb + e000511 commit 609345a
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 51 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
* [#488](https://github.com/jhedstrom/drupalextension/issues/488) Authenticate user in the backend bootstrap process on login.
## [4.0.1] 2019-10-08
### Fixed
* [#552](https://github.com/jhedstrom/drupalextension/issue/552) Remove hard-coded symfony/event-dispatcher requirement.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"behat/mink-extension": "~2.0",
"behat/mink-goutte-driver": "~1.0",
"behat/mink-selenium2-driver": "~1.1",
"drupal/drupal-driver": "^2.0.0",
"drupal/drupal-driver": "^2.1.0",
"symfony/browser-kit": "^3.4",
"symfony/dependency-injection": "~3.0",
"symfony/translation": "^3.4"
Expand Down
15 changes: 15 additions & 0 deletions features/backend_login.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@api @d8
Feature: Backend login/logout
In order to prove that backend authentication is working
As a developer
I need to utilize the backend login functionality of the authentication manager

Scenario: Log a user in on the backend
Given I am logged in as a user with the "authenticated user" role
Then I should be logged in on the backend

Scenario: Logout on the backend
Given I am logged in as a user with the "authenticated user" role
And I am logged in on the backend
When I log out
Then I should be logged out on the backend
37 changes: 37 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,43 @@ public function assertAuthenticatedByUsernameAndPassword($name, $password) {
$this->login($user);
}

/**
* Verifies a user is logged in on the backend.
*
* @Then I should be logged in on the backend
* @Then I am logged in on the backend
*/
public function assertBackendLogin()
{
if (!$user = $this->getUserManager()->getCurrentUser()) {
throw new \LogicException('No current user in the user manager.');
}
if (!$account = \Drupal::entityTypeManager()->getStorage('user')->load($user->uid)) {
throw new \LogicException('No user found in the system.');
}
if (!$account->id()) {
throw new \LogicException('Current user is anonymous.');
}
if ($account->id() != \Drupal::currentUser()->id()) {
throw new \LogicException('User logged in on the backend does not match current user.');
}
}

/**
* Verifies there is no user logged in on the backend.
*
* @Then I should be logged out on the backend
*/
public function assertBackendLoggedOut()
{
if ($this->getUserManager()->getCurrentUser()) {
throw new \LogicException('User is still logged in in the manager.');
}
if (!\Drupal::currentUser()->isAnonymous()) {
throw new \LogicException('User is still logged in on the backend.');
}
}

/**
* From here down is the Behat FeatureContext.
*
Expand Down
68 changes: 19 additions & 49 deletions src/Drupal/DrupalDriverManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* Drupal driver manager.
*/
class DrupalDriverManager
class DrupalDriverManager implements DrupalDriverManagerInterface
{

/**
Expand Down Expand Up @@ -50,33 +50,18 @@ public function __construct(array $drivers = array())
}
}

/**
* Register a new driver.
*
* @param string $name
* Driver name.
* @param \Drupal\Driver\DriverInterface $driver
* An instance of a DriverInterface.
*/
/**
* {@inheritdoc}
*/
public function registerDriver($name, DriverInterface $driver)
{
$name = strtolower($name);
$this->drivers[$name] = $driver;
}

/**
* Return a registered driver by name, or the default driver.
*
* @param string $name
* The name of the driver to return. If omitted the default driver is
* returned.
*
* @return \Drupal\Driver\DriverInterface
* The requested driver.
*
* @throws \InvalidArgumentException
* Thrown when the requested driver is not registered.
*/
/**
* {@inheritdoc}
*/
public function getDriver($name = null)
{
$name = strtolower($name) ?: $this->defaultDriverName;
Expand All @@ -99,15 +84,9 @@ public function getDriver($name = null)
return $driver;
}

/**
* Set the default driver name.
*
* @param string $name
* Default driver name to set.
*
* @throws \InvalidArgumentException
* Thrown when the driver is not registered.
*/
/**
* {@inheritdoc}
*/
public function setDefaultDriverName($name)
{
$name = strtolower($name);
Expand All @@ -119,34 +98,25 @@ public function setDefaultDriverName($name)
$this->defaultDriverName = $name;
}

/**
* Returns all registered drivers.
*
* @return \Drupal\Driver\DriverInterface[]
* An array of drivers.
*/
/**
* {@inheritdoc}
*/
public function getDrivers()
{
return $this->drivers;
}

/**
* Sets the Behat Environment.
*
* @param \Behat\Testwork\Environment\Environment $environment
* The Behat Environment to set.
*/
/**
* {@inheritdoc}
*/
public function setEnvironment(Environment $environment)
{
$this->environment = $environment;
}

/**
* Returns the Behat Environment.
*
* @return \Behat\Testwork\Environment\Environment
* The Behat Environment.
*/
/**
* {@inheritdoc}
*/
public function getEnvironment()
{
return $this->environment;
Expand Down
69 changes: 69 additions & 0 deletions src/Drupal/DrupalDriverManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Drupal;

use Behat\Testwork\Environment\Environment;
use Drupal\Driver\DriverInterface;

interface DrupalDriverManagerInterface
{
/**
* Register a new driver.
*
* @param string $name
* Driver name.
* @param \Drupal\Driver\DriverInterface $driver
* An instance of a DriverInterface.
*/
public function registerDriver($name, DriverInterface $driver);

/**
* Return a registered driver by name, or the default driver.
*
* @param string $name
* The name of the driver to return. If omitted the default driver is
* returned.
*
* @return \Drupal\Driver\DriverInterface
* The requested driver.
*
* @throws \InvalidArgumentException
* Thrown when the requested driver is not registered.
*/
public function getDriver($name = null);

/**
* Set the default driver name.
*
* @param string $name
* Default driver name to set.
*
* @throws \InvalidArgumentException
* Thrown when the driver is not registered.
*/
public function setDefaultDriverName($name);

/**
* Returns all registered drivers.
*
* @return \Drupal\Driver\DriverInterface[]
* An array of drivers.
*/
public function getDrivers();

/**
* Sets the Behat Environment.
*
* @param \Behat\Testwork\Environment\Environment $environment
* The Behat Environment to set.
*/
public function setEnvironment(Environment $environment);

/**
* Returns the Behat Environment.
*
* @return \Behat\Testwork\Environment\Environment
* The Behat Environment.
*/
public function getEnvironment();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Behat\Mink\Exception\DriverException;
use Behat\Mink\Mink;
use Drupal\Driver\AuthenticationDriverInterface;
use Drupal\DrupalDriverManagerInterface;
use Drupal\DrupalExtension\DrupalParametersTrait;
use Drupal\DrupalExtension\MinkAwareTrait;

Expand All @@ -23,18 +25,29 @@ class DrupalAuthenticationManager implements DrupalAuthenticationManagerInterfac
*/
protected $userManager;

/**
* The active driver.
*
* @var \Drupal\DrupalExtension\DrupalDriverManagerInterface
*/
protected $driverManager;

/**
* Constructs a DrupalAuthenticationManager object.
*
* @param \Behat\Mink\Mink $mink
* The Mink sessions manager.
* @param \Drupal\DrupalExtension\Manager\DrupalUserManagerInterface $drupalUserManager
* The Drupal user manager.
* @param DrupalDriverManagerInterface $driverManager
* @param array $minkParameters
* @param array $drupalParameters
*/
public function __construct(Mink $mink, DrupalUserManagerInterface $drupalUserManager, array $minkParameters, array $drupalParameters)
public function __construct(Mink $mink, DrupalUserManagerInterface $drupalUserManager, DrupalDriverManagerInterface $driverManager, array $minkParameters, array $drupalParameters)
{
$this->setMink($mink);
$this->userManager = $drupalUserManager;
$this->driverManager = $driverManager;
$this->setMinkParameters($minkParameters);
$this->setDrupalParameters($drupalParameters);
}
Expand Down Expand Up @@ -68,6 +81,11 @@ public function logIn(\stdClass $user)
}

$this->userManager->setCurrentUser($user);

// Log the user in on the backend if possible.
if ($this->driverManager->getDriver() instanceof AuthenticationDriverInterface) {
$this->driverManager->getDriver()->login($user);
}
}

/**
Expand All @@ -77,6 +95,11 @@ public function logout()
{
$this->getSession()->visit($this->locatePath($this->getDrupalText('logout_url')));
$this->userManager->setCurrentUser(false);

// Log the user out on the backend if possible.
if ($this->driverManager->getDriver() instanceof AuthenticationDriverInterface) {
$this->driverManager->getDriver()->logout();
}
}

/**
Expand Down Expand Up @@ -136,5 +159,10 @@ public function fastLogout()
$session->reset();
}
$this->userManager->setCurrentUser(false);

// Log the user out on the backend if possible.
if ($this->driverManager->getDriver() instanceof AuthenticationDriverInterface) {
$this->driverManager->getDriver()->logout();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ services:
arguments:
- "@mink"
- "@drupal.user_manager"
- "@drupal.drupal"
- "%mink.parameters%"
- "%drupal.parameters%"
drupal.user_manager:
Expand Down

0 comments on commit 609345a

Please sign in to comment.