diff --git a/core/modules/rest/src/Plugin/ResourceBase.php b/core/modules/rest/src/Plugin/ResourceBase.php index 33cb3aa..4e77eb9 100644 --- a/core/modules/rest/src/Plugin/ResourceBase.php +++ b/core/modules/rest/src/Plugin/ResourceBase.php @@ -64,7 +64,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->getParameter('serializer.formats'), - $container->get('logger.factory')->get('rest') + $container->get('logger.channel.rest') ); } diff --git a/core/modules/rest/src/Plugin/rest/resource/UserLoginResource.php b/core/modules/rest/src/Plugin/rest/resource/UserLoginResource.php index 1d75d62..6171490 100644 --- a/core/modules/rest/src/Plugin/rest/resource/UserLoginResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/UserLoginResource.php @@ -4,10 +4,12 @@ use Drupal\Core\Access\CsrfTokenGenerator; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Config\ImmutableConfig; use Drupal\Core\Flood\FloodInterface; use Drupal\rest\ResourceResponse; use Drupal\rest\Plugin\ResourceBase; use Drupal\user\Entity\User; +use Drupal\user\UserAuthInterface; use Drupal\user\UserStorageInterface; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -52,6 +54,13 @@ class UserLoginResource extends ResourceBase { protected $csrfToken; /** + * The User Authentication service. + * + * @var \Drupal\user\UserAuthInterface + */ + protected $userAuth; + + /** * Constructs a new UserLoginResource object. * * @param array $configuration @@ -72,13 +81,16 @@ class UserLoginResource extends ResourceBase { * The user storage. * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token * The Csrf Token Generator. + * @param \Drupal\user\UserAuthInterface $user_auth + * The User Authentication service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, ConfigFactoryInterface $config_factory, FloodInterface $flood, UserStorageInterface $user_storage, CsrfTokenGenerator $csrf_token) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, ConfigFactoryInterface $config_factory, FloodInterface $flood, UserStorageInterface $user_storage, CsrfTokenGenerator $csrf_token, UserAuthInterface $user_auth) { parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger, $flood); $this->configFactory = $config_factory; $this->flood = $flood; $this->userStorage = $user_storage; $this->csrfToken = $csrf_token; + $this->userAuth = $user_auth; } /** @@ -94,7 +106,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('config.factory'), $container->get('flood'), $container->get('entity.manager')->getStorage('user'), - $container->get('csrf_token') + $container->get('csrf_token'), + $container->get('user.auth') ); } @@ -119,7 +132,7 @@ public function post($credentials) { } // Flood control. - if (!$this->restFloodControl($this->configFactory->get('user.flood'), 'rest.login_cookie')) { + if (!$this->isFloodBlocked($this->configFactory->get('user.flood'), 'rest.login_cookie')) { throw new BadRequestHttpException('Blocked.'); } @@ -129,9 +142,9 @@ public function post($credentials) { } // Log in the user. - if ($uid = \Drupal::service('user.auth')->authenticate($credentials['name'], $credentials['pass'])) { + if ($uid = $this->userAuth->authenticate($credentials['name'], $credentials['pass'])) { /** @var \Drupal\user\Entity\User $user */ - $user = User::load($uid); + $user = $this->userStorage->load($uid); user_login_finalize($user); // Add some basics about the user's account. @@ -144,7 +157,8 @@ public function post($credentials) { 'csrf_token' => $this->csrfToken->get('rest'), ]; - return new ResourceResponse($response_data, 200, []); + $response = new ResourceResponse($response_data, 200, []); + return $response->addCacheableDependency($user); } $this->flood->register('rest.login_cookie', $this->configFactory->get('user.flood')->get('user_window')); @@ -172,7 +186,7 @@ protected function userIsBlocked($name) { * @return bool * TRUE if the user is allowed to proceed, FALSE otherwise. */ - protected function restFloodControl($config, $name) { + protected function isFloodBlocked(ImmutableConfig $config, $name) { $limit = $config->get('user_limit'); $interval = $config->get('user_window'); diff --git a/core/modules/rest/src/Plugin/rest/resource/UserLoginStatus.php b/core/modules/rest/src/Plugin/rest/resource/UserLoginStatus.php index daf98b0..5fe3019 100644 --- a/core/modules/rest/src/Plugin/rest/resource/UserLoginStatus.php +++ b/core/modules/rest/src/Plugin/rest/resource/UserLoginStatus.php @@ -15,7 +15,7 @@ * * @RestResource( * id = "user_login_status", - * label = @Translation("Watchdog database log"), + * label = @Translation("User Login Status"), * uri_paths = { * "canonical" = "/user/login/status" * } @@ -23,6 +23,10 @@ */ class UserLoginStatus extends ResourceBase { + // Logged status constants. + const LOGGED_IN = 'LOGGED_IN'; + const LOGGED_OUT = 'LOGGED_OUT'; + /** * The current user. * @@ -48,7 +52,6 @@ class UserLoginStatus extends ResourceBase { */ public function __construct(array $configuration, $plugin_id, $plugin_definition, array $serializer_formats, LoggerInterface $logger, AccountInterface $current_user) { parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger); - $this->currentUser = $current_user; } @@ -61,7 +64,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->getParameter('serializer.formats'), - $container->get('logger.factory')->get('rest'), + $container->get('logger.channel.rest'), $container->get('current_user') ); } @@ -74,10 +77,11 @@ public static function create(ContainerInterface $container, array $configuratio */ public function get() { if ($this->currentUser->isAuthenticated()) { - $response = new ResourceResponse('You are logged in.', 200, []); + $response = new ResourceResponse(self::LOGGED_IN, 200, []); + $response->addCacheableDependency($this->currentUser); } else { - $response = new ResourceResponse('You are not logged in.', 200, []); + $response = new ResourceResponse(self::LOGGED_OUT, 200, []); } return $response->addCacheableDependency((new CacheableMetadata())->setCacheMaxAge(0)); } diff --git a/core/modules/rest/src/Plugin/rest/resource/UserLogout.php b/core/modules/rest/src/Plugin/rest/resource/UserLogout.php index 5f34ea9..2a751dc 100644 --- a/core/modules/rest/src/Plugin/rest/resource/UserLogout.php +++ b/core/modules/rest/src/Plugin/rest/resource/UserLogout.php @@ -25,7 +25,7 @@ class UserLogout extends ResourceBase { */ public function post() { user_logout(); - return new ResourceResponse('You are logged out.', 200, []); + return new ResourceResponse(NULL, 204); } } diff --git a/core/modules/rest/src/Plugin/rest/resource/UserPasswordReset.php b/core/modules/rest/src/Plugin/rest/resource/UserPasswordReset.php index 34094bd..12aacf4 100644 --- a/core/modules/rest/src/Plugin/rest/resource/UserPasswordReset.php +++ b/core/modules/rest/src/Plugin/rest/resource/UserPasswordReset.php @@ -60,7 +60,7 @@ public static function create(ContainerInterface $container, array $configuratio $plugin_id, $plugin_definition, $container->getParameter('serializer.formats'), - $container->get('logger.factory')->get('rest'), + $container->get('logger.channel.rest'), $container->get('entity_type.manager')->getStorage('user') ); } @@ -77,16 +77,11 @@ public static function create(ContainerInterface $container, array $configuratio * The HTTP response. */ public function post($name, $langcode = NULL) { - // Verify that the email or username is filled. - if (!$name) { - throw new BadRequestHttpException('Missing Email or Username.'); - } - $name = trim($name); if (!$account = $this->loadUserByNameOrEmail($name)) { // No success, the user does not exist. - throw new BadRequestHttpException("Sorry, $name is not recognized as a user name or an e-mail address."); + throw new BadRequestHttpException($this->t("Sorry, %name is not recognized as a user name or an e-mail address.", ['%name' => $name])); } $mail = _user_mail_notify('password_reset', $account, $langcode); diff --git a/core/modules/rest/src/Tests/UserLoginTest.php b/core/modules/rest/src/Tests/UserLoginTest.php index 5c66aa0..28b0ab7 100644 --- a/core/modules/rest/src/Tests/UserLoginTest.php +++ b/core/modules/rest/src/Tests/UserLoginTest.php @@ -3,6 +3,7 @@ namespace Drupal\rest\Tests; use Drupal\Core\Url; +use Drupal\rest\Plugin\rest\resource\UserLoginStatus; use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; @@ -61,7 +62,7 @@ public function testLogin() { $url = Url::fromRoute('rest.user_login_status.GET.json'); $url->setRouteParameter('_format', 'json'); $this->httpRequest($url, 'GET', NULL, 'application/json'); - $this->assertResponseBody('200', '"You are not logged in."'); + $this->assertResponseBody('200', '"' . UserLoginStatus::LOGGED_OUT . '"'); $payload = []; $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); @@ -83,21 +84,20 @@ public function testLogin() { $response = $this->httpRequest('user_login', 'POST', json_encode($payload), 'application/json'); $response = json_decode($response); $this->assertEqual($name, $response->current_user->name, "The user name is correct."); - debug($this->cookies); $url = Url::fromRoute('rest.user_login_status.GET.json'); $url->setRouteParameter('_format', 'json'); $this->httpRequest($url, 'GET', NULL, 'application/json'); - $this->assertResponseBody('200', '"You are logged in."'); + $this->assertResponseBody('200', '"' . UserLoginStatus::LOGGED_IN . '"'); $payload = ['name' => $name, 'pass' => $pass]; $this->httpRequest('user_logout', 'POST', json_encode($payload), 'application/json'); - $this->assertResponseBody('200', '"You are logged out."'); + $this->assertResponse('204'); $url = Url::fromRoute('rest.user_login_status.GET.json'); $url->setRouteParameter('_format', 'json'); $this->httpRequest($url, 'GET', NULL, 'application/json'); - $this->assertResponseBody('200', '"You are not logged in."'); + $this->assertResponseBody('200', '"' . UserLoginStatus::LOGGED_OUT . '"'); } } diff --git a/core/modules/rest/tests/src/Unit/UserLoginResourceTest.php b/core/modules/rest/tests/src/Unit/UserLoginResourceTest.php index 1d94a90..01846a2 100644 --- a/core/modules/rest/tests/src/Unit/UserLoginResourceTest.php +++ b/core/modules/rest/tests/src/Unit/UserLoginResourceTest.php @@ -84,7 +84,7 @@ protected function setUp() { $this->csrfToken = $this->prophesize(CsrfTokenGenerator::class); - $this->testClass = new TestUserLoginResource([], 'plugin_id', '', [], $this->logger, $this->config, $this->flood, $this->userStorage, $this->csrfToken->reveal()); + $this->testClass = new TestUserLoginResource([], 'plugin_id', '', [], $this->logger, $this->config, $this->flood, $this->userStorage, $this->csrfToken->reveal(), $user_auth_service); } /** diff --git a/core/modules/rest/tests/src/Unit/UserPasswordReset.php b/core/modules/rest/tests/src/Unit/UserPasswordReset.php deleted file mode 100644 index e0fe4f1..0000000 --- a/core/modules/rest/tests/src/Unit/UserPasswordReset.php +++ /dev/null @@ -1,82 +0,0 @@ -getMock('Drupal\user\UserAuthInterface'); - $user_auth_service->expects($this->any()) - ->method('authenticate') - ->will($this->returnValue(FALSE)); - - $container = new ContainerBuilder(); - $container->set('user.auth', $user_auth_service); - \Drupal::setContainer($container); - - $this->flood = $this->getMock(FloodInterface::class); - - $this->userStorage = $this->getMockBuilder('\Drupal\user\UserStorage') - ->disableOriginalConstructor() - ->getMock(); - - $this->config = $this->getConfigFactoryStub([ - 'user.flood' => [], - ]); - - $this->logger = $this->getMock('Psr\Log\LoggerInterface'); - - $this->csrfToken = $this->prophesize(CsrfTokenGenerator::class); - - $this->testClass = new TestUserLoginResource([], 'plugin_id', '', [], $this->logger, $this->config, $this->flood, $this->userStorage, $this->csrfToken->reveal()); - } - -} diff --git a/core/modules/rest/tests/src/Unit/UserPasswordResetTest.php b/core/modules/rest/tests/src/Unit/UserPasswordResetTest.php new file mode 100644 index 0000000..de24be9 --- /dev/null +++ b/core/modules/rest/tests/src/Unit/UserPasswordResetTest.php @@ -0,0 +1,83 @@ +getMock('Drupal\user\UserAuthInterface'); + $user_auth_service->expects($this->any()) + ->method('authenticate') + ->will($this->returnValue(FALSE)); + + $container = new ContainerBuilder(); + $container->set('user.auth', $user_auth_service); + \Drupal::setContainer($container); + + $this->flood = $this->getMock(FloodInterface::class); + + $this->userStorage = $this->getMockBuilder('\Drupal\user\UserStorage') + ->disableOriginalConstructor() + ->getMock(); + + $this->config = $this->getConfigFactoryStub([ + 'user.flood' => [], + ]); + + $this->logger = $this->getMock('Psr\Log\LoggerInterface'); + + $this->csrfToken = $this->prophesize(CsrfTokenGenerator::class); + + $this->testClass = new TestUserLoginResource([], 'plugin_id', '', [], $this->logger, $this->config, $this->flood, $this->userStorage, $this->csrfToken->reveal()); + } + +}