tizen 2.3.1 release
[framework/web/wearable/wrt-security.git] / ace / include / ace / PolicyResult.h
1 /*
2  * Copyright (c) 2011 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 #ifndef _SRC_ACCESS_CONTROL_COMMON_POLICY_RESULT_H_
18 #define _SRC_ACCESS_CONTROL_COMMON_POLICY_RESULT_H_
19
20 #include <dpl/assert.h>
21 #include <dpl/optional_typedefs.h>
22 #include <boost/optional.hpp>
23 #include <ace/PolicyEffect.h>
24
25 typedef boost::optional<PolicyEffect> OptionalPolicyEffect;
26
27 class PolicyDecision
28 {
29 public:
30     enum Value { NOT_APPLICABLE = -1 };
31
32     PolicyDecision(PolicyEffect effect)
33       : m_isPolicyEffect(true)
34       , m_effect(effect)
35     {}
36
37     PolicyDecision(const PolicyDecision &decision)
38       : m_isPolicyEffect(decision.m_isPolicyEffect)
39       , m_effect(decision.m_effect)
40     {}
41
42     PolicyDecision(Value)
43       : m_isPolicyEffect(false)
44     {}
45
46     PolicyDecision& operator=(const PolicyDecision &second) {
47         if (this == &second)
48             return *this;
49
50         m_isPolicyEffect = second.m_isPolicyEffect;
51         m_effect = second.m_effect;
52
53         return *this;
54     }
55
56     bool operator==(const PolicyDecision &decision) const {
57         return (m_isPolicyEffect
58                  && decision.m_isPolicyEffect
59                  && m_effect == decision.m_effect)
60                || (!m_isPolicyEffect && !decision.m_isPolicyEffect);
61     }
62
63     bool operator==(Value) const {
64         return !m_isPolicyEffect;
65     }
66
67     bool operator!=(const PolicyDecision &decision) const {
68         return !(*this == decision);
69     }
70
71     bool operator!=(Value value) const {
72         return !(*this == value);
73     }
74
75     OptionalPolicyEffect getEffect() const
76     {
77         if (!m_isPolicyEffect) {
78             return OptionalPolicyEffect();
79         }
80         return OptionalPolicyEffect(m_effect);
81     }
82
83     std::ostream & toStream(std::ostream& stream) {
84         if (m_isPolicyEffect)
85             stream << m_effect;
86         else
87             stream << "NOT-APPLICABLE";
88         return stream;
89     }
90
91 private:
92     bool m_isPolicyEffect;
93     PolicyEffect m_effect;
94 };
95
96 inline static bool operator==(PolicyEffect e, const PolicyDecision &d) {
97   return d.operator==(e);
98 }
99
100 inline static bool operator!=(PolicyEffect e, const PolicyDecision &d) {
101   return !(e == d);
102 }
103
104 inline static std::ostream & operator<<(std::ostream& stream,
105                                         PolicyDecision decision)
106 {
107     return decision.toStream(stream);
108 }
109
110 class PolicyResult {
111 public:
112     enum Value { UNDETERMINED = -2 };
113
114     // This constructor is required by dpl controller and by dpl optional
115     PolicyResult()
116       : m_isDecision(false)
117       , m_decision(PolicyDecision::Value::NOT_APPLICABLE) // don't care
118     {}
119
120     PolicyResult(PolicyEffect effect)
121       : m_isDecision(true)
122       , m_decision(effect)
123     {}
124
125     PolicyResult(const PolicyDecision &decision)
126       : m_isDecision(true)
127       , m_decision(decision)
128     {}
129
130     PolicyResult(const PolicyResult &result)
131       : m_isDecision(result.m_isDecision)
132       , m_decision(result.m_decision)
133     {}
134
135     PolicyResult(PolicyDecision::Value value)
136       : m_isDecision(true)
137       , m_decision(value)
138     {}
139
140     PolicyResult(Value)
141       : m_isDecision(false)
142       , m_decision(PolicyDecision::Value::NOT_APPLICABLE) // don't care
143     {}
144
145     PolicyResult& operator=(const PolicyResult &second) {
146         if (this == &second)
147             return *this;
148
149         m_isDecision = second.m_isDecision;
150         m_decision = second.m_decision;
151
152         return *this;
153     }
154
155     bool operator==(const PolicyResult &result) const {
156           return (m_isDecision
157                 && result.m_isDecision
158                 && m_decision == result.m_decision)
159                 || (!m_isDecision && !result.m_isDecision);
160     }
161
162     bool operator==(Value) const {
163         return !m_isDecision;
164     }
165
166     bool operator!=(const PolicyResult &result) const {
167         return !(*this == result);
168     }
169
170     bool operator!=(Value value) const {
171         return !(*this == value);
172     }
173
174     OptionalPolicyEffect getEffect() const
175     {
176         if (!m_isDecision) {
177             return OptionalPolicyEffect();
178         }
179         return m_decision.getEffect();
180     }
181
182     static int serialize(const PolicyResult &policyResult)
183     {
184         if (!policyResult.m_isDecision) {
185             return BD_UNDETERMINED;
186         } else if (policyResult.m_decision ==
187             PolicyDecision::Value::NOT_APPLICABLE)
188         {
189             return BD_NOT_APPLICABLE;
190         } else if (policyResult.m_decision == PolicyEffect::PROMPT_BLANKET) {
191             return BD_PROMPT_BLANKET;
192         } else if (policyResult.m_decision == PolicyEffect::PROMPT_SESSION) {
193             return BD_PROMPT_SESSION;
194         } else if (policyResult.m_decision == PolicyEffect::PROMPT_ONESHOT) {
195             return BD_PROMPT_ONESHOT;
196         } else if (policyResult.m_decision == PolicyEffect::PERMIT) {
197             return BD_PERMIT;
198         } else if (policyResult.m_decision == PolicyEffect::DENY) {
199             return BD_DENY;
200         }
201         Assert(false && "Unknown value of policyResult.");
202     }
203
204     static PolicyResult deserialize(int dec){
205         switch (dec) {
206             case BD_DENY:
207                 return PolicyEffect::DENY;
208             case BD_PERMIT:
209                 return PolicyEffect::PERMIT;
210             case BD_PROMPT_ONESHOT:
211                 return PolicyEffect::PROMPT_ONESHOT;
212             case BD_PROMPT_SESSION:
213                 return PolicyEffect::PROMPT_SESSION;
214             case BD_PROMPT_BLANKET:
215                 return PolicyEffect::PROMPT_BLANKET;
216             case BD_NOT_APPLICABLE:
217                 return PolicyDecision::Value::NOT_APPLICABLE;
218             case BD_UNDETERMINED:
219                 return Value::UNDETERMINED;
220         }
221         Assert(false && "Broken database");
222     }
223
224     std::ostream & toStream(std::ostream& stream) {
225         if (m_isDecision)
226             stream << m_decision;
227         else
228             stream << "UNDETERMINED";
229         return stream;
230     }
231
232 private:
233     static const int BD_UNDETERMINED = 6;
234     static const int BD_NOT_APPLICABLE = 5;
235     static const int BD_PROMPT_BLANKET = 4;
236     static const int BD_PROMPT_SESSION = 3;
237     static const int BD_PROMPT_ONESHOT = 2;
238     static const int BD_PERMIT = 1;
239     static const int BD_DENY = 0;
240
241     bool m_isDecision;
242     PolicyDecision m_decision;
243 };
244
245 inline static bool operator==(const PolicyDecision &d, const PolicyResult &r) {
246     return r == d;
247 }
248
249 inline static bool operator!=(const PolicyDecision &d, const PolicyResult &r) {
250     return !(d == r);
251 }
252
253 inline static bool operator==(const PolicyEffect &e, const PolicyResult &r) {
254     return e == r;
255 }
256
257 inline static bool operator!=(const PolicyEffect &e, const PolicyResult &r) {
258     return !(e == r);
259 }
260
261 inline static std::ostream & operator<<(std::ostream& stream,
262                                         PolicyResult result)
263 {
264     return result.toStream(stream);
265 }
266
267 struct ExtendedPolicyResult {
268     ExtendedPolicyResult(const PolicyResult pr = PolicyEffect::DENY, int rule = -1)
269       : policyResult(pr)
270       , ruleId(rule)
271     {}
272     PolicyResult policyResult;
273     int ruleId;
274 };
275
276 typedef boost::optional<ExtendedPolicyResult> OptionalExtendedPolicyResult;
277
278 #endif // _SRC_ACCESS_CONTROL_COMMON_POLICY_RESULT_H_