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

@ -4,6 +4,7 @@
#include <QList>
#include <QDebug>
#include <QMap>
#include <QItemSelectionModel>
#include "context.h"
#include "filterui.h"
@ -23,6 +24,10 @@ IGridForm::IGridForm(QWidget *parent) :
m_columnDialog = new ColumnDialog(this);
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();
}
@ -91,6 +96,12 @@ QWidget *IGridForm::filterWidget()
void IGridForm::enableButtons()
{
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 &)
{
if (ui->tableView->currentIndex().isValid())
{
ui->btnEdit->setEnabled(canEditRecord());
ui->btnDelete->setEnabled(canDeleteRecord());
}
enableButtons();
}
void IGridForm::on_btnPrint_clicked()

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

Loading…
Cancel
Save