.../src/Plugin/rest/resource/UserLoginResource.php | 15 +++++---------- .../src/Plugin/rest/resource/UserLoginStatus.php | 20 +++++++++++++++----- .../rest/src/Plugin/rest/resource/UserLogout.php | 8 ++++---- .../src/Plugin/rest/resource/UserPasswordReset.php | 22 +++++++++++----------- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/core/modules/rest/src/Plugin/rest/resource/UserLoginResource.php b/core/modules/rest/src/Plugin/rest/resource/UserLoginResource.php index 24c1e16..1c05c8b 100644 --- a/core/modules/rest/src/Plugin/rest/resource/UserLoginResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/UserLoginResource.php @@ -15,11 +15,11 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; /** - * Allows user logs in by setting session cookies. + * Provides a resource to let users log in. * * @RestResource( * id = "user_login", - * label = @Translation("User Login"), + * label = @Translation("User login"), * uri_paths = { * "https://www.drupal.org/link-relations/create" = "/user/login", * }, @@ -114,7 +114,7 @@ public static function create(ContainerInterface $container, array $configuratio } /** - * Responds to the user login POST requests and logs in a user. + * Responds to user login POST requests and logs in a user. * * @param array $credentials * The login credentials. @@ -127,32 +127,27 @@ public function post($credentials) { throw new BadRequestHttpException('Missing credentials.'); } - // Verify that the username is filled. if (!isset($credentials['name'])) { throw new BadRequestHttpException('Missing credentials.name.'); } - // Verify that the password is filled. if (!isset($credentials['pass'])) { throw new BadRequestHttpException('Missing credentials.pass.'); } - // Flood control. if (!$this->isFloodBlocked()) { throw new BadRequestHttpException('Blocked.'); } - // Verify that the user is not blocked. if ($this->userIsBlocked($credentials['name'])) { throw new BadRequestHttpException('The user has not been activated or is blocked.'); } - // Log in the user. if ($uid = $this->userAuth->authenticate($credentials['name'], $credentials['pass'])) { - /** @var \Drupal\user\Entity\User $user */ + /** @var \Drupal\user\UserInterface $user */ $user = $this->userStorage->load($uid); $this->userLoginFinalize($user); - // Add some basics about the user's account. + // Send basic metadata about the logged in user. $response_data = [ 'current_user' => [ 'uid' => $user->id(), diff --git a/core/modules/rest/src/Plugin/rest/resource/UserLoginStatus.php b/core/modules/rest/src/Plugin/rest/resource/UserLoginStatus.php index 4ea6e0b..ced1d71 100644 --- a/core/modules/rest/src/Plugin/rest/resource/UserLoginStatus.php +++ b/core/modules/rest/src/Plugin/rest/resource/UserLoginStatus.php @@ -10,11 +10,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Provides a resource for database watchdog log entries. + * Provides a resource to check whether the current user is logged in. * * @RestResource( * id = "user_login_status", - * label = @Translation("User Login Status"), + * label = @Translation("User login status"), * uri_paths = { * "canonical" = "/user/login/status" * } @@ -22,8 +22,18 @@ */ class UserLoginStatus extends ResourceBase { - // Logged status constants. + /** + * String sent in responses, to describe the user as being logged in. + * + * @var string + */ const LOGGED_IN = 'LOGGED_IN'; + + /** + * String sent in responses, to describe the user as being logged out. + * + * @var string + */ const LOGGED_OUT = 'LOGGED_OUT'; /** @@ -34,7 +44,7 @@ class UserLoginStatus extends ResourceBase { protected $currentUser; /** - * Constructs a Drupal\rest\Plugin\ResourceBase object. + * Constructs a new UserLoginStatus object. * * @param array $configuration * A configuration array containing information about the plugin instance. @@ -70,7 +80,7 @@ public static function create(ContainerInterface $container, array $configuratio } /** - * Response to user login status GET requests. + * Responds to user login status GET requests. * * @return \Drupal\rest\ResourceResponse * A resource response. diff --git a/core/modules/rest/src/Plugin/rest/resource/UserLogout.php b/core/modules/rest/src/Plugin/rest/resource/UserLogout.php index 0613e32..48c4d7c 100644 --- a/core/modules/rest/src/Plugin/rest/resource/UserLogout.php +++ b/core/modules/rest/src/Plugin/rest/resource/UserLogout.php @@ -6,11 +6,11 @@ use Drupal\rest\ResourceResponse; /** - * Rest resources for logging out users. + * Provides a resource to let the currently logged in user log out. * * @RestResource( * id = "user_logout", - * label = @Translation("Logout"), + * label = @Translation("User logout"), * uri_paths = { * "https://www.drupal.org/link-relations/create" = "/user/logout", * } @@ -19,7 +19,7 @@ class UserLogout extends ResourceBase { /** - * Handles POST requests to the user logout path. + * Responds to user logout POST requests. * * @return \Drupal\rest\ResourceResponse * The response. @@ -30,7 +30,7 @@ public function post() { } /** - * Logs out the user. + * Logs the user out. */ protected function userLogout() { user_logout(); diff --git a/core/modules/rest/src/Plugin/rest/resource/UserPasswordReset.php b/core/modules/rest/src/Plugin/rest/resource/UserPasswordReset.php index 76bc101..6088fca 100644 --- a/core/modules/rest/src/Plugin/rest/resource/UserPasswordReset.php +++ b/core/modules/rest/src/Plugin/rest/resource/UserPasswordReset.php @@ -12,7 +12,7 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; /** - * Provides a rest resource for resetting a user password. + * Provides a resource for resetting a user's password. * * @RestResource( * id = "user_password_reset", @@ -32,7 +32,7 @@ class UserPasswordReset extends ResourceBase { protected $userStorage; /** - * Constructs a Drupal\rest\Plugin\ResourceBase object. + * Constructs a new UserPasswordReset object. * * @param array $configuration * A configuration array containing information about the plugin instance. @@ -68,27 +68,27 @@ public static function create(ContainerInterface $container, array $configuratio } /** - * Resets a user password using a POST request. + * Responds to user password reset POST requests. * * @param string $name - * The username or mail address, that should be resetted. + * The username or email address, that should be reset. * @param string|null $langcode - * The langcode. + * (optional) Language code to use for the notification, overriding account + * language. * - * @return \Drupal\rest\ResourceResponse::__construct + * @return \Drupal\rest\ResourceResponse * The HTTP response. */ public function post($name, $langcode = NULL) { $name = trim($name); if (!$account = $this->loadUserByNameOrEmail($name)) { - // No success, the user does not exist. - throw new BadRequestHttpException(new FormattableMarkup('Sorry, %name is not recognized as a user name or an e-mail address.', ['%name' => $name])); + throw new BadRequestHttpException(new FormattableMarkup('%name is not recognized as a username or an email address.', ['%name' => $name])); } $mail = $this->userMailNotify($account, $langcode); if (empty($mail)) { - throw new BadRequestHttpException('The email with the instructions was not sent as expected.'); + throw new BadRequestHttpException('The email with password reset instructions could not be sent.'); } $this->logger->notice('Password reset instructions mailed to %name at %email.', ['%name' => $account->getDisplayName(), '%email' => $account->getEmail()]); @@ -96,7 +96,7 @@ public function post($name, $langcode = NULL) { } /** - * Conditionally create and send a notification email. + * Conditionally creates and sends a notification email. * * @param \Drupal\Core\Session\AccountInterface $account * The user object of the account being notified. Must contain at @@ -114,7 +114,7 @@ protected function userMailNotify(AccountInterface $account, $langcode = NULL) { } /** - * Loads a user by name OR mail address. + * Loads a user by name or mail address. * * @param string $name * The username or mail address, that should be loaded.