0

So, I'm working on an Oauth2 implementation, and I'm forced to use a custom module as the existing modules all have slightly differing functionalities than my use case, assume Drupal-to-Drupal implementation, or request the user credentials to be submitted to the remote server (which is not how Oauth should be implemented).

Using the standard authflow, the users are redirected and authenticate against a remote server, and are returned with an access code which is then submitted for an auth token, which is finally used to get user info from the remote API. Finally, I take the json response, format it into an array to be created into an user object, and this is where I'm stuck.

The examples presented on the User External Login Register API page seem to assume querying an external database to check for the user's existence. In my case, I have the user info and name, and need to create a user and log them in with that information. This is my current attempt which doesn't seem to be working. I modified the API example use the user_load_by_name() function to see if the user exists. Otherwise, I'm not sure how I should approach this otherwise.

So, my question is: 1) How do I correctly use this function? the example uses it without a module prefix, but if I declare it as a regular function I get a fatal error about re-declaring functions.

  $cs_user = array(
        'name' => $user_info['data']['user']['lastname'] . "_psr",
        'mail' => $user_info['data']['user']['email'],
        'pass' => $user_info['data']['user']['lastname'] . "_user_pass",
        'init' => $user_info['data']['user']['lastname'] . "_psr",
        'signature_format' => 'full_html',
        'status' => 1,
        'roles' => array(DRUPAL_AUTHENTICATED_RID => 'authenticated user', 
                            22 => 'CS User External',),
        'field_first_name' => array(array('value' => $user_info['data']['user']['firstname'])),                    
        'field_last_name' => array(array('value' => $user_info['data']['user']['lastname'])),
        'field_chapter' => array(array('value' => $user_info['data']['user']['groups']['0']['title'])), 
       );
    //format the user info, feed it to external_user_login_register
    $name = $cs_user['name'];
    $module = 'chapterspot_signon';
    echo "<hr>";
    }
    function chapterspot_signon_user_external_login_register($name, $module){
        $account = user_load_by_name($name); 
$account = $name;
        if (!$account) {
        // Register this new user.
        $account = user_save(drupal_anonymous_user(), $cs_user);
        // Terminate if an error occurred during user_save().
        if (!$account) {
          drupal_set_message(t("Error saving user account."), 'error');
          return;
        }
        user_set_authmaps($account, array(
          "authname_{$module}" => $name,
        ));
      }
      // Log user in.
      $form_state['uid'] = $account->uid;
      user_login_submit(array(), $form_state);
      drupal_goto('/members');
    }

Update: The issue here is how to instantiate the function? The API page shows the function being called without a module prefix. When I run the code as user_external_login_register() I get a fatal error. When I try to run the function as mymodule_user_external_login_register nothing happens e.g.

$name = $cs_user['name'];
$module = 'chapterspot_signon';    
function chapterspot_signon_user_external_login_register($name, $module){
echo "This is working";    
} 

Update 2: Apparently, the code within the function works by itself.

0
+100

Not sure if this fixes the situation for you, but you can run the code inside the function without the wrapper. As you point out, the external server is handling the authentication for you, so there's no reason to pass the details through the user module validation functions. You will still need to do any user maintenance in your module, but this code will log a user in and pass them to the page you want.

  $account = user_load_by_name($name);   
  if (!$account) {
  // Register this new user.
  $account = user_save(drupal_anonymous_user(), $cs_user);
  // Terminate if an error occurred during user_save().
  if (!$account) {
  drupal_set_message(t("Error saving user account."), 'error');
  return;
  }
  user_set_authmaps($account, array(
  "authname_{$module}" => $name,
  ));
  }
      // Log user in.
      $form_state['uid'] = $account->uid;
      user_login_submit(array(), $form_state);
      drupal_goto('/members');
0

It seems a couple of mistakes in your above code.

Your $cs_user array below line should be populated with name instead of lastname

'name' => $user_info['data']['user']['lastname'] . "_psr",

Also, It seems 2nd line of your function function chapterspot_signon_user_external_login_register($name, $module) is not needed. Because if you Initialize your $account variable with $name then your condition if (!$account) { never comes true and the code written into that if condition will never executed.

You can try two approaches it will work for you:

1) Call Built-in function user_external_login_register($name, $module) from your custom module.

Example:

user_external_login_register("addweb-solution", "your-custom-module-name");
drupal_goto('/members');

2) You can write your own function as below in your custom module:

function test_user_external_login_register($name, $module){
        $account = user_load_by_name($name);
        // $account = $name;
        if (!$account) {
            $userinfo = array('name' => $name,
              'pass' => user_password(),
              'init' => $name,
              'status' => 1,
              'access' => REQUEST_TIME,
            );
            // Register this new user.
            $account = user_save(drupal_anonymous_user(), $userinfo);
            // Terminate if an error occurred during user_save().
            if (!$account) {
              drupal_set_message(t("Error saving user account."), 'error');
              return;
            }
            user_set_authmaps($account, array(
              "authname_{$module}" => $name,
            ));
        }
        // Log user in.
        $form_state['uid'] = $account->uid;
        user_login_submit(array(), $form_state);
        drupal_goto('/members');

}

Hope this will helps you.

Thanks!

  • Appreciate your response. Will try to clean my code up a little with your suggestion and report back. – nizz0k Mar 12 '18 at 11:42
  • In response to your first comment, I want the user name to be made up of the user's last name and my suffix. – nizz0k Mar 13 '18 at 20:55
  • Just get the last name by $get_name = $account->get('field_lastname')->getValue(); //suffix with anything u want $name = $get_name[0]['value']. "psr"; – Shreya Shetty Mar 14 '18 at 6:23
  • The $name variable is correct. I'm pulling that info out of the json object returned from the Oauth server. The user doesn't exist, but I need to check if the user name is in the system for later to handle the same user coming back. The problem is the user_external_login_validate() doesn't instantiate. – nizz0k Mar 14 '18 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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