Implemented permission settings for Roles.

This commit is contained in:
2015-12-22 16:08:47 +01:00
parent 09f6266123
commit 94fba56dab
16 changed files with 295 additions and 80 deletions
+47 -19
View File
@@ -13,6 +13,7 @@
#include "coreplugin.h"
#include "users/users.h"
#include "roles/roles.h"
#include "permissionservice.h"
Context &Context::instance()
{
@@ -76,6 +77,8 @@ void Context::openDb(const QString &path)
m_db = new odb::sqlite::database(path.toStdString());
m_settings->setValue("db/path", path);
m_dbOpened = true;
checkPermissions();
}
void Context::destroy()
@@ -96,6 +99,11 @@ void Context::destroy()
m_plugins.clear();
}
QStringList Context::defaultPerms()
{
return QStringList() << PERM_READ << PERM_ADD << PERM_EDIT << PERM_DELETE;
}
Context::Context()
{
m_db = NULL;
@@ -105,29 +113,32 @@ Context::Context()
void Context::checkDb(const QString &path)
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(path);
db.open();
QSqlQuery q(db);
QString verSql = "SELECT pluginId, schemaVersion FROM system";
QString createSysSql = "CREATE TABLE \"system\" (\"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \"pluginId\" TEXT NULL, \"schemaVersion\" TEXT NULL)";
if (q.exec(verSql))
{
QMap<QString, int> schemas;
while (q.next())
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "check");
db.setDatabaseName(path);
db.open();
QSqlQuery q(db);
QString verSql = "SELECT pluginId, schemaVersion FROM system";
QString createSysSql = "CREATE TABLE \"system\" (\"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \"pluginId\" TEXT NULL, \"schemaVersion\" TEXT NULL)";
if (q.exec(verSql))
{
schemas[q.value("pluginId").toString()] = q.value("schemaVersion").toInt();
QMap<QString, int> schemas;
while (q.next())
{
schemas[q.value("pluginId").toString()] = q.value("schemaVersion").toInt();
}
checkSchema(db, schemas);
}
else
{
q.exec(createSysSql);
checkSchema(db, QMap<QString, int>());
}
checkSchema(db, schemas);
}
else
{
q.exec(createSysSql);
checkSchema(db, QMap<QString, int>());
}
db.close();
db.close();
}
QSqlDatabase::removeDatabase("check");
}
void Context::checkSchema(const QSqlDatabase &db, const QMap<QString, int> &schemaMap)
@@ -197,3 +208,20 @@ void Context::createSchema(IPlugin *plugin, const QSqlDatabase &db, const QMap<Q
}
}
}
void Context::checkPermissions()
{
PermissionService permService;
foreach (IPlugin *plugin, m_plugins) {
foreach (QString perm, defaultPerms()) {
QSharedPointer<Permission> p = permService.forNameAndPlugin(perm, plugin->pluginId());
if (p.isNull())
{
QSharedPointer<Permission> nPerm(new Permission);
nPerm->setPermissionName(perm);
nPerm->setPluginId(plugin->pluginId());
permService.save(nPerm);
}
}
}
}