]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add API endpoints for API basic user management
authorMarcin Haba <marcin.haba@bacula.pl>
Fri, 12 Nov 2021 19:23:21 +0000 (20:23 +0100)
committerMarcin Haba <marcin.haba@bacula.pl>
Fri, 12 Nov 2021 19:23:21 +0000 (20:23 +0100)
19 files changed:
gui/baculum/protected/API/Class/BasicConfig.php
gui/baculum/protected/API/Lang/en/messages.mo
gui/baculum/protected/API/Lang/en/messages.po
gui/baculum/protected/API/Lang/pl/messages.mo
gui/baculum/protected/API/Lang/pl/messages.po
gui/baculum/protected/API/Lang/pt/messages.mo
gui/baculum/protected/API/Lang/pt/messages.po
gui/baculum/protected/API/Lang/ru/messages.mo
gui/baculum/protected/API/Lang/ru/messages.po
gui/baculum/protected/API/Pages/API/BasicUser.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/BasicUsers.php [new file with mode: 0644]
gui/baculum/protected/API/Pages/API/OAuth2Client.php
gui/baculum/protected/API/Pages/API/config.xml
gui/baculum/protected/API/Pages/API/endpoints.xml
gui/baculum/protected/API/Pages/Panel/APIBasicUsers.php
gui/baculum/protected/API/openapi_baculum.json
gui/baculum/protected/Common/Class/BasicUserConfig.php
gui/baculum/protected/Common/Class/Errors.php
gui/baculum/protected/Common/Portlets/NewAuthClient.tpl

index b41ecc902cf3124b8791c9e9caf71c073b7f2bbc..84e4fd58456e4360ff417762229cfde406d3b468 100644 (file)
@@ -131,6 +131,31 @@ class BasicConfig extends ConfigFileModule {
                return $is_valid;
        }
 
+       /**
+        * Get users.
+        * NOTE: User list bases on basic password file.
+        * @see BasicAPIUserConfig
+        *
+        * @return array basic user list
+        */
+       public function getUsers() {
+               $basic_users = [];
+               $basic_apiuser = $this->getModule('basic_apiuser')->getUsers();
+               $basic_config = $this->getConfig();
+               foreach($basic_apiuser as $user => $pwd) {
+                       $bconsole_cfg_path = '';
+                       if (key_exists($user, $basic_config) && key_exists('bconsole_cfg_path', $basic_config[$user])) {
+                               $bconsole_cfg_path = $basic_config[$user]['bconsole_cfg_path'];
+                       }
+                       $basic_users[] = [
+                               'username' => $user,
+                               'bconsole_cfg_path' => $bconsole_cfg_path
+                       ];
+               }
+               return $basic_users;
+
+       }
+
        /**
         * Add single basic user to config.
         * NOTE: Basic password hashes are stored in separate file.
@@ -198,15 +223,18 @@ class BasicConfig extends ConfigFileModule {
         * @return boolean true on success, otherwise false
         */
        public function removeUser($username) {
-               $success = false;
                $config = $this->getConfig();
                if (key_exists($username, $config)) {
                        unset($config[$username]);
-                       $success = $this->setConfig($config);
-               }
-               if ($success) {
-                       $success = $this->getModule('basic_apiuser')->removeUser($username);
+                       $this->setConfig($config);
                }
+               /**
+                * There is returned only state of removing user from password file because
+                * because user can be defined in password file but it does not have
+                * to be defined in basic.conf file. It is for backward compatibility
+                * with config files.
+                */
+               $success = $this->getModule('basic_apiuser')->removeUser($username);
                return $success;
        }
 }
index d00e4cca8bed78257a077ade6edf708e2f17b42c..17872bad29ea3b16d4d0736888704178d88b2410 100644 (file)
Binary files a/gui/baculum/protected/API/Lang/en/messages.mo and b/gui/baculum/protected/API/Lang/en/messages.mo differ
index a001c3226b28f8b5cbd78c14e8f227e5a6f5d04c..dfe00ea0e6976dd097ada156d46b99e37b6ba74b 100644 (file)
@@ -685,3 +685,6 @@ msgstr "Binaries:"
 
 msgid "Configs:"
 msgstr "Configs:"
+
+msgid "Dedicated Bconsole config"
+msgstr "Dedicated Bconsole config"
index b1faff5726b40b6f088cd7503e179cd277bc4894..afd1d1e20683fc60a47bb3ed9ee2c54c90a1aefe 100644 (file)
Binary files a/gui/baculum/protected/API/Lang/pl/messages.mo and b/gui/baculum/protected/API/Lang/pl/messages.mo differ
index cc7c55fc03776e331d5bf270421d3b205e79517b..208a350445495875af2984c10206c5ddedb04d4f 100644 (file)
@@ -691,3 +691,6 @@ msgstr "Pliki bin.:"
 
 msgid "Configs:"
 msgstr "Pliki konfig.:"
