Skip to content

Commit feb793b

Browse files
committed
less code, better output, slower
1 parent 01ffcc2 commit feb793b

File tree

2 files changed

+12
-52
lines changed

2 files changed

+12
-52
lines changed

resources/lib/UnityUser.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,6 @@ public function addSSHKey(string $key, UnityUser $operator, bool $send_mail = tr
244244
return true;
245245
}
246246

247-
/**
248-
* @return int number of keys added -- a key might not be added because it was already there
249-
* @throws NoKeyLoadedException if any key is invalid
250-
*/
251-
public function addSSHKeys(array $keys, UnityUser $operator, bool $send_mail = true): int
252-
{
253-
foreach ($keys as $key) {
254-
PublicKeyLoader::load($key); // throws NoKeyLoadedException
255-
}
256-
$keysBefore = $this->getSSHKeys();
257-
// merge, then remove duplicates, then re-index
258-
$keysAfter = array_values(array_unique(array_merge($keysBefore, $keys)));
259-
$this->setSSHKeys($keysAfter, $operator, $send_mail);
260-
return count($keysAfter) - count($keysBefore);
261-
}
262-
263247
public function removeSSHKey(int $index, UnityUser $operator, bool $send_mail = true)
264248
{
265249
$keys = $this->getSSHKeys();

webroot/panel/account.php

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,44 +41,20 @@
4141
break;
4242
}
4343
$keys = array_map("trim", $keys);
44-
try {
45-
if (count($keys) == 1) {
46-
$keyWasAdded = $USER->addSSHKey($keys[0], $OPERATOR);
47-
if ($keyWasAdded) {
48-
UnityHTTPD::messageSuccess("SSH Key Added", "");
49-
UnityHTTPD::redirect();
50-
} else {
51-
UnityHTTPD::messageWarning("SSH Key Not Added", "This key already exists");
52-
UnityHTTPD::redirect();
53-
}
44+
foreach ($keys as $key) {
45+
try {
46+
$keyWasAdded = $USER->addSSHKey($key, $OPERATOR);
47+
} catch (NoKeyLoadedException) {
48+
UnityHTTPD::messageError("SSH Key Not Added", "This key is invalid");
49+
UnityHTTPD::redirect();
50+
}
51+
if ($keyWasAdded) {
52+
UnityHTTPD::messageSuccess("SSH Key Added", "");
53+
UnityHTTPD::redirect();
5454
} else {
55-
$numKeysAdded = $USER->addSSHKeys($keys, $OPERATOR);
56-
$numKeysNotAdded = count($keys) - $numKeysAdded;
57-
if ($numKeysAdded == 0) {
58-
UnityHTTPD::messageWarning(
59-
"SSH Keys Not Added",
60-
"These keys already exist"
61-
);
62-
UnityHTTPD::redirect();
63-
} elseif ($numKeysAdded == count($keys)) {
64-
UnityHTTPD::messageSuccess("SSH Keys Added", "");
65-
UnityHTTPD::redirect();
66-
} else {
67-
UnityHTTPD::messageWarning(
68-
"Some SSH Keys Not Added",
69-
// pluralization may be wrong here but I don't care
70-
sprintf(
71-
"%s keys were added. The other %s keys already existed.",
72-
$numKeysAdded,
73-
$numKeysNotAdded
74-
)
75-
);
76-
UnityHTTPD::redirect();
77-
}
55+
UnityHTTPD::messageInfo("SSH Key Not Added", "This key already exists");
56+
UnityHTTPD::redirect();
7857
}
79-
} catch (NoKeyLoadedException $e) {
80-
UnityHTTPD::messageError("Invalid SSH Key", "");
81-
UnityHTTPD::redirect();
8258
}
8359
break;
8460
case "delKey":

0 commit comments

Comments
 (0)