Skip to content

Commit 01ffcc2

Browse files
committed
fix bug, return less information
1 parent 8973e1f commit 01ffcc2

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

resources/lib/UnityUser.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,20 +245,19 @@ public function addSSHKey(string $key, UnityUser $operator, bool $send_mail = tr
245245
}
246246

247247
/**
248-
* @return array bools corresponding to each key
249-
* true if key added, false if key not added because it was already there
248+
* @return int number of keys added -- a key might not be added because it was already there
250249
* @throws NoKeyLoadedException if any key is invalid
251250
*/
252-
public function addSSHKeys(array $keys, UnityUser $operator, bool $send_mail = true): array
251+
public function addSSHKeys(array $keys, UnityUser $operator, bool $send_mail = true): int
253252
{
254253
foreach ($keys as $key) {
255254
PublicKeyLoader::load($key); // throws NoKeyLoadedException
256255
}
257256
$keysBefore = $this->getSSHKeys();
258-
$output = array_map(fn($key) => !in_array($key, $keysBefore), $keys);
259-
$newKeys = array_filter($keys, fn($key) => !in_array($key, $keysBefore));
260-
$this->setSSHKeys(array_merge($this->getSSHKeys(), $newKeys), $operator, $send_mail);
261-
return $output;
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);
262261
}
263262

264263
public function removeSSHKey(int $index, UnityUser $operator, bool $send_mail = true)

webroot/panel/account.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@
5252
UnityHTTPD::redirect();
5353
}
5454
} else {
55-
$wasEachKeyAdded = $USER->addSSHKeys($keys, $OPERATOR);
56-
$numKeysAdded = count(array_filter($wasEachKeyAdded, fn($x) => $x === true));
55+
$numKeysAdded = $USER->addSSHKeys($keys, $OPERATOR);
5756
$numKeysNotAdded = count($keys) - $numKeysAdded;
5857
if ($numKeysAdded == 0) {
5958
UnityHTTPD::messageWarning(

0 commit comments

Comments
 (0)