You are here

function legal_user_login in Legal 7.2

Same name and namespace in other branches
  1. 8 legal.module \legal_user_login()
  2. 7 legal.module \legal_user_login()
  3. 2.0.x legal.module \legal_user_login()

Implements hook_user_login().

File

./legal.module, line 402
Module file for Legal.

Code

function legal_user_login(&$edit, $account) {
  global $user;
  global $language;
  if ($user->uid == 1) {
    return;
  }
  $accept_every_login = variable_get('accept_every_login', '0');

  // Get last accepted version for this account
  $legal_account = legal_get_accept($user->uid);

  // If no version has been accepted yet, get version with current language revision.
  if (empty($legal_account['version'])) {
    $conditions = legal_get_conditions($language->language);

    // No conditions set yet.
    if (empty($conditions['conditions'])) {
      return;
    }
  }
  else {
    $conditions = legal_get_conditions($legal_account['language']);

    // No conditions set yet.
    if (empty($conditions['conditions'])) {
      return;
    }

    // Check latest version of T&C has been accepted.
    $accepted = legal_version_check($user->uid, $conditions['version'], $conditions['revision'], $legal_account);
    if ($accepted) {
      if ($accept_every_login == 0) {
        return;
      }
      if ($accept_every_login == 1 && isset($_SESSION['legal']['legal_login']) && $_SESSION['legal']['legal_login'] == 1) {
        return;
      }
    }
  }
  $uid = $user->uid;

  // Log the user out and regenerate the Drupal session.
  module_invoke_all('user_logout', $user);
  drupal_session_regenerate();

  // We have to use $GLOBALS to unset a global variable.
  $user = drupal_anonymous_user();
  $query = NULL;

  // Deal with destination from password reset one time login link,
  // by creating a new one time login link and setting it as the destination
  // after the T&Cs have been accepted.
  if (arg(0) == 'user' && arg(1) == 'reset') {
    $token = drupal_hash_base64(drupal_random_bytes(55));

    // This is a new, anonymous-user session.
    $_SESSION['pass_reset_' . $uid] = $token;
    $query = array(
      'destination' => "user/{$uid}/edit?pass-reset-token={$token}",
    );
  }
  if (!empty($_REQUEST['destination'])) {
    $query = array(
      'destination' => $_REQUEST['destination'],
    );
  }
  unset($_GET['destination']);
  $result = db_select('users', 'u')
    ->fields('u')
    ->condition('uid', $uid)
    ->range(0, 1)
    ->execute()
    ->fetchAllAssoc('uid');
  $signatory = array_pop($result);
  $token = drupal_hash_base64(drupal_random_bytes(55));
  $hash = user_pass_rehash($signatory->pass, $token, $signatory->login, $uid);
  user_cookie_save(array(
    'legal_hash' => $hash,
    'legal_id' => $uid,
  ));
  $query['token'] = $token;
  drupal_goto('legal_accept', array(
    'query' => $query,
  ));
}