forked from zaporylie/drupal-sshkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshkey.pages.inc
197 lines (173 loc) · 5.62 KB
/
sshkey.pages.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
<?php
function sshkey_list_page($entity_type, $entity_id) {
$destination = drupal_get_destination();
// Add the local actions.
if (sshkey_access('create', $entity_type, $entity_id)) {
$local_actions['add'] = array(
'title' => t('Add a public key'),
'href' => "ssh-keys/{$entity_type}/{$entity_id}/add",
'query' => $destination,
'modal' => TRUE,
);
$build['actions'] = array(
'#theme' => 'links',
'#links' => $local_actions,
'#attributes' => array('class' => 'item-list action-links'),
);
}
$header = array(
'name' => array('data' => t('Title'), 'field' => 'title', 'sort' => 'asc'),
'fingerprint' => array('data' => t('Fingerprint'), 'field' => 'fingerprint'),
'operations' => array('data' => t('Operations')),
);
$query = db_select('sshkey')->extend('PagerDefault')->extend('TableSort');
$query->addField('sshkey', 'key_id');
$query->condition('entity_type', $entity_type);
$query->condition('entity_id', $entity_id);
$query->orderByHeader($header);
$query->limit(25);
$key_ids = $query->execute()->fetchCol();
$keys = sshkey_load_multiple($key_ids);
$rows = array();
foreach ($key_ids as $key_id) {
$key = $keys[$key_id];
$key_base_uri = "ssh-keys/{$entity_type}/{$entity_id}/{$key_id}";
$rows[$key_id]['title'] = check_plain($key->title);
$rows[$key_id]['fingerprint'] = theme('sshkey_fingerprint', array('key' => $key));
$operations = array();
if (sshkey_access('view', $entity_type, $entity_id, $key)) {
$operations['view'] = array(
'title' => t('View'),
'href' => $key_base_uri . '/view',
);
}
if (sshkey_access('edit', $entity_type, $entity_id, $key)) {
$operations['edit'] = array(
'title' => t('Edit'),
'href' => $key_base_uri . '/edit',
'query' => $destination,
'modal' => TRUE,
);
}
if (sshkey_access('delete', $entity_type, $entity_id, $key)) {
$operations['delete'] = array(
'title' => t('Delete'),
'href' => $key_base_uri . '/delete',
'query' => $destination,
'modal' => TRUE,
);
}
$rows[$key_id]['operations'] = array(
'data' => array(
'#theme' => 'links',
'#links' => $operations,
'#attributes' => array('class' => array('links', 'inline', 'nowrap')),
),
);
}
$empty_text[] = t('No SSH public keys available.');
if (sshkey_access('create', $entity_type, $entity_id)) {
$empty_text[] = l(t('Add a public key'), "ssh-keys/{$entity_type}/{$entity_id}/add", array('query' => $destination, 'modal' => TRUE));
}
$build['keys_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => implode(' ', $empty_text),
);
$build['keys_table_pager'] = array('#theme' => 'pager');
return $build;
}
/**
* Page callback for viewing an SSH key.
*/
function sshkey_view_key($entity_type, $entity_id, $key) {
// We are returning plain text, so tell the browser.
drupal_add_http_header('Content-Type', 'text/plain; charset=utf-8');
print $key->value;
drupal_page_footer();
}
function sshkey_edit_form($form, $form_state, $entity_type, $entity_id, $key = NULL) {
$entity = entity_load($entity_type, array($entity_id));
$entity = reset($entity);
if (!isset($key)) {
$key = new stdClass();
$key->is_new = TRUE;
$key->key_id = NULL;
$key->fingerprint = NULL;
$key->title = '';
$key->value = '';
$key->entity_type = $entity_type;
$key->entity_id = $entity_id;
}
$form['#sshkey'] = $key;
$form['#entity'] = $entity;
$form['key_id'] = array(
'#type' => 'value',
'#value' => isset($key->key_id) ? $key->key_id : NULL,
);
$form['entity_type'] = array(
'#type' => 'value',
'#value' => $entity_type,
);
$form['entity_id'] = array(
'#type' => 'value',
'#value' => $entity_id,
);
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#description' => t("If this field is left blank, the key's title will be automatically generated."),
'#default_value' => $key->title,
'#maxlength' => 128,
);
$form['value'] = array(
'#type' => 'textarea',
'#title' => t('Key'),
'#default_value' => $key->value,
'#rows' => 10,
'#wysiwyg' => FALSE,
'#required' => TRUE,
);
$form['actions'] = array(
'#weight' => 100,
);
$form['actions']['save'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
$form['actions']['cancel'] = array(
'#type' => 'link',
'#title' => t('Cancel'),
'#href' => isset($_GET['destination']) ? $_GET['destination'] : '<front>',
);
return $form;
}
function sshkey_edit_form_validate($form, &$form_state) {
sshkey_validate($form_state['values'], $form, $form_state);
}
function sshkey_edit_form_submit($form, &$form_state) {
form_state_values_clean($form_state);
$key = (object) $form_state['values'];
sshkey_save($key);
drupal_set_message(t('The SSH public key %title has been saved.', array('%title' => $key->title)));
}
function sshkey_delete_form($form, $form_state, $key) {
$form['#sshkey'] = $key;
$form['key_id'] = array(
'#type' => 'value',
'#value' => $key->key_id,
);
return confirm_form(
$form,
t('Are you sure you want to delete the public key %title?', array('%title' => $key->title)),
isset($_GET['destination']) ? $_GET['destination'] : '<front>',
'',
t('Delete'),
t('Cancel')
);
}
function sshkey_delete_form_submit($form, $form_state) {
sshkey_delete($form_state['values']['key_id']);
drupal_set_message(t('The SSH public key %title has been deleted.', array('%title' => $form['#sshkey']->title)));
}