Support for adding user roles

print
Zdenek Jonak 9 years ago
parent d2f391558a
commit 5dbebcc947

@ -56,7 +56,7 @@ private:
QDateTime m_createDate; QDateTime m_createDate;
bool m_active; bool m_active;
#pragma db value_not_null inverse(m_listRoles) #pragma db value_not_null inverse(m_listRoles)
QList<QWeakPointer<User> > m_listUsers; QOdbList<QWeakPointer<User> > m_listUsers;
#pragma db value_not_null #pragma db value_not_null
QOdbList<QSharedPointer<Permission> > m_listPermissions; QOdbList<QSharedPointer<Permission> > m_listPermissions;
}; };

@ -22,6 +22,16 @@ void User::setListRoles(const QList<QSharedPointer<Role> > &listRoles)
m_listRoles = listRoles; m_listRoles = listRoles;
} }
void User::addRole(QSharedPointer<Role> role)
{
m_listRoles.append(role);
}
void User::clearRoles()
{
m_listRoles.clear();
}
QString User::login() const QString User::login() const
{ {
return m_login; return m_login;

@ -9,6 +9,7 @@
#include <QList> #include <QList>
#include <QDateTime> #include <QDateTime>
#include <odb/core.hxx> #include <odb/core.hxx>
#include <odb/qt/list.hxx>
#pragma db object #pragma db object
class User : public QObject class User : public QObject
@ -53,6 +54,10 @@ public:
QList<QSharedPointer<Role> > listRoles() const; QList<QSharedPointer<Role> > listRoles() const;
void setListRoles(const QList<QSharedPointer<Role> > &listRoles); void setListRoles(const QList<QSharedPointer<Role> > &listRoles);
void addRole(QSharedPointer<Role> role);
void clearRoles();
private: private:
friend class odb::access; friend class odb::access;
@ -66,8 +71,8 @@ private:
QDateTime m_createDate; QDateTime m_createDate;
bool m_active; bool m_active;
bool m_isAdmin; bool m_isAdmin;
#pragma db value_not_null unordered #pragma db value_not_null
QList<QSharedPointer<Role> > m_listRoles; QOdbList<QSharedPointer<Role> > m_listRoles;
}; };
#endif // USER_H #endif // USER_H

@ -23,6 +23,7 @@
CREATE TABLE \"User_listRoles\" ( CREATE TABLE \"User_listRoles\" (
\"object_id\" INTEGER NOT NULL, \"object_id\" INTEGER NOT NULL,
\"index\" INTEGER NOT NULL,
\"value\" INTEGER NOT NULL, \"value\" INTEGER NOT NULL,
CONSTRAINT \"object_id_fk\" CONSTRAINT \"object_id_fk\"
FOREIGN KEY (\"object_id\") FOREIGN KEY (\"object_id\")
@ -36,6 +37,9 @@ CREATE TABLE \"User_listRoles\" (
CREATE INDEX \"User_listRoles_object_id_i\" CREATE INDEX \"User_listRoles_object_id_i\"
ON \"User_listRoles\" (\"object_id\"); ON \"User_listRoles\" (\"object_id\");
CREATE INDEX \"User_listRoles_index_i\"
ON \"User_listRoles\" (\"index\");
CREATE TABLE \"Role\" ( CREATE TABLE \"Role\" (
\"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
\"name\" TEXT NULL, \"name\" TEXT NULL,
@ -68,7 +72,9 @@ CREATE TABLE \"Permission\" (
\"permissionName\" TEXT NULL, \"permissionName\" TEXT NULL,
\"lastModDate\" TEXT NULL, \"lastModDate\" TEXT NULL,
\"createDate\" TEXT NULL, \"createDate\" TEXT NULL,
\"active\" INTEGER NOT NULL);" ], \"active\" INTEGER NOT NULL);
" ],
"dependencies" : [] "dependencies" : []
} }
} }

@ -1,6 +1,11 @@
#include "userform.h" #include "userform.h"
#include "ui_userform.h" #include "ui_userform.h"
#include <QTableWidgetItem>
#include <QHeaderView>
#include "../data/core-data.h"
#include "../service.h"
UserForm::UserForm(QWidget *parent) : UserForm::UserForm(QWidget *parent) :
AutoForm<User>(parent), AutoForm<User>(parent),
ui(new Ui::UserForm) ui(new Ui::UserForm)
@ -17,3 +22,37 @@ UserForm::~UserForm()
{ {
delete ui; delete ui;
} }
void UserForm::bindOtherToUi()
{
Service<Role> srv;
QList<QSharedPointer<Role> > roles = this->entity()->listRoles();
ui->tableWidget->setRowCount(srv.all().count());
ui->tableWidget->setColumnCount(1);
int i = 0;
foreach(QSharedPointer<Role> r,srv.all())
{
auto it = std::find_if(ALL(roles),[r](QSharedPointer<Role> 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<Role> 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;
}

@ -20,6 +20,11 @@ public:
private: private:
Ui::UserForm *ui; Ui::UserForm *ui;
// AutoForm interface
protected:
void bindOtherToUi();
bool bindOtherToData();
}; };
#endif // USERFORM_H #endif // USERFORM_H

@ -44,7 +44,7 @@
<item row="2" column="1"> <item row="2" column="1">
<widget class="QLineEdit" name="name"/> <widget class="QLineEdit" name="name"/>
</item> </item>
<item row="3" column="1"> <item row="4" column="1">
<widget class="QCheckBox" name="isAdmin"> <widget class="QCheckBox" name="isAdmin">
<property name="layoutDirection"> <property name="layoutDirection">
<enum>Qt::RightToLeft</enum> <enum>Qt::RightToLeft</enum>
@ -54,7 +54,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="1"> <item row="5" column="1">
<widget class="QCheckBox" name="active"> <widget class="QCheckBox" name="active">
<property name="layoutDirection"> <property name="layoutDirection">
<enum>Qt::RightToLeft</enum> <enum>Qt::RightToLeft</enum>
@ -64,6 +64,16 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="1">
<widget class="QTableWidget" name="tableWidget"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Roles</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>

Loading…
Cancel
Save