+
+msgid "Dedicated Bconsole config"
+msgstr "Dedykowana konfiguracja Bconsole"
index a7351ef70c2b529ca7dd2c9f7652fc736338062c..7d8008aa760993203d5241ebedac2ab62aa05950 100644 (file)
Binary files a/gui/baculum/protected/API/Lang/pt/messages.mo and b/gui/baculum/protected/API/Lang/pt/messages.mo differ
index 1c3ce986bcb480576f27d960ca5f88e4a9a38849..0c5d798ff64fe5d75976c0dce19e2e6ff1e3c26e 100644 (file)
@@ -692,3 +692,6 @@ msgstr "Binários:"
 msgid "Configs:"
 msgstr "Configurações:"
 
+
+msgid "Dedicated Bconsole config"
+msgstr "Dedicated Bconsole config"
index ecd7ee8b0031a33b6890bd3f0195bf7bbf0c5036..5d14856f5c8804a3f8e47132e421441e4ceed4e5 100644 (file)
Binary files a/gui/baculum/protected/API/Lang/ru/messages.mo and b/gui/baculum/protected/API/Lang/ru/messages.mo differ
index 6cae288e30c928ba69db824c4541ab883cab7e92..64c13649f0ef0fca22510bcf8793c521d6e38ff6 100644 (file)
@@ -692,3 +692,6 @@ msgstr "Двоичные файлы:"
 msgid "Configs:"
 msgstr "Настройка:"
 
