开发者

How do I redirect the users who aren't logged in when they go to the /user page?

开发者 https://www.devze.com 2023-02-27 14:34 出处:网络
I\'m working on a Drupal project where a custom module is used to r开发者_JAVA技巧edirect users to another domain for authentication and I wanted to have the /user page redirect to this outside authen

I'm working on a Drupal project where a custom module is used to r开发者_JAVA技巧edirect users to another domain for authentication and I wanted to have the /user page redirect to this outside authentication server if the user is not logged in. How should I go about doing this?


You can capture the condition in hook_init by taking the login status with user_is_anonymous() and then by the 1st query argument of 'user'.

<?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');
  }
}

If you were able to authenticate behind the scenes with PHP, you could also make the login seemless by using hook_user with the 'login' operation.


I'm sure you could use some combination of the rules module and actions and triggers configuration.

When the user is on page /user and they are not logged in redirect to x.


As examples of modules implementing external authentication, you could look at openid.module, and http_auth_ext.module.
OpenID uses user_external_load(), which probably requires to use user_external_login_register().
External HTTP authentication uses 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']);
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消