1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Implementations of administration functions for the acl module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implementation of acl_get_usernames().
|
10
|
*/
|
11
|
function _acl_get_usernames($acl_id) {
|
12
|
$users = array();
|
13
|
$result = db_query("SELECT u.uid, u.name FROM {users} u LEFT JOIN {acl_user} aclu ON aclu.uid = u.uid WHERE acl_id = :acl_id", array(
|
14
|
'acl_id' => $acl_id));
|
15
|
foreach ($result as $user) {
|
16
|
$users[$user->uid] = _acl_format_username($user);
|
17
|
}
|
18
|
return $users;
|
19
|
}
|
20
|
|
21
|
/**
|
22
|
* Implementation of acl_edit_form().
|
23
|
*/
|
24
|
function _acl_edit_form($acl_id, $label = NULL, $new_acl = FALSE) {
|
25
|
$users = array();
|
26
|
if (!$new_acl) {
|
27
|
// Ensure the ACL in question even exists.
|
28
|
if (!($record = db_query("SELECT name, number FROM {acl} WHERE acl_id = :acl_id", array(
|
29
|
'acl_id' => $acl_id,
|
30
|
))->fetchAssoc())) {
|
31
|
return array();
|
32
|
}
|
33
|
$users = _acl_get_usernames($acl_id);
|
34
|
}
|
35
|
if (!isset($label)) {
|
36
|
$label = (isset($record['name']) ? $record['name'] : (isset($record['number']) ? $record['number'] : $acl_id));
|
37
|
}
|
38
|
|
39
|
$form = array(
|
40
|
'#type' => 'fieldset',
|
41
|
'#collapsible' => TRUE,
|
42
|
'#title' => check_plain($label),
|
43
|
'#tree' => TRUE,
|
44
|
);
|
45
|
|
46
|
$form['acl_id'] = array(
|
47
|
'#type' => 'value',
|
48
|
'#value' => $acl_id,
|
49
|
);
|
50
|
|
51
|
$form['deletions'] = array(
|
52
|
'#type' => 'checkboxes',
|
53
|
'#options' => array(),
|
54
|
); // placeholder
|
55
|
$form['delete_button'] = array(
|
56
|
'#type' => 'button',
|
57
|
'#name' => 'acl_' . $acl_id,
|
58
|
'#value' => t('Remove Checked'),
|
59
|
'#submit' => FALSE,
|
60
|
);
|
61
|
|
62
|
$form['add'] = array(
|
63
|
'#type' => 'textfield',
|
64
|
'#title' => t('Add user'),
|
65
|
'#maxlength' => 60,
|
66
|
'#size' => 40,
|
67
|
'#autocomplete_path' => 'user/autocomplete',
|
68
|
);
|
69
|
$form['add_button'] = array(
|
70
|
'#type' => 'button',
|
71
|
'#name' => 'acl_' . $acl_id,
|
72
|
'#value' => t('Add User'),
|
73
|
'#submit' => FALSE,
|
74
|
);
|
75
|
|
76
|
$form['user_list'] = array(
|
77
|
'#type' => 'hidden',
|
78
|
'#default_value' => serialize($users),
|
79
|
);
|
80
|
|
81
|
$form['#after_build'] = array('_acl_edit_form_after_build');
|
82
|
|
83
|
return $form;
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Process a form that had our buttons on it.
|
88
|
*/
|
89
|
function _acl_edit_form_after_build($form, &$form_state) {
|
90
|
// We can't use the form values because it's the entire structure
|
91
|
// and we have no clue where our values actually are. That's
|
92
|
// ok tho cause #value still works for us.
|
93
|
$user_list = acl_edit_form_get_user_list($form);
|
94
|
|
95
|
if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#value'] == $form['delete_button']['#value']) {
|
96
|
$deletions = $form['deletions']['#value'];
|
97
|
foreach ($deletions as $uid) {
|
98
|
unset($user_list[$uid]);
|
99
|
unset($form['deletions']['#value'][$uid]);
|
100
|
}
|
101
|
}
|
102
|
elseif (isset($form_state['triggering_element']) && $form_state['triggering_element']['#value'] == $form['add_button']['#value'] && !empty($form['add']['#value'])) {
|
103
|
$user = db_query("SELECT uid, name FROM {users} WHERE name = :name", array(
|
104
|
'name' => $form['add']['#value'],
|
105
|
))->fetchObject();
|
106
|
if (!$user) {
|
107
|
form_error($form['add'], t("Invalid user specified."));
|
108
|
}
|
109
|
else {
|
110
|
$user_list[$user->uid] = _acl_format_username($user);
|
111
|
$form['add']['#value'] = NULL;
|
112
|
}
|
113
|
}
|
114
|
|
115
|
if (count($user_list) != 0) {
|
116
|
$form['deletions']['#type'] = 'checkboxes';
|
117
|
$form['deletions']['#title'] = t("Current users");
|
118
|
$form['deletions']['#options'] = $user_list;
|
119
|
$form['deletions']['#value'] = array(); // don't carry value through.
|
120
|
$form['deletions'] = form_builder(!empty($form['#post']) ? $form['#post']['form_id'] : 'acl_form', $form['deletions'], $form_state);
|
121
|
}
|
122
|
else {
|
123
|
$form['delete_button']['#type'] = 'value';
|
124
|
}
|
125
|
$form['user_list']['#value'] = serialize($user_list);
|
126
|
|
127
|
return $form;
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* Write the results of a form.
|
132
|
*
|
133
|
* The module that embedded our form must call this function!
|
134
|
*/
|
135
|
function acl_save_form($form, $priority = NULL) {
|
136
|
$users = acl_edit_form_get_user_list($form);
|
137
|
db_delete('acl_user')
|
138
|
->condition('acl_id', $form['acl_id'])
|
139
|
->execute();
|
140
|
foreach ($users as $uid => $name) {
|
141
|
db_insert('acl_user')
|
142
|
->fields(array(
|
143
|
'acl_id' => $form['acl_id'],
|
144
|
'uid' => $uid,
|
145
|
))
|
146
|
->execute();
|
147
|
}
|
148
|
if (isset($priority)) {
|
149
|
db_update('acl_node')
|
150
|
->fields(array(
|
151
|
'priority' => $priority,
|
152
|
))
|
153
|
->condition('acl_id', $form['acl_id'])
|
154
|
->execute();
|
155
|
}
|
156
|
}
|
157
|
|
158
|
/**
|
159
|
* Decode and return the list of users.
|
160
|
*
|
161
|
* @param array $form
|
162
|
* The ACL form or form_state array.
|
163
|
* @param bool $get_default
|
164
|
* (optional) In the case of a form array, whether to return the
|
165
|
* '#default_value' (or the '#value').
|
166
|
*
|
167
|
* @return array
|
168
|
* An array of $uid => $username.
|
169
|
*/
|
170
|
function acl_edit_form_get_user_list($form, $get_default = FALSE) {
|
171
|
if (is_array($form['user_list'])) {
|
172
|
return unserialize($form['user_list'][$get_default ? '#default_value' : '#value']);
|
173
|
}
|
174
|
return unserialize($form['user_list']);
|
175
|
}
|
176
|
|