Commit 967b1611 authored by Leander Schulten's avatar Leander Schulten
Browse files

Added User Managment

parent e2cc0f38
......@@ -19,7 +19,8 @@ SOURCES += main.cpp \
applicationdata.cpp \
namedobject.cpp \
dmxchannelfilter.cpp \
syncservice.cpp
syncservice.cpp \
usermanagment.cpp
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
......@@ -43,4 +44,5 @@ HEADERS += \
applicationdata.h \
namedobject.h \
syncservice.h \
dmxchannelfilter.h
dmxchannelfilter.h \
usermanagment.h
......@@ -31,6 +31,7 @@ protected:
public:
enum Operation{UPDATE,CREATE,REMOVE,UPDATE_MEMBER,CREATE_MEMBER,REMOVE_MEMBER};
enum Access{READ,WRITE};
static bool areSyncServicesActive(){return !syncServices.empty();}
static void addCreateMessage(const QString &className, const QJsonObject &o);
static void addCreateMemberMessage(const QString &className,const ID &,const QString &memberName, const QJsonObject &o);
static void addUpdateMessage(const QString &className, const ID &id, const QString &varName,const QString &varValue);
......
#include "usermanagment.h"
#include <QCryptographicHash>
#include <QJsonArray>
UserManagment::UserManagment(const QJsonObject &o){
for(const auto o : o["user"].toArray()){
user.push_back(new User(o.toObject()));
}
}
void UserManagment::writeJsonObject(QJsonObject &o) const{
QJsonArray a;
for(const auto u : user){
QJsonObject o;
u->writeJsonObject(o);
a.append(o);
}
o.insert("user",a);
}
UserManagment::UserManagment()
{
}
void UserManagment::addUser(const QString &name, const QString &password){
user.push_back(new User(name,QCryptographicHash::hash(password.toUtf8(),QCryptographicHash::Sha3_256)));
}
bool UserManagment::removeUser(User *user, const QString &password){
const auto useriter = std::find(this->user.cbegin(),this->user.cend(),user);
if(useriter==this->user.cend()){
return false;
}
const auto hash = QCryptographicHash::hash(password.toUtf8(),QCryptographicHash::Sha3_256);
if(user->password==hash){
this->user.erase(useriter);
return true;
}
for(const auto u : this->user){
if(u->password==hash && u->permission == Admin){
this->user.erase(useriter);
return true;
}
}
return false;
}
bool UserManagment::changeUserName(User *user, const QString &newName, const QString &password){
const auto hash = QCryptographicHash::hash(password.toUtf8(),QCryptographicHash::Sha3_256);
if(user->password==hash){
user->setUsername(newName);
return true;
}
for(const auto u : this->user){
if(u->password==hash && u->permission == Admin){
user->setUsername(newName);
return true;
}
}
return false;
}
bool UserManagment::changeUserPermission(User *user, Permission newPermission, const QString &password){
const auto hash = QCryptographicHash::hash(password.toUtf8(),QCryptographicHash::Sha3_256);
for(const auto u : this->user){
if(u->password==hash && u->permission == Admin){
user->setPermission(newPermission);
return true;
}
}
return false;
}
bool UserManagment::changeUserPasswort(User *user, const QString &password, const QString &newPassword){
const auto hash = QCryptographicHash::hash(password.toUtf8(),QCryptographicHash::Sha3_256);
if(user->password==hash){
user->password=QCryptographicHash::hash(newPassword.toUtf8(),QCryptographicHash::Sha3_256);
return true;
}
return false;
}
User::User(const QJsonObject &o):username(o["username"].toString()),password(o["password"].toString().toUtf8()),permission(static_cast<UserManagment::Permission>(o["permission"].toInt())){
}
void User::writeJsonObject(QJsonObject &o) const{
o.insert("username",username);
o.insert("password",QString(password));
o.insert("permission",permission);
}
#ifndef USERMANAGMENT_H
#define USERMANAGMENT_H
#include <QObject>
#include <vector>
#include "idbase.h"
#include <QJsonObject>
class User;
class UserManagment : public QObject
{
Q_OBJECT
private:
std::vector<User*> user;
public:
static UserManagment * get(){static UserManagment m;return &m;}
enum Permission{Admin, Read, Write};
UserManagment();
UserManagment(const QJsonObject &o);
void writeJsonObject(QJsonObject &o)const;
void addUser(User * user);
Q_INVOKABLE void addUser(const QString & name, const QString & password);
Q_INVOKABLE bool removeUser(User * user,const QString &password);
Q_INVOKABLE bool changeUserName(User * user, const QString &newName,const QString &password);
Q_INVOKABLE bool changeUserPermission(User * user, Permission newPermission,const QString &password);
Q_INVOKABLE bool changeUserPasswort(User * user,const QString &password,const QString &newPassword);
const std::vector<User*>& getUser()const{return user;};
};
class User : public QObject, public IDBase<User>{
Q_OBJECT
Q_PROPERTY(QString name READ getUsername NOTIFY usernameChanged)
Q_PROPERTY(UserManagment::Permission permission READ getPermission NOTIFY permissionChanged)
friend class UserManagment;
private:
QString username;
QByteArray password;
UserManagment::Permission permission = UserManagment::Read;
void setUsername(const QString &u){if(u==username)return;username = u;emit usernameChanged(username);SyncService::addUpdateMessage("User",getID(),"username",username);}
void setPermission(UserManagment::Permission p){if(p==permission)return;permission = p;emit permissionChanged();SyncService::addUpdateMessage("User",getID(),"permission",QString::number(permission));}
User(const QJsonObject &o);
void writeJsonObject(QJsonObject &o)const;
public:
User(const QString & name, const QByteArray &password):username(name),password(password){}
QString getUsername()const{return username;}
UserManagment::Permission getPermission()const{return permission;}
signals:
void usernameChanged(QString);
void permissionChanged();
};
#endif // USERMANAGMENT_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment