3f65c58aa34baead98e1e988cbe255421ee085f0
[platform/core/security/cynara.git] / src / common / types / PolicyKey.h
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file        src/common/types/PolicyKey.h
18  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
20  * @version     1.0
21  * @brief       This file defines PolicyKey - triple, which defines a single
22                 policy rule
23  */
24
25 #ifndef CYNARA_COMMON_TYPES_POLICYKEY_H
26 #define CYNARA_COMMON_TYPES_POLICYKEY_H
27
28 #include <tuple>
29 #include <string>
30
31 namespace Cynara {
32
33 class PolicyKey;
34
35 class PolicyKeyFeature {
36 friend class PolicyKey;
37
38 public:
39     PolicyKeyFeature(const PolicyKeyFeature &) = default;
40     PolicyKeyFeature(PolicyKeyFeature &&) = default;
41
42     typedef std::string ValueType;
43
44     static PolicyKeyFeature create(ValueType value) {
45         return PolicyKeyFeature(value);
46     }
47
48     static PolicyKeyFeature createWildcard() {
49         return PolicyKeyFeature();
50     }
51
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);
56     }
57
58     bool operator==(const PolicyKeyFeature::ValueType &other) const {
59         return anyWildcard(*this, other) || valuesMatch(*this, other);
60     }
61
62     const std::string &toString() const;
63
64 protected:
65     PolicyKeyFeature(const ValueType &value) : m_value(value),
66         m_isWildcard(value == PolicyKeyFeature::m_wildcardValue) {}
67     PolicyKeyFeature() : m_value(m_wildcardValue), m_isWildcard(true) {}
68
69     static bool anyWildcard(const PolicyKeyFeature &pkf1, const PolicyKeyFeature &pkf2) {
70         return pkf1.isWildcard() || pkf2.isWildcard();
71     }
72
73     static bool valuesMatch(const PolicyKeyFeature &pkf1, const PolicyKeyFeature &pkf2) {
74         return pkf1.value() == pkf2.value();
75     }
76
77 private:
78     ValueType m_value;
79     bool m_isWildcard;
80     static std::string m_wildcardValue;
81
82 public:
83     bool isWildcard() const {
84         return m_isWildcard;
85     }
86     const ValueType& value() const {
87         return m_value;
88     }
89 };
90
91 class PolicyKey
92 {
93
94 public:
95     PolicyKey(const PolicyKeyFeature &clientId, const PolicyKeyFeature &userId,
96             const PolicyKeyFeature &privilegeId)
97         : m_client(clientId), m_user(userId), m_privilege(privilegeId) {};
98
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) {};
103
104     PolicyKey(const PolicyKey &) = default;
105     PolicyKey(PolicyKey &&) = default;
106
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);
110     }
111
112     std::string toString() const;
113
114 private:
115     PolicyKeyFeature m_client;
116     PolicyKeyFeature m_user;
117     PolicyKeyFeature m_privilege;
118
119 public:
120     const PolicyKeyFeature &client() const {
121         return m_client;
122     }
123
124     const PolicyKeyFeature &user() const {
125         return m_user;
126     }
127
128     const PolicyKeyFeature &privilege() const {
129         return m_privilege;
130     }
131 };
132
133 bool operator ==(const PolicyKeyFeature::ValueType &pkf1, const PolicyKeyFeature &pkf2);
134
135 } /* namespace Cynara */
136
137
138 #endif /* CYNARA_COMMON_TYPES_POLICYKEY_H */