]> git.mxchange.org Git - friendica.git/commitdiff
Add list command
authornupplaPhil <admin+github@philipp.info>
Tue, 25 Feb 2020 21:16:27 +0000 (22:16 +0100)
committernupplaPhil <admin+github@philipp.info>
Sat, 29 Feb 2020 16:10:31 +0000 (17:10 +0100)
src/Console/User.php
src/Content/Pager.php
src/Model/Register.php
src/Model/User.php
src/Module/Admin/Users.php

index ccd3caa3797b6f39353f1f0c5beba9928df1fa6a..3fdeac1c5631a02ed3ccbed7c5e98a3d9924aa6d 100644 (file)
@@ -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 [<nickname>] [-h|--help|-?] [-v]
        bin/console user block [<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
        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;
+               }
+       }
 }
index 5b4345a4c8bc4a2140884e50b063c8893eb40c30..a5e61bbf9f5827341ec669b5fac444f5a15bf5e8 100644 (file)
@@ -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 = '';
 
index 88e424309e692fb4bca121cb0c59ea2775ffd0db..be00699bface1273fba86e0483de1062590761a6 100644 (file)
@@ -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);
index 4c4534b29617ae361efea76a6c5afebbffb9a05f..85a999b37b54adf0840cb76fef1b5d6c287d2e07 100644 (file)
@@ -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);
+       }
 }
index 73a11e65a8de775f2a6db8648de632759c2066cb..66f70a90c195a953988de7eafd78784ddaa0b704 100644 (file)
@@ -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) {