diff --git a/sea_transport.pro b/sea_transport.pro index 5a2f590..4ec32c5 100644 --- a/sea_transport.pro +++ b/sea_transport.pro @@ -20,6 +20,7 @@ SOURCES += \ main.cpp \ storageeditdialog.cpp \ system/apparatus.cpp \ + system/auth_system.cpp \ usereditdialog.cpp \ vesseleditdialog.cpp @@ -36,6 +37,7 @@ HEADERS += \ entities/vessel_entity.h \ storageeditdialog.h \ system/apparatus.h \ + system/auth_system.h \ usereditdialog.h \ vesseleditdialog.h diff --git a/system/auth_system.cpp b/system/auth_system.cpp new file mode 100644 index 0000000..e7432c1 --- /dev/null +++ b/system/auth_system.cpp @@ -0,0 +1,56 @@ +#include "auth_system.h" + + +const user_entity& auth_system::get_user(const QString &login, bool &success) { + user_entity *out; + + success = false; + for (user_entity &item : this->_users) { + if (item.login() == login) { + success = true; + out = &item; + break; + } + } + + return *out; +} + +bool auth_system::remove_user(const QString &login) { + auto vit = this->_users.begin(); + for (; vit != this->_users.end(); vit++) { + if ((*vit).login() == login) { + this->_users.erase(vit); + return true; + } + } + + return false; +} + +bool auth_system::register_user(const QString &login, const QString &password, UserRole role) { + bool exists = false; + this->get_user(login, exists); + if (!exists) { + this->_users.push_back(user_entity(login, password, role)); + return true; + } + + return false; +} + +void auth_system::init(QDataStream &stream) { + int icnt; + stream >> icnt; + this->_users.resize(icnt); + for (int i = 0; i < icnt; i++) { + this->_users[i].deserialize(stream); + } +} + +void auth_system::shutdown(QDataStream &stream) { + stream << this->_users.size(); + for (auto &item : this->_users) { + item.serialize(stream); + } +} diff --git a/system/auth_system.h b/system/auth_system.h new file mode 100644 index 0000000..9e442d5 --- /dev/null +++ b/system/auth_system.h @@ -0,0 +1,24 @@ +#ifndef AUTH_SYSTEM_H +#define AUTH_SYSTEM_H + +#include + +#include + + +class auth_system +{ +private: + QVector _users; +public: + auth_system() = default; + + const 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); + + void init(QDataStream &stream); + void shutdown(QDataStream &stream); +}; + +#endif // AUTH_SYSTEM_H