From 5dbebcc947ae172e57120766592e4e43a5ffd6c3 Mon Sep 17 00:00:00 2001 From: Zdenek Jonak Date: Wed, 6 Jan 2016 22:33:38 +0100 Subject: [PATCH] Support for adding user roles --- core/data/role.h | 2 +- core/data/user.cpp | 10 ++++++++++ core/data/user.h | 9 +++++++-- core/metaData.json | 8 +++++++- core/users/userform.cpp | 39 +++++++++++++++++++++++++++++++++++++++ core/users/userform.h | 5 +++++ core/users/userform.ui | 14 ++++++++++++-- 7 files changed, 81 insertions(+), 6 deletions(-) diff --git a/core/data/role.h b/core/data/role.h index 3d4dbb4..44d1a38 100644 --- a/core/data/role.h +++ b/core/data/role.h @@ -56,7 +56,7 @@ private: QDateTime m_createDate; bool m_active; #pragma db value_not_null inverse(m_listRoles) - QList > m_listUsers; + QOdbList > m_listUsers; #pragma db value_not_null QOdbList > m_listPermissions; }; diff --git a/core/data/user.cpp b/core/data/user.cpp index 1b05ebe..c4ee8ed 100644 --- a/core/data/user.cpp +++ b/core/data/user.cpp @@ -22,6 +22,16 @@ void User::setListRoles(const QList > &listRoles) m_listRoles = listRoles; } +void User::addRole(QSharedPointer role) +{ + m_listRoles.append(role); +} + +void User::clearRoles() +{ + m_listRoles.clear(); +} + QString User::login() const { return m_login; diff --git a/core/data/user.h b/core/data/user.h index 488fbd0..0bce4d5 100644 --- a/core/data/user.h +++ b/core/data/user.h @@ -9,6 +9,7 @@ #include #include #include +#include #pragma db object class User : public QObject @@ -53,6 +54,10 @@ public: QList > listRoles() const; void setListRoles(const QList > &listRoles); + void addRole(QSharedPointer role); + + void clearRoles(); + private: friend class odb::access; @@ -66,8 +71,8 @@ private: QDateTime m_createDate; bool m_active; bool m_isAdmin; -#pragma db value_not_null unordered - QList > m_listRoles; +#pragma db value_not_null + QOdbList > m_listRoles; }; #endif // USER_H diff --git a/core/metaData.json b/core/metaData.json index f9339ab..5821dc8 100644 --- a/core/metaData.json +++ b/core/metaData.json @@ -23,6 +23,7 @@ CREATE TABLE \"User_listRoles\" ( \"object_id\" INTEGER NOT NULL, + \"index\" INTEGER NOT NULL, \"value\" INTEGER NOT NULL, CONSTRAINT \"object_id_fk\" FOREIGN KEY (\"object_id\") @@ -36,6 +37,9 @@ CREATE TABLE \"User_listRoles\" ( CREATE INDEX \"User_listRoles_object_id_i\" ON \"User_listRoles\" (\"object_id\"); +CREATE INDEX \"User_listRoles_index_i\" + ON \"User_listRoles\" (\"index\"); + CREATE TABLE \"Role\" ( \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \"name\" TEXT NULL, @@ -68,7 +72,9 @@ CREATE TABLE \"Permission\" ( \"permissionName\" TEXT NULL, \"lastModDate\" TEXT NULL, \"createDate\" TEXT NULL, - \"active\" INTEGER NOT NULL);" ], + \"active\" INTEGER NOT NULL); + +" ], "dependencies" : [] } } diff --git a/core/users/userform.cpp b/core/users/userform.cpp index b7d2b36..199afff 100644 --- a/core/users/userform.cpp +++ b/core/users/userform.cpp @@ -1,6 +1,11 @@ #include "userform.h" #include "ui_userform.h" +#include +#include +#include "../data/core-data.h" +#include "../service.h" + UserForm::UserForm(QWidget *parent) : AutoForm(parent), ui(new Ui::UserForm) @@ -17,3 +22,37 @@ UserForm::~UserForm() { delete ui; } + +void UserForm::bindOtherToUi() +{ + Service srv; + QList > roles = this->entity()->listRoles(); + ui->tableWidget->setRowCount(srv.all().count()); + ui->tableWidget->setColumnCount(1); + int i = 0; + foreach(QSharedPointer r,srv.all()) + { + auto it = std::find_if(ALL(roles),[r](QSharedPointer role){return r->id() == role->id();}); + QTableWidgetItem * ti; + ti = new QTableWidgetItem; + ti->setText(r->name()); + ti->setCheckState(it == roles.end() ? Qt::Unchecked : Qt::Checked); + ti->setData(Qt::UserRole,r->id()); + ui->tableWidget->setItem(i,0,ti); + i++; + } +} + +bool UserForm::bindOtherToData() +{ + Service srv; + this->entity()->clearRoles(); + for(int i = 0;i < ui->tableWidget->rowCount() ;i++){ + if(ui->tableWidget->item(i,0)->checkState()== Qt::Checked) + { + this->entity()->addRole(srv.loadById(ui->tableWidget->item(i,0)->data(Qt::UserRole).toInt())); + } + } + return true; +} + diff --git a/core/users/userform.h b/core/users/userform.h index b549723..2abd3a9 100644 --- a/core/users/userform.h +++ b/core/users/userform.h @@ -20,6 +20,11 @@ public: private: Ui::UserForm *ui; + + // AutoForm interface +protected: + void bindOtherToUi(); + bool bindOtherToData(); }; #endif // USERFORM_H diff --git a/core/users/userform.ui b/core/users/userform.ui index ea073bf..8e74725 100644 --- a/core/users/userform.ui +++ b/core/users/userform.ui @@ -44,7 +44,7 @@ - + Qt::RightToLeft @@ -54,7 +54,7 @@ - + Qt::RightToLeft @@ -64,6 +64,16 @@ + + + + + + + Roles + + +