2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file src/common/types/PolicyKey.h
18 * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19 * @author Aleksander Zdyb <a.zdyb@samsung.com>
21 * @brief This file defines PolicyKey - triple, which defines a single
25 #ifndef CYNARA_COMMON_TYPES_POLICYKEY_H
26 #define CYNARA_COMMON_TYPES_POLICYKEY_H
35 class PolicyKeyFeature {
36 friend class PolicyKey;
39 PolicyKeyFeature(const PolicyKeyFeature &) = default;
40 PolicyKeyFeature(PolicyKeyFeature &&) = default;
42 typedef std::string ValueType;
44 static PolicyKeyFeature create(ValueType value) {
45 return PolicyKeyFeature(value);
48 static PolicyKeyFeature createWildcard() {
49 return PolicyKeyFeature();
52 // TODO: Different features (client, user, privilege)
53 // shouldn't be comparable
54 bool operator==(const PolicyKeyFeature &other) const {
55 return anyWildcard(*this, other) || valuesMatch(*this, other);
58 bool operator==(const PolicyKeyFeature::ValueType &other) const {
59 return anyWildcard(*this, other) || valuesMatch(*this, other);
62 const std::string &toString() const;
65 PolicyKeyFeature(const ValueType &value) : m_value(value),
66 m_isWildcard(value == PolicyKeyFeature::m_wildcardValue) {}
67 PolicyKeyFeature() : m_value(m_wildcardValue), m_isWildcard(true) {}
69 static bool anyWildcard(const PolicyKeyFeature &pkf1, const PolicyKeyFeature &pkf2) {
70 return pkf1.isWildcard() || pkf2.isWildcard();
73 static bool valuesMatch(const PolicyKeyFeature &pkf1, const PolicyKeyFeature &pkf2) {
74 return pkf1.value() == pkf2.value();
80 static std::string m_wildcardValue;
83 bool isWildcard() const {
86 const ValueType& value() const {
95 PolicyKey(const PolicyKeyFeature &clientId, const PolicyKeyFeature &userId,
96 const PolicyKeyFeature &privilegeId)
97 : m_client(clientId), m_user(userId), m_privilege(privilegeId) {};
99 PolicyKey(const PolicyKeyFeature::ValueType &clientId,
100 const PolicyKeyFeature::ValueType &userId,
101 const PolicyKeyFeature::ValueType &privilegeId)
102 : m_client(clientId), m_user(userId), m_privilege(privilegeId) {};
104 PolicyKey(const PolicyKey &) = default;
105 PolicyKey(PolicyKey &&) = default;
107 bool operator==(const PolicyKey &other) const {
108 return std::tie(m_client, m_user, m_privilege)
109 == std::tie(other.m_client, other.m_user, other.m_privilege);
112 std::string toString() const;
115 PolicyKeyFeature m_client;
116 PolicyKeyFeature m_user;
117 PolicyKeyFeature m_privilege;
120 const PolicyKeyFeature &client() const {
124 const PolicyKeyFeature &user() const {
128 const PolicyKeyFeature &privilege() const {
133 bool operator ==(const PolicyKeyFeature::ValueType &pkf1, const PolicyKeyFeature &pkf2);
135 } /* namespace Cynara */
138 #endif /* CYNARA_COMMON_TYPES_POLICYKEY_H */