-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_key.php
More file actions
84 lines (83 loc) · 2.54 KB
/
ssh_key.php
File metadata and controls
84 lines (83 loc) · 2.54 KB
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
<?php
include_once(__DIR__ . '/inc/init.php');
fAuthorization::requireLoggedIn();
if (fRequest::isGet()) {
if (fRequest::get('id') != NULL) {
try {
$ssh_key = new SshKey(fRequest::get('id'));
if ($ssh_key->getUserId() != get_current_user_id()) {
throw new fValidationException('user_id mismatched.');
}
exit(make_response($ssh_key));
} catch (fException $e) {
exit(return_error($e));
}
} else {
exit(fetch_data($db));
}
} else if (fRequest::isPost()) {
$action = fRequest::get('method');
if ($action == 'create') {
try {
$db->query('BEGIN');
$title = title_decode(fRequest::get('title'));
$key = key_decode(fRequest::get('key'));
if (empty($title) || empty($key)) {
throw new fValidationException('Title or key is empty.');
} else if (fetch_data_by_key($db, $key)) {
throw new fValidationException('The public key has existed.');
}
$ssh_key = new SshKey();
$ssh_key->setTitle($title);
$ssh_key->setSshKey($key);
$ssh_key->setUserId(get_current_user_id());
$ssh_key->store();
//add_pub_key($ssh_key);
$db->query('COMMIT');
exit(make_response($ssh_key));
} catch (fException $e) {
$db->query('ROLLBACK');
exit(return_error($e));
}
} else if ($action == 'update') {
try {
$db->query('BEGIN');
$ssh_key = new SshKey(fRequest::get('id'));
$title = title_decode(fRequest::get('title'));
$key = key_decode(fRequest::get('key'));
$tmp_key = $ssh_key->getSshKey();
if (empty($title) || empty($key)) {
throw new fValidationException('Title or key is empty.');
} else if ($ssh_key->getUserId() != get_current_user_id()) {
throw new fValidationException('user_id mismatched.');
} else if ($key != $tmp_key && fetch_data_by_key($db, $key)) {
throw new fValidationException('The public key has existed.');
}
$ssh_key->setTitle($title);
$ssh_key->setSshKey($key);
$ssh_key->store();
//if ($key != $tmp_key) update_pub_key($ssh_key);
$db->query('COMMIT');
exit(make_response($ssh_key));
} catch (fException $e) {
$db->query('ROLLBACK');
exit(return_error($e));
}
} else if ($action == 'delete') {
try {
$db->query('BEGIN');
$ssh_key = new SshKey(fRequest::get('id'));
if ($ssh_key->getUserId() != get_current_user_id())
throw new fValidationException('user_id mismatched.');
//remove_pub_key($ssh_key);
$ssh_key->delete();
$db->query('COMMIT');
exit(fJSON::encode(array('success' => true)));
} catch (fException $e) {
$db->query('ROLLBACK');
exit(return_error($e));
}
}
} else {
exit('METHOD Denied!!');
}