Added methods for better permission checks in modules.

Added method for returning selected entity in grid.
Added polymorphic methods for delete record and fill grid.
master
Josef Rokos 8 years ago
parent b916d626d1
commit 35696fbfea

@ -99,7 +99,7 @@ public slots:
m_permissionDenied = false; m_permissionDenied = false;
connectService(); connectService();
m_tableModel->setData(service()->all()); m_tableModel->setData(listForGrid());
tableView()->setModel(m_tableModel); tableView()->setModel(m_tableModel);
QList<QVariant> varList = Context::instance().settings()->value("grids/" + pluginId() + "/hide").toList(); QList<QVariant> varList = Context::instance().settings()->value("grids/" + pluginId() + "/hide").toList();
@ -174,10 +174,8 @@ private slots:
protected: protected:
virtual void handleNewRecord() override virtual void handleNewRecord() override
{ {
PermissionEvaluator permEv; if (!checkPermAdd())
if (!permEv.hasPermission(pluginId(), PERM_ADD))
{ {
QMessageBox::critical(this, tr("Permission denied"), tr("You don't have permission to add new record."));
return; return;
} }
@ -194,10 +192,8 @@ protected:
virtual void handleEditRecord() override virtual void handleEditRecord() override
{ {
PermissionEvaluator permEv; if (!checkPermEdit())
if (!permEv.hasPermission(pluginId(), PERM_EDIT))
{ {
QMessageBox::critical(this, tr("Permission denied"), tr("You don't have permission to edit record."));
return; return;
} }
@ -214,10 +210,8 @@ protected:
void handleDeleteRecord() override void handleDeleteRecord() override
{ {
PermissionEvaluator permEv; if (!checkPermDelete())
if (!permEv.hasPermission(pluginId(), PERM_DELETE))
{ {
QMessageBox::critical(this, tr("Permission denied"), tr("You don't have permission to delete record."));
return; return;
} }
@ -234,7 +228,7 @@ protected:
if (reply == QMessageBox::Yes) if (reply == QMessageBox::Yes)
{ {
QSharedPointer<T> entity = m_tableModel->itemFromIndex(tableView()->currentIndex()); QSharedPointer<T> entity = m_tableModel->itemFromIndex(tableView()->currentIndex());
service()->erase(entity); doDelete(entity);
if (!m_permissionDenied) if (!m_permissionDenied)
{ {
@ -244,6 +238,42 @@ protected:
} }
} }
bool checkPermAdd()
{
PermissionEvaluator permEv;
if (!permEv.hasPermission(pluginId(), PERM_ADD))
{
QMessageBox::critical(this, tr("Permission denied"), tr("You don't have permission to add new record."));
return false;
}
return true;
}
bool checkPermEdit()
{
PermissionEvaluator permEv;
if (!permEv.hasPermission(pluginId(), PERM_EDIT))
{
QMessageBox::critical(this, tr("Permission denied"), tr("You don't have permission to edit record."));
return false;
}
return true;
}
bool checkPermDelete()
{
PermissionEvaluator permEv;
if (!permEv.hasPermission(pluginId(), PERM_DELETE))
{
QMessageBox::critical(this, tr("Permission denied"), tr("You don't have permission to delete record."));
return false;
}
return true;
}
virtual int currentRecordId() virtual int currentRecordId()
{ {
if (tableView()->currentIndex().isValid()) if (tableView()->currentIndex().isValid())
@ -254,6 +284,26 @@ protected:
return 0; return 0;
} }
QSharedPointer<T> currentEntity()
{
if (tableView()->currentIndex().isValid())
{
return m_tableModel->itemFromIndex(tableView()->currentIndex());
}
return QSharedPointer<T>();
}
virtual void doDelete(QSharedPointer<T> entity)
{
service()->erase(entity);
}
virtual QList<QSharedPointer<T> > listForGrid()
{
return service()->all();
}
void addRow(QSharedPointer<T> entity) void addRow(QSharedPointer<T> entity)
{ {
m_tableModel->addRow(entity); m_tableModel->addRow(entity);

@ -4,6 +4,7 @@
#include <QList> #include <QList>
#include <QDebug> #include <QDebug>
#include <QMap> #include <QMap>
#include <QItemSelectionModel>
#include "context.h" #include "context.h"
#include "filterui.h" #include "filterui.h"
@ -23,6 +24,10 @@ IGridForm::IGridForm(QWidget *parent) :
m_columnDialog = new ColumnDialog(this); m_columnDialog = new ColumnDialog(this);
connect(m_columnDialog, SIGNAL(accepted()), this, SLOT(columnsAccepted())); connect(m_columnDialog, SIGNAL(accepted()), this, SLOT(columnsAccepted()));
connect(ui->tableView->selectionModel(), &QItemSelectionModel::currentRowChanged, [this](const QModelIndex &current, const QModelIndex &){
currentIndexChanged(current);
});
m_varFiller = new VariableFiller(); m_varFiller = new VariableFiller();
} }
@ -91,6 +96,12 @@ QWidget *IGridForm::filterWidget()
void IGridForm::enableButtons() void IGridForm::enableButtons()
{ {
ui->btnNew->setEnabled(canAddRecord()); ui->btnNew->setEnabled(canAddRecord());
if (ui->tableView->currentIndex().isValid())
{
ui->btnEdit->setEnabled(canEditRecord());
ui->btnDelete->setEnabled(canDeleteRecord());
}
} }
@ -163,11 +174,7 @@ void IGridForm::on_btnFilter_toggled(bool checked)
void IGridForm::on_tableView_clicked(const QModelIndex &) void IGridForm::on_tableView_clicked(const QModelIndex &)
{ {
if (ui->tableView->currentIndex().isValid()) enableButtons();
{
ui->btnEdit->setEnabled(canEditRecord());
ui->btnDelete->setEnabled(canDeleteRecord());
}
} }
void IGridForm::on_btnPrint_clicked() void IGridForm::on_btnPrint_clicked()

@ -47,6 +47,7 @@ protected:
virtual bool canEditRecord() { return true; } virtual bool canEditRecord() { return true; }
virtual bool canDeleteRecord() { return true; } virtual bool canDeleteRecord() { return true; }
virtual int currentRecordId() = 0; virtual int currentRecordId() = 0;
virtual void currentIndexChanged(const QModelIndex &current) { Q_UNUSED(current) }
void hideColumns(const QList<int> &cols); void hideColumns(const QList<int> &cols);
QWidget *filterWidget(); QWidget *filterWidget();
void enableButtons(); void enableButtons();

Loading…
Cancel
Save