From 2adc6a0974ad6f7e9609a0fce268d4a53e2ce479 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Tue, 25 Feb 2020 22:16:27 +0100 Subject: [PATCH] Add list command --- src/Console/User.php | 58 ++++++++++++++++++++++++++++++++++++-- src/Content/Pager.php | 5 +++- src/Model/Register.php | 11 ++++++-- src/Model/User.php | 27 ++++++++++++++++++ src/Module/Admin/Users.php | 11 +------- 5 files changed, 95 insertions(+), 17 deletions(-) diff --git a/src/Console/User.php b/src/Console/User.php index ccd3caa379..3fdeac1c56 100644 --- a/src/Console/User.php +++ b/src/Console/User.php @@ -21,7 +21,9 @@ namespace Friendica\Console; +use Console_Table; use Friendica\App; +use Friendica\Content\Pager; use Friendica\Core\L10n; use Friendica\Database\Database; use Friendica\Model\Register; @@ -30,9 +32,7 @@ use RuntimeException; use Seld\CliPrompt\CliPrompt; /** - * tool to set a new password for a user - * - * With this tool, you can set a new password for a user + * tool to manage users of the current node */ class User extends \Asika\SimpleConsole\Console { @@ -63,6 +63,8 @@ Usage bin/console user deny [] [-h|--help|-?] [-v] bin/console user block [] [-h|--help|-?] [-v] bin/console user unblock [] [-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 Modify user settings per console commands. @@ -118,6 +120,8 @@ HELP; return $this->blockUser(false); case 'delete': return $this->deleteUser(); + case 'list': + return $this->listUser(); default: throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.'); } @@ -305,4 +309,52 @@ HELP; 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; + } + } } diff --git a/src/Content/Pager.php b/src/Content/Pager.php index 5b4345a4c8..a5e61bbf9f 100644 --- a/src/Content/Pager.php +++ b/src/Content/Pager.php @@ -30,10 +30,13 @@ use Friendica\Util\Strings; */ class Pager { + /** @var int Default count of items per page */ + const ITEMS_PER_PAGE = 50; + /** @var integer */ private $page = 1; /** @var integer */ - protected $itemsPerPage = 50; + protected $itemsPerPage = self::ITEMS_PER_PAGE; /** @var string */ protected $baseQueryString = ''; diff --git a/src/Model/Register.php b/src/Model/Register.php index 88e424309e..be00699bfa 100644 --- a/src/Model/Register.php +++ b/src/Model/Register.php @@ -21,6 +21,7 @@ namespace Friendica\Model; +use Friendica\Content\Pager; use Friendica\Database\DBA; use Friendica\Util\DateTimeFormat; use Friendica\Util\Strings; @@ -33,16 +34,20 @@ class Register /** * 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 * @throws \Exception */ - public static function getPending() + public static function getPending($start = 0, $count = Pager::ITEMS_PER_PAGE) { $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` 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); diff --git a/src/Model/User.php b/src/Model/User.php index 4c4534b296..85a999b37b 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -23,6 +23,7 @@ namespace Friendica\Model; use DivineOmega\PasswordExposed; use Exception; +use Friendica\Content\Pager; use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Logger; @@ -1316,4 +1317,30 @@ class User 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); + } } diff --git a/src/Module/Admin/Users.php b/src/Module/Admin/Users.php index 73a11e65a8..66f70a90c1 100644 --- a/src/Module/Admin/Users.php +++ b/src/Module/Admin/Users.php @@ -156,7 +156,6 @@ class Users extends BaseAdmin $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 = [ 'contact.name', 'user.email', @@ -179,16 +178,8 @@ class Users extends BaseAdmin $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` - 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); + $users = User::getUsers($pager->getStart(), $pager->getItemsPerPage(), $order, $order_direction); $adminlist = explode(',', str_replace(' ', '', DI::config()->get('config', 'admin_email'))); $_setup_users = function ($e) use ($adminlist) {