Projet

Général

Profil

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

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

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
/**
19
 * this is a workaround for og 7.x-2.x bug I believe
20
 */
21

    
22
function ldap_authorization_cleanse_empty_og_fields(&$user) {
23
  if (property_exists($user, 'og_user_group_ref') && is_array($user->og_user_group_ref) && count($user->og_user_group_ref) == 0) {
24
    unset($user->og_user_group_ref);
25
  }
26
  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 ) {
27
    unset($user->og_other_user_group_ref);
28
  }
29
}
30

    
31
/**
32
 * Implements hook_menu().
33
 */
34
function ldap_authorization_menu() {
35

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

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

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

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

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

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

    
85
  return $items;
86
}
87

    
88

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

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

    
115
/**
116
 * Implements hook_ldap_ldap_server_in_use().
117
 */
118
function ldap_authorization_ldap_server_in_use($sid, $server_name) {
119

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

    
134
/**
135
 * Implements hook_ldap_attributes_needed_alter().
136
 */
137
function ldap_authorization_ldap_attributes_needed_alter(&$attribute_maps, $params) {
138

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

    
153
}
154

    
155
/**
156
 * Implements hook_ldap_authorization_maps_alter().
157
 *
158
 * to suggest authorization ids to grant (drupal roles in this case)
159
 *
160
 * @param object $user drupal user object
161
 *
162
 * @param array $user_ldap_entry 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
 * @param array $authz_ids.  any new authorization ids (drupal user role names in this case) in form array('rolename1', 'rolename2',....)
169
 *
170
 * @param string $op = 'set' or 'query'
171
 *
172
 */
173
function ldap_authorization_ldap_authorization_maps_alter(&$user, &$user_ldap_entry, &$ldap_server, &$consumer_conf, &$authz_ids, $op) {
174

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

    
179

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

    
195
/**
196
 * param string $consumer_type is machine name of consumer such as drupal_role
197
 *
198
 * @return consumer object
199
 */
200
function ldap_authorization_get_consumer_object($consumer_type) {
201

    
202
  $consumer = ldap_authorization_get_consumers($consumer_type, TRUE, TRUE);
203

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

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

    
239
}
240

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

    
269
  }
270
  return array($new_authorizations, $notifications);
271
}
272

    
273
function ldap_authorization_help($path, $arg) {
274

    
275
  $authorization_help = t('LDAP authorization allows LDAP data such as group memberships,
276
    user attributes, etc to determine user authorization (Drupal roles, organic group memberships,
277
    etc.)
278
    More detailed help is available on drupal.org at !helplink.',
279
          array(
280
            '!helplink' => l(LDAP_SERVERS_DRUPAL_HELP_URL, LDAP_SERVERS_DRUPAL_HELP_URL),
281
          ));
282

    
283
  switch ($path) {
284
    case 'admin/config/people/ldap/authorization':
285
      $output = '<p>' . $authorization_help . '</p>';
286
      return $output;
287

    
288
    case 'admin/help#ldap_authorization':
289
      $output = '<p>' . $authorization_help . '</p>';
290
      return $output;
291
  }
292
}
293

    
294
/** just tokens for better watchdog and drupal_set_message arguments **/
295

    
296
function ldap_authorization_tokens($consumer) {
297
  $tokens = array();
298

    
299
  if (is_object($consumer)) {
300
    foreach (array('%', '!', '@') as $symbol) {
301
      foreach (array('name', 'namePlural', 'shortName', 'shortNamePlural', 'consumerModule', 'consumerType', 'mappingDirections', 'testLink', 'editLink') as $property) {
302
        $tokens[$symbol . 'consumer_' . $property] = $consumer->$property;
303
      }
304
      if (is_object($consumer->consumerConf)) {
305
        foreach (array('sid', 'status') as $property) {
306
          $tokens[$symbol . 'consumer_' . $property] = $consumer->consumerConf->$property;
307
        }
308
      }
309
    }
310
  }
311
  return $tokens;
312
}
313

    
314

    
315
function ldap_authorization_get_consumer_admin_object($consumer_type, $new = NULL) {
316
  ldap_servers_module_load_include('php', 'ldap_authorization', 'LdapAuthorizationConsumerConfAdmin.class');
317
  $consumer_object = ldap_authorization_get_consumer_object($consumer_type);
318
  $consumer_conf_admin = new LdapAuthorizationConsumerConfAdmin($consumer_object, $new);
319
  return $consumer_conf_admin;
320
}
321

    
322
function ldap_authorization_get_consumer_conf($consumer_type, $type = 'default', $new = NULL) {
323
  $consumer_object = ldap_authorization_get_consumer_object($consumer_type);
324
  if ($type == 'admin') {
325
    ldap_servers_module_load_include('php', 'ldap_authorization', 'LdapAuthorizationConsumerConfAdmin.class');
326
    $consumer_conf = new LdapAuthorizationConsumerConfAdmin($consumer_object, $new);
327
  }
328
  else {
329
    ldap_servers_module_load_include('php', 'ldap_authorization', 'LdapAuthorizationConsumerConf.class');
330
    $consumer_conf = new LdapAuthorizationConsumerConf($consumer_object, $new);
331
  }
332
  return $consumer_conf;
333
}
334

    
335

    
336
/**
337
 * Implements hook_ldap_user_attrs_list_alter().
338
 */
339

    
340
function ldap_authorization_ldap_user_attrs_list_alter(&$available_user_attrs, &$params) {
341

    
342
  // this is a case where a field is being denied synching configuration because its not meant to be used by other modules
343
  $available_user_attrs['[field.ldap_authorizations]'] = array(
344
    'name' => 'Field: LDAP Authorizations',
345
    'source' => 'LDAP Authorization modules',
346
    'configurable_to_drupal' => 0,
347
    'configurable_to_ldap' => 0,
348
    'notes' => t('configure at:') . 'admin/config/people/ldap/authorization',
349
    'direction' => NULL,
350
    'enabled' => FALSE,
351
    'config_module' => 'ldap_authorization',
352
    'prov_events' => array(),
353
    'prov_module' => 'ldap_authorization',
354
    'exclude_from_mapping_ui' => TRUE,
355
  );
356

    
357
}