+
+msgid "Dedicated Bconsole config"
+msgstr "Dedicated Bconsole config"
diff --git a/gui/baculum/protected/API/Pages/API/BasicUser.php b/gui/baculum/protected/API/Pages/API/BasicUser.php
new file mode 100644 (file)
index 0000000..a9e87cd
--- /dev/null
@@ -0,0 +1,273 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2021 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+/**
+ * Basic user endpoint.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category API
+ * @package Baculum API
+ */
+class BasicUser extends BaculumAPIServer {
+
+       public function get() {
+               $user = $this->Request->contains('id') ? $this->Request['id'] : 0;
+               $username = $this->getModule('basic_apiuser')->validateUsername($user) ? $user : null;
+               if (is_string($username)) {
+                       $basic_config = $this->getModule('basic_config')->getConfig($username);
+                       if (count($basic_config) > 0) {
+                               $this->output = array_merge($basic_config, ['username' => $username]);
+                               $this->error = BasicUserError::ERROR_NO_ERRORS;
+                       } else {
+                               $this->output = BasicUserError::MSG_ERROR_BASIC_USER_DOES_NOT_EXIST;
+                               $this->error = BasicUserError::ERROR_BASIC_USER_DOES_NOT_EXIST;
+                       }
+               } else {
+                       $this->output = BasicUserError::MSG_ERROR_BASIC_USER_INVALID_USERNAME;
+                       $this->error = BasicUserError::ERROR_BASIC_USER_INVALID_USERNAME;
+               }
+       }
+
+       public function create($params) {
+               $basic_apiuser = $this->getModule('basic_apiuser');
+               $basic_config = $this->getModule('basic_config');
+               $misc = $this->getModule('misc');
+               $basic_cfg = $basic_apiuser->getUsers();
+               $username = '';
+               $password = '';
+               $props = [];
+
+               if (property_exists($params, 'username') && key_exists($params->username, $basic_cfg)) {
+                       $this->output = BasicUserError::MSG_ERROR_BASIC_USER_ALREADY_EXISTS;
+                       $this->error = BasicUserError::ERROR_BASIC_USER_ALREADY_EXISTS;
+                       return;
+               }
+
+               if (property_exists($params, 'username') && $basic_apiuser->validateUsername($params->username)) {
+                       $username = $params->username;
+               } else {
+                       $this->output = BasicUserError::MSG_ERROR_BASIC_USER_INVALID_USERNAME;
+                       $this->error = BasicUserError::ERROR_BASIC_USER_INVALID_USERNAME;
+                       return;
+               }
+
+               if (property_exists($params, 'password')) {
+                       $password = $params->password;
+               } else {
+                       $this->output = BasicUserError::MSG_ERROR_BASIC_USER_INVALID_PASSWORD;
+                       $this->error = BasicUserError::ERROR_BASIC_USER_INVALID_PASSWORD;
+                       return;
+               }
+
+               if (property_exists($params, 'bconsole_cfg_path')) {
+                       if ($misc->isValidPath($params->bconsole_cfg_path)) {
+                               $props['bconsole_cfg_path'] = $params->bconsole_cfg_path;
+                       } else {
+                               $this->output = BasicUserError::MSG_ERROR_BASIC_USER_INVALID_BCONSOLE_CFG_PATH;
+                               $this->error = BasicUserError::ERROR_BASIC_USER_INVALID_BCONSOLE_CFG_PATH;
+                               return;
+                       }
+               } else {
+                       $props['bconsole_cfg_path'] = '';
+               }
+
+               if (property_exists($params, 'console') && property_exists($params, 'director')) {
+                       if (!$misc->isValidName($params->console)) {
+                               $this->output = BasicUserError::MSG_ERROR_BASIC_USER_INVALID_CONSOLE;
+                               $this->error = BasicUserError::ERROR_BASIC_USER_INVALID_CONSOLE;
+                               return;
+                       }
+                       if (!$misc->isValidName($params->director)) {
+                               $this->output = BasicUserError::MSG_ERROR_BASIC_USER_INVALID_DIRECTOR;
+                               $this->error = BasicUserError::ERROR_BASIC_USER_INVALID_DIRECTOR;
+                               return;
+                       }
+                       $bs = $this->getModule('bacula_setting');
+
+                       $dir_cfg = $bs->getConfig('bcons', 'Director', $params->director);
+                       if ($dir_cfg['exitcode'] != 0) {
+                               $this->output = $dir_cfg['output'];
+                               $this->error = BasicUserError::ERROR_INTERNAL_ERROR;
+                               return;
+                       }
+
+                       $console_cfg = $bs->getConfig('dir', 'Console', $params->console);
+                       if ($console_cfg['exitcode'] != 0) {
+                               $this->output = $console_cfg['output'];
+                               $this->error = BasicUserError::ERROR_INTERNAL_ERROR;
+                               return;
+                       }
+
+                       $cfg = [
+                               [
+                                       'Director' => [
+                                               'Name' => '"' . $dir_cfg['output']['Name'] . '"',
+                                               'DirPort' => $dir_cfg['output']['DirPort'],
+                                               'Address' => $dir_cfg['output']['Address'],
+                                               'Password' => 'XXXX'
+                                       ],
+                                       'Console' => [
+                                               'Name' => '"' . $console_cfg['output']['Name'] . '"',
+                                               'Password' => '"' . $console_cfg['output']['Password'] . '"'
+                                       ]
+                               ]
+                       ];
+                       $json_tools = $this->getModule('api_config')->getConfig('jsontools');
+                       $dir = $json_tools['bconfig_dir'];
+                       $file = sprintf('%s/bconsole-%s.cfg', $dir, $console_cfg['output']['Name']);
+                       $this->getModule('bacula_config')->setConfig('bcons', $cfg, $file);
+                       $props['bconsole_cfg_path'] = $file;
+               }
+
+               // save config
+               $result = $basic_config->addUser($username, $password, $props);
+
+               if ($result) {
+                       $this->output = $props;
+                       $this->error = BasicUserError::ERROR_NO_ERRORS;
+               } else {
+                       $this->output = BasicUserError::MSG_ERROR_INTERNAL_ERROR;
+                       $this->error = BasicUserError::ERROR_INTERNAL_ERROR;
+               }
+       }
+
+       public function set($id, $params) {
+               $basic_apiuser = $this->getModule('basic_apiuser');
+               $basic_config = $this->getModule('basic_config');
+               $misc = $this->getModule('misc');
+               $basic_cfg = $basic_apiuser->getUsers();
+               $username = '';
+               $password = '';
+               $props = [];
+
+               if (property_exists($params, 'username') && !key_exists($params->username, $basic_cfg)) {
+                       $this->output = BasicUserError::MSG_ERROR_BASIC_USER_DOES_NOT_EXIST;
+                       $this->error = BasicUserError::ERROR_BASIC_USER_DOES_NOT_EXIST;
+                       return;
+               }
+
+               if (property_exists($params, 'username') && $basic_apiuser->validateUsername($params->username)) {
+                       $username = $params->username;
+               } else {
+                       $this->output = BasicUserError::MSG_ERROR_BASIC_USER_INVALID_USERNAME;
+                       $this->error = BasicUserError::ERROR_BASIC_USER_INVALID_USERNAME;
+                       return;
+               }
+
+               if (property_exists($params, 'password')) {
+                       $password = $params->password;
+               } else {
+                       $this->output = BasicUserError::MSG_ERROR_BASIC_USER_INVALID_PASSWORD;
+                       $this->error = BasicUserError::ERROR_BASIC_USER_INVALID_PASSWORD;
+                       return;
+               }
+
+               if (property_exists($params, 'bconsole_cfg_path')) {
+                       if ($misc->isValidPath($params->bconsole_cfg_path)) {
+                               $props['bconsole_cfg_path'] = $params->bconsole_cfg_path;
+                       } else {
+                               $this->output = BasicUserError::MSG_ERROR_BASIC_USER_INVALID_BCONSOLE_CFG_PATH;
+                               $this->error = BasicUserError::ERROR_BASIC_USER_INVALID_BCONSOLE_CFG_PATH;
+                               return;
+                       }
+               } else {
+                       $props['bconsole_cfg_path'] = '';
+               }
+
+               if (property_exists($params, 'console') && property_exists($params, 'director')) {
+                       if (!$misc->isValidName($params->console)) {
+                               $this->output = BasicUserError::MSG_ERROR_BASIC_USER_INVALID_CONSOLE;
+                               $this->error = BasicUserError::ERROR_BASIC_USER_INVALID_CONSOLE;
+                               return;
+                       }
+                       if (!$misc->isValidName($params->director)) {
+                               $this->output = BasicUserError::MSG_ERROR_BASIC_USER_INVALID_DIRECTOR;
+                               $this->error = BasicUserError::ERROR_BASIC_USER_INVALID_DIRECTOR;
+                               return;
+                       }
+                       $bs = $this->getModule('bacula_setting');
+
+                       $dir_cfg = $bs->getConfig('bcons', 'Director', $params->director);
+                       if ($dir_cfg['exitcode'] != 0) {
+                               $this->output = $dir_cfg['output'];
+                               $this->error = BasicUserError::ERROR_INTERNAL_ERROR;
+                               return;
+                       }
+
+                       $console_cfg = $bs->getConfig('dir', 'Console', $params->console);
+                       if ($console_cfg['exitcode'] != 0) {
+                               $this->output = $console_cfg['output'];
+                               $this->error = BasicUserError::ERROR_INTERNAL_ERROR;
+                               return;
+                       }
+
+                       $cfg = [
+                               [
+                                       'Director' => [
+                                               'Name' => '"' . $dir_cfg['output']['Name'] . '"',
+                                               'DirPort' => $dir_cfg['output']['DirPort'],
+                                               'Address' => $dir_cfg['output']['Address'],
+                                               'Password' => 'XXXX'
+                                       ],
+                                       'Console' => [
+                                               'Name' => '"' . $console_cfg['output']['Name'] . '"',
+                                               'Password' => '"' . $console_cfg['output']['Password'] . '"'
+                                       ]
+                               ]
+                       ];
+                       $json_tools = $this->getModule('api_config')->getConfig('jsontools');
+                       $dir = $json_tools['bconfig_dir'];
+                       $file = sprintf('%s/bconsole-%s.cfg', $dir, $console_cfg['output']['Name']);
+                       $this->getModule('bacula_config')->setConfig('bcons', $cfg, $file);
+                       $props['bconsole_cfg_path'] = $file;
+               }
+
+               // save config
+               $result = $basic_config->editUser($username, $password, $props);
+
+               if ($result) {
+                       $this->output = $props;
+                       $this->error = BasicUserError::ERROR_NO_ERRORS;
+               } else {
+                       $this->output = BasicUserError::MSG_ERROR_INTERNAL_ERROR;
+                       $this->error = BasicUserError::ERROR_INTERNAL_ERROR;
+               }
+       }
+
+       public function remove($id) {
+               $user_cfg = $this->getModule('basic_apiuser')->getUserCfg($id);
+               if (count($user_cfg) > 0) {
+                       $result = $this->getModule('basic_config')->removeUser($id);
+                       if ($result) {
+                               $this->output = [];
+                               $this->error = BasicUserError::ERROR_NO_ERRORS;
+                       } else {
+                               $this->output = BasicUserError::MSG_ERROR_INTERNAL_ERROR;
+                               $this->error = BasicUserError::ERROR_INTERNAL_ERROR;
+                       }
+               } else {
+                       $this->output = BasicUserError::MSG_ERROR_BASIC_USER_DOES_NOT_EXIST;
+                       $this->error = BasicUserError::ERROR_BASIC_USER_DOES_NOT_EXIST;
+               }
+       }
+}
+?>
diff --git a/gui/baculum/protected/API/Pages/API/BasicUsers.php b/gui/baculum/protected/API/Pages/API/BasicUsers.php
new file mode 100644 (file)
index 0000000..7c676f2
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+/*
+ * Bacula(R) - The Network Backup Solution
+ * Baculum   - Bacula web interface
+ *
+ * Copyright (C) 2013-2021 Kern Sibbald
+ *
+ * The main author of Baculum is Marcin Haba.
+ * The original author of Bacula is Kern Sibbald, with contributions
+ * from many others, a complete list can be found in the file AUTHORS.
+ *
+ * You may use this file and others of this release according to the
+ * license defined in the LICENSE file, which includes the Affero General
+ * Public License, v3.0 ("AGPLv3") and some additional permissions and
+ * terms pursuant to its AGPLv3 Section 7.
+ *
+ * This notice must be preserved when any source code is
+ * conveyed and/or propagated.
+ *
+ * Bacula(R) is a registered trademark of Kern Sibbald.
+ */
+
+/**
+ * Basic users endpoint.
+ *
+ * @author Marcin Haba <marcin.haba@bacula.pl>
+ * @category API
+ * @package Baculum API
+ */
+class BasicUsers extends BaculumAPIServer {
+
+       public function get() {
+               $this->output = $this->getModule('basic_config')->getUsers();;
+               $this->error = ClientError::ERROR_NO_ERRORS;
+       }
+}
+?>
index 52fbbf4dcecdbe9a557de51186a42f3db7923e46..ab291bba4b36006437e5fcbaefc103e305d6e806 100644 (file)
@@ -3,7 +3,7 @@
  * Bacula(R) - The Network Backup Solution
  * Baculum   - Bacula web interface
  *
