You are here

linkedin_auth.module in LinkedIn Integration 7

Same filename and directory in other branches
  1. 6 linkedin_auth/linkedin_auth.module

Implement LinkedIn Authentication service for user login

File

linkedin_auth/linkedin_auth.module
View source
<?php

/**
 * @file
 * Implement LinkedIn Authentication service for user login
 */

/**
 * Implements hook_init().
 */
function linkedin_auth_init() {
  module_load_include('pages.inc', 'linkedin_auth');
  drupal_add_css(drupal_get_path('module', 'linkedin_auth') . '/linkedin_auth.css');
}

/*
 * Implementation of hook_menu
 */
function linkedin_auth_menu() {
  $items = array();

  //callback for oauth token request on linkedin API.
  $items['linkedin/login/%user'] = array(
    'type' => MENU_CALLBACK,
    'page callback' => 'linkedin_access_token',
    'page arguments' => array(
      2,
    ),
    'access callback' => 'linkedin_auth_token_access',
    'access arguments' => array(
      2,
    ),
    'file' => 'linkedin.inc',
    'file path' => drupal_get_path('module', 'linkedin'),
  );
  return $items;
}

/*
 * Custom access callback for linkedin/login/%user
 */

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function linkedin_auth_token_access($account) {
  if (user_is_anonymous()) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implements hook_theme().
 */
function linkedin_auth_theme($existing, $type, $theme, $path) {
  return array(
    'linkedin_auth_display_login_block_button' => array(
      'variables' => array(
        'display' => '',
        'path' => 'linkedin/login/0',
        'text' => 'Log in using LinkedIn',
      ),
      'file' => 'linkedin_auth.theme.inc',
    ),
  );
}

/**
 * Implements hook_form_alter : adds LinkedIn login to the login forms().
 */
function linkedin_auth_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {

    //add login link to the login form
    case 'user_login_block':
      if (variable_get('linkedin_auth_login_link_on_block', 0) == 1) {
        $form['linkedin_auth_links'] = array(
          '#markup' => theme('linkedin_auth_display_login_block_button', array(
            'display' => 'drupal_login_block',
          )),
          '#weight' => 1,
        );
      }
      break;
    case 'user_login':
      if (variable_get('linkedin_auth_login_link_on_page', 0) == 1) {
        $form['linkedin_auth_links'] = array(
          '#markup' => theme('linkedin_auth_display_login_block_button', array(
            'display' => 'drupal_login_page',
          )),
          '#weight' => 1,
        );
      }
      break;

    //Register using linkedin.
    case 'user_register_form':
      if (variable_get('linkedin_auth_bypass_register_checks', 0) == 1 && isset($_SESSION['linkedin_auth_register_bypass'])) {
        $form_state['#redirect'] = isset($_GET['destination']) ? $_GET['destination'] : '';

        //Only remove standard register submit, but play nicely wth other modules.
        $key = array_keys($form['#submit'], 'user_register_submit');
        unset($form['#submit'][$key[0]]);
        $form['#submit'][] = 'linkedin_auth_register_form_submit';
      }
      break;
  }
  return $form;
}

/**
 * Implements hook_block_info().
 */
function linkedin_auth_block_info() {
  $blocks['linkedin_auth_login_block'] = array(
    'info' => t('Login using LinkedIn'),
    'title' => t('LinkedIn Login'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function linkedin_auth_block_view($delta = '') {

  //No check on delta, we have only one block.
  if (user_is_anonymous()) {
    $content = theme('linkedin_auth_display_login_block_button', array(
      'display' => 'linkedin_login_block',
    ));
    $block['subject'] = t('Subject');
    $block['content'] = $content;
    return $block;
  }
}

/*
 * Implementation of hook_linkedin_external_login
 */
function linkedin_auth_linkedin_external_login($uid) {
  $name = db_query("SELECT authname FROM {authmap} WHERE uid = :uid AND module = :module", array(
    ':uid' => $uid,
    ':module' => 'linkedin',
  ))
    ->fetchField();
  user_external_login_register($name, 'linkedin');
  drupal_goto();
}

/*
 * Implementation of hook_linkedin_tie_external_login
 */
function linkedin_auth_linkedin_tie_external_login($uid, $token, $secret) {
  drupal_set_message('No user associated with this LinkedIn account. Please register.');
  if (variable_get('linkedin_auth_bypass_register_checks', 0) == 1) {
    $_SESSION['linkedin_auth_register_bypass'] = 1;
  }
  drupal_goto('user/register');
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function linkedin_auth_register_form_submit($form, &$form_state) {
  user_external_login_register($form_state['values']['name'], 'linkedin');
  global $user;
  $account = user_save($user, $form_state['values']);
  if (!$account) {
    drupal_set_message(t("Error saving user account."), 'error');
    drupal_goto();
  }
  linkedin_access_token($account);

  //Unset variables no longer needed.
  unset($_SESSION['random']);
  unset($_SESSION['oauth_token_secret']);
  unset($_SESSION['linkedin_auth_register_bypass']);
}

Functions

Namesort descending Description
linkedin_auth_block_info Implements hook_block_info().
linkedin_auth_block_view Implements hook_block_view().
linkedin_auth_form_alter Implements hook_form_alter : adds LinkedIn login to the login forms().
linkedin_auth_init Implements hook_init().
linkedin_auth_linkedin_external_login
linkedin_auth_linkedin_tie_external_login
linkedin_auth_menu
linkedin_auth_register_form_submit @todo Please document this function.
linkedin_auth_theme Implements hook_theme().
linkedin_auth_token_access @todo Please document this function.