mirror of
https://code.mensbeam.com/MensBeam/Arsse.git
synced 2024-12-22 21:22:40 +00:00
Add more user management functionality to the CLI
This commit is contained in:
parent
d40243a84c
commit
9428d7468a
3 changed files with 81 additions and 25 deletions
|
@ -18,7 +18,8 @@ if (\PHP_SAPI=="cli") {
|
||||||
// initialize the CLI; this automatically handles --help and --version
|
// initialize the CLI; this automatically handles --help and --version
|
||||||
$cli = new CLI;
|
$cli = new CLI;
|
||||||
// handle other CLI requests; some do not require configuration
|
// handle other CLI requests; some do not require configuration
|
||||||
$cli->dispatch();
|
$exitStatus = $cli->dispatch();
|
||||||
|
exit($exitStatus);
|
||||||
} else {
|
} else {
|
||||||
// load configuration
|
// load configuration
|
||||||
$conf = file_exists(BASE."config.php") ? new Conf(BASE."config.php") : new Conf;
|
$conf = file_exists(BASE."config.php") ? new Conf(BASE."config.php") : new Conf;
|
||||||
|
|
88
lib/CLI.php
88
lib/CLI.php
|
@ -6,6 +6,8 @@
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
namespace JKingWeb\Arsse;
|
namespace JKingWeb\Arsse;
|
||||||
|
|
||||||
|
use Docopt\Response as Opts;
|
||||||
|
|
||||||
class CLI {
|
class CLI {
|
||||||
protected $args = [];
|
protected $args = [];
|
||||||
|
|
||||||
|
@ -15,13 +17,17 @@ class CLI {
|
||||||
Usage:
|
Usage:
|
||||||
$prog daemon
|
$prog daemon
|
||||||
$prog feed refresh <n>
|
$prog feed refresh <n>
|
||||||
$prog conf save-defaults <file>
|
$prog conf save-defaults [<file>]
|
||||||
|
$prog user [list]
|
||||||
$prog user add <username> [<password>]
|
$prog user add <username> [<password>]
|
||||||
|
$prog user remove <username>
|
||||||
|
$prog user set-pass [--oldpass=<pass>] <username> [<password>]
|
||||||
|
$prog user auth <username> <password>
|
||||||
$prog --version
|
$prog --version
|
||||||
$prog --help | -h
|
$prog --help | -h
|
||||||
|
|
||||||
The Arsse command-line interface currently allows you to start the refresh
|
The Arsse command-line interface currently allows you to start the refresh
|
||||||
daemon, refresh a specific feed by numeric ID, add a user, or save default
|
daemon, refresh a specific feed by numeric ID, manage users, or save default
|
||||||
configuration to a sample file.
|
configuration to a sample file.
|
||||||
USAGE_TEXT;
|
USAGE_TEXT;
|
||||||
}
|
}
|
||||||
|
@ -35,6 +41,18 @@ USAGE_TEXT;
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function command(array $options, $args): string {
|
||||||
|
foreach ($options as $cmd) {
|
||||||
|
foreach (explode(" ", $cmd) as $part) {
|
||||||
|
if (!$args[$part]) {
|
||||||
|
continue 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $cmd;
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
protected function loadConf(): bool {
|
protected function loadConf(): bool {
|
||||||
$conf = file_exists(BASE."config.php") ? new Conf(BASE."config.php") : new Conf;
|
$conf = file_exists(BASE."config.php") ? new Conf(BASE."config.php") : new Conf;
|
||||||
Arsse::load($conf);
|
Arsse::load($conf);
|
||||||
|
@ -46,27 +64,24 @@ USAGE_TEXT;
|
||||||
public function dispatch(array $args = null): int {
|
public function dispatch(array $args = null): int {
|
||||||
// act on command line
|
// act on command line
|
||||||
$args = $args ?? $this->args;
|
$args = $args ?? $this->args;
|
||||||
if ($this->command("daemon", $args)) {
|
try {
|
||||||
|
switch ($this->command(["daemon", "feed refresh", "conf save-defaults", "user"], $args)) {
|
||||||
|
case "daemon":
|
||||||
$this->loadConf();
|
$this->loadConf();
|
||||||
return $this->daemon();
|
return $this->daemon();
|
||||||
} elseif ($this->command("feed refresh", $args)) {
|
case "feed refresh":
|
||||||
$this->loadConf();
|
$this->loadConf();
|
||||||
return $this->feedRefresh((int) $args['<n>']);
|
return $this->feedRefresh((int) $args['<n>']);
|
||||||
} elseif ($this->command("conf save-defaults", $args)) {
|
case "conf save-defaults":
|
||||||
return $this->confSaveDefaults($args['<file>']);
|
return $this->confSaveDefaults($args['<file>']);
|
||||||
} elseif ($this->command("user add", $args)) {
|
case "user":
|
||||||
$this->loadConf();
|
$this->loadConf();
|
||||||
return $this->userAdd($args['<username>'], $args['<password>']);
|
return $this->userManage($args);
|
||||||
}
|
}
|
||||||
|
} catch (AbstractException $e) {
|
||||||
|
fwrite(STDERR, $e->getMessage().\PHP_EOL);
|
||||||
|
return $e->getCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function command($cmd, $args): bool {
|
|
||||||
foreach (explode(" ", $cmd) as $part) {
|
|
||||||
if (!$args[$part]) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function daemon(bool $loop = true): int {
|
public function daemon(bool $loop = true): int {
|
||||||
|
@ -78,15 +93,52 @@ USAGE_TEXT;
|
||||||
return (int) !Arsse::$db->feedUpdate($id); // FIXME: exception error codes should be returned here
|
return (int) !Arsse::$db->feedUpdate($id); // FIXME: exception error codes should be returned here
|
||||||
}
|
}
|
||||||
|
|
||||||
public function confSaveDefaults(string $file): int {
|
public function confSaveDefaults(string $file = null): int {
|
||||||
|
$file = ($file=="-" ? null : $file) ?? STDOUT;
|
||||||
return (int) !(new Conf)->exportFile($file, true);
|
return (int) !(new Conf)->exportFile($file, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function userAdd(string $user, string $password = null): int {
|
public function userManage($args): int {
|
||||||
$passwd = Arsse::$user->add($user, $password);
|
switch ($this->command(["add", "remove", "set-pass", "list", "auth"], $args)) {
|
||||||
|
case "add":
|
||||||
|
return $this->userAddOrSetPassword("add", $args["<username>"], $args["<password>"]);
|
||||||
|
case "set-pass":
|
||||||
|
return $this->userAddOrSetPassword("passwordSet", $args["<username>"], $args["<password>"], $args["<oldpass>"]);
|
||||||
|
case "remove":
|
||||||
|
return (int) !Arsse::$user->remove($args["<username>"]);
|
||||||
|
case "auth":
|
||||||
|
return $this->userAuthenticate($args["<username>"], $args["<password>"]);
|
||||||
|
case "list":
|
||||||
|
case "":
|
||||||
|
return $this->userList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function userAddOrSetPassword(string $method, string $user, string $password = null, string $oldpass = null): int {
|
||||||
|
$args = \func_get_args();
|
||||||
|
array_shift($args);
|
||||||
|
$passwd = Arsse::$user->$method(...$args);
|
||||||
if (is_null($password)) {
|
if (is_null($password)) {
|
||||||
echo $passwd.\PHP_EOL;
|
echo $passwd.\PHP_EOL;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function userList(): int {
|
||||||
|
$list = Arsse::$user->list();
|
||||||
|
if ($list) {
|
||||||
|
echo implode(\PHP_EOL, $list).\PHP_EOL;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function userAuthenticate(string $user, string $password): int {
|
||||||
|
if (Arsse::$user->auth($user, $password)) {
|
||||||
|
echo Arsse::$lang->msg("CLI.Auth.Success").\PHP_EOL;
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
echo Arsse::$lang->msg("CLI.Auth.Failure").\PHP_EOL;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,9 @@
|
||||||
* See LICENSE and AUTHORS files for details */
|
* See LICENSE and AUTHORS files for details */
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
'CLI.Auth.Success' => 'Authentication successful',
|
||||||
|
'CLI.Auth.Failure' => 'Authentication failed',
|
||||||
|
|
||||||
'API.TTRSS.Category.Uncategorized' => 'Uncategorized',
|
'API.TTRSS.Category.Uncategorized' => 'Uncategorized',
|
||||||
'API.TTRSS.Category.Special' => 'Special',
|
'API.TTRSS.Category.Special' => 'Special',
|
||||||
'API.TTRSS.Category.Labels' => 'Labels',
|
'API.TTRSS.Category.Labels' => 'Labels',
|
||||||
|
|
Loading…
Reference in a new issue