From e534a64d9af8adffa0371d5ad5ae3307c13e75fd Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Fri, 2 Oct 2015 15:12:52 +0200 Subject: [PATCH] refs #474, role class so I can start with access rights in mapping GUI Remark: Added upfront, as I need that for the screenshots when writing the BLOG article --- src/blackgui/roles.cpp | 45 +++++++++++++++++++++++++++++ src/blackgui/roles.h | 47 +++++++++++++++++++++++++++++++ src/swiftdata/swiftdata.h | 2 +- src/swiftdata/swiftdata_menus.cpp | 22 +++++++++++++-- 4 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 src/blackgui/roles.cpp create mode 100644 src/blackgui/roles.h diff --git a/src/blackgui/roles.cpp b/src/blackgui/roles.cpp new file mode 100644 index 000000000..b22a98d84 --- /dev/null +++ b/src/blackgui/roles.cpp @@ -0,0 +1,45 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "roles.h" + +namespace BlackGui +{ + CRoles::CRoles() { } + + bool CRoles::hasRole(const QString &role) const + { + return m_roles.contains(role); + } + + bool CRoles::isAdmin() const + { + return m_roles.contains("ADMIN"); + } + + void CRoles::setAdmin(bool admin) + { + if (admin) + { + if (isAdmin()) { return; } + m_roles.append("ADMIN"); + } + else + { + m_roles.removeAll("ADMIN"); + } + } + + CRoles &CRoles::roles() + { + static CRoles roles; + return roles; + } + +} // roles diff --git a/src/blackgui/roles.h b/src/blackgui/roles.h new file mode 100644 index 000000000..cabecd1bb --- /dev/null +++ b/src/blackgui/roles.h @@ -0,0 +1,47 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKGUI_ROLES_H +#define BLACKGUI_ROLES_H + +#include "blackgui/blackguiexport.h" +#include + +namespace BlackGui +{ + /*! + * Roles + */ + class BLACKGUI_EXPORT CRoles + { + public: + //! Has role? + bool hasRole(const QString &role) const; + + //! Admin? + bool isAdmin() const; + + //! Set admin + void setAdmin(bool admin); + + //! Roles + static CRoles &roles(); + + private: + //! Constructor + CRoles(); + + QStringList m_roles; + }; + +} // ns + +#endif // guard diff --git a/src/swiftdata/swiftdata.h b/src/swiftdata/swiftdata.h index 163b88e34..6c3cd0fef 100644 --- a/src/swiftdata/swiftdata.h +++ b/src/swiftdata/swiftdata.h @@ -63,7 +63,7 @@ private: QScopedPointer ui; BlackGui::CManagedStatusBar m_statusBar; - BlackCore::CWebDataServices *m_webDataReader = nullptr; + BlackCore::CWebDataServices *m_webDataReader = nullptr; }; #endif // guard diff --git a/src/swiftdata/swiftdata_menus.cpp b/src/swiftdata/swiftdata_menus.cpp index 849a29abd..91ec4fd53 100644 --- a/src/swiftdata/swiftdata_menus.cpp +++ b/src/swiftdata/swiftdata_menus.cpp @@ -13,6 +13,7 @@ #include "blackgui/components/datainfoareacomponent.h" #include "blackgui/components/logcomponent.h" #include "blackgui/stylesheetutility.h" +#include "blackgui/roles.h" #include "blackmisc/statusmessagelist.h" #include "blackmisc/logmessage.h" #include "blackmisc/project.h" @@ -38,8 +39,8 @@ void CSwiftData::ps_onMenuClicked() } else if (sender == this->ui->menu_WindowFont) { -// this->ps_setMainPageToInfoArea(); -// this->ui->comp_MainInfoArea->selectSettingsTab(BlackGui::Components::CSettingsComponent::SettingTabGui); + // this->ps_setMainPageToInfoArea(); + // this->ui->comp_MainInfoArea->selectSettingsTab(BlackGui::Components::CSettingsComponent::SettingTabGui); } else if (sender == this->ui->menu_WindowMinimize) { @@ -76,6 +77,17 @@ void CSwiftData::ps_onMenuClicked() CDbMappingComponent *mappingComponent = this->ui->comp_MainInfoArea->getMappingComponent(); mappingComponent->resizeForMapping(); } + else + { + QAction *a = qobject_cast(sender); + if (a) + { + if (a->data() == "admin") + { + CRoles::roles().setAdmin(true); + } + } + } } void CSwiftData::initDynamicMenus() @@ -95,4 +107,10 @@ void CSwiftData::initDynamicMenus() this->ui->menu_Mapping->addAction(CIcons::save16(), "Save DB data", this->ui->comp_MainInfoArea->getDataInfoAreaComponent(), SLOT(writeDbDataToResourceDir())); } } + + if (CProject::isRunningInDeveloperEnvironment() && !CRoles::roles().isAdmin()) + { + QAction *a = this->ui->menu_Internals->addAction(CIcons::user16(), "Set administrator"); + a->setData("admin"); + } }