]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add support for plugin filter in client list endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Tue, 28 Feb 2023 14:22:55 +0000 (15:22 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Sun, 5 Mar 2023 06:06:30 +0000 (07:06 +0100)
gui/baculum/protected/API/Modules/ClientManager.php
gui/baculum/protected/API/Modules/Database.php
gui/baculum/protected/API/Pages/API/Clients.php
gui/baculum/protected/API/openapi_baculum.json

index f2608a24a9821dab0c57afd56576151c5660b5d2..2ee07aa27f0b0781f32ac08e7ee9e9ab4c6070f2 100644 (file)
@@ -33,15 +33,70 @@ use Prado\Data\ActiveRecord\TActiveRecordCriteria;
  */
 class ClientManager extends APIModule {
 
-       public function getClients($limit_val = 0, $offset_val = 0) {
-               $criteria = new TActiveRecordCriteria;
-               if(is_numeric($limit_val) && $limit_val > 0) {
-                       $criteria->Limit = $limit_val;
+       /**
+        * SQL query builder.
+        *
+        * @var TDbCommandBuilder command builder
+        */
+       private static $query_builder;
+
+       /**
+        * Get the SQL query builder instance.
+        * Note: Singleton
+        *
+        * @return TDbCommandBuilder command builder
+        */
+       private function getQueryBuilder() {
+               if (is_null(self::$query_builder)) {
+                       $record = ClientRecord::finder();
+                       $connection = $record->getDbConnection();
+                       $tableInfo = $record->getRecordGateway()->getRecordTableInfo($record);
+                       self::$query_builder = $tableInfo->createCommandBuilder($connection);
+               }
+               return self::$query_builder;
+       }
+
+       /**
+        * Get client list.
+        *
+        * @param mixed $limit_val result limit value
+        * @param int $offset_val result offset value
+        * @param array $criteria SQL criteria to get job list
+        * @return array clients or empty list if no client found
+        */
+       public function getClients($limit_val = 0, $offset_val = 0, $criteria = []) {
+               $limit = '';
+               if(is_int($limit_val) && $limit_val > 0) {
+                       $limit = ' LIMIT ' . $limit_val;
                }
+               $offset = '';
                if (is_int($offset_val) && $offset_val > 0) {
-                       $criteria->Offset = $offset_val;
+                       $offset = ' OFFSET ' . $offset_val;
+               }
+               $where = Database::getWhere($criteria);
+
+               $sql = 'SELECT *
+FROM Client 
+' . $where['where'] . $offset . $limit;
+
+               $builder = $this->getQueryBuilder();
+               if (count($where['params']) == 0) {
+                       /**
+                        * Please note that in case no params the TDbCommandBuilder::applyCriterias()
+                        * returns empty the PDO statement handler. From this reason here
+                        * the query is called directly by PDO.
+                        */
+                       $connection = JobRecord::finder()->getDbConnection();
+                       $connection->setActive(true);
+                       $pdo = $connection->getPdoInstance();
+                       $statement = $pdo->query($sql);
+
+               } else {
+                       $command = $builder->applyCriterias($sql, $where['params']);
+                       $statement = $command->getPdoStatement();
+                       $command->query();
                }
-               return ClientRecord::finder()->findAll($criteria);
+               return $statement->fetchAll(\PDO::FETCH_OBJ);
        }
 
        public function getClientByName($name) {
index 8134c284ca1b7a81eae78f0f806cd92234114cd2..226f039e7fcaa10ae5812e2466ccd931a6ec95c0 100644 (file)
@@ -194,6 +194,10 @@ class Database extends APIModule {
                                        } elseif (in_array($value[$i]['operator'], ['IS', 'IS NOT'])) {
                                                $cond[] = "{$key} {$value[$i]['operator']} {$value[$i]['vals']}";
                                                $value[$i]['operator'] = '';
+                                       } elseif ($value[$i]['operator'] == 'LIKE') {
+                                               $cond[] = "{$key} {$value[$i]['operator']} :{$kval}{$i}";
+                                               $vals[":{$kval}{$i}"] = $value[$i]['vals'];
+                                               $value[$i]['operator'] = '';
                                        } else {
                                                $cond[] = "$key = :{$kval}{$i}";
                                                $vals[":{$kval}{$i}"] = $value[$i]['vals'];
index a11d25c5165facb31eee9ad68219f0d93cf06976..69bc1f5f3610479b410065ca50652d7baf3eac61 100644 (file)
@@ -36,9 +36,22 @@ class Clients extends BaculumAPIServer {
                $misc = $this->getModule('misc');
                $limit = $this->Request->contains('limit') ? intval($this->Request['limit']) : 0;
                $offset = $this->Request->contains('offset') && $misc->isValidInteger($this->Request['offset']) ? (int)$this->Request['offset'] : 0;
+               $plugin = $this->Request->contains('plugin') && $misc->isValidAlphaNumeric($this->Request['plugin']) ? $this->Request['plugin'] : '';
                $result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.client'));
                if ($result->exitcode === 0) {
-                       $clients = $this->getModule('client')->getClients($limit, $offset);
+                       $params = [];
+                       if (!empty($plugin)) {
+                               $params['Plugins'] = [];
+                               $params['Plugins'][] = [
+                                       'operator' => 'LIKE',
+                                       'vals' => "%{$plugin}%"
+                               ];
+                       }
+                       $clients = $this->getModule('client')->getClients(
+                               $limit,
+                               $offset,
+                               $params
+                       );
                        array_shift($result->output);
                        $clients_output = array();
                        foreach($clients as $client) {
index 7c25719fb139a501b8fab8ce4d845fcd00112273..38e5b462110998221ea0cead292cc73f0ae4fbf6 100644 (file)
                                        },
                                        {
                                                "$ref": "#/components/parameters/Offset"
+                                       },
+                                       {
+                                               "name": "plugin",
+                                               "in": "query",
+                                               "description": "List clients that contain given plugin. Note, it searches by &midast;keyword&midast; so for PostgreSQL plugin both 'postgre' and 'postgresql' will work.",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
                                        }
                                ]
                        }