-
Notifications
You must be signed in to change notification settings - Fork 1
/
indicia_api.admin.inc
310 lines (273 loc) · 7.79 KB
/
indicia_api.admin.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/**
* @file
* Module's UI.
*/
/**
* Creates a blank form for a new key or populates it if editing.
*/
function indicia_api_key($form, &$form_state, $keys = NULL) {
$existing = !is_null($keys);
if ($existing) {
// Editing an existing key.
if (empty($keys)) {
// Requested an key with an id that doesn't exist in DB.
drupal_not_found();
return;
}
else {
// Since key ids are unique and the URL argument is one id number
// the $keys array returned from DB must contain information about only
// one key.
$key = $keys[0];
if (indicia_api_user_has_permission($key)) {
drupal_set_title(t('@title settings', array('@title' => $key['title'])));
}
else {
return drupal_access_denied();
}
}
}
else {
// New key, set variables to default values.
$key = array();
$key['enabled'] = 1;
$key['log'] = 0;
$key['title'] = '';
$key['description'] = '';
$key['api_key'] = indicia_api_generate_random_string(40);
$key['anonymous_user'] = 0;
}
// Build form.
$form['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enabled'),
'#default_value' => $key['enabled'],
'#description' => t('Check to enable key.'),
);
$form['log'] = array(
'#type' => 'select',
'#title' => t('Logging mode'),
'#options' => array(
0 => t('None'),
WATCHDOG_ERROR => t('Error'),
WATCHDOG_DEBUG => t('Debug'),
),
'#default_value' => $key['log'],
'#description' => t("Select key's logging mode."),
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $key['title'],
'#description' => t('Set the human readable title for this key.'),
'#required' => TRUE,
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Key description'),
'#description' => t('Short key description.'),
'#default_value' => $key['description'],
);
$form['api_key'] = array(
'#type' => 'textfield',
'#title' => t('API key'),
'#default_value' => $key['api_key'],
'#description' => t('Set the API key to be used for authentication.'),
'#required' => TRUE,
);
$form['anonymous_user'] = array(
'#type' => 'textfield',
'#title' => t('Anonymous user ID'),
'#default_value' => $key['anonymous_user'],
'#description' => t('Set a user ID to allow anonymous record submissions.'),
'#required' => FALSE,
);
if (!empty($key['id'])) {
// Editing existing key.
$form['id'] = array(
'#type' => 'value',
'#value' => $key['id'],
);
$form['changed'] = array(
'#type' => 'value',
'#value' => time(),
);
}
else {
// New key.
$time = time();
global $user;
$form['created_by'] = array(
'#type' => 'value',
'#value' => $user->uid,
);
$form['created'] = array(
'#type' => 'value',
'#value' => $time,
);
$form['changed'] = array(
'#type' => 'value',
'#value' => $time,
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['cancel'] = array(
'#type' => 'link',
'#title' => t('Cancel'),
'#href' => CONFIG_PATH,
'#attributes' => [
'class' => ['button'],
],
);
if ($existing) {
$form['delete'] = array(
'#markup' => l(t('Delete'), CONFIG_PATH . "/delete/{$key['id']}"),
);
}
// Check if user has access to create new key.
if (user_access('user mobile auth') || user_access('admin mobile auth')) {
return $form;
}
else {
return drupal_access_denied();
}
}
/**
* Main administrative page displays the information about stored key.
*
* Allows to edit/delete each key's information and add new ones.
*
* @return string
* The html of the page.
*/
function indicia_api_dashboard() {
$r = '<p>This dashboard allows you to manage API client keys. </p>';
// Create table.
$header = array('Enabled', 'Title', 'Description', 'Key', 'Logging', '');
$rows = array();
$keys = indicia_api_key_load();
foreach ($keys as $key) {
if (indicia_api_user_has_permission($key)) {
$row = array();
$row[0] = '<input type="radio"' . ($key['enabled'] ? 'checked' : 'disabled') . ' >';
$row[1] = $key['title'];
$row[2] = $key['description'];
$row[3] = [
'data' => $key['api_key'],
'style' => 'color: rgba(0,0,0,0.87); font-style: italic; background-color: #f5f5f5; margin: 0px 0px 26px 0px;',
];
$log_mode = $key['log'];
switch ($log_mode) {
case WATCHDOG_ERROR:
$row[4] = 'Error';
break;
case WATCHDOG_DEBUG:
$row[4] = 'Debug';
break;
default:
$row[4] = 'None';
}
$row[5] = l(t('Edit'), CONFIG_PATH . "/{$key['id']}");
$rows[] = $row;
}
}
$table = theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array('style' => 'width:100%; text-align:left'),
));
$r .= $table;
// Add links beneath the table.
$links = array(
'link1' => array(
'title' => t('Add new key'),
'href' => CONFIG_PATH . '/add',
'attributes' => array('class' => array('button')),
),
);
$r .= theme('links', array(
'links' => $links,
'attributes' => array('class' => array('links', 'inline')),
));
$r .= '</fieldset>';
return $r;
}
/**
* Submit handler to save an key.
*
* Implements hook_submit() to submit a form produced by
* indicia_api_key().
*/
function indicia_api_key_submit($form, &$form_state) {
if (empty($form_state['values']['secret'])) {
// Don't overwrite old password if wasn't touched while editing.
unset($form_state['values']['secret']);
}
if (empty($form_state['values']['id'])) {
// Save new key.
drupal_write_record('indicia_api', $form_state['values']);
$message = 'Created %key.';
}
else {
// Save existing key.
drupal_write_record('indicia_api', $form_state['values'], 'id');
$message = 'Saved %key.';
}
// Inform user and return to dashboard.
drupal_set_message(t($message, array('%key' => $form_state['values']['title'])));
$form_state['redirect'] = CONFIG_PATH;
}
/**
* A confirmation page to check if the user is sure to delete an key.
*/
function indicia_api_delete($form, &$form_state, $keys) {
// Requested an key key with an id that does not exist in DB.
if (empty($keys)) {
return drupal_not_found();
}
// Since key key ids are unique and the URL argument is one id number
// the $keys array returned from DB must contain information about only
// one key.
$key = $keys[0];
// Set up the form information to be passed to submit handler.
$form = array();
$form['id'] = array(
'#type' => 'value',
'#value' => $key['id'],
);
$form['title'] = array(
'#type' => 'value',
'#value' => $key['title'] ,
);
$message = t('Are you sure you want to delete %key ?', array('%key' => $key['title']));
// Check if the user has permission to delete an key.
if (indicia_api_user_has_permission($key)) {
return confirm_form($form, $message,
CONFIG_PATH,
t('This action cannot be undone.'),
t('Delete'),
t('Cancel'));
// Proceed to delete_submit handler,
}
return drupal_access_denied();
}
/**
* Submit handler to delete an key.
*
* Implements hook_submit() to submit a confirmation form produced by
* indicia_api_delete().
*/
function indicia_api_delete_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
// Delete key from database.
db_query('DELETE FROM {indicia_api} WHERE id = :id',
array(':id' => $form_state['values']['id']));
// Inform user and return to dashboard.
drupal_set_message(t('Deleted %key.', array('%key' => $form_state['values']['title'])));
}
$form_state['redirect'] = CONFIG_PATH;
}