From 87a55edc04757405243d512e555683980c02e1bb Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 4 Nov 2015 03:04:42 +0100 Subject: [PATCH] refs #502, added role support and user as data object (for distribution) --- src/blackcore/data/authenticateduser.h | 40 +++++++++++++++++++++ src/blackmisc/network/authenticateduser.cpp | 8 +++++ src/blackmisc/network/authenticateduser.h | 10 ++++-- src/blackmisc/network/rolelist.cpp | 9 +++++ src/blackmisc/network/rolelist.h | 3 ++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/blackcore/data/authenticateduser.h diff --git a/src/blackcore/data/authenticateduser.h b/src/blackcore/data/authenticateduser.h new file mode 100644 index 000000000..f24c7f5ef --- /dev/null +++ b/src/blackcore/data/authenticateduser.h @@ -0,0 +1,40 @@ +/* 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 BLACKCORE_DATA_AUTHENTICATEDUSER_H +#define BLACKCORE_DATA_AUTHENTICATEDUSER_H + +#include "blackcore/blackcoreexport.h" +#include "blackcore/datacache.h" +#include "blackmisc/network/authenticateduser.h" + +namespace BlackCore +{ + namespace Data + { + //! Trait for for global setup data + struct AuthenticatedUser : public BlackCore::CDataTrait + { + //! Key in data cache + static const char *key() { return "readers/global/user"; } + + //! Default value + static const BlackMisc::Network::CAuthenticatedUser &defaultValue() + { + static const BlackMisc::Network::CAuthenticatedUser user; + return user; + } + }; + + } // ns +} // ns + +#endif // guard diff --git a/src/blackmisc/network/authenticateduser.cpp b/src/blackmisc/network/authenticateduser.cpp index 8d04eab11..a03c5a20e 100644 --- a/src/blackmisc/network/authenticateduser.cpp +++ b/src/blackmisc/network/authenticateduser.cpp @@ -18,6 +18,9 @@ namespace BlackMisc { namespace Network { + CAuthenticatedUser::CAuthenticatedUser() + { } + CAuthenticatedUser::CAuthenticatedUser(int id, const QString &realname) : IDatastoreObjectWithIntegerKey(id), m_realname(realname.trimmed()) { } @@ -87,6 +90,11 @@ namespace BlackMisc return this->hasRole("ADMIN"); } + bool CAuthenticatedUser::isMappingAdmin() const + { + return this->hasRole("MAPPINGADMIN") || this->isAdmin(); + } + CIcon CAuthenticatedUser::toIcon() const { return CIconList::iconByIndex(CIcons::StandardIconUser16); diff --git a/src/blackmisc/network/authenticateduser.h b/src/blackmisc/network/authenticateduser.h index 1f3a0217c..0100c6355 100644 --- a/src/blackmisc/network/authenticateduser.h +++ b/src/blackmisc/network/authenticateduser.h @@ -41,7 +41,7 @@ namespace BlackMisc }; //! Default constructor. - CAuthenticatedUser() = default; + CAuthenticatedUser(); //! Constructor. CAuthenticatedUser(int id, const QString &realname); @@ -100,9 +100,12 @@ namespace BlackMisc //! Roles void setRoles(const CRoleList &roles) { m_roles = roles; } - //! Has roles? + //! Has role? bool hasRole(const QString &roleName) const { return m_roles.hasRole(roleName); } + //! Has any role? + bool hasAnyRole(const QStringList &roles) const { return m_roles.hasAnyRole(roles); } + //! Country const BlackMisc::CCountry &getCountry() const { return m_country; } @@ -112,6 +115,9 @@ namespace BlackMisc //! Admin? bool isAdmin() const; + //! Admin? + bool isMappingAdmin() const; + //! Authenticated void setAuthenticated(bool authenticated) { m_authenticated = authenticated; } diff --git a/src/blackmisc/network/rolelist.cpp b/src/blackmisc/network/rolelist.cpp index e97b5a9d0..4946cc25c 100644 --- a/src/blackmisc/network/rolelist.cpp +++ b/src/blackmisc/network/rolelist.cpp @@ -25,6 +25,15 @@ namespace BlackMisc return hasRole(role.getName()); } + bool CRoleList::hasAnyRole(const QStringList &roles) const + { + for (const QString &r : roles) + { + if (this->hasRole(r)) { return true; } + } + return false; + } + CRoleList::CRoleList(const CSequence &other) : CSequence(other) { } diff --git a/src/blackmisc/network/rolelist.h b/src/blackmisc/network/rolelist.h index 20d675f6d..10f421346 100644 --- a/src/blackmisc/network/rolelist.h +++ b/src/blackmisc/network/rolelist.h @@ -38,6 +38,9 @@ namespace BlackMisc //! Has role? bool hasRole(const CRole &role) const; + //! Has any role? + bool hasAnyRole(const QStringList &roles) const; + //! Construct from a base class object. CRoleList(const CSequence &other);