Skip to content

Commit e903afc

Browse files
authored
Merge pull request jhedstrom#187 from jhedstrom/186-backend-auth
Provide a method to directly authenticate on Drupal 8.
2 parents 92af5ee + bca0746 commit e903afc

File tree

6 files changed

+91
-2
lines changed

6 files changed

+91
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
9+
### Added
10+
* [#186](https://github.com/jhedstrom/DrupalDriver/issues/168) Provide a method to directly authenticate on Drupal 8.
911
## [2.0.0] 2019-09-27
1012
## [2.0.0 rc1] 2019-07-25
1113
### Changed

spec/Drupal/Driver/Cores/Drupal8Spec.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Drupal\Component\Utility\Random;
66

7+
use Drupal\Driver\Cores\CoreAuthenticationInterface;
78
use PhpSpec\ObjectBehavior;
89
use Prophecy\Argument;
910

@@ -23,4 +24,9 @@ function it_should_return_a_random_generator()
2324
{
2425
$this->getRandom()->shouldBeAnInstanceOf('Drupal\Component\Utility\Random');
2526
}
27+
28+
function it_is_an_auth_core()
29+
{
30+
$this->shouldBeAnInstanceOf(CoreAuthenticationInterface::class);
31+
}
2632
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Drupal\Driver;
4+
5+
/**
6+
* Indicates the driver can log users in and out on the backend.
7+
*/
8+
interface AuthenticationDriverInterface {
9+
10+
/**
11+
* Logs the user in.
12+
*/
13+
public function login(\stdClass $user);
14+
15+
/**
16+
* Logs the user out.
17+
*/
18+
public function logout();
19+
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Drupal\Driver\Cores;
4+
5+
/**
6+
* The core has the ability to directly authenticate users.
7+
*/
8+
interface CoreAuthenticationInterface {
9+
10+
/**
11+
* Logs a user in.
12+
*/
13+
public function login(\stdClass $user);
14+
15+
/**
16+
* Logs a user out.
17+
*/
18+
public function logout();
19+
20+
}

src/Drupal/Driver/Cores/Drupal8.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* Drupal 8 core.
2222
*/
23-
class Drupal8 extends AbstractCore {
23+
class Drupal8 extends AbstractCore implements CoreAuthenticationInterface {
2424

2525
/**
2626
* Tracks original configuration values.
@@ -632,6 +632,28 @@ protected function stopCollectingMailSystemMail() {
632632
}
633633
}
634634

635+
/**
636+
* {@inheritdoc}
637+
*/
638+
public function login(\stdClass $user) {
639+
$account = User::load($user->uid);
640+
\Drupal::service('account_switcher')->switchTo($account);
641+
}
642+
643+
/**
644+
* {@inheritdoc}
645+
*/
646+
public function logout() {
647+
try {
648+
while (TRUE) {
649+
\Drupal::service('account_switcher')->switchBack();
650+
}
651+
}
652+
catch (\RuntimeException $e) {
653+
// No more users are logged in.
654+
}
655+
}
656+
635657
/**
636658
* Store the original value for a piece of configuration.
637659
*

src/Drupal/Driver/DrupalDriver.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace Drupal\Driver;
44

5+
use Drupal\Driver\Cores\CoreAuthenticationInterface;
56
use Drupal\Driver\Exception\BootstrapException;
67

78
use Behat\Behat\Tester\Exception\PendingException;
89

910
/**
1011
* Fully bootstraps Drupal and uses native API calls.
1112
*/
12-
class DrupalDriver implements DriverInterface, SubDriverFinderInterface {
13+
class DrupalDriver implements DriverInterface, SubDriverFinderInterface, AuthenticationDriverInterface {
1314

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

372+
/**
373+
* {@inheritdoc}
374+
*/
375+
public function login(\stdClass $user) {
376+
if ($this->getCore() instanceof CoreAuthenticationInterface) {
377+
$this->getCore()->login($user);
378+
}
379+
}
380+
381+
/**
382+
* {@inheritdoc}
383+
*/
384+
public function logout() {
385+
if ($this->getCore() instanceof CoreAuthenticationInterface) {
386+
$this->getCore()->logout();
387+
}
388+
}
389+
371390
}

0 commit comments

Comments
 (0)