Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Administrative page callbacks for the ldap_authorization module.  Consumer configuration form and index.
6
 */
7

    
8
/**
9
 * Index of ldap authorization configurations.
10
 *
11
 * @return string html table
12
 */
13
function ldap_authorizations_admin_index() {
14

    
15
  $consumers = ldap_authorization_get_consumers(NULL, TRUE, FALSE);
16
  if (!is_array($consumers) || count($consumers) == 0) {
17
    drupal_set_message(t('No authorization consumer modules are enabled.  Enable
18
    LDAP Authorization Drupal Roles, OG LDAP, or another LDAP Authorization consuming module'), 'warning');
19
  }
20

    
21
  $servers = ldap_servers_get_servers(NULL, 'enabled');
22
  if (count($servers) == 0) {
23
    return t('ldap authorization can not be set up until ldap servers are configured.') . '  ' .
24
    l(t('Add LDAP Server'), 'admin/config/people/ldap/servers/add');
25
  }
26

    
27
  foreach ($consumers as $consumer_type => $consumer) {
28
    $consumers[$consumer_type] = ldap_authorization_get_consumer_object($consumer_type);
29
  }
30
  return theme('ldap_authorization_admin_index', ['consumers' => $consumers]);
31

    
32
}
33

    
34
/**
35
 * Form for adding, updating, and deleting a single ldap authorization configuration.
36
 *
37
 * @param array $form
38
 * @param array $form_state
39
 * @param string $op
40
 *   (add, edit, or delete)
41
 * @param string $consumer_type
42
 *   e.g. drupal_roles, og_group, etc.  Only needed for adds.
43
 *
44
 * @return array
45
 */
46
function ldap_authorization_admin_form($form, &$form_state, $consumer_type, $op = NULL) {
47
  ldap_servers_module_load_include('php', 'ldap_authorization', 'LdapAuthorizationConsumerConfAdmin.class');
48
  $consumer = ldap_authorization_get_consumer_object($consumer_type);
49

    
50
  if ($op == 'add' && is_object($consumer->consumerConf) && $consumer->consumerConf->inDatabase) {
51
    drupal_set_message(t('Only one configuration is allowed per consumer type.
52
      Configuration already exists for the cosumer type %consumer_type.  Please edit that configuration.',
53
      ['%consumer_type' => $consumer_type]), 'warning');
54
    drupal_goto(LDAP_SERVERS_MENU_BASE_PATH . '/authorization');
55
  }
56

    
57
  if (($op == 'edit' || $op == 'delete') && !is_object($consumer->consumerConf)) {
58
    drupal_set_message(t('Bad LDAP Authorization Configuration URL.'), 'error');
59
    drupal_goto(LDAP_SERVERS_MENU_BASE_PATH . '/authorization');
60
  }
61

    
62
  $servers = ldap_servers_get_servers(NULL, 'enabled');
63
  if (count($servers) == 0) {
64
    drupal_set_message(t('No ldap servers configured.  Please configure a server before an ldap authorization.'), 'error');
65
    drupal_goto('admin/config/people/ldap/authorization');
66
  }
67

    
68
  $new = ($op == 'add');
69
  $consumer_conf_admin = new LdapAuthorizationConsumerConfAdmin($consumer, $new);
70

    
71
  foreach ($servers as $sid => $server) {
72
    $server_options[$sid] = $server->name;
73
  }
74
  return $consumer_conf_admin->drupalForm($server_options, $op);
75

    
76
}
77

    
78
/**
79
 * Validate handler for the ldap_authorization_admin_form.
80
 */
81
function ldap_authorization_admin_form_validate($form, &$form_state) {
82

    
83
  list($consumer, $op, $op_past, $new) = _ldap_authorization_admin_parse_form($form, $form_state);
84
  $values = $form_state['values'];
85
  ldap_servers_module_load_include('php', 'ldap_authorization', 'LdapAuthorizationConsumerConfAdmin.class');
86
  $consumer_conf_admin = new LdapAuthorizationConsumerConfAdmin($consumer, $new);
87

    
88
  $errors = $consumer_conf_admin->drupalFormValidate($op, $values);
89
  foreach ($errors as $error_name => $error_text) {
90
    $error_text = check_plain($error_text);
91
    form_set_error($error_name, t($error_text));
92
  }
93

    
94
}
95

    
96
/**
97
 * Submit handler function for ldap_authorization_admin_form.
98
 */
99
function ldap_authorization_admin_form_submit($form, &$form_state) {
100
  list($consumer, $op, $op_past_tense, $new) = _ldap_authorization_admin_parse_form($form, $form_state);
101
  $values = $form_state['values'];
102
  ldap_servers_module_load_include('php', 'ldap_authorization', 'LdapAuthorizationConsumerConfAdmin.class');
103
  $consumer_conf = new LdapAuthorizationConsumerConfAdmin($consumer, $new);
104
  // Add form data to object and save or create.
105
  $consumer_conf->drupalFormSubmit($op, $values);
106

    
107
  if ($consumer_conf->hasError == FALSE) {
108
    drupal_set_message(t('LDAP Authorization %name !verb', ['!verb' => $op_past_tense, '%name' => $consumer->name]), 'status');
109
    drupal_goto(LDAP_SERVERS_MENU_BASE_PATH . '/authorization');
110
  }
111

    
112
  form_set_error($consumer_conf->errorName, $consumer_conf->errorMsg);
113
  $consumer_conf->clearError();
114

    
115
}
116

    
117
/**
118
 * Helper function for parsing ldap authorization config form.
119
 */
120
function _ldap_authorization_admin_parse_form($form, &$form_state) {
121
  $op = drupal_strtolower($form_state['clicked_button']['#value']);
122
  $values = $form_state['values'];
123

    
124
  if ($values['consumer_type']) {
125
    $consumer_type = $values['consumer_type'];
126
    $consumer = ldap_authorization_get_consumer_object($consumer_type);
127
  }
128
  else {
129
    return FALSE;
130
  }
131

    
132
  switch ($op) {
133
    case 'add':
134
      $op_past_tense = 'Added';
135
      $new = TRUE;
136
      break;
137

    
138
    case 'save':
139
    case 'update':
140
    case 'edit':
141
      $op_past_tense = 'Updated';
142
      $new = FALSE;
143
      break;
144

    
145
    case 'delete':
146
      $op_past_tense = 'Deleted';
147
      $new = FALSE;
148
      break;
149
  }
150

    
151
  return [$consumer, $op, $op_past_tense, $new];
152

    
153
}