You are here

user_service.module in Services 5

The module which exposes services related to user activities

File

services/user_service/user_service.module
View source
<?php

/**
 * @file
 * The module which exposes services related to user activities
 */

/**
 * Implementation of hook_help().
 */
function user_service_help($section) {
  switch ($section) {
    case 'admin/help#services_user':
      return t('<p>Provides user methods to services applications.  Requires services.module.</p>');
    case 'admin/modules#description':
      return t('Provides user methods to services applications.  Requires services.module.');
  }
}

/**
 * Implementation of hook_service().
 */
function user_service_service() {
  return array(
    // user.login
    array(
      '#method' => 'user.login',
      '#callback' => 'user_service_login',
      '#args' => array(
        array(
          '#name' => 'username',
          '#type' => 'string',
          '#description' => t('A valid username.'),
        ),
        array(
          '#name' => 'password',
          '#type' => 'string',
          '#description' => t('A valid password.'),
        ),
      ),
      '#return' => 'struct',
      '#help' => t('Logs in a user.'),
    ),
    // user.logout
    array(
      '#method' => 'user.logout',
      '#callback' => 'user_service_logout',
      '#args' => array(),
      '#return' => 'struct',
      '#help' => t('Logs out a user.'),
    ),
  );
}

/**
 * login a user
 */
function user_service_login($username, $password) {
  global $user;
  if ($user->uid) {

    // user is already logged in
    return services_error(t('Already logged in as !user.', array(
      '!user' => $user->name,
    )));
  }
  $user = user_authenticate($username, $password);
  if ($user->uid) {

    // Regenerate the session ID to prevent against session fixation attacks.
    sess_regenerate();
    module_invoke_all('user', 'login', NULL, $user);
    $return = new stdClass();
    $return->sessid = session_id();
    $return->user = $user;
    return $return;
  }
  session_destroy();
  return services_error(t('Wrong username or password.'));
}

/**
 * Logout user
 */
function user_service_logout() {
  global $user;
  if (!$user->uid) {

    // user is not logged in
    return services_error(t('User is not logged in.'));
  }
  watchdog('user', t('Session closed for %name.', array(
    '%name' => theme('placeholder', $user->name),
  )));

  // Destroy the current session:
  session_destroy();
  module_invoke_all('user', 'logout', NULL, $user);

  // Load the anonymous user
  $user = drupal_anonymous_user();
  return TRUE;
}

Functions

Namesort descending Description
user_service_help Implementation of hook_help().
user_service_login login a user
user_service_logout Logout user
user_service_service Implementation of hook_service().