Files
feuerwerk/tools/make-user.php
T

37 lines
1.0 KiB
PHP

<?php
declare(strict_types=1);
/**
* Creates an account or sets the password of one that exists. The API has no route for it:
* signing up is a follow-up issue, so until then accounts are made here, on the server.
*
* Usage: php tools/make-user.php <username> <password>
* php tools/make-user.php --list
*/
require __DIR__ . '/../api/lib/UserStore.php';
$file = dirname(__DIR__) . '/data/users.json';
$store = new UserStore($file);
$args = array_slice($argv, 1);
if (($args[0] ?? '') === '--list') {
foreach ($store->names() as $name) {
echo $name, "\n";
}
exit(0);
}
if (count($args) !== 2 || $args[0] === '' || $args[1] === '') {
fwrite(STDERR, "Usage: php tools/make-user.php <username> <password>\n");
exit(2);
}
[$username, $password] = $args;
if (!UserStore::isValidName($username)) {
fwrite(STDERR, "Invalid user name: {$username}\n");
exit(2);
}
$existed = $store->exists($username);
$store->put($username, $password);
echo ($existed ? 'password set for ' : 'created '), $username, "\n";