Auth subsystem done
This commit is contained in:
parent
43aefada4c
commit
6fb0559ae5
3 changed files with 82 additions and 0 deletions
|
|
@ -20,6 +20,7 @@ SOURCES += \
|
||||||
main.cpp \
|
main.cpp \
|
||||||
storageeditdialog.cpp \
|
storageeditdialog.cpp \
|
||||||
system/apparatus.cpp \
|
system/apparatus.cpp \
|
||||||
|
system/auth_system.cpp \
|
||||||
usereditdialog.cpp \
|
usereditdialog.cpp \
|
||||||
vesseleditdialog.cpp
|
vesseleditdialog.cpp
|
||||||
|
|
||||||
|
|
@ -36,6 +37,7 @@ HEADERS += \
|
||||||
entities/vessel_entity.h \
|
entities/vessel_entity.h \
|
||||||
storageeditdialog.h \
|
storageeditdialog.h \
|
||||||
system/apparatus.h \
|
system/apparatus.h \
|
||||||
|
system/auth_system.h \
|
||||||
usereditdialog.h \
|
usereditdialog.h \
|
||||||
vesseleditdialog.h
|
vesseleditdialog.h
|
||||||
|
|
||||||
|
|
|
||||||
56
system/auth_system.cpp
Normal file
56
system/auth_system.cpp
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
24
system/auth_system.h
Normal file
24
system/auth_system.h
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#ifndef AUTH_SYSTEM_H
|
||||||
|
#define AUTH_SYSTEM_H
|
||||||
|
|
||||||
|
#include<QVector>
|
||||||
|
|
||||||
|
#include <entities/user_entity.h>
|
||||||
|
|
||||||
|
|
||||||
|
class auth_system
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
QVector<user_entity> _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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue