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, &$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' => $name))->fetch())
{
// A user exists. Loads the user information.
$user = user_load($user->uid);
// If the user is an administator, log him in using the Drupal validator.
if (in_array("administrator", $user->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->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).