Add list command

This commit is contained in:
nupplaPhil 2020-02-25 22:16:27 +01:00
parent ecf7f40704
commit 2adc6a0974
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
5 changed files with 95 additions and 17 deletions

View file

@ -21,7 +21,9 @@
namespace Friendica\Console; namespace Friendica\Console;
use Console_Table;
use Friendica\App; use Friendica\App;
use Friendica\Content\Pager;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Model\Register; use Friendica\Model\Register;
@ -30,9 +32,7 @@ use RuntimeException;
use Seld\CliPrompt\CliPrompt; use Seld\CliPrompt\CliPrompt;
/** /**
* tool to set a new password for a user * tool to manage users of the current node
*
* With this tool, you can set a new password for a user
*/ */
class User extends \Asika\SimpleConsole\Console class User extends \Asika\SimpleConsole\Console
{ {
@ -63,6 +63,8 @@ Usage
bin/console user deny [<nickname>] [-h|--help|-?] [-v] bin/console user deny [<nickname>] [-h|--help|-?] [-v]
bin/console user block [<nickname>] [-h|--help|-?] [-v] bin/console user block [<nickname>] [-h|--help|-?] [-v]
bin/console user unblock [<nickname>] [-h|--help|-?] [-v] bin/console user unblock [<nickname>] [-h|--help|-?] [-v]
bin/console user list pending [start=0 [count=50]] [-h|--help|-?] [-v]
bin/console user list all [start=0 [count=50]] [-h|--help|-?] [-v]
Description Description
Modify user settings per console commands. Modify user settings per console commands.
@ -118,6 +120,8 @@ HELP;
return $this->blockUser(false); return $this->blockUser(false);
case 'delete': case 'delete':
return $this->deleteUser(); return $this->deleteUser();
case 'list':
return $this->listUser();
default: default:
throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.'); throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
} }
@ -305,4 +309,52 @@ HELP;
return UserModel::remove($user['uid'] ?? -1); return UserModel::remove($user['uid'] ?? -1);
} }
/**
* List user of the current node
*
* @return bool True, if the command was successful
*/
private function listUser()
{
$subCmd = $this->getArgument(1);
$start = $this->getArgument(2, 0);
$count = $this->getArgument(3, Pager::ITEMS_PER_PAGE);
$table = new Console_Table();
switch ($subCmd) {
case 'pending':
$table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register Date', 'Comment']);
$pending = Register::getPending($start, $count);
foreach ($pending as $contact) {
$table->addRow([
$contact['nick'],
$contact['name'],
$contact['url'],
$contact['email'],
$contact['created'],
$contact['note'],
]);
}
$this->out($table->getTable());
return true;
case 'all':
default:
$table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register Date', 'Comment']);
$contacts = UserModel::getUsers($start, $count);
foreach ($contacts as $contact) {
$table->addRow([
$contact['nick'],
$contact['name'],
$contact['url'],
$contact['email'],
$contact['created'],
$contact['note'],
]);
}
$this->out($table->getTable());
return true;
}
}
} }

View file

@ -30,10 +30,13 @@ use Friendica\Util\Strings;
*/ */
class Pager class Pager
{ {
/** @var int Default count of items per page */
const ITEMS_PER_PAGE = 50;
/** @var integer */ /** @var integer */
private $page = 1; private $page = 1;
/** @var integer */ /** @var integer */
protected $itemsPerPage = 50; protected $itemsPerPage = self::ITEMS_PER_PAGE;
/** @var string */ /** @var string */
protected $baseQueryString = ''; protected $baseQueryString = '';

View file

@ -21,6 +21,7 @@
namespace Friendica\Model; namespace Friendica\Model;
use Friendica\Content\Pager;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings; use Friendica\Util\Strings;
@ -33,16 +34,20 @@ class Register
/** /**
* Return the list of pending registrations * Return the list of pending registrations
* *
* @param int $start Start count (Default is 0)
* @param int $count Count of the items per page (Default is @see Pager::ITEMS_PER_PAGE)
*
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public static function getPending() public static function getPending($start = 0, $count = Pager::ITEMS_PER_PAGE)
{ {
$stmt = DBA::p( $stmt = DBA::p(
"SELECT `register`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`email` "SELECT `register`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`email`, `contact`.`nick`
FROM `register` FROM `register`
INNER JOIN `contact` ON `register`.`uid` = `contact`.`uid` INNER JOIN `contact` ON `register`.`uid` = `contact`.`uid`
INNER JOIN `user` ON `register`.`uid` = `user`.`uid`" INNER JOIN `user` ON `register`.`uid` = `user`.`uid`
LIMIT ?, ?", $start, $count
); );
return DBA::toArray($stmt); return DBA::toArray($stmt);

View file

@ -23,6 +23,7 @@ namespace Friendica\Model;
use DivineOmega\PasswordExposed; use DivineOmega\PasswordExposed;
use Exception; use Exception;
use Friendica\Content\Pager;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Logger; use Friendica\Core\Logger;
@ -1316,4 +1317,30 @@ class User
return $statistics; return $statistics;
} }
/**
* Get all users of the current node
*
* @param int $start Start count (Default is 0)
* @param int $count Count of the items per page (Default is @see Pager::ITEMS_PER_PAGE)
* @param string $order Order of the user list (Default is 'contact.name')
* @param string $order_direction Order direction (Default is ASC)
*
* @return array The list of the users
* @throws Exception
*/
public static function getUsers($start = 0, $count = Pager::ITEMS_PER_PAGE, $order = 'contact.name', $order_direction = '+')
{
$sql_order = '`' . str_replace('.', '`.`', $order) . '`';
$sql_order_direction = ($order_direction === '+') ? 'ASC' : 'DESC';
$usersStmt = DBA::p("SELECT `user`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`account_expired`, `contact`.`last-item` AS `lastitem_date`, `contact`.`nick`
FROM `user`
INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
WHERE `user`.`verified`
ORDER BY $sql_order $sql_order_direction LIMIT ?, ?", $start, $count
);
return DBA::toArray($usersStmt);
}
} }

View file

@ -156,7 +156,6 @@ class Users extends BaseAdmin
$pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 100); $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 100);
// @TODO Move below block to Model\User::getUsers($start, $count, $order = 'contact.name', $order_direction = '+')
$valid_orders = [ $valid_orders = [
'contact.name', 'contact.name',
'user.email', 'user.email',
@ -179,16 +178,8 @@ class Users extends BaseAdmin
$order = $new_order; $order = $new_order;
} }
} }
$sql_order = '`' . str_replace('.', '`.`', $order) . '`';
$sql_order_direction = ($order_direction === '+') ? 'ASC' : 'DESC';
$usersStmt = DBA::p("SELECT `user`.*, `contact`.`name`, `contact`.`url`, `contact`.`micro`, `user`.`account_expired`, `contact`.`last-item` AS `lastitem_date` $users = User::getUsers($pager->getStart(), $pager->getItemsPerPage(), $order, $order_direction);
FROM `user`
INNER JOIN `contact` ON `contact`.`uid` = `user`.`uid` AND `contact`.`self`
WHERE `user`.`verified`
ORDER BY $sql_order $sql_order_direction LIMIT ?, ?", $pager->getStart(), $pager->getItemsPerPage()
);
$users = DBA::toArray($usersStmt);
$adminlist = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email'))); $adminlist = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email')));
$_setup_users = function ($e) use ($adminlist) { $_setup_users = function ($e) use ($adminlist) {