From 6d42968d1a1f42a45f96b52df5e80226b67eee59 Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Mon, 24 Jul 2023 15:23:15 +0200 Subject: [PATCH] added abbility to enable/disable login for ftp users; fixes #1146 Signed-off-by: Michael Kaufmann --- lib/Froxlor/Api/Commands/Ftps.php | 16 ++++++++++++++-- lib/Froxlor/UI/Callbacks/Style.php | 5 +++++ lib/Froxlor/UI/Callbacks/Text.php | 8 ++++++++ .../customer/ftp/formfield.ftp_add.php | 8 +++++++- .../customer/ftp/formfield.ftp_edit.php | 8 +++++++- lib/tablelisting/customer/tablelisting.ftps.php | 15 +++++++++++++-- 6 files changed, 54 insertions(+), 6 deletions(-) diff --git a/lib/Froxlor/Api/Commands/Ftps.php b/lib/Froxlor/Api/Commands/Ftps.php index 41dc8cb9..e29ebf99 100644 --- a/lib/Froxlor/Api/Commands/Ftps.php +++ b/lib/Froxlor/Api/Commands/Ftps.php @@ -72,6 +72,8 @@ class Ftps extends ApiCommand implements ResourceEntity * optional whether to add additional usernames to the group * @param bool $is_defaultuser * optional whether this is the standard default ftp user which is being added so no usage is decreased + * @param bool $login_enabled + * optional whether to allow login (default) or not * * @access admin, customer * @return string json-encoded array @@ -84,6 +86,7 @@ class Ftps extends ApiCommand implements ResourceEntity } $is_defaultuser = $this->getBoolParam('is_defaultuser', true, 0); + $login_enabled = $this->getBoolParam('login_enabled', true, 1); if (($this->getUserDetail('ftps_used') < $this->getUserDetail('ftps') || $this->getUserDetail('ftps') == '-1') || $this->isAdmin() && $is_defaultuser == 1) { // required parameters @@ -176,13 +179,14 @@ class Ftps extends ApiCommand implements ResourceEntity $stmt = Database::prepare("INSERT INTO `" . TABLE_FTP_USERS . "` (`customerid`, `username`, `description`, `password`, `homedir`, `login_enabled`, `uid`, `gid`, `shell`) - VALUES (:customerid, :username, :description, :password, :homedir, 'y', :guid, :guid, :shell)"); + VALUES (:customerid, :username, :description, :password, :homedir, :loginenabled, :guid, :guid, :shell)"); $params = [ "customerid" => $customer['customerid'], "username" => $username, "description" => $description, "password" => $cryptPassword, "homedir" => $path, + "loginenabled" => $login_enabled ? 'Y' : 'N', "guid" => $customer['guid'], "shell" => $shell ]; @@ -389,6 +393,8 @@ class Ftps extends ApiCommand implements ResourceEntity * optional, description for ftp-user * @param string $shell * optional, default /bin/false (not changeable when deactivated) + * @param bool $login_enabled + * optional whether to allow login (default) or not * @param int $customerid * optional, required when called as admin (if $loginname is not specified) * @param string $loginname @@ -419,6 +425,7 @@ class Ftps extends ApiCommand implements ResourceEntity $password = $this->getParam('ftp_password', true, ''); $description = $this->getParam('ftp_description', true, $result['description']); $shell = $this->getParam('shell', true, $result['shell']); + $login_enabled = $this->getBoolParam('login_enabled', true, ($result['login_enabled'] == 'Y' ? 1 : 0)); // validation $password = Validate::validate($password, 'password', '', '', [], true); @@ -430,6 +437,10 @@ class Ftps extends ApiCommand implements ResourceEntity $shell = "/bin/false"; } + if ($login_enabled != 1) { + $login_enabled = 0; + } + // get needed customer info to reduce the ftp-user-counter by one $customer = $this->getCustomerData(); @@ -480,13 +491,14 @@ class Ftps extends ApiCommand implements ResourceEntity $stmt = Database::prepare(" UPDATE `" . TABLE_FTP_USERS . "` - SET `description` = :desc, `shell` = :shell + SET `description` = :desc, `shell` = :shell, `login_enabled` = :loginenabled WHERE `customerid` = :customerid AND `id` = :id "); Database::pexecute($stmt, [ "desc" => $description, "shell" => $shell, + "loginenabled" => $login_enabled ? 'Y' : 'N', "customerid" => $customer['customerid'], "id" => $id ], true, true); diff --git a/lib/Froxlor/UI/Callbacks/Style.php b/lib/Froxlor/UI/Callbacks/Style.php index 1019151c..315fbb47 100644 --- a/lib/Froxlor/UI/Callbacks/Style.php +++ b/lib/Froxlor/UI/Callbacks/Style.php @@ -34,6 +34,11 @@ class Style return $attributes['fields']['deactivated'] ? 'bg-danger' : ''; } + public static function loginDisabled(array $attributes): string + { + return $attributes['fields']['login_enabled'] == 'N' ? 'bg-danger' : ''; + } + public static function resultIntegrityBad(array $attributes): string { return $attributes['fields']['result'] ? '' : 'bg-warning'; diff --git a/lib/Froxlor/UI/Callbacks/Text.php b/lib/Froxlor/UI/Callbacks/Text.php index 2ec9735e..22bbf91f 100644 --- a/lib/Froxlor/UI/Callbacks/Text.php +++ b/lib/Froxlor/UI/Callbacks/Text.php @@ -43,6 +43,14 @@ class Text ]; } + public static function yesno(array $attributes): array + { + return [ + 'macro' => 'boolean', + 'data' => $attributes['data'] == 'Y' + ]; + } + public static function customerfullname(array $attributes): string { return User::getCorrectFullUserDetails($attributes['fields'], true); diff --git a/lib/formfields/customer/ftp/formfield.ftp_add.php b/lib/formfields/customer/ftp/formfield.ftp_add.php index c2320cd2..32e0e1ab 100644 --- a/lib/formfields/customer/ftp/formfield.ftp_add.php +++ b/lib/formfields/customer/ftp/formfield.ftp_add.php @@ -79,7 +79,13 @@ return [ 'type' => 'select', 'select_var' => $shells, 'selected' => '/bin/false' - ] + ], + 'login_enabled' => [ + 'label' => lng('panel.active'), + 'type' => 'checkbox', + 'value' => '1', + 'checked' => true + ], ] ] ] diff --git a/lib/formfields/customer/ftp/formfield.ftp_edit.php b/lib/formfields/customer/ftp/formfield.ftp_edit.php index 6375d568..e7659746 100644 --- a/lib/formfields/customer/ftp/formfield.ftp_edit.php +++ b/lib/formfields/customer/ftp/formfield.ftp_edit.php @@ -65,7 +65,13 @@ return [ 'type' => 'select', 'select_var' => $shells, 'selected' => $result['shell'] ?? '/bin/false' - ] + ], + 'login_enabled' => [ + 'label' => lng('panel.active'), + 'type' => 'checkbox', + 'value' => '1', + 'checked' => $result['login_enabled'] == 'Y', + ], ] ] ] diff --git a/lib/tablelisting/customer/tablelisting.ftps.php b/lib/tablelisting/customer/tablelisting.ftps.php index 4eed75ea..3d58af12 100644 --- a/lib/tablelisting/customer/tablelisting.ftps.php +++ b/lib/tablelisting/customer/tablelisting.ftps.php @@ -25,6 +25,8 @@ use Froxlor\Settings; use Froxlor\UI\Callbacks\Ftp; +use Froxlor\UI\Callbacks\Style; +use Froxlor\UI\Callbacks\Text; use Froxlor\UI\Listing; return [ @@ -51,13 +53,19 @@ return [ 'label' => lng('panel.shell'), 'field' => 'shell', 'visible' => Settings::Get('system.allow_customer_shell') == '1' + ], + 'login_enabled' => [ + 'label' => lng('panel.active'), + 'field' => 'login_enabled', + 'callback' => [Text::class, 'yesno'], ] ], 'visible_columns' => Listing::getVisibleColumnsForListing('ftp_list', [ 'username', 'description', 'homedir', - 'shell' + 'shell', + 'login_enabled', ]), 'actions' => [ 'edit' => [ @@ -81,6 +89,9 @@ return [ 'id' => ':id' ], ] - ] + ], + 'format_callback' => [ + [Style::class, 'loginDisabled'] + ], ] ];