Skip to content

Commit

Permalink
Provide a method to directly authenticate on Drupal 8.
Browse files Browse the repository at this point in the history
- Fixes #186
  • Loading branch information
jhedstrom committed May 17, 2018
1 parent acf9c1b commit 5340b42
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
6 changes: 6 additions & 0 deletions spec/Drupal/Driver/Cores/Drupal8Spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\Component\Utility\Random;

use Drupal\Driver\Cores\CoreAuthenticationInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

Expand All @@ -23,4 +24,9 @@ function it_should_return_a_random_generator()
{
$this->getRandom()->shouldBeAnInstanceOf('Drupal\Component\Utility\Random');
}

function it_is_an_auth_core()
{
$this->shouldBeAnInstanceOf(CoreAuthenticationInterface::class);
}
}
20 changes: 20 additions & 0 deletions src/Drupal/Driver/AuthenticationDriverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Drupal\Driver;

/**
* Indicates the driver can log users in and out on the backend.
*/
interface AuthenticationDriverInterface {

/**
* Logs the user in.
*/
public function login(\stdClass $user);

/**
* Logs the user out.
*/
public function logout();

}
20 changes: 20 additions & 0 deletions src/Drupal/Driver/Cores/CoreAuthenticationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Drupal\Driver\Cores;

/**
* The core has the ability to directly authenticate users.
*/
interface CoreAuthenticationInterface {

/**
* Logs a user in.
*/
public function login(\stdClass $user);

/**
* Logs a user out.
*/
public function logout();

}
25 changes: 24 additions & 1 deletion src/Drupal/Driver/Cores/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Request;

/**
* Drupal 8 core.
*/
class Drupal8 extends AbstractCore {
class Drupal8 extends AbstractCore implements CoreAuthenticationInterface {

/**
* Tracks original configuration values.
Expand Down Expand Up @@ -612,4 +613,26 @@ protected function stopCollectingMailSystemMail() {
}
}

/**
* {@inheritdoc}
*/
public function login(\stdClass $user) {
$account = User::load($user->uid);
\Drupal::service('account_switcher')->switchTo($account);
}

/**
* {@inheritdoc}
*/
public function logout() {
try {
while (TRUE) {
\Drupal::service('account_switcher')->switchBack();
}
}
catch (\RuntimeException $e) {
// No more users are logged in.
}
}

}
21 changes: 20 additions & 1 deletion src/Drupal/Driver/DrupalDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace Drupal\Driver;

use Drupal\Driver\Cores\CoreAuthenticationInterface;
use Drupal\Driver\Exception\BootstrapException;

use Behat\Behat\Tester\Exception\PendingException;

/**
* Fully bootstraps Drupal and uses native API calls.
*/
class DrupalDriver implements DriverInterface, SubDriverFinderInterface {
class DrupalDriver implements DriverInterface, SubDriverFinderInterface, AuthenticationDriverInterface {

/**
* Track whether Drupal has been bootstrapped.
Expand Down Expand Up @@ -368,4 +369,22 @@ public function sendMail($body, $subject, $to, $langcode) {
return $this->getCore()->sendMail($body, $subject, $to, $langcode);
}

/**
* {@inheritdoc}
*/
public function login(\stdClass $user) {
if ($this->getCore() instanceof CoreAuthenticationInterface) {
$this->getCore()->login($user);
}
}

/**
* {@inheritdoc}
*/
public function logout() {
if ($this->getCore() instanceof CoreAuthenticationInterface) {
$this->getCore()->logout();
}
}

}

0 comments on commit 5340b42

Please sign in to comment.