2 * Copyright (c) 2011 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 #ifndef _SRC_ACCESS_CONTROL_COMMON_POLICY_RESULT_H_
18 #define _SRC_ACCESS_CONTROL_COMMON_POLICY_RESULT_H_
20 #include <dpl/assert.h>
21 #include <dpl/optional.h>
22 #include <dpl/optional_typedefs.h>
24 #include <ace/PolicyEffect.h>
26 typedef DPL::Optional<PolicyEffect> OptionalPolicyEffect;
31 enum Value { NOT_APPLICABLE = -1 };
33 PolicyDecision(PolicyEffect effect)
34 : m_isPolicyEffect(true)
38 PolicyDecision(const PolicyDecision &decision)
39 : m_isPolicyEffect(decision.m_isPolicyEffect)
40 , m_effect(decision.m_effect)
44 : m_isPolicyEffect(false)
47 bool operator==(const PolicyDecision &decision) const {
48 return (m_isPolicyEffect
49 && decision.m_isPolicyEffect
50 && m_effect == decision.m_effect)
51 || (!m_isPolicyEffect && !decision.m_isPolicyEffect);
54 bool operator==(Value) const {
55 return !m_isPolicyEffect;
58 bool operator!=(const PolicyDecision &decision) const {
59 return !(*this == decision);
62 bool operator!=(Value value) const {
63 return !(*this == value);
66 OptionalPolicyEffect getEffect() const
68 if (!m_isPolicyEffect) {
69 return OptionalPolicyEffect();
71 return OptionalPolicyEffect(m_effect);
74 std::ostream & toStream(std::ostream& stream) {
78 stream << "NOT-APPLICABLE";
83 bool m_isPolicyEffect;
84 PolicyEffect m_effect;
87 inline static bool operator==(PolicyEffect e, const PolicyDecision &d) {
88 return d.operator==(e);
91 inline static bool operator!=(PolicyEffect e, const PolicyDecision &d) {
95 inline static std::ostream & operator<<(std::ostream& stream,
96 PolicyDecision decision)
98 return decision.toStream(stream);
103 enum Value { UNDETERMINED = -2 };
105 // This constructor is required by dpl controller and by dpl optional
107 : m_isDecision(false)
108 , m_decision(PolicyDecision::Value::NOT_APPLICABLE) // don't care
111 PolicyResult(PolicyEffect effect)
116 PolicyResult(const PolicyDecision &decision)
118 , m_decision(decision)
121 PolicyResult(const PolicyResult &result)
122 : m_isDecision(result.m_isDecision)
123 , m_decision(result.m_decision)
126 PolicyResult(PolicyDecision::Value value)
132 : m_isDecision(false)
133 , m_decision(PolicyDecision::Value::NOT_APPLICABLE) // don't care
136 bool operator==(const PolicyResult &result) const {
138 && result.m_isDecision
139 && m_decision == result.m_decision)
140 || (!m_isDecision && !result.m_isDecision);
143 bool operator==(Value) const {
144 return !m_isDecision;
147 bool operator!=(const PolicyResult &result) const {
148 return !(*this == result);
151 bool operator!=(Value value) const {
152 return !(*this == value);
155 OptionalPolicyEffect getEffect() const
158 return OptionalPolicyEffect();
160 return m_decision.getEffect();
163 static int serialize(const PolicyResult &policyResult)
165 if (!policyResult.m_isDecision) {
166 return BD_UNDETERMINED;
167 } else if (policyResult.m_decision ==
168 PolicyDecision::Value::NOT_APPLICABLE)
170 return BD_NOT_APPLICABLE;
171 } else if (policyResult.m_decision == PolicyEffect::PROMPT_BLANKET) {
172 return BD_PROMPT_BLANKET;
173 } else if (policyResult.m_decision == PolicyEffect::PROMPT_SESSION) {
174 return BD_PROMPT_SESSION;
175 } else if (policyResult.m_decision == PolicyEffect::PROMPT_ONESHOT) {
176 return BD_PROMPT_ONESHOT;
177 } else if (policyResult.m_decision == PolicyEffect::PERMIT) {
179 } else if (policyResult.m_decision == PolicyEffect::DENY) {
182 Assert(false && "Unknown value of policyResult.");
185 static PolicyResult deserialize(int dec){
188 return PolicyEffect::DENY;
190 return PolicyEffect::PERMIT;
191 case BD_PROMPT_ONESHOT:
192 return PolicyEffect::PROMPT_ONESHOT;
193 case BD_PROMPT_SESSION:
194 return PolicyEffect::PROMPT_SESSION;
195 case BD_PROMPT_BLANKET:
196 return PolicyEffect::PROMPT_BLANKET;
197 case BD_NOT_APPLICABLE:
198 return PolicyDecision::Value::NOT_APPLICABLE;
199 case BD_UNDETERMINED:
200 return Value::UNDETERMINED;
202 Assert(false && "Broken database");
205 std::ostream & toStream(std::ostream& stream) {
207 stream << m_decision;
209 stream << "UNDETERMINED";
214 static const int BD_UNDETERMINED = 6;
215 static const int BD_NOT_APPLICABLE = 5;
216 static const int BD_PROMPT_BLANKET = 4;
217 static const int BD_PROMPT_SESSION = 3;
218 static const int BD_PROMPT_ONESHOT = 2;
219 static const int BD_PERMIT = 1;
220 static const int BD_DENY = 0;
223 PolicyDecision m_decision;
226 inline static bool operator==(const PolicyDecision &d, const PolicyResult &r) {
230 inline static bool operator!=(const PolicyDecision &d, const PolicyResult &r) {
234 inline static bool operator==(const PolicyEffect &e, const PolicyResult &r) {
238 inline static bool operator!=(const PolicyEffect &e, const PolicyResult &r) {
242 inline static std::ostream & operator<<(std::ostream& stream,
245 return result.toStream(stream);
248 struct ExtendedPolicyResult {
249 ExtendedPolicyResult(const PolicyResult pr = PolicyEffect::DENY, int rule = -1)
253 PolicyResult policyResult;
257 typedef DPL::Optional<ExtendedPolicyResult> OptionalExtendedPolicyResult;
259 #endif // _SRC_ACCESS_CONTROL_COMMON_POLICY_RESULT_H_