enhance phpdoc and add @access to specify which usergroup can use the ApiCommands; add --import-settings parameter to config-services.php CLI script to gain even more automatism when setting up

Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2018-02-24 10:57:52 +01:00
parent 81d6a856d9
commit 9a61a56732
9 changed files with 217 additions and 62 deletions

View File

@@ -21,6 +21,8 @@ class Admins extends ApiCommand implements ResourceEntity
/**
* lists all admin entries
*
* @access admin
* @throws Exception
* @return array count|list
*/
public function list()
@@ -52,7 +54,8 @@ class Admins extends ApiCommand implements ResourceEntity
* optional, the admin-id
* @param string $loginname
* optional, the loginname
*
*
* @access admin
* @throws Exception
* @return array
*/
@@ -84,6 +87,13 @@ class Admins extends ApiCommand implements ResourceEntity
throw new Exception("Not allowed to execute given command.", 403);
}
/**
* create a new admin user
*
* @access admin
* @throws Exception
* @return array
*/
public function add()
{
if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) {
@@ -290,6 +300,18 @@ class Admins extends ApiCommand implements ResourceEntity
throw new Exception("Not allowed to execute given command.", 403);
}
/**
* update an admin user by given id or loginname
*
* @param int $id
* optional, the admin-id
* @param string $loginname
* optional, the loginname
*
* @access admin
* @throws Exception
* @return array
*/
public function update()
{
if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) {
@@ -551,7 +573,8 @@ class Admins extends ApiCommand implements ResourceEntity
* optional, the admin-id
* @param string $loginname
* optional, the loginname
*
*
* @access admin
* @throws Exception
* @return array
*/
@@ -619,7 +642,8 @@ class Admins extends ApiCommand implements ResourceEntity
* optional, the admin-id
* @param string $loginname
* optional, the loginname
*
*
* @access admin
* @throws Exception
* @return array
*/

View File

@@ -21,6 +21,7 @@ class Customers extends ApiCommand implements ResourceEntity
/**
* lists all customer entries
*
* @access admin
* @return array count|list
*/
public function list()
@@ -61,20 +62,21 @@ class Customers extends ApiCommand implements ResourceEntity
* @param string $loginname
* optional, the loginname
*
* @access admin, customer
* @throws Exception
* @return array
*/
public function get()
{
$id = $this->getParam('id', true, 0);
$ln_optional = ($id <= 0 ? false : true);
$loginname = $this->getParam('loginname', $ln_optional, '');
if ($id <= 0 && empty($loginname)) {
throw new Exception("Either 'id' or 'loginname' parameter must be given", 406);
}
if ($this->isAdmin()) {
$id = $this->getParam('id', true, 0);
$ln_optional = ($id <= 0 ? false : true);
$loginname = $this->getParam('loginname', $ln_optional, '');
if ($id <= 0 && empty($loginname)) {
throw new Exception("Either 'id' or 'loginname' parameter must be given", 406);
}
$result_stmt = Database::prepare("
SELECT * FROM `" . TABLE_PANEL_CUSTOMERS . "`
WHERE " . ($id > 0 ? "`customerid` = :idln" : "`loginname` = :idln") . ($this->getUserDetail('customers_see_all') ? '' : " AND `adminid` = :adminid"));
@@ -84,17 +86,32 @@ class Customers extends ApiCommand implements ResourceEntity
if ($this->getUserDetail('customers_see_all') == '0') {
$params['adminid'] = $this->getUserDetail('adminid');
}
$result = Database::pexecute_first($result_stmt, $params, true, true);
if ($result) {
$this->logger()->logAction(ADM_ACTION, LOG_NOTICE, "[API] get customer '" . $result['loginname'] . "'");
return $this->response(200, "successfull", $result);
} else {
if (($id > 0 && $id != $this->getUserDetail('customerid')) || ! empty($loginname) && $loginname != $this->getUserDetail('loginname')) {
throw new Exception("You cannot access data of other customers", 401);
}
$key = ($id > 0 ? "id #" . $id : "loginname '" . $loginname . "'");
throw new Exception("Customer with " . $key . " could not be found", 404);
$result_stmt = Database::prepare("
SELECT * FROM `" . TABLE_PANEL_CUSTOMERS . "`
WHERE " . ($id > 0 ? "`customerid` = :idln" : "`loginname` = :idln"));
$params = array(
'idln' => ($id <= 0 ? $loginname : $id)
);
}
throw new Exception("Not allowed to execute given command.", 403);
$result = Database::pexecute_first($result_stmt, $params, true, true);
if ($result) {
$this->logger()->logAction($this->isAdmin() ? ADM_ACTION : USR_ACTION, LOG_NOTICE, "[API] get customer '" . $result['loginname'] . "'");
return $this->response(200, "successfull", $result);
}
$key = ($id > 0 ? "id #" . $id : "loginname '" . $loginname . "'");
throw new Exception("Customer with " . $key . " could not be found", 404);
}
/**
* create a new customer with default ftp-user and standard-subdomain (if wanted)
*
* @access admin
* @return array
*/
public function add()
{
if ($this->isAdmin()) {
@@ -236,7 +253,9 @@ class Customers extends ApiCommand implements ResourceEntity
))->get();
$loginname_check = json_decode($dup_check_result, true)['data'];
} catch (Exception $e) {
$loginname_check = array('loginname' => '');
$loginname_check = array(
'loginname' => ''
);
}
// Check if an admin with the loginname already exists
@@ -246,7 +265,9 @@ class Customers extends ApiCommand implements ResourceEntity
))->get();
$loginname_check_admin = json_decode($dup_check_result, true)['data'];
} catch (Exception $e) {
$loginname_check_admin = array('loginname' => '');
$loginname_check_admin = array(
'loginname' => ''
);
}
if (strtolower($loginname_check['loginname']) == strtolower($loginname) || strtolower($loginname_check_admin['loginname']) == strtolower($loginname)) {
@@ -646,17 +667,29 @@ class Customers extends ApiCommand implements ResourceEntity
throw new Exception("Not allowed to execute given command.", 403);
}
/**
* update customer entry by either id or loginname
*
* @param int $id
* optional, the customer-id
* @param string $loginname
* optional, the loginname
*
* @access admin, customer
* @throws Exception
* @return array
*/
public function update()
{
if ($this->isAdmin()) {
$id = $this->getParam('id', true, 0);
$ln_optional = ($id <= 0 ? false : true);
$loginname = $this->getParam('loginname', $ln_optional, '');
if ($id <= 0 && empty($loginname)) {
throw new Exception("Either 'id' or 'loginname' parameter must be given", 406);
}
$json_result = Customers::getLocal($this->getUserData(), array(
'id' => $id,
'loginname' => $loginname
@@ -729,36 +762,18 @@ class Customers extends ApiCommand implements ResourceEntity
if (Settings::Get('ticket.enabled') != '1') {
$tickets = - 1;
}
if (empty($theme)) {
$theme = Settings::Get('panel.default_theme');
}
$diskspace = $diskspace * 1024;
$traffic = $traffic * 1024 * 1024;
if (((($this->getUserDetail('diskspace_used') + $diskspace - $result['diskspace']) > $this->getUserDetail('diskspace')) && ($this->getUserDetail('diskspace') / 1024) != '-1')
|| ((($this->getUserDetail('mysqls_used') + $mysqls - $result['mysqls']) > $this->getUserDetail('mysqls')) && $this->getUserDetail('mysqls') != '-1')
|| ((($this->getUserDetail('emails_used') + $emails - $result['emails']) > $this->getUserDetail('emails')) && $this->getUserDetail('emails') != '-1')
|| ((($this->getUserDetail('email_accounts_used') + $email_accounts - $result['email_accounts']) > $this->getUserDetail('email_accounts')) && $this->getUserDetail('email_accounts') != '-1')
|| ((($this->getUserDetail('email_forwarders_used') + $email_forwarders - $result['email_forwarders']) > $this->getUserDetail('email_forwarders')) && $this->getUserDetail('email_forwarders') != '-1')
|| ((($this->getUserDetail('email_quota_used') + $email_quota - $result['email_quota']) > $this->getUserDetail('email_quota')) && $this->getUserDetail('email_quota') != '-1' && Settings::Get('system.mail_quota_enabled') == '1')
|| ((($this->getUserDetail('ftps_used') + $ftps - $result['ftps']) > $this->getUserDetail('ftps')) && $this->getUserDetail('ftps') != '-1')
|| ((($this->getUserDetail('tickets_used') + $tickets - $result['tickets']) > $this->getUserDetail('tickets')) && $this->getUserDetail('tickets') != '-1')
|| ((($this->getUserDetail('subdomains_used') + $subdomains - $result['subdomains']) > $this->getUserDetail('subdomains')) && $this->getUserDetail('subdomains') != '-1')
|| (($diskspace / 1024) == '-1' && ($this->getUserDetail('diskspace') / 1024) != '-1')
|| ($mysqls == '-1' && $this->getUserDetail('mysqls') != '-1')
|| ($emails == '-1' && $this->getUserDetail('emails') != '-1')
|| ($email_accounts == '-1' && $this->getUserDetail('email_accounts') != '-1')
|| ($email_forwarders == '-1' && $this->getUserDetail('email_forwarders') != '-1')
|| ($email_quota == '-1' && $this->getUserDetail('email_quota') != '-1' && Settings::Get('system.mail_quota_enabled') == '1')
|| ($ftps == '-1' && $this->getUserDetail('ftps') != '-1')
|| ($tickets == '-1' && $this->getUserDetail('tickets') != '-1')
|| ($subdomains == '-1' && $this->getUserDetail('subdomains') != '-1')
) {
if (((($this->getUserDetail('diskspace_used') + $diskspace - $result['diskspace']) > $this->getUserDetail('diskspace')) && ($this->getUserDetail('diskspace') / 1024) != '-1') || ((($this->getUserDetail('mysqls_used') + $mysqls - $result['mysqls']) > $this->getUserDetail('mysqls')) && $this->getUserDetail('mysqls') != '-1') || ((($this->getUserDetail('emails_used') + $emails - $result['emails']) > $this->getUserDetail('emails')) && $this->getUserDetail('emails') != '-1') || ((($this->getUserDetail('email_accounts_used') + $email_accounts - $result['email_accounts']) > $this->getUserDetail('email_accounts')) && $this->getUserDetail('email_accounts') != '-1') || ((($this->getUserDetail('email_forwarders_used') + $email_forwarders - $result['email_forwarders']) > $this->getUserDetail('email_forwarders')) && $this->getUserDetail('email_forwarders') != '-1') || ((($this->getUserDetail('email_quota_used') + $email_quota - $result['email_quota']) > $this->getUserDetail('email_quota')) && $this->getUserDetail('email_quota') != '-1' && Settings::Get('system.mail_quota_enabled') == '1') || ((($this->getUserDetail('ftps_used') + $ftps - $result['ftps']) > $this->getUserDetail('ftps')) && $this->getUserDetail('ftps') != '-1') || ((($this->getUserDetail('tickets_used') + $tickets - $result['tickets']) > $this->getUserDetail('tickets')) && $this->getUserDetail('tickets') != '-1') || ((($this->getUserDetail('subdomains_used') + $subdomains - $result['subdomains']) > $this->getUserDetail('subdomains')) && $this->getUserDetail('subdomains') != '-1') || (($diskspace / 1024) == '-1' && ($this->getUserDetail('diskspace') / 1024) != '-1') || ($mysqls == '-1' && $this->getUserDetail('mysqls') != '-1') || ($emails == '-1' && $this->getUserDetail('emails') != '-1') || ($email_accounts == '-1' && $this->getUserDetail('email_accounts') != '-1') || ($email_forwarders == '-1' && $this->getUserDetail('email_forwarders') != '-1') || ($email_quota == '-1' && $this->getUserDetail('email_quota') != '-1' && Settings::Get('system.mail_quota_enabled') == '1') || ($ftps == '-1' && $this->getUserDetail('ftps') != '-1') || ($tickets == '-1' && $this->getUserDetail('tickets') != '-1') || ($subdomains == '-1' && $this->getUserDetail('subdomains') != '-1')) {
standard_error('youcantallocatemorethanyouhave', '', true);
}
// Either $name and $firstname or the $company must be inserted
if ($name == '' && $company == '') {
standard_error(array(
@@ -1160,6 +1175,7 @@ class Customers extends ApiCommand implements ResourceEntity
* @param bool $delete_userfiles
* optional, default false
*
* @access admin
* @throws Exception
* @return array
*/
@@ -1405,6 +1421,7 @@ class Customers extends ApiCommand implements ResourceEntity
* @param string $loginname
* optional, the loginname
*
* @access admin
* @throws Exception
* @return array
*/
@@ -1414,11 +1431,11 @@ class Customers extends ApiCommand implements ResourceEntity
$id = $this->getParam('id', true, 0);
$ln_optional = ($id <= 0 ? false : true);
$loginname = $this->getParam('loginname', $ln_optional, '');
if ($id <= 0 && empty($loginname)) {
throw new Exception("Either 'id' or 'loginname' parameter must be given", 406);
}
$json_result = Customers::getLocal($this->getUserData(), array(
'id' => $id,
'loginname' => $loginname

View File

@@ -21,6 +21,8 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
/**
* lists all fpm-daemon entries
*
* @access admin
* @throws Exception
* @return array count|list
*/
public function list()
@@ -72,6 +74,8 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
*
* @param int $id fpm-daemon-id
*
* @access admin
* @throws Exception
* @return array
*/
public function get()
@@ -93,6 +97,13 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
throw new Exception("Not allowed to execute given command.", 403);
}
/**
* create a new fpm-daemon entry
*
* @access admin
* @throws Exception
* @return array
*/
public function add()
{
if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) {
@@ -166,6 +177,15 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
throw new Exception("Not allowed to execute given command.", 403);
}
/**
* update a fpm-daemon entry by given id
*
* @param int $id
*
* @access admin
* @throws Exception
* @return array
*/
public function update()
{
if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) {
@@ -251,6 +271,7 @@ class FpmDaemons extends ApiCommand implements ResourceEntity
*
* @param int $id fpm-daemon-id
*
* @access admin
* @throws Exception
* @return array
*/

View File

@@ -21,6 +21,7 @@ class Froxlor extends ApiCommand
/**
* checks whether there is a newer version of froxlor available
*
* @access admin
* @throws Exception
* @return string
*/
@@ -82,6 +83,8 @@ class Froxlor extends ApiCommand
/**
*
* @todo import settings
*
* @access admin
*/
public function importSettings()
{}
@@ -89,6 +92,8 @@ class Froxlor extends ApiCommand
/**
*
* @todo export settings to file
*
* @access admin
*/
public function exportSettings()
{}
@@ -96,6 +101,8 @@ class Froxlor extends ApiCommand
/**
* return a list of all settings
*
* @access admin
* @throws Exception
* @return array count|list
*/
public function listSettings()
@@ -126,6 +133,7 @@ class Froxlor extends ApiCommand
* @param string $key
* settinggroup.varname couple
*
* @access admin
* @throws Exception
* @return string
*/
@@ -146,6 +154,7 @@ class Froxlor extends ApiCommand
* @param string $value
* optional the new value, default is ''
*
* @access admin
* @throws Exception
* @return string
*/
@@ -153,7 +162,7 @@ class Froxlor extends ApiCommand
{
// currently not implemented as it required validation too so no wrong settings are being stored via API
throw new Exception("Not available yet.", 501);
if ($this->isAdmin() && $this->getUserDetail('change_serversettings')) {
$setting = $this->getParam('key');
$value = $this->getParam('value', true, '');
@@ -173,6 +182,7 @@ class Froxlor extends ApiCommand
* @param string $module
* optional, return list of functions for a specific module
*
* @access admin, customer
* @throws Exception
* @return array
*/
@@ -260,7 +270,7 @@ class Froxlor extends ApiCommand
'head' => 'There is no comment-block for "' . $module . '.' . $function . '"'
);
}
$clines = explode("\n", $comment);
$result = array();
$result['params'] = array();
@@ -277,6 +287,15 @@ class Froxlor extends ApiCommand
'desc' => (isset($r[3]) ? trim($r['3']) : '')
);
$param_desc = true;
} // check access-section
elseif (strpos($c, '@access')) {
preg_match('/^\*\s\@access\s(.*)/', $c, $r);
if (! isset($r[0]) || empty($r[0])) {
$r[1] = 'This function has no restrictions';
}
$result['access'] = array(
'groups' => (isset($r[1]) ? trim($r[1]) : '')
);
} // check return-section
elseif (strpos($c, '@return')) {
preg_match('/^\*\s\@return\s(\w+)(\s.*)?/', $c, $r);
@@ -312,6 +331,7 @@ class Froxlor extends ApiCommand
}
}
}
$result['head'] =trim($result['head']);
return $result;
} catch (\ReflectionException $e) {
return array();

View File

@@ -21,6 +21,8 @@ class IpsAndPorts extends ApiCommand implements ResourceEntity
/**
* lists all ip/port entries
*
* @access admin
* @throws Exception
* @return array count|list
*/
public function list()
@@ -48,7 +50,8 @@ class IpsAndPorts extends ApiCommand implements ResourceEntity
*
* @param int $id
* ip-port-id
*
*
* @access admin
* @throws Exception
* @return array
*/
@@ -71,6 +74,13 @@ class IpsAndPorts extends ApiCommand implements ResourceEntity
throw new Exception("Not allowed to execute given command.", 403);
}
/**
* create a new ip/port entry
*
* @access admin
* @throws Exception
* @return array
*/
public function add()
{
if ($this->isAdmin() && $this->getUserDetail('change_serversettings')) {
@@ -199,6 +209,15 @@ class IpsAndPorts extends ApiCommand implements ResourceEntity
throw new Exception("Not allowed to execute given command.", 403);
}
/**
* update ip/port entry by given id
*
* @param int $id
*
* @access admin
* @throws ErrorException
* @return array
*/
public function update()
{
if ($this->isAdmin() && $this->getUserDetail('change_serversettings')) {
@@ -349,7 +368,8 @@ class IpsAndPorts extends ApiCommand implements ResourceEntity
*
* @param int $id
* ip-port-id
*
*
* @access admin
* @throws Exception
* @return array
*/

View File

@@ -31,6 +31,7 @@ class Mysqls extends ApiCommand implements ResourceEntity
* @param int $dbserver
* optional, specify database-server, default is none
*
* @access admin, customer
* @throws Exception
* @return array
*/
@@ -130,6 +131,7 @@ class Mysqls extends ApiCommand implements ResourceEntity
* @param string $loginname
* optional, admin-only, select dbs of a specific customer by loginname
*
* @access admin, customer
* @return array count|list
*/
public function list()
@@ -220,6 +222,7 @@ class Mysqls extends ApiCommand implements ResourceEntity
* @param int $dbserver
* optional, specify database-server, default is none
*
* @access admin, customer
* @throws Exception
* @return array
*/

View File

@@ -21,6 +21,8 @@ class PhpSettings extends ApiCommand implements ResourceEntity
/**
* lists all php-setting entries
*
* @access admin
* @throws Exception
* @return array count|list
*/
public function list()
@@ -106,6 +108,8 @@ class PhpSettings extends ApiCommand implements ResourceEntity
*
* @param int $id php-settings-id
*
* @access admin
* @throws Exception
* @return array
*/
public function get()
@@ -127,6 +131,13 @@ class PhpSettings extends ApiCommand implements ResourceEntity
throw new Exception("Not allowed to execute given command.", 403);
}
/**
* add new php-settings entry
*
* @access admin
* @throws Exception
* @return array
*/
public function add()
{
if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) {
@@ -226,6 +237,15 @@ class PhpSettings extends ApiCommand implements ResourceEntity
throw new Exception("Not allowed to execute given command.", 403);
}
/**
* update a php-setting entry by given id
*
* @param int $id
*
* @access admin
* @throws Exception::
* @return array
*/
public function update()
{
if ($this->isAdmin() && $this->getUserDetail('change_serversettings') == 1) {
@@ -333,6 +353,7 @@ class PhpSettings extends ApiCommand implements ResourceEntity
*
* @param int $id php-settings-id
*
* @access admin
* @throws Exception
* @return array
*/