Implemented application login.
Minor UI improvement. Fixed memory leaks.
This commit is contained in:
+21
-1
@@ -15,6 +15,11 @@
|
||||
#include "roles/roles.h"
|
||||
#include "permissionservice.h"
|
||||
|
||||
Context::~Context()
|
||||
{
|
||||
this->destroy();
|
||||
}
|
||||
|
||||
Context &Context::instance()
|
||||
{
|
||||
static Context ctx;
|
||||
@@ -91,7 +96,10 @@ void Context::destroy()
|
||||
m_dbOpened = false;
|
||||
}
|
||||
|
||||
delete m_settings;
|
||||
if (m_settings != NULL && m_settings->parent() == NULL)
|
||||
{
|
||||
delete m_settings;
|
||||
}
|
||||
|
||||
foreach (IPlugin *plugin, m_plugins)
|
||||
{
|
||||
@@ -112,6 +120,16 @@ Context::Context()
|
||||
m_dbOpened = false;
|
||||
}
|
||||
|
||||
QSharedPointer<User> Context::currentUser() const
|
||||
{
|
||||
return m_currentUser;
|
||||
}
|
||||
|
||||
void Context::setCurrentUser(const QSharedPointer<User> ¤tUser)
|
||||
{
|
||||
m_currentUser = currentUser;
|
||||
}
|
||||
|
||||
void Context::checkDb(const QString &path)
|
||||
{
|
||||
{
|
||||
@@ -226,4 +244,6 @@ void Context::checkPermissions()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
permService.checkForAdmin();
|
||||
}
|
||||
|
||||
@@ -7,10 +7,12 @@
|
||||
#include <QSqlDatabase>
|
||||
#include <QStringList>
|
||||
#include <QSettings>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include "define.h"
|
||||
#include "core_global.h"
|
||||
#include "transaction.h"
|
||||
#include "data/core-data.h"
|
||||
|
||||
#include <odb/database.hxx>
|
||||
#include <odb/session.hxx>
|
||||
@@ -20,6 +22,7 @@ class IPlugin;
|
||||
class CORESHARED_EXPORT Context
|
||||
{
|
||||
public:
|
||||
~Context();
|
||||
static Context &instance();
|
||||
QList<IPlugin*> plugins();
|
||||
IPlugin *plugin(const QString &pluginId);
|
||||
@@ -31,6 +34,9 @@ public:
|
||||
void destroy();
|
||||
QStringList defaultPerms();
|
||||
|
||||
QSharedPointer<User> currentUser() const;
|
||||
void setCurrentUser(const QSharedPointer<User> ¤tUser);
|
||||
|
||||
private:
|
||||
Context();
|
||||
QList<IPlugin*> m_plugins;
|
||||
@@ -38,6 +44,7 @@ private:
|
||||
QSettings *m_settings;
|
||||
bool m_dbOpened;
|
||||
odb::session m_session;
|
||||
QSharedPointer<User> m_currentUser;
|
||||
|
||||
QStringList m_solved;
|
||||
|
||||
|
||||
@@ -7,5 +7,6 @@
|
||||
#include "imetadataplugin.h"
|
||||
#include "transaction.h"
|
||||
#include "gridform.h"
|
||||
#include "permissionservice.h"
|
||||
|
||||
#endif // CORE_H
|
||||
|
||||
+6
-13
@@ -70,14 +70,11 @@
|
||||
<property name="toolTip">
|
||||
<string>Apply</string>
|
||||
</property>
|
||||
<property name="toolTipDuration">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Go</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset>
|
||||
<iconset resource="rc.qrc">
|
||||
<normaloff>:/icons/ok.svg</normaloff>:/icons/ok.svg</iconset>
|
||||
</property>
|
||||
<property name="iconSize">
|
||||
@@ -119,14 +116,11 @@
|
||||
<property name="toolTip">
|
||||
<string>Save</string>
|
||||
</property>
|
||||
<property name="toolTipDuration">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Save</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset>
|
||||
<iconset resource="rc.qrc">
|
||||
<normaloff>:/icons/save.svg</normaloff>:/icons/save.svg</iconset>
|
||||
</property>
|
||||
<property name="iconSize">
|
||||
@@ -145,14 +139,11 @@
|
||||
<property name="toolTip">
|
||||
<string>Manage</string>
|
||||
</property>
|
||||
<property name="toolTipDuration">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Manage</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset>
|
||||
<iconset resource="rc.qrc">
|
||||
<normaloff>:/icons/list.svg</normaloff>:/icons/list.svg</iconset>
|
||||
</property>
|
||||
<property name="iconSize">
|
||||
@@ -229,6 +220,8 @@
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<resources/>
|
||||
<resources>
|
||||
<include location="rc.qrc"/>
|
||||
</resources>
|
||||
<connections/>
|
||||
</ui>
|
||||
|
||||
@@ -30,6 +30,16 @@ public:
|
||||
}
|
||||
virtual ~GridForm()
|
||||
{
|
||||
if (m_form != NULL && m_form->parent() == NULL)
|
||||
{
|
||||
delete m_form;
|
||||
}
|
||||
|
||||
if (m_tableModel != NULL && m_tableModel->parent() == NULL)
|
||||
{
|
||||
delete m_tableModel;
|
||||
}
|
||||
|
||||
delete m_formHandler;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,9 @@
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QToolButton" name="btnNew">
|
||||
<property name="toolTip">
|
||||
<string>Add record</string>
|
||||
</property>
|
||||
<property name="autoFillBackground">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
@@ -45,6 +48,9 @@
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="btnEdit">
|
||||
<property name="toolTip">
|
||||
<string>Edit record</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>E</string>
|
||||
</property>
|
||||
@@ -65,6 +71,9 @@
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="btnDelete">
|
||||
<property name="toolTip">
|
||||
<string>Delete record</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>D</string>
|
||||
</property>
|
||||
@@ -85,6 +94,9 @@
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="btnFilter">
|
||||
<property name="toolTip">
|
||||
<string>Filter</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>F</string>
|
||||
</property>
|
||||
@@ -108,6 +120,9 @@
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="btnPrint">
|
||||
<property name="toolTip">
|
||||
<string>Print</string>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>P</string>
|
||||
</property>
|
||||
|
||||
@@ -21,6 +21,7 @@ IGridForm::IGridForm(QWidget *parent) :
|
||||
|
||||
IGridForm::~IGridForm()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void IGridForm::setPluginId(const QString &pluginId)
|
||||
|
||||
@@ -10,14 +10,11 @@
|
||||
IMetaDataPlugin::IMetaDataPlugin()
|
||||
{
|
||||
m_service = NULL;
|
||||
m_ui = NULL;
|
||||
}
|
||||
|
||||
IMetaDataPlugin::~IMetaDataPlugin()
|
||||
{
|
||||
if (m_service != NULL)
|
||||
{
|
||||
delete m_service;
|
||||
}
|
||||
}
|
||||
|
||||
QString IMetaDataPlugin::pluginName()
|
||||
|
||||
@@ -10,7 +10,7 @@ class CORESHARED_EXPORT IMetaDataPlugin : public IPlugin
|
||||
{
|
||||
public:
|
||||
IMetaDataPlugin();
|
||||
~IMetaDataPlugin();
|
||||
virtual ~IMetaDataPlugin();
|
||||
|
||||
// IPlugin interface
|
||||
public:
|
||||
|
||||
+12
-1
@@ -19,7 +19,18 @@ public:
|
||||
m_service = NULL;
|
||||
}
|
||||
|
||||
virtual ~IPlugin() { }
|
||||
virtual ~IPlugin() {
|
||||
if (m_service != NULL)
|
||||
{
|
||||
delete m_service;
|
||||
}
|
||||
|
||||
if (m_ui != NULL && m_ui->parent() == NULL)
|
||||
{
|
||||
delete m_ui;
|
||||
}
|
||||
}
|
||||
|
||||
virtual QString pluginName() = 0;
|
||||
virtual QString pluginId() = 0;
|
||||
virtual QString pluginDescription() = 0;
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
#include "core-odb.hxx"
|
||||
#include "permissionservice.h"
|
||||
#include <QCryptographicHash>
|
||||
|
||||
typedef odb::query<Permission> permQuery;
|
||||
typedef odb::result<Permission> permResult;
|
||||
|
||||
PermissionService::PermissionService()
|
||||
{
|
||||
@@ -36,3 +41,51 @@ QSharedPointer<Permission> PermissionService::forNameAndPlugin(const QString &na
|
||||
return p;
|
||||
}
|
||||
|
||||
bool PermissionService::checkLogin(const QString &login, const QString &password)
|
||||
{
|
||||
|
||||
QSharedPointer<User> user = loadUser(login);
|
||||
if (user.data())
|
||||
{
|
||||
return user->password() == encryptPassword(password) && user->active();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
QSharedPointer<User> PermissionService::loadUser(const QString &login)
|
||||
{
|
||||
odb::database *db = Context::instance().db();
|
||||
|
||||
Transaction tr;
|
||||
return db->query_one<User>("login = " + odb::query<User>::_ref(login));
|
||||
}
|
||||
|
||||
void PermissionService::checkForAdmin()
|
||||
{
|
||||
odb::database *db = Context::instance().db();
|
||||
|
||||
Transaction tr;
|
||||
odb::query<User> q(odb::query<User>::isAdmin == true);
|
||||
odb::result<User> r = db->query<User>(q);
|
||||
|
||||
if (r.empty())
|
||||
{
|
||||
QSharedPointer<User> admin(new User);
|
||||
admin->setLogin("admin");
|
||||
admin->setName("Administrator");
|
||||
admin->setIsAdmin(true);
|
||||
admin->setPassword(encryptPassword("admin"));
|
||||
admin->setActive(true);
|
||||
|
||||
db->persist(admin);
|
||||
}
|
||||
|
||||
tr.commit();
|
||||
}
|
||||
|
||||
QString PermissionService::encryptPassword(const QString &plainPasswd)
|
||||
{
|
||||
return QString(QCryptographicHash::hash(plainPasswd.toUtf8(),QCryptographicHash::Sha256).toBase64());
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
#define PERMISSIONSERVICE_H
|
||||
|
||||
#include "service.h"
|
||||
#include "permission.h"
|
||||
#include "core-odb.hxx"
|
||||
#include "data/core-data.h"
|
||||
#include "core_global.h"
|
||||
#include <odb/core.hxx>
|
||||
#include <odb/database.hxx>
|
||||
@@ -14,9 +13,6 @@
|
||||
#include <QSharedPointer>
|
||||
#include <QString>
|
||||
|
||||
typedef odb::query<Permission> permQuery;
|
||||
typedef odb::result<Permission> permResult;
|
||||
|
||||
class CORESHARED_EXPORT PermissionService : public Service<Permission>
|
||||
{
|
||||
public:
|
||||
@@ -25,6 +21,10 @@ public:
|
||||
|
||||
QList<QSharedPointer<Permission> > forPlugin(const QString &pluginId);
|
||||
QSharedPointer<Permission> forNameAndPlugin(const QString &name, const QString &pluginId);
|
||||
bool checkLogin(const QString &login, const QString &password);
|
||||
QSharedPointer<User> loadUser(const QString &login);
|
||||
void checkForAdmin();
|
||||
QString encryptPassword(const QString &plainPasswd);
|
||||
};
|
||||
|
||||
#endif // PERMISSIONSERVICE_H
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
#include <QHeaderView>
|
||||
#include "../data/core-data.h"
|
||||
#include "../service.h"
|
||||
#include "../permissionservice.h"
|
||||
#include "../emptystringvalidator.h"
|
||||
#include "../samestringvalidator.h"
|
||||
#include <QCryptographicHash>
|
||||
|
||||
UserForm::UserForm(QWidget *parent) :
|
||||
AutoForm<User>(parent),
|
||||
@@ -69,8 +69,9 @@ bool UserForm::bindOtherToData()
|
||||
this->entity()->addRole(srv.loadById(ui->tableWidget->item(i,0)->data(Qt::UserRole).toInt()));
|
||||
}
|
||||
}
|
||||
PermissionService permService;
|
||||
if (m_passChanged){
|
||||
this->entity()->setPassword(QString(QCryptographicHash::hash(ui->password->text().toUtf8(),QCryptographicHash::Sha256).toBase64()));
|
||||
this->entity()->setPassword(permService.encryptPassword(ui->password->text()));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user