From 28f6d59336b98567463c6d2c56d5be04f789751a Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Mon, 17 Oct 2022 17:26:13 +0200 Subject: [PATCH] allow optional length parameter for Froxlor.generatePassword API command Signed-off-by: Michael Kaufmann --- lib/Froxlor/Api/Commands/Froxlor.php | 6 +++++- lib/Froxlor/System/Crypt.php | 6 ++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/Froxlor/Api/Commands/Froxlor.php b/lib/Froxlor/Api/Commands/Froxlor.php index c99314ed..761b1c8e 100644 --- a/lib/Froxlor/Api/Commands/Froxlor.php +++ b/lib/Froxlor/Api/Commands/Froxlor.php @@ -254,12 +254,16 @@ class Froxlor extends ApiCommand /** * returns a random password based on froxlor settings for min-length, included characters, etc. * + * @param int $length + * optional length of password, defaults to 10 + * * @access admin, customer * @return string */ public function generatePassword() { - return $this->response(Crypt::generatePassword()); + $length = $this->getParam('length', true, 10); + return $this->response(Crypt::generatePassword($length)); } /** diff --git a/lib/Froxlor/System/Crypt.php b/lib/Froxlor/System/Crypt.php index e0f5330e..086fb370 100644 --- a/lib/Froxlor/System/Crypt.php +++ b/lib/Froxlor/System/Crypt.php @@ -36,13 +36,15 @@ class Crypt /** * Generates a random password */ - public static function generatePassword() + public static function generatePassword(int $length = 0) { $alpha_lower = 'abcdefghijklmnopqrstuvwxyz'; $alpha_upper = strtoupper($alpha_lower); $numeric = '0123456789'; $special = Settings::Get('panel.password_special_char'); - $length = Settings::Get('panel.password_min_length') > 3 ? Settings::Get('panel.password_min_length') : 10; + if (empty($length)) { + $length = Settings::Get('panel.password_min_length') > 3 ? Settings::Get('panel.password_min_length') : $length; + } $pw = self::specialShuffle($alpha_lower); $n = floor(($length) / 4);