/[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.21 - (show annotations) (download) (as text)
Thu Jul 17 20:28:39 2008 UTC (16 months, 1 week ago) by weitzman
Branch: MAIN
Changes since 1.20: +101 -95 lines
File MIME type: text/x-php
#206223 by Paul Kishimoto. Port to Drupal6.
1 <?php
2 // $Id$
3
4 function webserver_auth_menu() {
5 $items = array();
6 $items['admin/settings/webserver_auth'] = array(
7 'title' => t('Webserver authentication'),
8 'description' => t('Configure a domain for generating email addresses. Optional.'),
9 'page callback' => 'drupal_get_form',
10 'page arguments' => array('webserver_auth_settings'),
11 'access arguments' => array('administer site configuration'),
12 );
13 return $items;
14 }
15
16 function webserver_auth_init() {
17 global $user;
18
19 $authname = '';
20
21 // Make sure we get the remote user whichever way it is available.
22 if (isset($_SERVER['REDIRECT_REMOTE_USER'])) {
23 $authname = $_SERVER['REDIRECT_REMOTE_USER'];
24 }
25 elseif (isset($_SERVER['REMOTE_USER'])) {
26 $authname = $_SERVER['REMOTE_USER'];
27 }
28
29 // Perform some cleanup so plaintext passwords aren't available under
30 // mod_auth_kerb.
31 unset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
32
33 // Retrieve user credentials
34 $result = db_query("SELECT uid FROM {authmap} WHERE authname = '%s' AND module = 'webserver_auth'", $authname);
35 $expected = db_fetch_array($result);
36
37 if (isset($user) && $user->uid === $expected['uid']) {
38 // Do nothing: user is already logged into Drupal with session data matching
39 // HTTP authentication.
40 }
41 else {
42 if (!empty($authname)) {
43 // User is logged into webserver via HTTP authentication.
44 // Try to log into Drupal.
45 $user = user_external_load($authname);
46
47 if (!$user) {
48 // If unsuccessful, register the user. This will trigger
49 // webserver_auth_user() and any other _user() hooks.
50 user_external_login_register($authname, 'webserver_auth');
51 }
52 }
53 }
54 }
55
56 /**
57 * Implementation of hook_user().
58 */
59 function webserver_auth_user($op, &$edit, &$account, $category = NULL) {
60 if ($op == 'insert' && $category = 'account') {
61 $account->name = trim($account->name);
62 // Pretty up the username for NTLM authentication (i.e. Windows)
63 if (variable_get('webserver_auth_strip_prefix', TRUE)) {
64 // Get 'bar' from 'foo1\foo2\bar'
65 $account->name = array_pop(explode("\\", $account->name));
66 }
67 if (variable_get('webserver_auth_strip_domain', TRUE)) {
68 // Get 'foo' from 'foo@bar'
69 $account->name = array_shift(explode('@', $account->name));
70 }
71
72 // Generate an e-mail address automagically
73 if ($domain = variable_get('webserver_auth_email_domain', '')) {
74 if ($account->name) {
75 $account->mail = $account->name. '@'. $domain;
76 }
77 }
78 // run some custom code to modify the user object at creation time
79 if ($code = variable_get('webserver_auth_insert', '')) {
80 eval('?>'. $code);
81 }
82 }
83 elseif ($op == 'logout') {
84 global $base_url;
85 // kick user out of a secure session so they aren't automatically logged back in
86 $base_url = str_replace('https://', 'http://', $base_url);
87 }
88 }
89
90 function webserver_auth_settings() {
91 $form['webserver_auth_email_domain'] = array(
92 '#type' => 'textfield',
93 '#title' => t('Email domain'),
94 '#default_value' => variable_get('webserver_auth_email_domain', ''),
95 '#size' => 30,
96 '#maxlength' => 55,
97 '#description' => t('Append this domain name to each new user in order generate his email address.'),
98 );
99 $form['advanced'] = array(
100 '#type' => 'fieldset',
101 '#title' => t('Advanced settings'),
102 '#collapsible' => TRUE,
103 '#collapsed' => TRUE,
104 'webserver_auth_strip_prefix' => array(
105 '#type' => 'checkbox',
106 '#title' => t('Strip prefix'),
107 '#default_value' => variable_get('webserver_auth_strip_prefix', TRUE),
108 '#description' => t("Strip NTLM-style prefixes (e.g. 'foo1\foo2') from the login name ('foo1\foo2\bar') to generate the username ('bar')."),
109 ),
110 'webserver_auth_strip_domain' => array(
111 '#type' => 'checkbox',
112 '#title' => t('Strip domain'),
113 '#default_value' => variable_get('webserver_auth_strip_domain', TRUE),
114 '#description' => t("Strip a domain name (e.g. '@EXAMPLE.COM') from the login name ('newuser@EXAMPLE.COM') to generate the username ('newuser')."),
115 ),
116 'webserver_auth_insert' => array(
117 '#type' => 'textarea',
118 '#title' => 'User account modification',
119 '#default_value' => variable_get('webserver_auth_insert', ''),
120 '#description' => t("Modify user accounts at the time of creation. Use PHP code (enclosed in <code>&lt;?php</code> and <code>?&gt;</code>). The variable <code>\$account</code> is available as in <a href=\"http://api.drupal.org/api/function/hook_user/6\">hook_user('insert',...)</a>. Changes to the user object must be explicitly saved to the database to be made permanent."),
121 ),
122 );
123 return system_settings_form($form);
124 }

  ViewVC Help
Powered by ViewVC 1.1.2