/[drupal]/contributions/modules/webserver_auth/webserver_auth.module
ViewVC logotype

Contents of /contributions/modules/webserver_auth/webserver_auth.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.20 - (hide annotations) (download) (as text)
Mon Apr 21 16:43:26 2008 UTC (19 months, 1 week ago) by weitzman
Branch: MAIN
CVS Tags: DRUPAL-5--0-0
Branch point for: DRUPAL-5
Changes since 1.19: +15 -4 lines
File MIME type: text/x-php
#247961 by barry_johnson. REMOTE_USER vs REDIRECT_REMOTE_USER bug.
1 weitzman 1.1 <?php
2 weitzman 1.20 // $Id: webserver_auth.module,v 1.19 2008/03/10 21:43:42 weitzman Exp $
3 weitzman 1.19
4     function webserver_auth_menu($may_cache) {
5     if ($may_cache) {
6     $items[] = array(
7     'title' => t('Webserver authentication'),
8     'path' => "admin/settings/webserver_auth",
9     'callback' => "drupal_get_form",
10     'callback arguments' => array('webserver_auth_settings'),
11     'description' => t('Configure a domain for generating email addresses. Optional.'),
12     );
13     }
14     return $items;
15    
16     }
17 weitzman 1.1
18 weitzman 1.5 function webserver_auth_init() {
19     global $user, $account;
20 weitzman 1.10
21 weitzman 1.20 $remote_user = "";
22    
23     //lets make sure we get the remote user whichever way it is available
24     if (isset($_SERVER["REDIRECT_REMOTE_USER"])) {
25     $remote_user = $_SERVER["REDIRECT_REMOTE_USER"];
26     } elseif (isset($_SERVER["REMOTE_USER"])) {
27     $remote_user = $_SERVER["REMOTE_USER"];
28     }
29    
30     // two ways to get $name
31     if ($name != $remote_user) {
32     //this might be something to add as an admin panel function later
33     //$name = strtolower($remote_user);
34     $name = $remote_user;
35 weitzman 1.16 }
36    
37     if (isset($user) && $user->id && $user->name === $name) {
38     //do nothing because user is already logged into Drupal, and hasn't presented different credentials vis web server
39 weitzman 1.1 }
40     else {
41 weitzman 1.16 if ($name) {
42 weitzman 1.10 // user is logged into webserver.
43 weitzman 1.5 $account->name = $name;
44     //modules get to change the user bits before saving. use a global $account to do so.
45 weitzman 1.12 // only loaded modules will see this hook
46 weitzman 1.5 module_invoke_all("webserver_auth");
47 weitzman 1.12 // if we are in bootstrap, load user.module ourselves
48 weitzman 1.19 if (!module_exists('user')) {
49 weitzman 1.12 drupal_load('module', 'user');
50     }
51 weitzman 1.10
52 weitzman 1.5 // try to log into Drupal. if unsuccessful, register the user
53 weitzman 1.18 $test_user = user_external_load($account->name);
54     if (!$test_user->uid) {
55 weitzman 1.1 if (variable_get("user_register", 1) == 1) {
56 weitzman 1.14 $user_default = array("name" => $account->name, "pass" => "cyan", "init" => db_escape_string($name), "authname_webserver_auth" => $account->name, "status" => 1, "roles" => array(DRUPAL_AUTHENTICATED_RID));
57 weitzman 1.16 // TODO - the hook_user('register') will fire but only for loaded modules. could be a problem for sites using page cache and that hook+operation
58 weitzman 1.13 $user = user_save("", array_merge($user_default, (array)$account));
59 weitzman 1.5 watchdog("user", "new user: $user->name (webserver_auth)", l(t("edit user"), "admin/user/edit/$user->uid"));
60 weitzman 1.1 }
61     }
62 weitzman 1.18 else{
63     $user = $test_user;
64     }
65 weitzman 1.1 }
66     else {
67     // do nothing. user isn't logged into web server
68     }
69     }
70     }
71    
72 weitzman 1.5 // using a global to change your bits. module_invoke_all miffs me.
73     function webserver_auth_webserver_auth() {
74     global $account;
75 weitzman 1.10
76 weitzman 1.17 $account->name = trim($account->name);
77 weitzman 1.3 // pretties up the username for NTLM authentication (i.e. Windows)
78 weitzman 1.6 if ($_SERVER["AUTH_TYPE"] == "NTLM" || $_SERVER["AUTH_TYPE"] == 'Negotiate') {
79 weitzman 1.17 if (!(strpos($account->name, "\\") === false)) {
80     $account->name = substr($account->name, strrpos($account->name, "\\")+1);
81     }
82     if (!(strpos($account->name, "@") === false)) {
83     $account->name = substr($account->name, 0, strrpos($account->name, "@"));
84     }
85 weitzman 1.5 }
86 weitzman 1.10
87 weitzman 1.5 if ($domain = variable_get("webserver_auth_domain", "")) {
88     if ($account->name) {
89     $account->mail = $account->name. "@$domain";
90     }
91 weitzman 1.3 }
92     }
93    
94 weitzman 1.5 function webserver_auth_settings() {
95 weitzman 1.13 $form["webserver_auth_domain"] = array(
96     '#type' => 'textfield',
97     '#title' => t("Email Domain"),
98     '#default_value' => variable_get("webserver_auth_domain", ""),
99     '#size' => 30,
100     '#maxlength' => 55,
101 weitzman 1.19 '#description' => t("Append this domain name to each new user in order generate his email address. Currently only used for NTLM authentication."),
102 weitzman 1.13 );
103 weitzman 1.19 return system_settings_form($form);
104 weitzman 1.5 }
105    
106 weitzman 1.3 function webserver_auth_help($section) {
107     $output ="";
108    
109     switch ($section) {
110     case 'admin/help#webserver_auth':
111     break;
112 weitzman 1.8 case 'admin/modules#description':
113 weitzman 1.3 $output .= t("Use web server authentication instead of Drupal");
114     break;
115     }
116    
117     return $output;
118     }

  ViewVC Help
Powered by ViewVC 1.1.2