Projet

Général

Profil

Paste
Télécharger (5,14 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ldap / ldap_authorization / ldap_authorization.admin.inc @ 7547bb19

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Administrative page callbacks for the ldap_authorization module.  Consumer configuration form and index.
6
 */
7
8
9
/**
10
 * index of ldap authorization configurations
11
 *
12
 * @return string html table
13
 */
14
15
function ldap_authorizations_admin_index() {
16
17
  $consumers = ldap_authorization_get_consumers(NULL, TRUE, FALSE);
18
  if (!is_array($consumers) || count($consumers) == 0) {
19
    drupal_set_message(t('No authorization consumer modules are enabled.  Enable
20
    LDAP Authorization Drupal Roles, OG LDAP, or another LDAP Authorization consuming module'), 'warning');
21
  }
22
23
  $servers = ldap_servers_get_servers(NULL, 'enabled');
24
  if (count($servers) == 0) {
25
    return t('ldap authorization can not be set up until ldap servers are configured.') . '  ' .
26
    l(t('Add LDAP Server'), 'admin/config/people/ldap/servers/add');
27
  }
28
29
  foreach ($consumers as $consumer_type => $consumer) {
30
    $consumers[$consumer_type] = ldap_authorization_get_consumer_object($consumer_type);
31
  }
32
  return theme('ldap_authorization_admin_index', array('consumers' => $consumers));
33
34
}
35
36
37
38
/**
39
 * form for adding, updating, and deleting a single ldap authorization configuration
40
 *
41
 * @param form array $form
42
 * @param form state array $form_state
43
 * @param string $op (add, edit, or delete)
44
 * @param string $consumer_type e.g. drupal_roles, og_group, etc.  Only needed for adds
45
 * @return drupal form array
46
 */
47
48
function ldap_authorization_admin_form($form, &$form_state, $consumer_type, $op = NULL) {
49
  ldap_servers_module_load_include('php', 'ldap_authorization', 'LdapAuthorizationConsumerConfAdmin.class');
50
  $consumer = ldap_authorization_get_consumer_object($consumer_type);
51
52
  if ($op == 'add' && is_object($consumer->consumerConf) && $consumer->consumerConf->inDatabase) {
53
    drupal_set_message(t('Only one configuration is allowed per consumer type.
54
      Configuration already exists for the cosumer type %consumer_type.  Please edit that configuration.',
55
      array('%consumer_type' => $consumer_type)), 'warning');
56
    drupal_goto(LDAP_SERVERS_MENU_BASE_PATH . '/authorization');
57
  }
58
59
  if (($op == 'edit' || $op == 'delete') && !is_object($consumer->consumerConf)) {
60
    drupal_set_message(t('Bad LDAP Authorization Configuration URL.'), 'error');
61
    drupal_goto(LDAP_SERVERS_MENU_BASE_PATH . '/authorization');
62
  }
63
64
  $servers = ldap_servers_get_servers(NULL, 'enabled');
65
  if (count($servers) == 0) {
66
    drupal_set_message(t('No ldap servers configured.  Please configure a server before an ldap authorization.'), 'error');
67
    drupal_goto('admin/config/people/ldap/authorization');
68
  }
69
70
71
  $new = ($op == 'add');
72
  $consumer_conf_admin = new LdapAuthorizationConsumerConfAdmin($consumer, $new);
73
74
  foreach ($servers as $sid => $server) {
75
    $server_options[$sid] = $server->name;
76
  }
77
  return $consumer_conf_admin->drupalForm($server_options, $op);
78
79
}
80
81
82
/**
83
 * validate handler for the ldap_authorization_admin_form
84
 */
85
86
function ldap_authorization_admin_form_validate($form, &$form_state) {
87
88
  list($consumer, $op, $op_past, $new)  = _ldap_authorization_admin_parse_form($form, $form_state);
89
  $values = $form_state['values'];
90
  ldap_servers_module_load_include('php', 'ldap_authorization', 'LdapAuthorizationConsumerConfAdmin.class');
91
  $consumer_conf_admin = new LdapAuthorizationConsumerConfAdmin($consumer, $new);
92
93
  $errors = $consumer_conf_admin->drupalFormValidate($op, $values);
94
  foreach ($errors as $error_name => $error_text) {
95
    $error_text = check_plain($error_text);
96
    form_set_error($error_name, t($error_text));
97
  }
98
99
}
100
101
102
/**
103
 * submit handler function for ldap_authorization_admin_form
104
 */
105
106
function ldap_authorization_admin_form_submit($form, &$form_state) {
107
  list($consumer, $op, $op_past_tense, $new)  = _ldap_authorization_admin_parse_form($form, $form_state);
108
  $values = $form_state['values'];
109
  ldap_servers_module_load_include('php', 'ldap_authorization', 'LdapAuthorizationConsumerConfAdmin.class');
110
  $consumer_conf = new LdapAuthorizationConsumerConfAdmin($consumer, $new);
111
  $consumer_conf->drupalFormSubmit($op, $values);  // add form data to object and save or create
112
113
  if ($consumer_conf->hasError == FALSE) {
114
    drupal_set_message(t('LDAP Authorization %name !verb', array('!verb' => $op_past_tense, '%name' => $consumer->name)), 'status');
115
    drupal_goto(LDAP_SERVERS_MENU_BASE_PATH . '/authorization');
116
  }
117
118
  form_set_error($consumer_conf->errorName, $consumer_conf->errorMsg);
119
  $consumer_conf->clearError();
120
121
}
122
123
/**
124
 * helper function for parsing ldap authorization config form
125
 */
126
127
function _ldap_authorization_admin_parse_form($form, &$form_state) {
128
  $op = drupal_strtolower($form_state['clicked_button']['#value']);
129
  $values = $form_state['values'];
130
131
  if ($values['consumer_type']) {
132
    $consumer_type = $values['consumer_type'];
133
    $consumer = ldap_authorization_get_consumer_object($consumer_type);
134
  }
135
  else {
136
    return FALSE;
137
  }
138
139
  switch ($op) {
140
    case 'add':
141
      $op_past_tense = 'Added';
142
      $new = TRUE;
143
    break;
144
145
    case 'save':
146
    case 'update':
147
    case 'edit':
148
      $op_past_tense = 'Updated';
149
      $new = FALSE;
150
    break;
151
152
    case 'delete':
153
      $op_past_tense = 'Deleted';
154
      $new = FALSE;
155
    break;
156
  }
157
158
  return array($consumer, $op, $op_past_tense, $new);
159
160
}