External login should set custom properties in user object I have a Drupal website and a Symfony based API. I have configured Drupal to use my API for logging in. The users are stored in Symfony.

When logging in, Drupal will store a user in the database to create a user object. On this user object, I want to set some custom properties. For example an id which is a reference back to my Symfony user so that I can load the users settings, etc. later on.

I have not managed to figure out how to do this. I hope someone has an example on how to do this or can point me in the right direction.

Below is the code I use for logging the user in. First I check if the entered username matches an administrator that Drupal knows of. If it does, I will use the default Drupal login validation. If not, I will login through the API.

/**
 *  Validates the login. Uses either the default Drupal validation or a custom validation.
 */
function myy_authenticate_validate($form, &amp;$form_state)
{
    // Retrieve name and password.
    $name = $form_state["values"]["name"];
    $pass = $form_state["values"]["pass"];

    // Check if the user exists in the Drupal users
    if ($user = db_query("SELECT * FROM {users} u WHERE name = :name", array(':name' =&gt; $name))-&gt;fetch())
    {
        // A user exists. Loads the user information.
        $user = user_load($user-&gt;uid);

        // If the user is an administator, log him in using the Drupal validator.
        if (in_array("administrator", $user-&gt;roles))
            return user_login_authenticate_validate($form, $form_state);
    }

    // The user didn't exist in the Drupal users. Now, try to authenciate using the custom validation.
    $authenticated = myy_symfony_auth($name, $pass);

    // Check if we did authenciate
    if ($authenticated)
    {
        // This sets up the external user with Drupal by creating a local entry. If they don't exist they are added.
        user_external_login_register($name, "myy");

        // We must set the "uid" to pass back. This looks up the logged in user and sets the Drupal UID.
        $user = user_external_load($name, $pass);
        $form_state["uid"] = $user-&gt;uid;
    }
    else
    {
        // Do nothing. Drupal will handle the errors.
    }
}


myy_symfony_auth is a method which returns whether or not we were able to login using the API.

Does anyone have an idea how I can add custom properties to the user object when a login succeeds? It is only relevant to do this when the login was performed using the API (meaning that it was not an administrator who logged in).
I've done the same thing in D7. I see you're using the user-login-form directly and skipping over UID = 1 .... thats ok.

What you want todo is essentially copy the PHP logic of user_external_login_register into your own function, you're already calling your own *_authenticate() function to process the login page.

So you would change your:

// This sets up the external user with Drupal by creating a local entry.
// If they don't exist they are added.
user_external_login_register($name, "myy");


To like:

// This sets up the external user with Drupal by creating a local entry.
// If they don't exist they are added.
MYMODULE_user_external_login_register($name, "myy", $external_data);


and define the necessary function that mimics the D7 login logic AND adds you're own fields for the user upon creation (I mean literally copy the D7 function body into your function body and tweak as needed)  -- I've added additional fields using the Field UI found at /admin/people to my User accounts as seen below.

So I have this function to do that:

function MYMODULE_auth_idstaff_user_external_login_register($name, $module, $external_ldap_details) {
  $account = user_external_load($name);
  if (!$account) {
    // Register this new user.
    $roles = user_roles();
    $userinfo = array(
      'name' =&gt; $name,
      'pass' =&gt; user_password(),
      'init' =&gt; $name,
      'status' =&gt; 1,
      'access' =&gt; REQUEST_TIME,
      // This is where we are different from the default function and add our fields.
      'roles'        =&gt; array(array_search('staff', $roles) =&gt; 1),
      'mail' =&gt; $external_ldap_details['mail'],
      'field_first_name'   =&gt; array(LANGUAGE_NONE =&gt; array( 0 =&gt; array('value' =&gt; $external_ldap_details['first_name']))),
      'field_last_name'    =&gt; array(LANGUAGE_NONE =&gt; array( 0 =&gt; array('value' =&gt; $external_ldap_details['last_name']))),
    );
    $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" =&gt; $name));
  }

  // Log user in.
  $form_state['uid'] = $account-&gt;uid;
  user_login_submit(array(), $form_state);
}


I've actually done the same exact thing in Symfony 1.4.x a few years ago ... except it was all object oriented lol (because Symfony's forms are Objects).