Projet

Général

Profil

Paste
Télécharger (13,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_authorization / ldap_authorization.module @ bc175c27

1
<?php
2

    
3
/**
4
 * @file
5
 * Ldap authorization module.
6
 */
7

    
8
define('LDAP_AUTHORIZATION_PROJECT_TAG', 'ldap_authorization');
9
define('LDAP_AUTHORIZATION_USER_LDAP_NOT_FOUND', 101);
10
define('LDAP_AUTHORIZATION_USER_NOT_LDAP_AUTHENTICATED', 102);
11
define('LDAP_AUTHORIZATION_MAP_NOT_CONF_FOR_LOGON', 103);
12
define('LDAP_AUTHORIZATION_NOT_APPLY_USER_1', 104);
13
define('LDAP_AUTHORIZATION_SERVER_CONFIG_NOT_FOUND', 105);
14

    
15
define('LDAP_AUTHORIZATION_NO_LDAP_SERVERS', 'ldap_authorization_no_ldap_servers');
16

    
17
/**
18
 * This is a workaround for og 7.x-2.x bug I believe.
19
 */
20
function ldap_authorization_cleanse_empty_og_fields(&$user) {
21
  if (property_exists($user, 'og_user_group_ref') && is_array($user->og_user_group_ref) && count($user->og_user_group_ref) == 0) {
22
    unset($user->og_user_group_ref);
23
  }
24
  if (property_exists($user, 'og_other_user_group_ref') && is_array($user->og_other_user_group_ref) && count($user->og_other_user_group_ref) == 0) {
25
    unset($user->og_other_user_group_ref);
26
  }
27
}
28

    
29
/**
30
 * Implements hook_menu().
31
 */
32
function ldap_authorization_menu() {
33

    
34
  $items['admin/config/people/ldap/authorization'] = array(
35
    'title' => 'Authorization',
36
    'page callback' => 'ldap_authorizations_admin_index',
37
    'page arguments' => array(),
38
    'type' => MENU_LOCAL_TASK,
39
    'access arguments' => array('administer site configuration'),
40
    'file' => 'ldap_authorization.admin.inc',
41
    'weight' => 3,
42
  );
43

    
44
  $items['admin/config/people/ldap/authorization/list'] = array(
45
    'title' => 'List',
46
    'type' => MENU_DEFAULT_LOCAL_TASK,
47
  );
48

    
49
  $items['admin/config/people/ldap/authorization/edit/%'] = array(
50
    'title' => 'Edit LDAP Authorization Configuration',
51
    'page callback' => 'drupal_get_form',
52
    'page arguments' => array('ldap_authorization_admin_form', 6, 'edit'),
53
    'access arguments' => array('administer site configuration'),
54
    'file' => 'ldap_authorization.admin.inc',
55
  );
56

    
57
  $items['admin/config/people/ldap/authorization/delete/%'] = array(
58
    'title' => 'Delete LDAP Authorization Configuration',
59
    'description' => 'Delete an ldap authorization configuration',
60
    'page callback' => 'drupal_get_form',
61
    'page arguments' => array('ldap_authorization_admin_form', 6, 'delete'),
62
    'access arguments' => array('administer site configuration'),
63
    'file' => 'ldap_authorization.admin.inc',
64
  );
65

    
66
  $items['admin/config/people/ldap/authorization/test/%'] = array(
67
    'title' => 'Test LDAP Authorization Configuration',
68
    'description' => 'Test an ldap authorization configuration',
69
    'page callback' => 'drupal_get_form',
70
    'page arguments' => array('ldap_authorization_test_form', 6, 'test'),
71
    'access arguments' => array('administer site configuration'),
72
    'file' => 'ldap_authorization.admin.test.inc',
73
  );
74

    
75
  $items['admin/config/people/ldap/authorization/add/%'] = array(
76
    'title' => 'Add Authorization Configuration',
77
    'page callback' => 'drupal_get_form',
78
    'page arguments' => array('ldap_authorization_admin_form', 6, 'add'),
79
    'access arguments' => array('administer site configuration'),
80
    'file' => 'ldap_authorization.admin.inc',
81
  );
82

    
83
  return $items;
84
}
85

    
86
/**
87
 * Implements hook_user_login() login operation.
88
 */
89
function ldap_authorization_user_login(&$edit, $user) {
90
  list($authorizations, $notifications) = ldap_authorizations_user_authorizations($user, 'set', NULL, 'logon');
91
  if (variable_get('ldap_help_watchdog_detail', FALSE)) {
92
    foreach ($authorizations as $consumer_type => $authorization_ids) {
93
      $ul = theme('item_list', array('title' => $consumer_type, 'items' => array_keys($authorization_ids), 'type' => 'ul'));
94
      watchdog('ldap_authentication', 'ldap_authorization_user_login.authorizations' . $ul, array(), WATCHDOG_DEBUG);
95
    }
96
  }
97
}
98

    
99
/**
100
 * Ldap_authorization_maps_alter_invoke invokes hook_ldap_authorization_maps_alter() in every module.
101
 *
102
 * We cannot use module_invoke() for this, because the arguments need to
103
 * be passed by reference.
104
 */
105
function ldap_authorization_maps_alter_invoke(&$user, &$user_ldap_entry, &$ldap_server, &$consumer_conf, &$proposed_ldap_authorizations, $op) {
106
  foreach (module_implements('ldap_authorization_maps_alter') as $module) {
107
    $function = $module . '_ldap_authorization_maps_alter';
108
    $function($user, $user_ldap_entry, $ldap_server, $consumer_conf, $proposed_ldap_authorizations, $op);
109
  }
110
}
111

    
112
/**
113
 * Implements hook_ldap_ldap_server_in_use().
114
 */
115
function ldap_authorization_ldap_server_in_use($sid, $server_name) {
116

    
117
  $use_warnings = array();
118
  $consumers = ldap_authorization_get_consumers();
119
  foreach (ldap_authorization_get_consumers() as $consumer_type => $consumer_conf) {
120
    $consumer_conf['%server_name'] = $server_name;
121
    $consumer_obj = ldap_authorization_get_consumer_object($consumer_type);
122
    if ($sid == $consumer_obj->consumerConf->sid) {
123
      $use_warnings[] = t('This server (%server_name) may not
124
        be deleted or disabled because it is being used by the module consumer_module to
125
        authorize consumer_name_plural.', $consumer_conf);
126
    }
127
  }
128
  return $use_warnings;
129
}
130

    
131
/**
132
 * Implements hook_ldap_attributes_needed_alter().
133
 */
134
function ldap_authorization_ldap_attributes_needed_alter(&$attribute_maps, $params) {
135

    
136
  if (isset($params['ldap_context'])) {
137
    $parts = explode('__', $params['ldap_context']);
138
    if (count($parts) == 2 && $parts[0] == 'ldap_authorization') {
139
      $consumer_type = $parts[1];
140
      $consumer_conf = ldap_authorization_get_consumer_conf($consumer_type);
141
      if ($consumer_conf->server->groupUserMembershipsAttrExists) {
142
        $attribute_name = $consumer_conf->server->groupUserMembershipsAttr;
143
        if ($attribute_name) {
144
          // array($attribute_name, 0, NULL);.
145
          $attribute_maps[$attribute_name] = ldap_servers_set_attribute_map($attribute_name);
146
        }
147
      }
148
    }
149
  }
150

    
151
}
152

    
153
/**
154
 * Implements hook_ldap_authorization_maps_alter().
155
 *
156
 * To suggest authorization ids to grant (drupal roles in this case)
157
 *
158
 * @param object $user
159
 *   drupal user object.
160
 *
161
 * @param array $user_ldap_entry
162
 *   is ldap data from ldap entry which drupal user is mapped to.
163
 *
164
 * @param object $ldap_server
165
 *
166
 * @param array $consumer_conf
167
 *   .
168
 *
169
 * @param array $authz_ids
170
 *   any new authorization ids (drupal user role names in this case) in form array('rolename1', 'rolename2',....)
171
 *
172
 * @param string $op
173
 *   = 'set' or 'query'.
174
 */
175
function ldap_authorization_ldap_authorization_maps_alter(&$user, &$user_ldap_entry, &$ldap_server, &$consumer_conf, &$authz_ids, $op) {
176

    
177
  ldap_servers_module_load_include('inc', 'ldap_authorization', 'ldap_authorization');
178
  _ldap_authorization_ldap_authorization_maps_alter($user, $user_ldap_entry, $ldap_server, $consumer_conf, $authz_ids, $op);
179
}
180

    
181
/**
182
 *
183
 */
184
function ldap_authorization_theme() {
185
  return array(
186
    'ldap_authorization_test_results' => array(
187
      'variables' => array('data' => NULL),
188
      'render element' => 'element',
189
      'file' => 'ldap_authorization.theme.inc',
190
    ),
191
    'ldap_authorization_admin_index' => array(
192
      'variables' => array('consumers' => NULL),
193
      'render element' => 'element',
194
      'file' => 'ldap_authorization.theme.inc',
195
    ),
196
  );
197
}
198

    
199
/**
200
 * Param string $consumer_type is machine name of consumer such as drupal_role.
201
 *
202
 * @return consumer object
203
 */
204
function ldap_authorization_get_consumer_object($consumer_type) {
205

    
206
  $consumer = ldap_authorization_get_consumers($consumer_type, TRUE, TRUE);
207

    
208
  if ($consumer) {
209
    require_once drupal_get_path('module', $consumer['consumer_module']) . '/' . $consumer['consumer_class_file'];
210
    $class = $consumer['consumer_class_name'];
211
    $consumer_obj = new $class($consumer_type);
212
    $consumer_obj->detailedWatchdogLog = variable_get('ldap_help_watchdog_detail', 0);
213
    return $consumer_obj;
214
  }
215
  else {
216
    return FALSE;
217
  }
218
}
219

    
220
/**
221
 * @param string $consumer_type
222
 *   is machine name of consumer type such as "drupal_role".
223
 * @param bool $reset
224
 *   signifies clear static variable.
225
 * @param bool $flatten
226
 *   signies return individual consumer not keyed on consumer type.
227
 *
228
 * @return array (1) if $flatten is true, consumer configuration array
229
 *   otherwise (2) associative array of consumer configurations keyed on consumer type such as "drupal_role"
230
 */
231
function ldap_authorization_get_consumers($consumer_type = NULL, $reset = FALSE, $flatten = FALSE) {
232
  static $consumers;
233
  if ($reset || !is_array($consumers)) {
234
    $consumers = module_invoke_all('ldap_authorization_consumer');
235
  }
236
  if (!$consumer_type) {
237
    return $consumers;
238
  }
239
  elseif ($flatten) {
240
    return isset($consumers[$consumer_type]) ? $consumers[$consumer_type] : FALSE;
241
  }
242
  else {
243
    return isset($consumers[$consumer_type]) ? array($consumer_type => $consumers[$consumer_type]) : array();
244
  }
245

    
246
}
247

    
248
/**
249
 * @rationale: need not be called from hook_user, so this function separated out
250
 * so it can be called from a batch synchronization process for example
251
 *
252
 * @param drupal user object $user
253
 * @param string $op
254
 *   indicating operation such as query, set, test_query, etc.
255
 * @param string $consumer_type
256
 *   e.g. drupal_role, or og_groups.
257
 * @param string $context
258
 *
259
 * @return array of form:
260
 *   $authorizations[<consumer_type>][<authorization_id>]
261
 */
262
function ldap_authorizations_user_authorizations(&$user, $op = 'query', $consumer_type = NULL, $context = NULL) {
263
  ldap_servers_module_load_include('inc', 'ldap_authorization', 'ldap_authorization');
264
  if ($consumer_type != NULL) {
265
    list($new_authorizations, $notifications) = _ldap_authorizations_user_authorizations($user, $op, $consumer_type, $context);
266
  }
267
  else {
268
    $consumers = ldap_authorization_get_consumers();
269
    $new_authorizations = array();
270
    $notifications = array();
271
    foreach ($consumers as $consumer_type => $consumer) {
272
      list($new_authorizations_i, $notifications_i) = _ldap_authorizations_user_authorizations($user, $op, $consumer_type, $context);
273
      $new_authorizations = $new_authorizations + $new_authorizations_i;
274
      $notifications = $notifications + $notifications_i;
275
    }
276

    
277
  }
278
  return array($new_authorizations, $notifications);
279
}
280

    
281
/**
282
 *
283
 */
284
function ldap_authorization_help($path, $arg) {
285

    
286
  $authorization_help = t('LDAP authorization allows LDAP data such as group memberships,
287
    user attributes, etc to determine user authorization (Drupal roles, organic group memberships,
288
    etc.)
289
    More detailed help is available on drupal.org at !helplink.',
290
          array(
291
            '!helplink' => l(LDAP_SERVERS_DRUPAL_HELP_URL, LDAP_SERVERS_DRUPAL_HELP_URL),
292
          ));
293

    
294
  switch ($path) {
295
    case 'admin/config/people/ldap/authorization':
296
      $output = '<p>' . $authorization_help . '</p>';
297
      return $output;
298

    
299
    case 'admin/help#ldap_authorization':
300
      $output = '<p>' . $authorization_help . '</p>';
301
      return $output;
302
  }
303
}
304

    
305
/**
306
 * Just tokens for better watchdog and drupal_set_message arguments .**/
307
function ldap_authorization_tokens($consumer) {
308
  $tokens = array();
309

    
310
  if (is_object($consumer)) {
311
    foreach (array('%', '!', '@') as $symbol) {
312
      foreach (array('name', 'namePlural', 'shortName', 'shortNamePlural', 'consumerModule', 'consumerType', 'mappingDirections', 'testLink', 'editLink') as $property) {
313
        $tokens[$symbol . 'consumer_' . $property] = $consumer->$property;
314
      }
315
      if (is_object($consumer->consumerConf)) {
316
        foreach (array('sid', 'status') as $property) {
317
          $tokens[$symbol . 'consumer_' . $property] = $consumer->consumerConf->$property;
318
        }
319
      }
320
    }
321
  }
322
  return $tokens;
323
}
324

    
325
/**
326
 *
327
 */
328
function ldap_authorization_get_consumer_admin_object($consumer_type, $new = NULL) {
329
  ldap_servers_module_load_include('php', 'ldap_authorization', 'LdapAuthorizationConsumerConfAdmin.class');
330
  $consumer_object = ldap_authorization_get_consumer_object($consumer_type);
331
  $consumer_conf_admin = new LdapAuthorizationConsumerConfAdmin($consumer_object, $new);
332
  return $consumer_conf_admin;
333
}
334

    
335
/**
336
 *
337
 */
338
function ldap_authorization_get_consumer_conf($consumer_type, $type = 'default', $new = NULL) {
339
  $consumer_object = ldap_authorization_get_consumer_object($consumer_type);
340
  if ($type == 'admin') {
341
    ldap_servers_module_load_include('php', 'ldap_authorization', 'LdapAuthorizationConsumerConfAdmin.class');
342
    $consumer_conf = new LdapAuthorizationConsumerConfAdmin($consumer_object, $new);
343
  }
344
  else {
345
    ldap_servers_module_load_include('php', 'ldap_authorization', 'LdapAuthorizationConsumerConf.class');
346
    $consumer_conf = new LdapAuthorizationConsumerConf($consumer_object, $new);
347
  }
348
  return $consumer_conf;
349
}
350

    
351
/**
352
 * Implements hook_ldap_user_attrs_list_alter().
353
 */
354
function ldap_authorization_ldap_user_attrs_list_alter(&$available_user_attrs, &$params) {
355

    
356
  // This is a case where a field is being denied synching configuration because its not meant to be used by other modules.
357
  $available_user_attrs['[field.ldap_authorizations]'] = array(
358
    'name' => 'Field: LDAP Authorizations',
359
    'source' => 'LDAP Authorization modules',
360
    'configurable_to_drupal' => 0,
361
    'configurable_to_ldap' => 0,
362
    'notes' => t('configure at:') . 'admin/config/people/ldap/authorization',
363
    'direction' => NULL,
364
    'enabled' => FALSE,
365
    'config_module' => 'ldap_authorization',
366
    'prov_events' => array(),
367
    'prov_module' => 'ldap_authorization',
368
    'exclude_from_mapping_ui' => TRUE,
369
  );
370

    
371
}