Validating Registration Data with an External Iden

2019-05-18 15:11发布

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?

标签: drupal
2条回答
Root(大扎)
2楼-- · 2019-05-18 16:00

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.

查看更多
你好瞎i
3楼-- · 2019-05-18 16:05

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.

查看更多
登录 后发表回答