Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am looking for a mechanism to Log into Drupal system without a Password and only with some link provided.

My Use case is: Intranet system (made with ASP) passes the Link like: http://example.com/user_name=jhilke&user_email=jhilke@example.com&user_role=developer&TY89823jjkhds234.
To break down the above link separated by &

  • URL: http://example.com
  • USERNAME: user_name=jhilke
  • EMAIL: user_email=jhilke@example.com
  • USER ROLE: user_role=developer
  • HASH: TY89823jjkhds234

Now I need to Log in the User or Create new User (if the user doesn't exists) in Drupal site with exact Username and Role but with just the link and no password.

I have used this URL Login module so far to login to Drupal site without password and with some hash/encrypted key it generates. For eg: The link that URL Login module creates is like http://example.com/l/YUb&8767GUYuiu and logs in the user exactly as the role they are assigned, but it doesn't satisfy the other criteria like username and roles in the link.

Is there any way I can achieve this.

share|improve this question

2 Answers 2

Have a look at

user_external_login_register

Helper function for authentication modules. Either logs in or registers the current user, based on username. Either way, the global $user object is populated and login tasks are performed.

The example at:

https://api.drupal.org/comment/13434#comment-13434

Is a worthwhile read too.

share|improve this answer
    
The comment section is what I am looking for. I need to try it, will let you know if it satisfies my need. A big thanks !!! –  Jhilke Dai Mar 15 at 10:08

Quick code, not tested, but should show idea, change CUSTOMMODULENAME to you customm module name, if not - create one:

<?php

function CUSTOMMODULENAME_init() {
  global $user;
  $g = $_GET;
  if (isset($g['user_name']) && isset($g['user_email']) && isset($g['user_role'])) {
    $name = $g['user_name'];
    unset($g['user_name']);
    $mail = $g['user_email'];
    unset($g['user_email']);
    $user_role = $g['user_role'];
    unset($g['user_role']);
    unset($g['q']);

    $hash = reset($g);// Since you hash hasn't key we assume that's current key
    if ($hash) {
      $account = user_load_by_name($name);
      if ($account) {
        if (CUSTOMMODULENAME_checkhash($account, $hash)) {
          $user = $account;
          $edit['uid'] = $account->uid;
          user_login_finalize($edit);
          drupal_set_message(t('Successful login.'));
        }
      }
      else {
        if (CUSTOMMODULENAME_checkhash(FALSE, $hash, $name, $mail, $user_role)) {
          $pass = 111; // Generate pass here, random or from hash.
          $new_user = array(
            'name' => $name,
            'pass' => $pass, // note: do not md5 the password
            'mail' => $mail,
            'status' => 1,
            'init' => $mail,
            'roles' => array(
              DRUPAL_AUTHENTICATED_RID => 'authenticated user',
            ),
          );
          $role = user_role_load_by_name($user_role);
          if ($role) {
            $new_user['roles'][$role->rid] = $user_role;
          }
          // The first parameter is sent blank so a new user is created.
          $account = user_save('', $new_user);

          $user = $account;
          $edit['uid'] = $account->uid;
          user_login_finalize($edit);
          drupal_set_message(t('Successful register and login.'));
        }
      }
    }
  }

}

function CUSTOMMODULENAME_checkhash($account, $hash, $name = '', $mail = '' , $user_role = '') {
  // Write check hashing function here.
  // if $account empty, well it's mean check hash for new user. So use name, mail, user_role.
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.