- * Copyright (C) 2013-2020 Kern Sibbald
+ * Copyright (C) 2013-2021 Kern Sibbald
  *
  * The main author of Baculum is Marcin Haba.
  * The original author of Bacula is Kern Sibbald, with contributions
@@ -130,14 +130,14 @@ class OAuth2Client extends BaculumAPIServer {
 
                        $dir_cfg = $bs->getConfig('bcons', 'Director', $params->director);
                        if ($dir_cfg['exitcode'] != 0) {
-                               $this->output = $dir_cfg->output;
+                               $this->output = $dir_cfg['output'];
                                $this->error = OAuth2Error::ERROR_INTERNAL_ERROR;
                                return;
                        }
 
                        $console_cfg = $bs->getConfig('dir', 'Console', $params->console);
                        if ($console_cfg['exitcode'] != 0) {
-                               $this->output = $console_cfg->output;
+                               $this->output = $console_cfg['output'];
                                $this->error = OAuth2Error::ERROR_INTERNAL_ERROR;
                                return;
                        }
@@ -257,14 +257,14 @@ class OAuth2Client extends BaculumAPIServer {
 
                        $dir_cfg = $bs->getConfig('bcons', 'Director', $params->director);
                        if ($dir_cfg['exitcode'] != 0) {
-                               $this->output = $dir_cfg->output;
+                               $this->output = $dir_cfg['output'];
                                $this->error = OAuth2Error::ERROR_INTERNAL_ERROR;
                                return;
                        }
 
                        $console_cfg = $bs->getConfig('dir', 'Console', $params->console);
                        if ($console_cfg['exitcode'] != 0) {
-                               $this->output = $console_cfg->output;
+                               $this->output = $console_cfg['output'];
                                $this->error = OAuth2Error::ERROR_INTERNAL_ERROR;
                                return;
                        }
index a44502c1aabbeedfb05a90db8ec2c827e9c2bad5..c5cc3731fc46e8da58e86a3aa64a7fcadfb6b5a6 100644 (file)
@@ -11,6 +11,7 @@
                <module id="oauth2_token" class="Application.API.Class.OAuth2.TokenManager" />
 
                <!-- Basic user config -->
+               <module id="basic_apiuser" class="Application.API.Class.BasicAPIUserConfig" />
                <module id="basic_config" class="Application.API.Class.BasicConfig" />
 
                <!-- API Server modules -->
index 6e54bf0569beedf4d0655e521ac98bd7c24b0d23..4f507d65d7db89a466686c2679cdbaaae48ae18b 100644 (file)
        <!-- OAuth2 client endpoints -->
        <url ServiceParameter="OAuth2Clients" pattern="api/v2/oauth2/clients/" />
        <url ServiceParameter="OAuth2Client" pattern="api/v2/oauth2/clients/{id}/" parameters.id="[a-zA-Z0-9\-_]{32}" />
+       <!-- Basic user endpoints -->
+       <url ServiceParameter="BasicUsers" pattern="api/v2/basic/users/" />
+       <url ServiceParameter="BasicUser" pattern="api/v2/basic/users/{id}/" parameters.id="[a-zA-Z0-9]+" />
 
        <!-- API v1 -->
        <!-- general endpoint -->
index d9c6f278a07a05b1eac4bf24f5d7723988960928..ea93bc0fe1a84106c06d88fbe0c036643c0dbd10 100644 (file)
@@ -46,7 +46,7 @@ class APIBasicUsers extends BaculumAPIPage {
        }
 
        public function loadBasicUsers($sender, $param) {
-               $users = $this->getBasicUsers();
+               $users = $this->getModule('basic_config')->getUsers();
                $this->getCallbackClient()->callClientFunction(
                        'oAPIBasicUsers.load_basic_users_cb',
                        [$users]
@@ -74,23 +74,6 @@ class APIBasicUsers extends BaculumAPIPage {
                }
        }
 
-       private function getBasicUsers() {
-               $basic_users = array();
-               $basic_apiuser = $this->getModule('basic_apiuser')->getUsers();
-               $basic_config = $this->getModule('basic_config')->getConfig();
-               foreach($basic_apiuser as $user => $pwd) {
-                       $bconsole_cfg_path = '';
-                       if (key_exists($user, $basic_config) && key_exists('bconsole_cfg_path', $basic_config[$user])) {
-                               $bconsole_cfg_path = $basic_config[$user]['bconsole_cfg_path'];
-                       }
-                       $basic_users[] = [
-                               'username' => $user,
-                               'bconsole_cfg_path' => $bconsole_cfg_path
-                       ];
-               }
-               return $basic_users;
-       }
-
        public function deleteBasicUser($sender, $param) {
                $username = $param->getCallbackParameter();
                $this->getModule('basic_config')->removeUser($username);
index 472c55d48d98e61d4c672658dd5fe40f4e50533f..4df9b3e05629fe26ba076a3d0972bc9f5c39332e 100644 (file)
                        "OAuth2Client": {
                                "$ref": "#/definitions/OAuth2Client"
                        },
+                       "BasicUsers": {
+                               "type": "array",
+                               "items": {
+                                       "$ref": "#/definitions/BasicUser"
+                               }
+                       },
+                       "BasicUser": {
+                               "$ref": "#/definitions/BasicUser"
+                       },
                        "AutochangerDriveVolume": {
                                "$ref": "#/definitions/AutochangerDriveVolume"
                        },
                                                        "type": "string"
                                                }
                                        },
+                                       {
+                                               "name": "console",
+                                               "in": "body",
+                                               "required": false,
+                                               "description": "Director Console name to create dedicated bconsole.conf that is assigned to account. Parameter must be used together with 'director' parameter.",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "director",
+                                               "in": "body",
+                                               "required": false,
+                                               "description": "Director Name to create dedicated bconsole.conf that is assigned to account. Parameter must be used together with 'console' parameter.",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
                                        {
                                                "name": "name",
                                                "in": "body",
                                                        "type": "string"
                                                }
                                        },
+                                       {
+                                               "name": "console",
+                                               "in": "body",
+                                               "required": false,
+                                               "description": "Director Console name to create dedicated bconsole.conf that is assigned to account. Parameter must be used together with 'director' parameter.",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "director",
+                                               "in": "body",
+                                               "required": false,
+                                               "description": "Director Name to create dedicated bconsole.conf that is assigned to account. Parameter must be used together with 'console' parameter.",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
                                        {
                                                "name": "name",
                                                "in": "body",
                                        }
                                ]
                        }
+               },
+               "/api/v2/basic/users": {
+                       "get": {
+                               "tags": ["basic"],
+                               "summary": "Basic user list",
+                               "description": "Get Basic user list.",
+                               "responses": {
+                                       "200": {
+                                               "description": "List of Basic users properties",
+                                               "content": {
+                                                       "application/json": {
+                                                               "schema": {
+                                                                       "type": "object",
+                                                                       "properties": {
+                                                                               "output": {
+                                                                                       "$ref": "#/components/schemas/BasicUsers"
+                                                                               },
+                                                                               "error": {
+                                                                                       "type": "integer",
+                                                                                       "description": "Error code",
+                                                                                       "enum": [0, 1000]
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               },
+               "/api/v2/basic/users/{username}": {
+                       "get": {
+                               "tags": ["basic"],
+                               "summary": "Specific Basic user config",
+                               "description": "Get specific Basic user config",
+                               "responses": {
+                                       "200": {
+                                               "description": "Specific Basic user config",
+                                               "content": {
+                                                       "application/json": {
+                                                               "schema": {
+                                                                       "type": "object",
+                                                                       "properties": {
+                                                                               "output": {
+                                                                                       "$ref": "#/components/schemas/BasicUser"
+                                                                               },
+                                                                               "error": {
+                                                                                       "type": "integer",
+                                                                                       "description": "Error code",
+                                                                                       "enum": [0, 140, 142, 1000]
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               },
+                               "parameters": [
+                                       {
+                                               "name": "username",
+                                               "in": "path",
+                                               "required": true,
+                                               "description": "Basic user name",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       }
+                               ]
+                       },
+                       "put": {
+                               "tags": ["basic"],
+                               "summary": "Set Basic user settings",
+                               "description": "Set specific Basic user settings",
+                               "consumes": [ "application/json" ],
+                               "responses": {
+                                       "200": {
+                                               "description": "Set Basic user settings",
+                                               "content": {
+                                                       "application/json": {
+                                                               "schema": {
+                                                                       "type": "object",
+                                                                       "properties": {
+                                                                               "output": {
+                                                                                       "type": "object",
+                                                                                       "description": "Updated Basic user settings"
+                                                                               },
+                                                                               "error": {
+                                                                                       "type": "integer",
+                                                                                       "description": "Error code",
+                                                                                       "enum": [0, 140, 142, 143, 144, 145, 146, 1000]
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               },
+                               "parameters": [
+                                       {
+                                               "name": "username",
+                                               "in": "path",
+                                               "required": true,
+                                               "description": "Basic user name",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "password",
+                                               "in": "body",
+                                               "required": false,
+                                               "description": "Basic user password",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "bconsole_cfg_path",
+                                               "in": "body",
+                                               "required": false,
+                                               "description": "Dedicated Bconsole configuration file path",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "console",
+                                               "in": "body",
+                                               "required": false,
+                                               "description": "Director Console name to create dedicated bconsole.conf that is assigned to account. Parameter must be used together with 'director' parameter.",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "director",
+                                               "in": "body",
+                                               "required": false,
+                                               "description": "Director Name to create dedicated bconsole.conf that is assigned to account. Parameter must be used together with 'console' parameter.",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       }
+                               ]
+                       },
+                       "post": {
+                               "tags": ["basic"],
+                               "summary": "Create Basic user account",
+                               "description": "Create specific Basic user account",
+                               "consumes": [ "application/json" ],
+                               "responses": {
+                                       "200": {
+                                               "description": "Create Basic user account",
+                                               "content": {
+                                                       "application/json": {
+                                                               "schema": {
+                                                                       "type": "object",
+                                                                       "properties": {
+                                                                               "output": {
+                                                                                       "type": "object",
+                                                                                       "description": "New Basic user settings"
+                                                                               },
+                                                                               "error": {
+                                                                                       "type": "integer",
+                                                                                       "description": "Error code",
+                                                                                       "enum": [0, 141, 142, 143, 144, 145, 146, 1000]
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               },
+                               "parameters": [
+                                       {
+                                               "name": "username",
+                                               "in": "path",
+                                               "required": true,
+                                               "description": "Basic user name",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "password",
+                                               "in": "body",
+                                               "required": true,
+                                               "description": "Basic user password",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "bconsole_cfg_path",
+                                               "in": "body",
+                                               "required": false,
+                                               "description": "Dedicated Bconsole configuration file path",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "console",
+                                               "in": "body",
+                                               "required": false,
+                                               "description": "Director Console name to create dedicated bconsole.conf that is assigned to account. Parameter must be used together with 'director' parameter.",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       },
+                                       {
+                                               "name": "director",
+                                               "in": "body",
+                                               "required": false,
+                                               "description": "Director Name to create dedicated bconsole.conf that is assigned to account. Parameter must be used together with 'console' parameter.",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       }
+                               ]
+                       },
+                       "delete": {
+                               "tags": ["basic"],
+                               "summary": "Delete Basic user account",
+                               "description": "Delete Basic user account.",
+                               "responses": {
+                                       "200": {
+                                               "description": "Delete Basic user account",
+                                               "content": {
+                                                       "application/json": {
+                                                               "schema": {
+                                                                       "type": "object",
+                                                                       "properties": {
+                                                                               "output": {
+                                                                                       "type": "array",
+                                                                                       "items": {
+                                                                                       }
+                                                                               },
+                                                                               "error": {
+                                                                                       "type": "integer",
+                                                                                       "description": "Error code",
+                                                                                       "enum": [0, 140, 1000]
+                                                                               }
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               },
+                               "parameters": [
+                                       {
+                                               "name": "username",
+                                               "in": "path",
+                                               "required": true,
+                                               "description": "Basic user name",
+                                               "schema": {
+                                                       "type": "string"
+                                               }
+                                       }
+                               ]
+                       }
                }
        },
        "definitions": {
                                }
                        }
                },
+               "BasicUser": {
+                       "type": "object",
+                       "properties": {
+                               "bconsole_cfg_path": {
+                                       "description": "Dedicated Bconsole configuration file",
+                                       "type": "string"
+                               },
+                               "username": {
+                                       "description": "User login name",
+                                       "type": "string"
+                               }
+                       }
+               },
                "AutochangerDriveVolume": {
                        "type": "object",
                        "properties": {
index 68c340e097de67a8b380d2d174b1bae4989ce534..0b5d891a6746de105ecdad1172967cb8b8ddd5b2 100644 (file)
@@ -3,7 +3,7 @@
  * Bacula(R) - The Network Backup Solution
  * Baculum   - Bacula web interface
  *
- * Copyright (C) 2013-2020 Kern Sibbald
+ * Copyright (C) 2013-2021 Kern Sibbald
  *
  * The main author of Baculum is Marcin Haba.
  * The original author of Bacula is Kern Sibbald, with contributions
@@ -41,6 +41,11 @@ class BasicUserConfig extends CommonModule {
         */
        const USER_PATTERN = '[a-zA-Z0-9]+';
 
+       /**
+        * Password allowed characters pattern.
+        */
+       const PASSWORD_PATTERN = '[\S\s]{5,60}';
+
        /**
         * Get config file path to store users' parameters.
         * @return string config path
@@ -195,7 +200,6 @@ class BasicUserConfig extends CommonModule {
         * @return boolean true if users removed successfully, otherwise false
         */
        public function removeUsers(array $usernames) {
-               $result = false;
                $all_users = $this->getUsers();
                for ($i = 0; $i < count($usernames); $i++) {
                        if (key_exists($usernames[$i], $all_users)) {
@@ -225,4 +229,8 @@ class BasicUserConfig extends CommonModule {
                $result = file_put_contents($this->getConfigPath(), '', LOCK_EX) !== false;
                return $result;
        }
+
+       public function validateUsername($username) {
+               return (preg_match('/^' . self::USER_PATTERN . '$/', $username) === 1);
+       }
 }
index b5537b34ca8600b16754afedcb73509127a8f3ee..3ed0823caa26986295486347bbaa1aed112f936d 100644 (file)
@@ -3,7 +3,7 @@
  * Bacula(R) - The Network Backup Solution
  * Baculum   - Bacula web interface
  *
- * Copyright (C) 2013-2020 Kern Sibbald
+ * Copyright (C) 2013-2021 Kern Sibbald
  *
  * The main author of Baculum is Marcin Haba.
  * The original author of Bacula is Kern Sibbald, with contributions
@@ -247,4 +247,22 @@ class DeviceError extends GenericError {
        const MSG_ERROR_DEVICE_DRIVE_DOES_NOT_BELONG_TO_AUTOCHANGER = 'Drive does not belong to selected autochanger.';
 }
 
+class BasicUserError extends GenericError {
+
+       const ERROR_BASIC_USER_DOES_NOT_EXIST = 140;
+       const ERROR_BASIC_USER_ALREADY_EXISTS = 141;
+       const ERROR_BASIC_USER_INVALID_USERNAME = 142;
+       const ERROR_BASIC_USER_INVALID_BCONSOLE_CFG_PATH = 143;
+       const ERROR_BASIC_USER_INVALID_CONSOLE = 144;
+       const ERROR_BASIC_USER_INVALID_DIRECTOR = 145;
+       const ERROR_BASIC_USER_INVALID_PASSWORD= 146;
+
+       const MSG_ERROR_BASIC_USER_DOES_NOT_EXIST = 'Basic user does not exist.';
+       const MSG_ERROR_BASIC_USER_ALREADY_EXISTS = 'Basic user already exists.';
+       const MSG_ERROR_BASIC_USER_INVALID_USERNAME = 'Invalid basic user username';
+       const MSG_ERROR_BASIC_USER_INVALID_BCONSOLE_CFG_PATH = 'Invalid bconsole config path.';
+       const MSG_ERROR_BASIC_USER_INVALID_CONSOLE = 'Invalid Console name.';
+       const MSG_ERROR_BASIC_USER_INVALID_DIRECTOR = 'Invalid Director name.';
+       const MSG_ERROR_BASIC_USER_INVALID_PASSWORD = 'Invalid password.';
+}
 ?>
index 13fbe95b31bcae4108be9655dbe5b92cdb91930b..40b34ff1470853beb1129dcd0d3ad6e4dcd183a7 100644 (file)
@@ -62,7 +62,7 @@
                                        Display="Dynamic"
                                        ControlCssClass="invalidate"
                                        ControlToValidate="APIBasicPassword"
-                                       RegularExpression="[\S\s]{5,60}"
+                                       RegularExpression="<%=BasicUserConfig::PASSWORD_PATTERN%>"
                                        ValidationGroup="<%=$this->ClientID%>Basic"
                                        Text="<%[ Password must be longer than 4 chars. ]%>"
                                />
@@ -96,7 +96,7 @@
                                        Display="Dynamic"
                                        ControlCssClass="invalidate"
                                        ControlToValidate="RetypeAPIBasicPassword"
-                                       RegularExpression="[\S\s]{5,60}"
+                                       RegularExpression="<%=BasicUserConfig::PASSWORD_PATTERN%>"
                                        ValidationGroup="<%=$this->ClientID%>Basic"
                                        Text="<%[ Password must be longer than 4 chars. ]%>"
                                />