Added first connection wizard to create the administrator account
This commit is contained in:
@@ -36,6 +36,7 @@
|
||||
#include "PlayQueueView.hpp"
|
||||
|
||||
#include "settings/DatabaseView.hpp"
|
||||
#include "settings/FirstConnectionView.hpp"
|
||||
|
||||
#include "LmsApplication.hpp"
|
||||
|
||||
@@ -90,29 +91,26 @@ LmsApplication::LmsApplication(const Wt::WEnvironment& env, Wt::Dbo::SqlConnecti
|
||||
messageResourceBundle().use(appRoot() + "playqueue");
|
||||
messageResourceBundle().use(appRoot() + "release");
|
||||
messageResourceBundle().use(appRoot() + "releases");
|
||||
messageResourceBundle().use(appRoot() + "settings");
|
||||
messageResourceBundle().use(appRoot() + "settings-database");
|
||||
messageResourceBundle().use(appRoot() + "settings-first-connection");
|
||||
messageResourceBundle().use(appRoot() + "tracks");
|
||||
messageResourceBundle().use(appRoot() + "templates");
|
||||
|
||||
setTitle("LMS");
|
||||
|
||||
// If here is no account in the database, launch the first connection wizard
|
||||
bool firstConnection;
|
||||
{
|
||||
Wt::Dbo::Transaction transaction(DboSession());
|
||||
|
||||
firstConnection = (Database::User::getAll(DboSession()).size() == 0);
|
||||
|
||||
// Create a fake user
|
||||
// TODO settings
|
||||
Database::User::create(DboSession());
|
||||
}
|
||||
|
||||
LMS_LOG(UI, DEBUG) << "Creating root widget. First connection = " << std::boolalpha << firstConnection;
|
||||
|
||||
// If here is no account in the database, launch the first connection wizard
|
||||
// if (firstConnection)
|
||||
// createFirstConnectionUI();
|
||||
// else
|
||||
if (firstConnection)
|
||||
createFirstConnectionUI();
|
||||
else
|
||||
createLmsUI();
|
||||
}
|
||||
|
||||
@@ -163,7 +161,7 @@ LmsApplication::createFirstConnectionUI()
|
||||
// Hack, use the auth widget builtin strings
|
||||
builtinLocalizedStrings().useBuiltin(skeletons::AuthStrings_xml1);
|
||||
|
||||
// root()->addWidget( new Settings::FirstConnectionFormView());
|
||||
root()->addWidget(new Settings::FirstConnectionView());
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -19,16 +19,31 @@
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include "DirectoryValidator.hpp"
|
||||
#include "Validators.hpp"
|
||||
|
||||
namespace UserInterface {
|
||||
|
||||
Wt::WValidator* createNameValidator()
|
||||
{
|
||||
Wt::WLengthValidator *v = new Wt::WLengthValidator();
|
||||
v->setMandatory(true);
|
||||
v->setMinimumLength(::Database::User::MinNameLength);
|
||||
v->setMaximumLength(::Database::User::MaxNameLength);
|
||||
return v;
|
||||
}
|
||||
|
||||
Wt::WValidator* createMandatoryValidator()
|
||||
{
|
||||
auto v = new Wt::WValidator();
|
||||
v->setMandatory(true);
|
||||
return v;
|
||||
}
|
||||
|
||||
DirectoryValidator::DirectoryValidator(Wt::WObject *parent)
|
||||
: Wt::WValidator(parent)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Wt::WValidator::Result
|
||||
DirectoryValidator::validate(const Wt::WString& input) const
|
||||
{
|
||||
@@ -19,26 +19,23 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Wt/WRegExpValidator>
|
||||
#include <Wt/WLengthValidator>
|
||||
|
||||
#include "database/User.hpp"
|
||||
|
||||
namespace UserInterface {
|
||||
|
||||
static inline Wt::WValidator *createEmailValidator()
|
||||
{
|
||||
Wt::WValidator *res = new Wt::WRegExpValidator("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}");
|
||||
res->setMandatory(true);
|
||||
return res;
|
||||
}
|
||||
Wt::WValidator* createNameValidator();
|
||||
Wt::WValidator* createMandatoryValidator();
|
||||
|
||||
static inline Wt::WValidator *createNameValidator() {
|
||||
Wt::WLengthValidator *v = new Wt::WLengthValidator();
|
||||
v->setMandatory(true);
|
||||
v->setMinimumLength(3);
|
||||
v->setMaximumLength(::Database::User::MaxNameLength);
|
||||
return v;
|
||||
}
|
||||
class DirectoryValidator : public Wt::WValidator
|
||||
{
|
||||
public:
|
||||
DirectoryValidator(Wt::WObject *parent = 0);
|
||||
|
||||
Wt::WValidator::Result validate(const Wt::WString& input) const override;
|
||||
|
||||
};
|
||||
|
||||
} // namespace UserInterface
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
#include <Wt/WFormModel>
|
||||
#include <Wt/WStringListModel>
|
||||
|
||||
#include "common/DirectoryValidator.hpp"
|
||||
#include "common/Validators.hpp"
|
||||
#include "database/MediaDirectory.hpp"
|
||||
#include "scanner/MediaScanner.hpp"
|
||||
#include "utils/Logger.hpp"
|
||||
@@ -214,12 +214,6 @@ class DatabaseModel : public Wt::WFormModel
|
||||
|
||||
}
|
||||
|
||||
Wt::WValidator *createMandatoryValidator()
|
||||
{
|
||||
Wt::WValidator* v = new Wt::WValidator();
|
||||
v->setMandatory(true);
|
||||
return v;
|
||||
}
|
||||
|
||||
Wt::WStringListModel* _updatePeriodModel;
|
||||
Wt::WStringListModel* _updateStartTimeModel;
|
||||
|
||||
@@ -21,8 +21,6 @@
|
||||
|
||||
#include <Wt/WContainerWidget>
|
||||
#include <Wt/WTemplateFormView>
|
||||
#include <Wt/WText>
|
||||
#include <Wt/WSignal>
|
||||
|
||||
namespace UserInterface {
|
||||
namespace Settings {
|
||||
@@ -34,17 +32,12 @@ class DatabaseView : public Wt::WTemplateFormView
|
||||
public:
|
||||
DatabaseView(Wt::WContainerWidget *parent = 0);
|
||||
|
||||
Wt::Signal<void>& changed() { return _sigChanged; }
|
||||
|
||||
private:
|
||||
|
||||
Wt::Signal<void> _sigChanged;
|
||||
|
||||
void processSave();
|
||||
void processDiscard();
|
||||
void processImmediateScan();
|
||||
|
||||
Wt::WText *_applyInfo;
|
||||
DatabaseModel *_model;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <Wt/WFormModel>
|
||||
#include <Wt/WLineEdit>
|
||||
#include <Wt/WPushButton>
|
||||
#include <Wt/Auth/Identity>
|
||||
|
||||
#include "utils/Logger.hpp"
|
||||
|
||||
#include "common/Validators.hpp"
|
||||
#include "LmsApplication.hpp"
|
||||
|
||||
#include "FirstConnectionView.hpp"
|
||||
|
||||
namespace UserInterface {
|
||||
namespace Settings {
|
||||
|
||||
class FirstConnectionModel : public Wt::WFormModel
|
||||
{
|
||||
public:
|
||||
|
||||
// Associate each field with a unique string literal.
|
||||
static const Field AdminLoginField;
|
||||
static const Field PasswordField;
|
||||
static const Field PasswordConfirmField;
|
||||
|
||||
FirstConnectionModel(Wt::WObject *parent = 0)
|
||||
: Wt::WFormModel(parent)
|
||||
{
|
||||
addField(AdminLoginField);
|
||||
addField(PasswordField);
|
||||
addField(PasswordConfirmField);
|
||||
|
||||
setValidator(AdminLoginField, createNameValidator());
|
||||
setValidator(PasswordField, createMandatoryValidator());
|
||||
setValidator(PasswordConfirmField, createMandatoryValidator());
|
||||
}
|
||||
|
||||
void saveData()
|
||||
{
|
||||
Wt::Dbo::Transaction transaction(DboSession());
|
||||
|
||||
// Check if a user already exist
|
||||
// If it's the case, just do nothing
|
||||
if (!Database::User::getAll(DboSession()).empty())
|
||||
throw std::runtime_error("Admin user already created");
|
||||
|
||||
// Create user
|
||||
Wt::Auth::User authUser = DbHandler().getUserDatabase().registerNew();
|
||||
Database::User::pointer user = DbHandler().getUser(authUser);
|
||||
|
||||
// Account
|
||||
authUser.setIdentity(Wt::Auth::Identity::LoginName, valueText(AdminLoginField));
|
||||
Database::Handler::getPasswordService().updatePassword(authUser, valueText(PasswordField));
|
||||
|
||||
user.modify()->setAdmin( true );
|
||||
}
|
||||
|
||||
bool validateField(Field field)
|
||||
{
|
||||
Wt::WString error;
|
||||
|
||||
if (field == PasswordField)
|
||||
{
|
||||
// Password is mandatory if we create the user
|
||||
if (!valueText(PasswordField).empty())
|
||||
{
|
||||
// Evaluate the strength of the password
|
||||
Wt::Auth::AbstractPasswordService::StrengthValidatorResult res
|
||||
= Database::Handler::getPasswordService().strengthValidator()->evaluateStrength(valueText(PasswordField),
|
||||
valueText(AdminLoginField), "");
|
||||
|
||||
if (!res.isValid())
|
||||
error = res.message();
|
||||
}
|
||||
else
|
||||
return Wt::WFormModel::validateField(field);
|
||||
}
|
||||
else if (field == PasswordConfirmField)
|
||||
{
|
||||
if (validation(PasswordField).state() == Wt::WValidator::Valid)
|
||||
{
|
||||
if (valueText(PasswordField) != valueText(PasswordConfirmField))
|
||||
error = Wt::WString::tr("Wt.Auth.passwords-dont-match");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Wt::WFormModel::validateField(field);
|
||||
}
|
||||
|
||||
setValidation(field, Wt::WValidator::Result( error.empty() ? Wt::WValidator::Valid : Wt::WValidator::Invalid, error));
|
||||
|
||||
return (validation(field).state() == Wt::WValidator::Valid);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
const Wt::WFormModel::Field FirstConnectionModel::AdminLoginField = "admin-login";
|
||||
const Wt::WFormModel::Field FirstConnectionModel::PasswordField = "password";
|
||||
const Wt::WFormModel::Field FirstConnectionModel::PasswordConfirmField = "password-confirm";
|
||||
|
||||
FirstConnectionView::FirstConnectionView(Wt::WContainerWidget *parent)
|
||||
: Wt::WTemplateFormView(parent)
|
||||
{
|
||||
auto model = new FirstConnectionModel(this);
|
||||
|
||||
setTemplateText(Wt::WString::tr("template-settings-first-connection"));
|
||||
addFunction("tr", &WTemplate::Functions::tr);
|
||||
addFunction("id", &WTemplate::Functions::id);
|
||||
|
||||
// AdminLogin
|
||||
Wt::WLineEdit* accountEdit = new Wt::WLineEdit();
|
||||
setFormWidget(FirstConnectionModel::AdminLoginField, accountEdit);
|
||||
|
||||
// Password
|
||||
Wt::WLineEdit* passwordEdit = new Wt::WLineEdit();
|
||||
setFormWidget(FirstConnectionModel::PasswordField, passwordEdit );
|
||||
passwordEdit->setEchoMode(Wt::WLineEdit::Password);
|
||||
|
||||
// Password confirmation
|
||||
Wt::WLineEdit* passwordConfirmEdit = new Wt::WLineEdit();
|
||||
setFormWidget(FirstConnectionModel::PasswordConfirmField, passwordConfirmEdit);
|
||||
passwordConfirmEdit->setEchoMode(Wt::WLineEdit::Password);
|
||||
|
||||
auto saveButton = new Wt::WPushButton(Wt::WString::tr("msg-create"));
|
||||
bindWidget("create-btn", saveButton);
|
||||
saveButton->clicked().connect(std::bind([=]
|
||||
{
|
||||
updateModel(model);
|
||||
|
||||
if (model->validate())
|
||||
{
|
||||
model->saveData();
|
||||
notify(Wt::WString::tr("msg-settings-first-connection-done"));
|
||||
saveButton->setEnabled(false);
|
||||
}
|
||||
|
||||
updateView(model);
|
||||
}));
|
||||
|
||||
updateView(model);
|
||||
}
|
||||
|
||||
} // namespace Settings
|
||||
} // namespace UserInterface
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Emeric Poupon
|
||||
* Copyright (C) 2018 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
@@ -17,17 +17,23 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <Wt/WValidator>
|
||||
#pragma once
|
||||
|
||||
#include <Wt/WContainerWidget>
|
||||
#include <Wt/WTemplateFormView>
|
||||
|
||||
namespace UserInterface {
|
||||
namespace Settings {
|
||||
|
||||
class DirectoryValidator : public Wt::WValidator
|
||||
class FirstConnectionView : public Wt::WTemplateFormView
|
||||
{
|
||||
public:
|
||||
DirectoryValidator(Wt::WObject *parent = 0);
|
||||
|
||||
Wt::WValidator::Result validate(const Wt::WString& input) const;
|
||||
FirstConnectionView(Wt::WContainerWidget *parent = 0);
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace Settings
|
||||
} // namespace UserInterface
|
||||
|
||||
|
||||
Reference in New Issue
Block a user