tags:

views:

153

answers:

2

When my user tries to register, I'd like to make sure his information is valid, by checking an external identity repository (e.g. call a web service or look up a directory server).

Is that possible with any existing module? If not, what would be the best way to develop this functionality?

+1  A: 

If you are using an LDAP server to authenticate, there is the LDAP module. Check that out.

For authenticating with some other web service, you'll have to write a module and implement hook_user, particularly the 'login' case. If, upon login, the user's credentials do not match those in your web service, you can log them out and display a message, perhaps.

theunraveler
+1 for LDAP module and `hook_user` recommendation, but I disagree on the 'check on login' part. The check should happen *before* the login/registration, using the 'validate' action. That way the user will not be logged in in the first place, so no need to log them out again - see my example in separate answer.
Henrik Opel
Interesting...I never would have found that out from the "validate" documentation on api.drupal.org. Seems like that one needs a rewrite to be a bit clearer. Thanks for the correction though.
theunraveler
@theunraveler: To prevent a misunderstanding here - using `form_set_error()` during the 'validate' action would only prevent the login after *registration*, as it is the registration form being validated (and the account edit form, if one does not check for that). It would not work to prevent the *login* of an existing user, in which case your approach would be an option (with the alternative of adding a custom validate callback to the login form).
Henrik Opel
That's what I thought and, hence, why I didn't suggest it :-)
theunraveler
+1  A: 

I'm not aware of an existing module allowing the addition of custom validation, but it is fairly easy to implement this using the 'validate' action of hook_user():

function yourModule_user($op, &$edit, &$account, $category = NULL) {
  // Are we in the validation phase of a new user registration?
  if ('validate' == $op && 'user_register' == $edit['form_id'] && 'account' == $category) {
    // Yes, do custom validation...
    // NOTE: Just an example to validate by email.
    // Check the other elements in $edit array (e.g. 'name') for more options
    $mail_is_valid = yourModule_custom_mail_validation($edit['mail']);
    // Is the mail address OK?
    if (!$mail_is_valid) {
      // No, set error on mail form field
      form_set_error('mail', t('your custom error message'));
    }
  }
}

This would stop the registration process and redisplay the registration form with the error message on the mail field as long as yourModule_custom_mail_validation() does not return TRUE.

If you want the validation to happen for existing users editing their account also, you could drop the

 && 'user_register' == $edit['form_id']

part from the first if clause - the code would then run for every user edit form submission, not just on registration.

Henrik Opel

related questions