Projet

Général

Profil

Paste
Télécharger (12,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_authorization / ldap_authorization.module @ 91af538d

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'] = [
35
    'title' => 'Authorization',
36
    'page callback' => 'ldap_authorizations_admin_index',
37
    'page arguments' => [],
38
    'type' => MENU_LOCAL_TASK,
39
    'access arguments' => ['administer site configuration'],
40
    'file' => 'ldap_authorization.admin.inc',
41
    'weight' => 3,
42
  ];
43

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

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

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

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

    
75
  $items['admin/config/people/ldap/authorization/add/%'] = [
76
    'title' => 'Add Authorization Configuration',
77
    'page callback' => 'drupal_get_form',
78
    'page arguments' => ['ldap_authorization_admin_form', 6, 'add'],
79
    'access arguments' => ['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', ['title' => $consumer_type, 'items' => array_keys($authorization_ids), 'type' => 'ul']);
94
      watchdog('ldap_authentication', 'ldap_authorization_user_login.authorizations' . $ul, [], 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 = [];
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
          $attribute_maps[$attribute_name] = ldap_servers_set_attribute_map($attribute_name);
145
        }
146
      }
147
    }
148
  }
149

    
150
}
151

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

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

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

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

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

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

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

    
245
}
246

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

    
276
  }
277
  return [$new_authorizations, $notifications];
278
}
279

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

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

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

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

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

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

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

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

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

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

    
370
}