User edit dialog completed
This commit is contained in:
parent
3adbef8c08
commit
e24554a0a0
11 changed files with 315 additions and 47 deletions
|
|
@ -1,21 +1,36 @@
|
|||
#include "adminpanel.h"
|
||||
#include "ui_adminpanel.h"
|
||||
#include <QStringListModel >
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void AdminPanel::on_user_add() {
|
||||
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_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;
|
||||
}
|
||||
|
||||
void AdminPanel::on_delivery_point_add() {
|
||||
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_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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,12 @@
|
|||
#define ADMINPANEL_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QMessageBox>
|
||||
#include <QDebug>
|
||||
|
||||
#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();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1162</width>
|
||||
<height>567</height>
|
||||
<height>532</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="sizePolicy">
|
||||
|
|
@ -37,7 +37,10 @@
|
|||
<item row="0" column="1">
|
||||
<widget class="QLabel" name="lab_user">
|
||||
<property name="text">
|
||||
<string>Hello, user %1</string>
|
||||
<string>Hello, %1</string>
|
||||
</property>
|
||||
<property name="textFormat">
|
||||
<enum>Qt::MarkdownText</enum>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
|
|
@ -65,6 +68,9 @@
|
|||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0" colspan="2">
|
||||
<widget class="QTableView" name="tv_users">
|
||||
<property name="selectionBehavior">
|
||||
<enum>QAbstractItemView::SelectRows</enum>
|
||||
</property>
|
||||
<attribute name="horizontalHeaderStretchLastSection">
|
||||
<bool>true</bool>
|
||||
</attribute>
|
||||
|
|
@ -92,6 +98,16 @@
|
|||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pb_users_edit">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Edit</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pb_users_remove">
|
||||
<property name="enabled">
|
||||
|
|
@ -120,6 +136,16 @@
|
|||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pb_vessels_edit">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Edit</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pb_vessels_remove">
|
||||
<property name="enabled">
|
||||
|
|
@ -147,6 +173,9 @@
|
|||
</item>
|
||||
<item row="0" column="1" colspan="2">
|
||||
<widget class="QTableView" name="tv_vessels">
|
||||
<property name="selectionBehavior">
|
||||
<enum>QAbstractItemView::SelectRows</enum>
|
||||
</property>
|
||||
<attribute name="horizontalHeaderStretchLastSection">
|
||||
<bool>true</bool>
|
||||
</attribute>
|
||||
|
|
@ -154,7 +183,7 @@
|
|||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QWidget" name="tab">
|
||||
<widget class="QWidget" name="tab_dps">
|
||||
<attribute name="title">
|
||||
<string>Delivery points</string>
|
||||
</attribute>
|
||||
|
|
@ -181,6 +210,16 @@
|
|||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pb_dp_edit">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Edit</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pb_dp_remove">
|
||||
<property name="enabled">
|
||||
|
|
@ -195,6 +234,9 @@
|
|||
</item>
|
||||
<item row="0" column="1" colspan="2">
|
||||
<widget class="QTableView" name="tv_dp">
|
||||
<property name="selectionBehavior">
|
||||
<enum>QAbstractItemView::SelectRows</enum>
|
||||
</property>
|
||||
<attribute name="horizontalHeaderStretchLastSection">
|
||||
<bool>true</bool>
|
||||
</attribute>
|
||||
|
|
@ -206,17 +248,6 @@
|
|||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QMenuBar" name="menubar">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1162</width>
|
||||
<height>21</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusbar"/>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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("<br>- Title cannot be empty (all spaces - empty too)");
|
||||
}
|
||||
if (emptyPassword) {
|
||||
message.append("<br>- Password cannot be empty (all spaces - empty too)");
|
||||
}
|
||||
if (lowerank) {
|
||||
message.append("<br>- 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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,21 +2,36 @@
|
|||
#define USEREDITDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
#include <QMessageBox>
|
||||
|
||||
#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
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@
|
|||
</item>
|
||||
<item>
|
||||
<property name="text">
|
||||
<string>Captain</string>
|
||||
<string>Skipper</string>
|
||||
</property>
|
||||
</item>
|
||||
</widget>
|
||||
|
|
|
|||
|
|
@ -61,5 +61,6 @@ QVariant UsersViewModel::data(const QModelIndex &index, int role) const {
|
|||
}
|
||||
|
||||
void UsersViewModel::update() {
|
||||
dataChanged(QModelIndex(), QModelIndex());
|
||||
this->beginResetModel();
|
||||
this->endResetModel();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue