diff --git a/sea_transport/adminpanel.cpp b/sea_transport/adminpanel.cpp index 2418e16..1bda76f 100644 --- a/sea_transport/adminpanel.cpp +++ b/sea_transport/adminpanel.cpp @@ -1,21 +1,36 @@ #include "adminpanel.h" #include "ui_adminpanel.h" -#include + AdminPanel::AdminPanel(QWidget *parent) : QMainWindow(parent), ui(new Ui::AdminPanel) { ui->setupUi(this); -// connect(ui->pb_logout, &QPushButton::clicked, this, &AdminPanel::on_logout_requested); + connect(ui->pb_logout, &QPushButton::clicked, this, &AdminPanel::on_logout_requested); -// connect(ui->pb_vessels_add, &QPushButton::clicked, this, &AdminPanel::on_vessel_add); -// connect(ui->pb_vessels_remove, &QPushButton::clicked, this, &AdminPanel::on_vessel_remove); + connect(ui->pb_vessels_add, &QPushButton::clicked, this, [this](){ + this->on_vessel_add_edit(false); + }); + connect(ui->pb_vessels_edit, &QPushButton::clicked, this, [this](){ + this->on_vessel_add_edit(true); + }); + connect(ui->pb_vessels_remove, &QPushButton::clicked, this, &AdminPanel::on_vessel_remove); -// connect(ui->pb_users_add, &QPushButton::clicked, this, &AdminPanel::on_user_add); -// connect(ui->pb_users_remove, &QPushButton::clicked, this, &AdminPanel::on_user_remove); + connect(ui->pb_users_add, &QPushButton::clicked, this, [this](){ + this->on_user_add_edit(false); + }); + connect(ui->pb_users_edit, &QPushButton::clicked, this, [this](){ + this->on_user_add_edit(true); + }); + connect(ui->pb_users_remove, &QPushButton::clicked, this, &AdminPanel::on_user_remove); -// connect(ui->pb_dp_add, &QPushButton::clicked, this, &AdminPanel::on_delivery_point_add); -// connect(ui->pb_dp_remove, &QPushButton::clicked, this, &AdminPanel::on_delivery_point_remove); + connect(ui->pb_dp_add, &QPushButton::clicked, this, [this](){ + this->on_delivery_point_add_edit(false); + }); + connect(ui->pb_dp_edit, &QPushButton::clicked, this, [this](){ + this->on_delivery_point_add_edit(true); + }); + connect(ui->pb_dp_remove, &QPushButton::clicked, this, &AdminPanel::on_delivery_point_remove); uvm = new UsersViewModel(this); @@ -27,48 +42,175 @@ AdminPanel::AdminPanel(QWidget *parent) : QMainWindow(parent), ui(new Ui::AdminP dpvm = new DeliveryPointsViewModel(this); ui->tv_dp->setModel(dpvm); + connect(ui->tv_users->selectionModel(), &QItemSelectionModel::selectionChanged, [this](const QItemSelection &selected) { + ui->pb_users_remove->setEnabled(selected.length() > 0); + ui->pb_users_edit->setEnabled(selected.length() == 1); + }); - uvm->update(); - vvm->update(); - dpvm->update(); + connect(ui->tv_vessels->selectionModel(), &QItemSelectionModel::selectionChanged, [this](const QItemSelection &selected) { + ui->pb_vessels_remove->setEnabled(selected.length() > 0); + ui->pb_vessels_edit->setEnabled(selected.length() == 1); + }); + + connect(ui->tv_dp->selectionModel(), &QItemSelectionModel::selectionChanged, [this](const QItemSelection &selected) { + ui->pb_dp_remove->setEnabled(selected.length() > 0); + ui->pb_dp_edit->setEnabled(selected.length() == 1); + }); + + ui->tw_tabs->setCurrentIndex(0); } AdminPanel::~AdminPanel() { delete ui; delete uvm; + delete vvm; + delete dpvm; } AdminPanel& AdminPanel::set_user(const user_entity &user) { this->user = user; - ui->lab_user->setText(tr("Hello user %1").arg(user.login())); + ui->lab_user->setText(tr("Hello, **%1**").arg(user.login())); return *this; } void AdminPanel::on_logout_requested() { - + this->close(); } -void AdminPanel::on_vessel_add() { +void AdminPanel::on_vessel_add_edit(bool edit) { } void AdminPanel::on_vessel_remove() { + auto selected = ui->tv_vessels->selectionModel()->selectedRows(); + if (selected.length() == 0) { + return; + } + QMessageBox delConf(this); + delConf.setIcon(QMessageBox::Question); + delConf.setWindowTitle(tr("Deletion confirmation")); + delConf.setText(tr("Are you sure you want to delete these vessels?")); + delConf.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + delConf.setDefaultButton(QMessageBox::No); + if (delConf.exec() == QMessageBox::No) { + return; + } + + foreach (auto mIdx, selected) { + int idx = mIdx.row(); + qDebug() << idx << ' ' << mIdx.data() << '\n'; + } + + vvm->update(); } -void AdminPanel::on_user_add() { +void AdminPanel::on_user_add_edit(bool edit) { + auto selected = ui->tv_users->selectionModel()->selectedRows(); + if (edit && selected.length() != 1) { + return; + } + user_entity usr; + if (edit) { + int idx = selected[0].row(); + usr = apparatus::instance()->get_auth_subsystem()->users()[idx]; + if (usr.id() == this->user.id()) { + QMessageBox::critical(this, "Error", "You cannot edit yourself"); + return; + } + } + + UserEditDialog ued(this); + ued.setWindowTitle(edit? "Edit user" : "New user"); + ued.set_user(&usr, edit); + if (ued.exec() != UserEditDialog::Accepted) { + return; + } + + auto data = ued.user(); + if (edit) { + bool success; + auto user = apparatus::instance()->get_auth_subsystem()->get_user(usr.login(), success); + if (success) { + user->set_password(data->password); + user->set_role(data->role); + QMessageBox::information(this, "Info", "User edited successfully (note: you cannot change login)"); + } + else { + QMessageBox::critical(this, "Error", "Error while editing user"); + return; + } + } + else { + bool success = apparatus::instance()->get_auth_subsystem()->register_user(data->login, data->password, data->role); + if (success) { + QMessageBox::information(this, "Info", "User created successfully"); + } + else { + QMessageBox::critical(this, "Error", "Error while creating user"); + return; + } + } + + uvm->update(); } void AdminPanel::on_user_remove() { + auto selected = ui->tv_users->selectionModel()->selectedRows(); + if (selected.length() == 0) { + return; + } + QMessageBox delConf(this); + delConf.setIcon(QMessageBox::Question); + delConf.setWindowTitle(tr("Deletion confirmation")); + delConf.setText(tr("Are you sure you want to delete these users?")); + delConf.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + delConf.setDefaultButton(QMessageBox::No); + if (delConf.exec() == QMessageBox::No) { + return; + } + + auto _u = apparatus::instance()->get_auth_subsystem()->users(); + foreach (auto mIdx, selected) { + int idx = mIdx.row(); + auto ent = _u[idx]; + if (ent.id() == user.id()) { + QMessageBox::critical(this, "Error", "You cannot delete yourself!"); + break; + } + apparatus::instance()->get_auth_subsystem()->remove_user(ent.login()); + } + + uvm->update(); } -void AdminPanel::on_delivery_point_add() { +void AdminPanel::on_delivery_point_add_edit(bool edit) { } void AdminPanel::on_delivery_point_remove() { + auto selected = ui->tv_dp->selectionModel()->selectedRows(); + if (selected.length() == 0) { + return; + } + QMessageBox delConf(this); + delConf.setIcon(QMessageBox::Question); + delConf.setWindowTitle(tr("Deletion confirmation")); + delConf.setText(tr("Are you sure you want to delete these delivery points?")); + delConf.setStandardButtons(QMessageBox::Yes | QMessageBox::No); + delConf.setDefaultButton(QMessageBox::No); + if (delConf.exec() == QMessageBox::No) { + return; + } + + foreach (auto mIdx, selected) { + int idx = mIdx.row(); + qDebug() << idx << ' ' << mIdx.data() << '\n'; + } + + dpvm->update(); } diff --git a/sea_transport/adminpanel.h b/sea_transport/adminpanel.h index 8152158..3489f39 100644 --- a/sea_transport/adminpanel.h +++ b/sea_transport/adminpanel.h @@ -2,6 +2,12 @@ #define ADMINPANEL_H #include +#include +#include + +#include "usereditdialog.h" +#include "vesseleditdialog.h" +#include "deliverypointeditdialog.h" #include "viewmodels/usersviewmodel.h" #include "viewmodels/vesselsviewmodel.h" @@ -36,13 +42,13 @@ private: void on_logout_requested(); - void on_vessel_add(); + void on_vessel_add_edit(bool edit); void on_vessel_remove(); - void on_user_add(); + void on_user_add_edit(bool edit); void on_user_remove(); - void on_delivery_point_add(); + void on_delivery_point_add_edit(bool edit); void on_delivery_point_remove(); }; diff --git a/sea_transport/adminpanel.ui b/sea_transport/adminpanel.ui index 98e15da..cc86acc 100644 --- a/sea_transport/adminpanel.ui +++ b/sea_transport/adminpanel.ui @@ -7,7 +7,7 @@ 0 0 1162 - 567 + 532 @@ -37,7 +37,10 @@ - Hello, user %1 + Hello, %1 + + + Qt::MarkdownText Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -65,6 +68,9 @@ + + QAbstractItemView::SelectRows + true @@ -92,6 +98,16 @@ + + + + false + + + Edit + + + @@ -120,6 +136,16 @@ + + + + false + + + Edit + + + @@ -147,6 +173,9 @@ + + QAbstractItemView::SelectRows + true @@ -154,7 +183,7 @@ - + Delivery points @@ -181,6 +210,16 @@ + + + + false + + + Edit + + + @@ -195,6 +234,9 @@ + + QAbstractItemView::SelectRows + true @@ -206,17 +248,6 @@ - - - - 0 - 0 - 1162 - 21 - - - - diff --git a/sea_transport/entities/user_entity.cpp b/sea_transport/entities/user_entity.cpp index 5cd56a7..a54471f 100644 --- a/sea_transport/entities/user_entity.cpp +++ b/sea_transport/entities/user_entity.cpp @@ -20,6 +20,14 @@ bool user_entity::verify_password(const QString &password) const { return (this->_pwd_hash == QCryptographicHash::hash(password.toLocal8Bit(), QCryptographicHash::Sha3_256)); } +void user_entity::set_password(const QString &new_password) { + this->_pwd_hash = QCryptographicHash::hash(new_password.toLocal8Bit(), QCryptographicHash::Sha3_256); +} + +void user_entity::set_role(UserRole new_role) { + this->_role = new_role; +} + void user_entity::serialize(QDataStream &output) { output << this->_id << this->_login << this->_role << this->_pwd_hash; } diff --git a/sea_transport/entities/user_entity.h b/sea_transport/entities/user_entity.h index 890279c..1bf4203 100644 --- a/sea_transport/entities/user_entity.h +++ b/sea_transport/entities/user_entity.h @@ -27,6 +27,8 @@ public: const QString login() const; UserRole role() const; bool verify_password(const QString &password) const; + void set_password(const QString &new_password); + void set_role(UserRole new_role); void serialize(QDataStream &output); void deserialize(QDataStream &input); diff --git a/sea_transport/system/auth_system.cpp b/sea_transport/system/auth_system.cpp index 6ac11b7..f10adc4 100644 --- a/sea_transport/system/auth_system.cpp +++ b/sea_transport/system/auth_system.cpp @@ -2,7 +2,7 @@ #include "auth_system.h" -const user_entity* auth_system::get_user(const QString &login, bool &success) { +user_entity* auth_system::get_user(const QString &login, bool &success) { user_entity *out = nullptr; success = false; diff --git a/sea_transport/system/auth_system.h b/sea_transport/system/auth_system.h index ecc42ae..21dd056 100644 --- a/sea_transport/system/auth_system.h +++ b/sea_transport/system/auth_system.h @@ -13,7 +13,7 @@ private: public: auth_system() = default; - const user_entity* get_user(const QString &login, bool &success); + user_entity* get_user(const QString &login, bool &success); bool remove_user(const QString &login); bool register_user(const QString &login, const QString &password, UserRole role); diff --git a/sea_transport/usereditdialog.cpp b/sea_transport/usereditdialog.cpp index a052c47..c0c2650 100644 --- a/sea_transport/usereditdialog.cpp +++ b/sea_transport/usereditdialog.cpp @@ -1,14 +1,77 @@ #include "usereditdialog.h" #include "ui_usereditdialog.h" -UserEditDialog::UserEditDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::UserEditDialog) -{ +UserEditDialog::UserEditDialog(QWidget *parent) : QDialog(parent), ui(new Ui::UserEditDialog) { ui->setupUi(this); + + connect(ui->btn_save, &QPushButton::clicked, this, &UserEditDialog::accept); + connect(ui->btn_discard, &QPushButton::clicked, this, &UserEditDialog::reject); } -UserEditDialog::~UserEditDialog() -{ +UserEditDialog::~UserEditDialog() { delete ui; } + +UserEditDialog::user_data* UserEditDialog::user() const { + return this->_user; +} + +void UserEditDialog::set_user(user_entity* user, bool edit) { + if (edit) { + this->_user = new UserEditDialog::user_data { + user->login(), "", user->role(), true + }; + + ui->et_login->setText(user->login()); + ui->et_password->setText("##########UNEDITED##########"); + ui->cb_role->setCurrentIndex((int)user->role()); + } + else { + this->_user = new UserEditDialog::user_data{}; + } + this->_user->edit = edit; +} + +void UserEditDialog::accept() { + UserRole role; + switch (ui->cb_role->currentIndex()) { + case 0: + role = UserRole::ADMINISTRATOR; + break; + case 1: + role = UserRole::DISPATCHER; + break; + case 2: + role = UserRole::SKIPPER; + break; + } + bool emptyTitle = ui->et_login->text().trimmed().isEmpty(); + bool emptyPassword = ui->et_password->text().trimmed().isEmpty(); + bool lowerank = this->_user->edit && this->_user->role < role; + if (emptyTitle || emptyPassword || lowerank) { + QMessageBox errDlg(this); + errDlg.setTextFormat(Qt::RichText); + errDlg.setWindowTitle(tr("Error")); + errDlg.setIcon(QMessageBox::Critical); + QString message = tr("Some errors happend, while saving your note:"); + if (emptyTitle) { + message.append("
- Title cannot be empty (all spaces - empty too)"); + } + if (emptyPassword) { + message.append("
- Password cannot be empty (all spaces - empty too)"); + } + if (lowerank) { + message.append("
- You cannot lower users rank"); + } + errDlg.setText(message); + errDlg.exec(); + return; + } + ; + + this->_user->login = ui->et_login->text().trimmed(); + this->_user->password = ui->et_password->text().trimmed(); + this->_user->role = role; + + QDialog::accept(); +} diff --git a/sea_transport/usereditdialog.h b/sea_transport/usereditdialog.h index 26a93e7..7fb13ca 100644 --- a/sea_transport/usereditdialog.h +++ b/sea_transport/usereditdialog.h @@ -2,21 +2,36 @@ #define USEREDITDIALOG_H #include +#include + +#include "entities/user_entity.h" + namespace Ui { class UserEditDialog; } -class UserEditDialog : public QDialog -{ +class UserEditDialog : public QDialog { Q_OBJECT + Ui::UserEditDialog *ui; + + struct user_data { + QString login; + QString password; + UserRole role; + bool edit; + } *_user; public: explicit UserEditDialog(QWidget *parent = nullptr); ~UserEditDialog(); -private: - Ui::UserEditDialog *ui; + UserEditDialog::user_data* user() const; + void set_user(user_entity* user, bool edit); + + +public slots: + void accept() Q_DECL_OVERRIDE; }; #endif // USEREDITDIALOG_H diff --git a/sea_transport/usereditdialog.ui b/sea_transport/usereditdialog.ui index 674bbb2..8ec17a4 100644 --- a/sea_transport/usereditdialog.ui +++ b/sea_transport/usereditdialog.ui @@ -68,7 +68,7 @@ - Captain + Skipper diff --git a/sea_transport/viewmodels/usersviewmodel.cpp b/sea_transport/viewmodels/usersviewmodel.cpp index 72290fb..9269d34 100644 --- a/sea_transport/viewmodels/usersviewmodel.cpp +++ b/sea_transport/viewmodels/usersviewmodel.cpp @@ -61,5 +61,6 @@ QVariant UsersViewModel::data(const QModelIndex &index, int role) const { } void UsersViewModel::update() { - dataChanged(QModelIndex(), QModelIndex()); + this->beginResetModel(); + this->endResetModel(); }