From 84925ed56fb4a299998773293b92c920779ccbec Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Sat, 5 Oct 2024 07:10:47 -0500 Subject: Refactor handling of valid account names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Move our system_names and system_groups out to a util header. * Qt UI: Validate that the user's chosen account name is not contained in either system_names nor system_groups. Closes: #388 Acked-by: Síle Ekaterin Liszka --- ui/qt5/accountpage.cc | 27 +++++++++++++++++++++++---- ui/qt5/accountpage.hh | 4 +--- 2 files changed, 24 insertions(+), 7 deletions(-) (limited to 'ui/qt5') diff --git a/ui/qt5/accountpage.cc b/ui/qt5/accountpage.cc index f4be6ac..bc0eb2f 100644 --- a/ui/qt5/accountpage.cc +++ b/ui/qt5/accountpage.cc @@ -11,12 +11,15 @@ */ #include "accountpage.hh" +#include "util/user.hh" + #ifdef HAS_INSTALL_ENV # include "commitpage.hh" #endif /* HAS_INSTALL_ENV */ #include #include +#include #include AccountPage::AccountPage(QWidget *parent) : HorizonWizardPage(parent) { @@ -48,19 +51,35 @@ AccountPage::AccountPage(QWidget *parent) : HorizonWizardPage(parent) { } bool AccountPage::isComplete() const { - return std::all_of(accountWidgets.begin(), accountWidgets.end(), + return std::all_of(accountWidgets.cbegin(), accountWidgets.cend(), [](UserAccountWidget *widget) { if(widget == nullptr) return true; return widget->isValid(); }); } -#ifdef HAS_INSTALL_ENV bool AccountPage::validatePage() { +#ifdef HAS_INSTALL_ENV /* hack to re-initialise page in case the user goes back */ horizonWizard()->removePage(HorizonWizard::Page_Commit); horizonWizard()->setPage(HorizonWizard::Page_Commit, new CommitPage); +#endif /* HAS_INSTALL_ENV */ - return true; + return std::all_of(accountWidgets.cbegin(), accountWidgets.cend(), + [this](const auto &widget) { + auto accountText = widget->accountText(); + if(accountText.isEmpty()) return true; + + auto username = accountText.toStdString(); + if(system_names.find(username) != system_names.end() || + system_groups.find(username) != system_groups.end()) { + QMessageBox::critical(this, tr("Invalid Username"), + tr("The username you have chosen (%1) is reserved " + "by the system. Choose a different username.") + .arg(accountText)); + return false; + } + + return true; + }); } -#endif /* HAS_INSTALL_ENV */ diff --git a/ui/qt5/accountpage.hh b/ui/qt5/accountpage.hh index 9ca7577..cf1a4ff 100644 --- a/ui/qt5/accountpage.hh +++ b/ui/qt5/accountpage.hh @@ -20,11 +20,9 @@ class AccountPage : public HorizonWizardPage { public: - AccountPage(QWidget *parent = nullptr); + explicit AccountPage(QWidget *parent = nullptr); bool isComplete() const override; -#ifdef HAS_INSTALL_ENV bool validatePage() override; -#endif std::array accountWidgets; }; -- cgit v1.2.3-70-g09d2