| 1 |
<?php
|
| 2 |
// $Id: ldapgroups.admin.inc,v 1.10 2009/03/30 08:33:26 miglius Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Module admin page callbacks.
|
| 7 |
*/
|
| 8 |
|
| 9 |
//////////////////////////////////////////////////////////////////////////////
|
| 10 |
// ldapgroups settings
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implements the settings page.
|
| 14 |
*
|
| 15 |
* @return
|
| 16 |
* The form structure.
|
| 17 |
*/
|
| 18 |
function ldapgroups_admin_settings() {
|
| 19 |
$form['list']['#value'] = ldapgroups_admin_list();
|
| 20 |
|
| 21 |
return $form;
|
| 22 |
}
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implements the LDAP servers list.
|
| 26 |
*
|
| 27 |
* @return
|
| 28 |
* The HTML table with the servers list.
|
| 29 |
*/
|
| 30 |
function ldapgroups_admin_list() {
|
| 31 |
$rows = array();
|
| 32 |
$result = db_query("SELECT sid, name, status FROM {ldapauth} ORDER BY weight");
|
| 33 |
while ($row = db_fetch_object($result)) {
|
| 34 |
$rows[] = array(
|
| 35 |
'data' => array(
|
| 36 |
$row->name,
|
| 37 |
l(t('edit'), 'admin/settings/ldap/ldapgroups/edit/'. $row->sid),
|
| 38 |
l(t('reset'), 'admin/settings/ldap/ldapgroups/reset/'. $row->sid),
|
| 39 |
),
|
| 40 |
'class' => $row->status ? 'menu-enabled' : 'menu-disabled',
|
| 41 |
);
|
| 42 |
}
|
| 43 |
|
| 44 |
$header = array(
|
| 45 |
t('Server'),
|
| 46 |
array('data' => t('Operations'), 'colspan' => 2),
|
| 47 |
);
|
| 48 |
|
| 49 |
return theme('table', $header, $rows);
|
| 50 |
}
|
| 51 |
|
| 52 |
/**
|
| 53 |
* Implements the LDAP server edit page.
|
| 54 |
*
|
| 55 |
* @param $form_state
|
| 56 |
* A form state array.
|
| 57 |
* @param $op
|
| 58 |
* An operatin - edit or reset.
|
| 59 |
* @param $sid
|
| 60 |
* A LDAP server ID.
|
| 61 |
*
|
| 62 |
* @return
|
| 63 |
* The form structure.
|
| 64 |
*/
|
| 65 |
|
| 66 |
function ldapgroups_admin_edit(&$form_state, $op, $sid) {
|
| 67 |
if (($op == 'reset') && $sid) {
|
| 68 |
$form['sid'] = array(
|
| 69 |
'#type' => 'value',
|
| 70 |
'#value' => $sid,
|
| 71 |
);
|
| 72 |
return confirm_form(
|
| 73 |
$form,
|
| 74 |
t('Are you sure you want to reset the groups mapping to defaults ?'),
|
| 75 |
'admin/settings/ldap/ldapgroups',
|
| 76 |
t('<em>This action cannot be undone.</p>'),
|
| 77 |
t('Reset'),
|
| 78 |
t('Cancel')
|
| 79 |
);
|
| 80 |
}
|
| 81 |
elseif ($op == 'edit' && $sid) {
|
| 82 |
$edit = db_fetch_array(db_query("SELECT * FROM {ldapauth} WHERE sid = %d", $sid));
|
| 83 |
|
| 84 |
$form['description'] = array(
|
| 85 |
'#value' => t('Configure LDAP groups to Drupal roles mapping settings for the %server.', array('%server' => $edit['name'])),
|
| 86 |
);
|
| 87 |
|
| 88 |
$form['group_dn'] = array(
|
| 89 |
'#type' => 'fieldset',
|
| 90 |
'#title' => t('Group by DN'),
|
| 91 |
'#collapsible' => TRUE,
|
| 92 |
'#collapsed' => !$edit['ldapgroups_in_dn'],
|
| 93 |
);
|
| 94 |
$form['group_dn']['ldapgroups_in_dn'] = array(
|
| 95 |
'#type' => 'checkbox',
|
| 96 |
'#title' => t('Group is specified in user\'s DN'),
|
| 97 |
'#default_value' => $edit['ldapgroups_in_dn'],
|
| 98 |
'#description' => '<p>Check this option if your users\' DNs look like <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">cn=jdoe,<strong>ou=Group1</strong>,cn=example,cn=com</em> and <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">Group1</em> turns out to be the group you want.</p>'
|
| 99 |
);
|
| 100 |
$form['group_dn']['ldapgroups_dn_attribute'] = array(
|
| 101 |
'#type' => 'textfield',
|
| 102 |
'#title' => t('Attribute of the DN which contains the group name'),
|
| 103 |
'#default_value' => $edit['ldapgroups_dn_attribute'],
|
| 104 |
'#size' => 50,
|
| 105 |
'#maxlength' => 255,
|
| 106 |
'#description' => t('The name of the attribute which contains the group name. In the example above, it would be <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">ou</em>, as the DN contains the string <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">ou=Group1</em> and <em style="font-style: normal; padding: 1px 3px; border: 1px solid #8888CC; background-color: #DDDDFF">Group1</em> happens to be the desired group name.'),
|
| 107 |
);
|
| 108 |
|
| 109 |
$form['group_attr'] = array(
|
| 110 |
'#type' => 'fieldset',
|
| 111 |
'#title' => t('Group by attribute'),
|
| 112 |
'#collapsible' => TRUE,
|
| 113 |
'#collapsed' => !$edit['ldapgroups_in_attr'],
|
| 114 |
);
|
| 115 |
$form['group_attr']['ldapgroups_in_attr'] = array(
|
| 116 |
'#type' => 'checkbox',
|
| 117 |
'#title' => t('Groups are specified by LDAP attributes'),
|
| 118 |
'#default_value' => $edit['ldapgroups_in_attr'],
|
| 119 |
);
|
| 120 |
$form['group_attr']['ldapgroups_attr'] = array(
|
| 121 |
'#type' => 'textarea',
|
| 122 |
'#title' => t('Attribute names (one per line)'),
|
| 123 |
'#default_value' => implode("\n", ($edit['ldapgroups_attr'] ? unserialize($edit['ldapgroups_attr']) : array())),
|
| 124 |
'#cols' => 50,
|
| 125 |
'#rows' => 6,
|
| 126 |
'#description' => t('If the groups are stored in the user entries, along with the rest of their data, then enter here a list of attributes which may contain them.'),
|
| 127 |
);
|
| 128 |
|
| 129 |
$form['group_entry'] = array(
|
| 130 |
'#type' => 'fieldset',
|
| 131 |
'#title' => t('Group by entry'),
|
| 132 |
'#collapsible' => TRUE,
|
| 133 |
'#collapsed' => !$edit['ldapgroups_as_entries'],
|
| 134 |
);
|
| 135 |
$form['group_entry']['ldapgroups_as_entries'] = array(
|
| 136 |
'#type' => 'checkbox',
|
| 137 |
'#title' => t('Groups exist as LDAP entries where a multivalued attribute contains the members\' CNs'),
|
| 138 |
'#default_value' => $edit['ldapgroups_as_entries'],
|
| 139 |
);
|
| 140 |
$form['group_entry']['ldapgroups_entries'] = array(
|
| 141 |
'#type' => 'textarea',
|
| 142 |
'#title' => t('LDAP DNs containing groups (one per line)'),
|
| 143 |
'#default_value' => implode("\n", ($edit['ldapgroups_entries'] ? unserialize($edit['ldapgroups_entries']) : array())),
|
| 144 |
'#cols' => 50,
|
| 145 |
'#rows' => 6,
|
| 146 |
'#description' => t('Enter here a list of LDAP nodes from where groups should be searched for. The module will look them up recursively from the given nodes.'),
|
| 147 |
);
|
| 148 |
$form['group_entry']['ldapgroups_entries_attribute'] = array(
|
| 149 |
'#type' => 'textfield',
|
| 150 |
'#title' => t('Attribute holding group members'),
|
| 151 |
'#default_value' => $edit['ldapgroups_entries_attribute'],
|
| 152 |
'#size' => 50,
|
| 153 |
'#maxlength' => 255,
|
| 154 |
'#description' => t('Name of the multivalued attribute which holds the CNs of group members, for example: !attr', array('!attr' => theme('placeholder', LDAPGROUPS_DEFAULT_ENTRIES_ATTRIBUTE))),
|
| 155 |
);
|
| 156 |
$form['groups_limit'] = array(
|
| 157 |
'#type' => 'fieldset',
|
| 158 |
'#title' => t('LDAP group to Drupal role limits'),
|
| 159 |
'#collapsible' => TRUE,
|
| 160 |
'#collapsed' => !$edit['ldapgroups_groups'],
|
| 161 |
);
|
| 162 |
$form['groups_limit']['ldapgroups_groups'] = array(
|
| 163 |
'#type' => 'textarea',
|
| 164 |
'#title' => t('LDAP groups which allow automatic account creation'),
|
| 165 |
'#default_value' => implode("\n", ($edit['ldapgroups_groups'] ? unserialize($edit['ldapgroups_groups']) : array())),
|
| 166 |
'#cols' => 50,
|
| 167 |
'#rows' => 5,
|
| 168 |
'#description' => t('Leave blank to automatically create accounts for all LDAP authenticated users. Otherwise, enter a one per line list of LDAP groups. If the user is not in any of those groups, the login will be denied.'),
|
| 169 |
);
|
| 170 |
$form['group_filter'] = array(
|
| 171 |
'#type' => 'fieldset',
|
| 172 |
'#title' => t('LDAP group to Drupal role filtering'),
|
| 173 |
'#description' => t('The module automatically decides names for the Drupal roles based in the names of the LDAP groups. For example:<ul><li>LDAP group: Admins => Drupal role: Admins</li><li>LDAP group: ou=Underlings,dc=myorg,dc=mytld => Drupal role: Underlings.</li></ul>'),
|
| 174 |
'#collapsible' => TRUE,
|
| 175 |
'#collapsed' => !($edit['ldapgroups_mappings'] || $edit['ldapgroups_filter_php']),
|
| 176 |
);
|
| 177 |
$mappings = '';
|
| 178 |
foreach (($edit['ldapgroups_mappings'] ? unserialize($edit['ldapgroups_mappings']) : array()) as $group => $role)
|
| 179 |
$mappings .= $group .'|'. $role ."\n";
|
| 180 |
$form['group_filter']['ldapgroups_mappings'] = array(
|
| 181 |
'#type' => 'textarea',
|
| 182 |
'#title' => t('Mapping of LDAP groups to Drupal roles'),
|
| 183 |
'#default_value' => $mappings,
|
| 184 |
'#cols' => 50,
|
| 185 |
'#rows' => 5,
|
| 186 |
'#description' => t('Enter a list of LDAP groups and their Drupal role mappings, one per line with a | delimiter. Should be in the form [ldap group]|[drupal role] such as:<br/>cn=ED IT NAG Staff,DC=ad,DC=uiuc,DC=edu|admin<br/>cn=Ed Webs UIUC Webmasters,DC=ad,DC=uiuc,DC=edu|committee member'),
|
| 187 |
);
|
| 188 |
$form['group_filter']['ldapgroups_mappings_filter'] = array(
|
| 189 |
'#type' => 'checkbox',
|
| 190 |
'#title' => t('Use LDAP group to Drupal roles filtering'),
|
| 191 |
'#default_value' => $edit['ldapgroups_mappings_filter'],
|
| 192 |
'#description' => t('If enabled, only above mapped groups will be mapped to Drupal roles. If not enabled, a Drupal role will be created for every group the user is associated with.')
|
| 193 |
);
|
| 194 |
$form['group_filter']['ldapgroups_filter_php'] = array(
|
| 195 |
'#type' => 'textarea',
|
| 196 |
'#title' => t('PHP to filter roles by'),
|
| 197 |
'#default_value' => $edit['ldapgroups_filter_php'],
|
| 198 |
'#cols' => 25,
|
| 199 |
'#rows' => 5,
|
| 200 |
'#description' => t('Enter PHP to filter LDAP groups. Careful, bad PHP code here will break your site. If left empty, no filtering will be done. The groups array <code>$groups</code> is available in the code context. It should return a filtered <code>$groups</code> array as in example below. The code is evaluated before the above mapping is applied.<br /><code>$groups = array_filter($groups, create_function(\'$a\', \'return preg_match(\\\'/Staff/\\\', $a);\'));</code><br /><code>return $groups;</code>'),
|
| 201 |
);
|
| 202 |
|
| 203 |
$form['sid'] = array(
|
| 204 |
'#type' => 'hidden',
|
| 205 |
'#value' => $sid,
|
| 206 |
);
|
| 207 |
|
| 208 |
$form['submit'] = array(
|
| 209 |
'#type' => 'submit',
|
| 210 |
'#value' => t('Update'),
|
| 211 |
);
|
| 212 |
|
| 213 |
return $form;
|
| 214 |
}
|
| 215 |
else {
|
| 216 |
drupal_goto('admin/settings/ldap/ldapgroups');
|
| 217 |
}
|
| 218 |
}
|
| 219 |
|
| 220 |
/**
|
| 221 |
* Validate hook for the settings form.
|
| 222 |
*/
|
| 223 |
function ldapgroups_admin_edit_validate($form, &$form_state) {
|
| 224 |
$op = $form_state['clicked_button']['#value'];
|
| 225 |
$values = $form_state['values'];
|
| 226 |
switch ($op) {
|
| 227 |
case t('Update'):
|
| 228 |
if ($values['ldapgroups_in_dn'] && !trim($values['ldapgroups_dn_attribute']))
|
| 229 |
form_set_error('ldapgroups_dn_attribute', t('DN attribute is missing.'));
|
| 230 |
if ($values['ldapgroups_in_attr'] && !trim($values['ldapgroups_attr']))
|
| 231 |
form_set_error('ldapgroups_attr', t('Attribute names are missing.'));
|
| 232 |
if ($values['ldapgroups_as_entries'] && !trim($values['ldapgroups_entries']))
|
| 233 |
form_set_error('ldapgroups_entries', t('Nodes are missing.'));
|
| 234 |
if ($values['ldapgroups_as_entries'] && !trim($values['ldapgroups_entries_attribute']))
|
| 235 |
form_set_error('ldapgroups_entries_attribute', t('Attribute is missing.'));
|
| 236 |
|
| 237 |
$form_state['ldapgroups_attr'] = array();
|
| 238 |
foreach ((trim($values['ldapgroups_attr']) ? explode("\n", trim($values['ldapgroups_attr'])) : array()) as $line)
|
| 239 |
if (trim($line))
|
| 240 |
$form_state['ldapgroups_attr'][] = trim($line);
|
| 241 |
$form_state['ldapgroups_attr'] = !empty($form_state['ldapgroups_attr']) ? serialize($form_state['ldapgroups_attr']) : '';
|
| 242 |
|
| 243 |
$form_state['ldapgroups_entries'] = array();
|
| 244 |
foreach ((trim($values['ldapgroups_entries']) ? explode("\n", trim($values['ldapgroups_entries'])) : array()) as $line)
|
| 245 |
if (trim($line))
|
| 246 |
$form_state['ldapgroups_entries'][] = trim($line);
|
| 247 |
$form_state['ldapgroups_entries'] = !empty($form_state['ldapgroups_entries']) ? serialize($form_state['ldapgroups_entries']) : '';
|
| 248 |
|
| 249 |
$form_state['ldapgroups_mappings'] = array();
|
| 250 |
$ldapgroups_role_mappings = TRUE;
|
| 251 |
foreach ((trim($values['ldapgroups_mappings']) ? explode("\n", trim($values['ldapgroups_mappings'])) : array()) as $line) {
|
| 252 |
if (count($mapping = explode('|', trim($line))) == 2)
|
| 253 |
$form_state['ldapgroups_mappings'] += array(trim($mapping[0]) => trim($mapping[1]));
|
| 254 |
else
|
| 255 |
$ldapgroups_role_mappings = FALSE;
|
| 256 |
}
|
| 257 |
$form_state['ldapgroups_mappings'] = !empty($form_state['ldapgroups_mappings']) ? serialize($form_state['ldapgroups_mappings']) : '';
|
| 258 |
if (!$ldapgroups_role_mappings)
|
| 259 |
form_set_error('ldapgroups_mappings', t('Bad mapping syntax.'));
|
| 260 |
|
| 261 |
if ($values['ldapgroups_mappings_filter'] && !trim($values['ldapgroups_mappings']))
|
| 262 |
form_set_error('ldapgroups_mappings', t('Mappings are missing.'));
|
| 263 |
|
| 264 |
$form_state['ldapgroups_groups'] = array();
|
| 265 |
foreach ((trim($values['ldapgroups_groups']) ? explode("\n", trim($values['ldapgroups_groups'])) : array()) as $line)
|
| 266 |
if (trim($line))
|
| 267 |
$form_state['ldapgroups_groups'][] = trim($line);
|
| 268 |
$form_state['ldapgroups_groups'] = !empty($form_state['ldapgroups_groups']) ? serialize($form_state['ldapgroups_groups']) : '';
|
| 269 |
break;
|
| 270 |
}
|
| 271 |
}
|
| 272 |
|
| 273 |
/**
|
| 274 |
* Submit hook for the settings form.
|
| 275 |
*/
|
| 276 |
function ldapgroups_admin_edit_submit($form, &$form_state) {
|
| 277 |
$op = $form_state['clicked_button']['#value'];
|
| 278 |
$values = $form_state['values'];
|
| 279 |
switch ($op) {
|
| 280 |
case t('Update'):
|
| 281 |
|
| 282 |
// Update the ldapgroups configuration.
|
| 283 |
db_query("UPDATE {ldapauth} SET ldapgroups_in_dn = %d, ldapgroups_dn_attribute = '%s', ldapgroups_in_attr = %d, ldapgroups_attr = '%s', ldapgroups_as_entries = %d, ldapgroups_entries = '%s', ldapgroups_entries_attribute = '%s', ldapgroups_mappings = '%s', ldapgroups_mappings_filter = %d, ldapgroups_filter_php = '%s', ldapgroups_groups = '%s' WHERE sid = %d", $values['ldapgroups_in_dn'], trim($values['ldapgroups_dn_attribute']), $values['ldapgroups_in_attr'], $form_state['ldapgroups_attr'], $values['ldapgroups_as_entries'], $form_state['ldapgroups_entries'], trim($values['ldapgroups_entries_attribute']), $form_state['ldapgroups_mappings'], $values['ldapgroups_mappings_filter'], trim($values['ldapgroups_filter_php']), $form_state['ldapgroups_groups'], $values['sid']);
|
| 284 |
drupal_set_message(t('The configuration options have been saved.'));
|
| 285 |
$form_state['redirect'] = 'admin/settings/ldap/ldapgroups';
|
| 286 |
break;
|
| 287 |
case t('Reset'):
|
| 288 |
if ($values['confirm'] == 1) {
|
| 289 |
|
| 290 |
// Settings reset.
|
| 291 |
db_query("UPDATE {ldapauth} SET ldapgroups_in_dn = 0, ldapgroups_dn_attribute = '', ldapgroups_in_attr = 0, ldapgroups_attr = '', ldapgroups_as_entries = 0, ldapgroups_entries = '', ldapgroups_entries_attribute = '', ldapgroups_mappings = '', ldapgroups_mappings_filter = '0', ldapgroups_filter_php = '', ldapgroups_groups = '' WHERE sid = %d", $values['sid']);
|
| 292 |
drupal_set_message(t('The configuration options have been reset to their default values.'));
|
| 293 |
}
|
| 294 |
$form_state['redirect'] = 'admin/settings/ldap/ldapgroups';
|
| 295 |
break;
|
| 296 |
}
|
| 297 |
}
|
| 298 |
|