我正在一个Drupal项目中工作,其中一个自定义模块用于将用户重定向到另一个域进行身份验证,如果用户没有登录,我想将/user页面重定向到这个外部身份验证服务器。我该怎么做呢?
您可以通过使用user_is_anonymous()获取登录状态,然后通过'user'的第一个查询参数来捕获hook_init中的条件。
<?php
/**
* Implements hook_init().
*/
function mycustommodule_init() {
// if 1st argument is user, and they are not logged in, send them away
if (user_is_anonymous() && arg(0) == 'user') {
drupal_goto('http://example.com/login');
}
}
如果您能够使用PHP在后台进行身份验证,那么您还可以通过使用'login'操作的hook_user来使登录变得毫无问题。
我相信您可以使用rules
模块和actions
和triggers
配置的一些组合。
当用户在page/user且未登录时重定向到x
作为实现外部身份验证的模块示例,您可以查看openid。
http_auth_ext.moduleOpenID使用user_external_load(),这可能需要使用user_external_login_register()。
外部HTTP认证使用hook_init()。
function http_auth_ext_init() {
global $user;
// Call authentication on any page if it has not been already completed
if (! $user->uid && ! $_COOKIE['http_auth_ext_complete']) {
http_auth_ext_login($_SERVER['REMOTE_USER']);
}
}