user_service.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/services/services/user_service/user_service.module

The module which exposes services related to user activities

Functions & methods

NameDescription
user_service_help
user_service_loginlogin a user
user_service_logoutLogout user
user_service_serviceImplementation of hook_service().

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * The module which exposes services related to user activities
  5. */
  6. /**
  7. * Implementation of hook_help().
  8. */
  9. function user_service_help($section) {
  10. switch ($section) {
  11. case 'admin/help#services_user':
  12. return t('<p>Provides user methods to services applications. Requires services.module.</p>');
  13. case 'admin/modules#description':
  14. return t('Provides user methods to services applications. Requires services.module.');
  15. }
  16. }
  17. /**
  18. * Implementation of hook_service().
  19. */
  20. function user_service_service() {
  21. return array(
  22. // user.login
  23. array(
  24. '#method' => 'user.login',
  25. '#callback' => 'user_service_login',
  26. '#args' => array(
  27. array(
  28. '#name' => 'username',
  29. '#type' => 'string',
  30. '#description' => t('A valid username.'),
  31. ),
  32. array(
  33. '#name' => 'password',
  34. '#type' => 'string',
  35. '#description' => t('A valid password.'),
  36. ),
  37. ),
  38. '#return' => 'struct',
  39. '#help' => t('Logs in a user.')),
  40. // user.logout
  41. array(
  42. '#method' => 'user.logout',
  43. '#callback' => 'user_service_logout',
  44. '#args' => array(),
  45. '#return' => 'struct',
  46. '#help' => t('Logs out a user.')),
  47. );
  48. }
  49. /**
  50. * login a user
  51. */
  52. function user_service_login($username, $password) {
  53. global $user;
  54. if ($user->uid) {
  55. // user is already logged in
  56. return services_error(t('Already logged in as !user.', array('!user' => $user->name)));
  57. }
  58. $user = user_authenticate($username, $password);
  59. if ($user->uid) {
  60. // Regenerate the session ID to prevent against session fixation attacks.
  61. sess_regenerate();
  62. module_invoke_all('user', 'login', NULL, $user);
  63. $return = new stdClass();
  64. $return->sessid = session_id();
  65. $return->user = $user;
  66. return $return;
  67. }
  68. session_destroy();
  69. return services_error(t('Wrong username or password.'));
  70. }
  71. /**
  72. * Logout user
  73. */
  74. function user_service_logout() {
  75. global $user;
  76. if (!$user->uid) {
  77. // user is not logged in
  78. return services_error(t('User is not logged in.'));
  79. }
  80. watchdog('user', t('Session closed for %name.', array('%name' => theme('placeholder', $user->name))));
  81. // Destroy the current session:
  82. session_destroy();
  83. module_invoke_all('user', 'logout', NULL, $user);
  84. // Load the anonymous user
  85. $user = drupal_anonymous_user();
  86. return TRUE;
  87. }