Mercurial > hg > isophonics-drupal-site
comparison core/modules/user/src/Authentication/Provider/Cookie.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\user\Authentication\Provider; | |
4 | |
5 use Drupal\Core\Authentication\AuthenticationProviderInterface; | |
6 use Drupal\Core\Database\Connection; | |
7 use Drupal\Core\Session\AccountInterface; | |
8 use Drupal\Core\Session\UserSession; | |
9 use Drupal\Core\Session\SessionConfigurationInterface; | |
10 use Symfony\Component\HttpFoundation\Request; | |
11 use Symfony\Component\HttpFoundation\Session\SessionInterface; | |
12 | |
13 /** | |
14 * Cookie based authentication provider. | |
15 */ | |
16 class Cookie implements AuthenticationProviderInterface { | |
17 | |
18 /** | |
19 * The session configuration. | |
20 * | |
21 * @var \Drupal\Core\Session\SessionConfigurationInterface | |
22 */ | |
23 protected $sessionConfiguration; | |
24 | |
25 /** | |
26 * The database connection. | |
27 * | |
28 * @var \Drupal\Core\Database\Connection | |
29 */ | |
30 protected $connection; | |
31 | |
32 /** | |
33 * Constructs a new cookie authentication provider. | |
34 * | |
35 * @param \Drupal\Core\Session\SessionConfigurationInterface $session_configuration | |
36 * The session configuration. | |
37 * @param \Drupal\Core\Database\Connection $connection | |
38 * The database connection. | |
39 */ | |
40 public function __construct(SessionConfigurationInterface $session_configuration, Connection $connection) { | |
41 $this->sessionConfiguration = $session_configuration; | |
42 $this->connection = $connection; | |
43 } | |
44 | |
45 /** | |
46 * {@inheritdoc} | |
47 */ | |
48 public function applies(Request $request) { | |
49 return $request->hasSession() && $this->sessionConfiguration->hasSession($request); | |
50 } | |
51 | |
52 /** | |
53 * {@inheritdoc} | |
54 */ | |
55 public function authenticate(Request $request) { | |
56 return $this->getUserFromSession($request->getSession()); | |
57 } | |
58 | |
59 /** | |
60 * Returns the UserSession object for the given session. | |
61 * | |
62 * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session | |
63 * The session. | |
64 * | |
65 * @return \Drupal\Core\Session\AccountInterface|null | |
66 * The UserSession object for the current user, or NULL if this is an | |
67 * anonymous session. | |
68 */ | |
69 protected function getUserFromSession(SessionInterface $session) { | |
70 if ($uid = $session->get('uid')) { | |
71 // @todo Load the User entity in SessionHandler so we don't need queries. | |
72 // @see https://www.drupal.org/node/2345611 | |
73 $values = $this->connection | |
74 ->query('SELECT * FROM {users_field_data} u WHERE u.uid = :uid AND u.default_langcode = 1', [':uid' => $uid]) | |
75 ->fetchAssoc(); | |
76 | |
77 // Check if the user data was found and the user is active. | |
78 if (!empty($values) && $values['status'] == 1) { | |
79 // Add the user's roles. | |
80 $rids = $this->connection | |
81 ->query('SELECT roles_target_id FROM {user__roles} WHERE entity_id = :uid', [':uid' => $values['uid']]) | |
82 ->fetchCol(); | |
83 $values['roles'] = array_merge([AccountInterface::AUTHENTICATED_ROLE], $rids); | |
84 | |
85 return new UserSession($values); | |
86 } | |
87 } | |
88 | |
89 // This is an anonymous session. | |
90 return NULL; | |
91 } | |
92 | |
93 } |