Commit 48c47ef9 authored by Leander Schulten's avatar Leander Schulten
Browse files

Added Permissions.

Added UserModel for Qt.
Changed API
parent c48af24b
......@@ -2,48 +2,31 @@
#include <QCryptographicHash>
#include <QJsonArray>
UserManagment::UserManagment(const QJsonObject &o){
load(o);
UserManagment::UserManagment():readUser(new User("Default","")),currentUser(readUser){
/*auto pass = QCryptographicHash::hash(QString("12345").toUtf8(),QCryptographicHash::Sha3_256);
qDebug()<<pass;
auto admin = new User("Admin",pass);
for(int i = 0 ; i< LAST_PERMISSION;++i){
admin->setPermission(static_cast<Permission>(i));
}*/
}
void UserManagment::load(const QJsonObject &o){
for(const auto u : o["user"].toArray()){
user.push_back(new User(u.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)));
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);
delete user;
return true;
}
for(const auto u : this->user){
if(u->password==hash && u->permission == Admin){
this->user.erase(useriter);
for(const auto u : IDBase<User>::getAllIDBases()){
if(u->password==hash && u->havePermission(Admin)){
delete u;
return true;
}
}
......@@ -56,8 +39,8 @@ bool UserManagment::changeUserName(User *user, const QString &newName, const QSt
user->setUsername(newName);
return true;
}
for(const auto u : this->user){
if(u->password==hash && u->permission == Admin){
for(const auto u : IDBase<User>::getAllIDBases()){
if(u->password==hash && u->havePermission(Admin)){
user->setUsername(newName);
return true;
}
......@@ -67,8 +50,8 @@ bool UserManagment::changeUserName(User *user, const QString &newName, const QSt
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){
for(const auto u : IDBase<User>::getAllIDBases()){
if(u->password==hash && u->havePermission(Admin)){
user->setPermission(newPermission);
return true;
}
......@@ -85,18 +68,92 @@ bool UserManagment::changeUserPasswort(User *user, const QString &password, cons
return false;
}
User::User(const QJsonObject &o):username(o["username"].toString()),password(o["password"].toString().toLatin1()),permission(static_cast<UserManagment::Permission>(o["permission"].toInt())){
bool UserManagment::login(User *user, const QString &password){
if(user==nullptr){
return false;
}
const auto hash = QCryptographicHash::hash(password.toUtf8(),QCryptographicHash::Sha3_256);
if(user->password==hash){
currentUser = user;
emit currentUserChanged();
return true;
}
return false;
}
void UserManagment::logout(User *user){
if(currentUser==user){
currentUser = readUser;
emit currentUserChanged();
}
}
void UserManagment::logout(){
currentUser = readUser;
emit currentUserChanged();
}
// UserPermissionModel
QVariant UserPermissionModel::data(const QModelIndex &index, int role) const{
if(index.row()<0 && index.row()>=rowCount(index)){
return QVariant("Index Out of Range");
}
switch (role) {
case HavePermissionRole:
case Qt::EditRole:
return QVariant(user->havePermission(static_cast<UserManagment::Permission>(index.row())));
case Qt::DisplayRole:
case PermissionNameRole:
return QVariant(UserManagment::get()->metaEnum.valueToKey(index.row()));
default:
return QVariant("Unknown role!");
}
}
bool UserPermissionModel::setData(const QModelIndex &index, const QVariant &value, int role){
if(role==HavePermissionRole){
if(UserManagment::get()->getCurrentUser()->havePermission(UserManagment::Admin)){
if(index.row()>=0 && index.row()<rowCount(index)){
const UserManagment::Permission p = static_cast<UserManagment::Permission>(index.row());
if(user->havePermission(p)!=value.toBool()){
user->setPermission(p,value.toBool());
emit dataChanged(index,index,{HavePermissionRole});
return true;
}
}
}
}
return false;
}
//USER
void User::createUser(const QJsonObject &o){
if(o["password"].toString().length()!=0){// we dont want a user without a password
new User(o);
}
}
User::User(const QJsonObject &o):username(o["username"].toString()),password(QByteArray::fromBase64(o["password"].toString().toLatin1())),permissionModel(this){
const auto array = o["permissions"].toArray();
for(const auto & i : array){
permissions.insert(static_cast<UserManagment::Permission>(i.toInt()));
}
}
void User::writeJsonObject(QJsonObject &o) const{
o.insert("username",username);
o.insert("password",QString::fromLatin1(password.constData()));
o.insert("permission",permission);
o.insert("password",QString::fromLatin1(password.toBase64()));
QJsonArray a;
for(auto i = permissions.cbegin();i!=permissions.cend();++i){
a.push_back(*i);
}
o.insert("permissions",a);
}
User * UserManagment::getUserByName(const QString &name) const{
for(auto u = user.cbegin();u!=user.cend();++u){
for(auto u = IDBase<User>::getAllIDBases().cbegin();u!=IDBase<User>::getAllIDBases().cend();++u){
if((**u).getUsername()==name){
return *u;
}
......@@ -105,6 +162,20 @@ User * UserManagment::getUserByName(const QString &name) const{
}
void User::setPermission(UserManagment::Permission p, bool get){
if(get){
if (permissions.find(p)==permissions.cend()) {
permissions.insert(p);
emit permissionChanged(p,true);
}
}else{
if (permissions.find(p)!=permissions.cend()) {
permissions.erase(permissions.find(p));
emit permissionChanged(p,false);
}
}
}
......
......@@ -14,46 +14,91 @@ class User;
class UserManagment : public QObject
{
Q_OBJECT
Q_PROPERTY(User* currentUser READ getCurrentUser NOTIFY currentUserChanged)
Q_PROPERTY(User* defaultUser READ getCurrentUser CONSTANT)
private:
std::vector<User*> user;
User * readUser;
User * currentUser;
public:
Q_INVOKABLE User * getDefaultUser()const{return readUser;}
static UserManagment * get(){static UserManagment m;return &m;}
enum Permission{Admin, Read, Write};
enum Permission{
Admin, Read, Write,
ADD_DEVICE,REMOVE_DEVICE,CHANGE_POSITION, CHANGE_NAME,CHANGE_DEVICE_DMX_CHANNEL,
DEVICE_TAB,DEVICE_PROTOTYPE_TAB,PROGRAMM_PROTOTYPE_TAY,PROGRAMM_TAB,
MOVE_CONTROL_ITEM, ADD_CONTROL_ITEM, CHANGE_GROUP_NAME, CHANGE_GROUP_DEVICES, CHANGE_MIN_MAX_MAPPING, CHANGE_TIMEOUTS,
LAST_PERMISSION
};
Q_ENUM(Permission)
const QMetaEnum metaEnum = QMetaEnum::fromType<Permission>();
UserManagment();
UserManagment(const QJsonObject &o);
void load(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;}
Q_INVOKABLE bool login(User * user, const QString &passwort);
Q_INVOKABLE void logout();
void logout(User * user);
User* getUserByName(const QString & name)const;
User * getCurrentUser()const{return currentUser;}
signals:
void currentUserChanged();
};
class UserPermissionModel : public QAbstractListModel{
Q_OBJECT
User* user;
public:
UserPermissionModel(User * user):user(user){}
enum GroupModelRoles{
HavePermissionRole = Qt::UserRole+1,
PermissionNameRole
};
virtual int rowCount(const QModelIndex & = QModelIndex())const override {return UserManagment::LAST_PERMISSION;}
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole)const override;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole)override;
virtual Qt::ItemFlags flags(const QModelIndex &index) const override{
auto r = QAbstractListModel::flags(index);
r.setFlag(Qt::ItemIsEditable);
return r;
}
QHash<int,QByteArray> roleNames() const override{
auto r = QAbstractListModel::roleNames();
r[HavePermissionRole] = "havePermission";
r[PermissionNameRole] = "permissionName";
return r;
}
};
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)
Q_PROPERTY(QAbstractListModel * permissionModel READ getPermissionModel CONSTANT)
friend class UserManagment;
friend class UserPermissionModel;
private:
QString username;
QByteArray password;
UserManagment::Permission permission = UserManagment::Read;
std::set<UserManagment::Permission> permissions;
UserPermissionModel permissionModel;
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));}
void setPermission(UserManagment::Permission p,bool get = true);
User(const QString & name, const QByteArray &password):username(name),password(password),permissionModel(this){}
User(const QJsonObject &o);
void writeJsonObject(QJsonObject &o)const;
public:
User(const QString & name, const QByteArray &password):username(name),password(password){}
static void createUser(const QJsonObject &o);
void writeJsonObject(QJsonObject &o)const;
~User(){UserManagment::get()->logout(this);}
QString getUsername()const{return username;}
UserManagment::Permission getPermission()const{return permission;}
std::set<UserManagment::Permission> getPermissions()const{return permissions;}
Q_INVOKABLE bool havePermission(UserManagment::Permission p){return permissions.find(p)!=permissions.cend();}
UserPermissionModel * getPermissionModel(){return &permissionModel;}
signals:
void usernameChanged(QString);
void permissionChanged();
void permissionChanged(UserManagment::Permission,bool);
};
#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