Add start point in Storage::checkPolicy()
[platform/core/security/cynara.git] / src / common / types / PolicyBucket.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        PolicyBucket.h
18  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
19  * @author      Aleksander Zdyb <a.zdyb@partner.samsung.com>
20  * @version     1.0
21  * @brief       This file defines PolicyBucket type - a policy aggregation
22                 entity name
23  */
24
25 #ifndef SRC_COMMON_TYPES_POLICYBUCKET_H_
26 #define SRC_COMMON_TYPES_POLICYBUCKET_H_
27
28 #include <algorithm>
29 #include <memory>
30 #include <string>
31
32 #include <exceptions/NotImplementedException.h>
33 #include <types/pointers.h>
34 #include <types/Policy.h>
35 #include <types/PolicyBucketId.h>
36 #include <types/PolicyCollection.h>
37 #include <types/PolicyKey.h>
38 #include <types/PolicyType.h>
39
40 namespace Cynara {
41
42 // TODO: Move this const somewhere else
43 const PolicyBucketId defaultPolicyBucketId("");
44
45 class PolicyBucket {
46 public:
47
48     typedef PolicyCollection::value_type value_type;
49     typedef const_policy_iterator const_iterator;
50
51     // TODO: Review usefulness of ctors
52     PolicyBucket() : m_defaultPolicy(PredefinedPolicyType::DENY) {}
53     PolicyBucket(const PolicyBucketId &id, const PolicyResult &defaultPolicy)
54         : m_defaultPolicy(defaultPolicy), m_id(id) {}
55     PolicyBucket(const PolicyCollection &policies)
56         : m_policyCollection(makePolicyMap(policies)),
57           m_defaultPolicy(PredefinedPolicyType::DENY) {}
58     PolicyBucket(const PolicyBucketId &id,
59                  const PolicyResult &defaultPolicy,
60                  const PolicyCollection &policies)
61         : m_policyCollection(makePolicyMap(policies)),
62           m_defaultPolicy(defaultPolicy),
63           m_id(id) {}
64
65     PolicyBucket filtered(const PolicyKey &key) const;
66     void insertPolicy(PolicyPtr policy);
67     void deletePolicy(const PolicyKey &key);
68
69     // TODO: Try to change interface, so this method is not needed
70     void deletePolicy(std::function<bool(PolicyPtr)> predicate);
71
72     static PolicyMap makePolicyMap(const PolicyCollection &policies);
73
74 private:
75     PolicyMap m_policyCollection;
76     PolicyResult m_defaultPolicy;
77     PolicyBucketId m_id;
78
79 public:
80
81     const_policy_iterator begin(void) const {
82         return const_policy_iterator(m_policyCollection.begin());
83     }
84
85     const_policy_iterator end(void) const {
86         return const_policy_iterator(m_policyCollection.end());
87     }
88
89     PolicyMap::size_type size(void) const noexcept {
90         return m_policyCollection.size();
91     }
92
93     bool empty(void) const noexcept {
94         return m_policyCollection.empty();
95     }
96
97     const PolicyResult &defaultPolicy(void) const {
98         return m_defaultPolicy;
99     }
100
101     const PolicyBucketId &id(void) const {
102         return m_id;
103     }
104
105     // TODO: Consider StorageBackend to be only one to alter this property
106     void setDefaultPolicy(const PolicyResult &defaultPolicy) {
107         m_defaultPolicy = defaultPolicy;
108     }
109 };
110
111 } /* namespace Cynara */
112 #endif /* SRC_COMMON_TYPES_POLICYBUCKET_H_ */