From: Lukasz Wojciechowski Date: Sat, 29 Nov 2014 23:46:28 +0000 (+0100) Subject: Implement listing Policies in Storage X-Git-Tag: accepted/tizen/common/20150119.084431~71 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=df9ef16e6f017b9aa5b3ce4952324232b23a5900;p=platform%2Fcore%2Fsecurity%2Fcynara.git Implement listing Policies in Storage Implement listPolicies() in Storage and InMemoryStorageBackend. UnitTests for Storage::listPolicies() were added. Change-Id: I113c3c0f9b5c1d1d5cbed44e3d23d5d7e489a227 --- diff --git a/src/common/types/PolicyBucket.cpp b/src/common/types/PolicyBucket.cpp index 622bc05..052ac35 100644 --- a/src/common/types/PolicyBucket.cpp +++ b/src/common/types/PolicyBucket.cpp @@ -89,6 +89,16 @@ void PolicyBucket::deletePolicy(std::function predicate) { } } +PolicyBucket::Policies PolicyBucket::listPolicies(const PolicyKey &filter) const { + PolicyBucket::Policies policies; + for (auto iter = m_policyCollection.begin(); iter != m_policyCollection.end(); ++iter) { + auto &policyPtr = iter->second; + if (policyPtr->key().matchFilter(filter)) + policies.push_back(*policyPtr); + } + return policies; +} + PolicyMap PolicyBucket::makePolicyMap(const PolicyCollection &policies) { PolicyMap result; for (const auto &policy : policies) { diff --git a/src/common/types/PolicyBucket.h b/src/common/types/PolicyBucket.h index df57d0f..262c7d3 100644 --- a/src/common/types/PolicyBucket.h +++ b/src/common/types/PolicyBucket.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -47,6 +48,7 @@ public: typedef PolicyCollection::value_type value_type; typedef const_policy_iterator const_iterator; + typedef std::vector Policies; // TODO: Review usefulness of ctors //delete default constructor in order to prevent creation of buckets with no id @@ -62,6 +64,7 @@ public: PolicyBucket filtered(const PolicyKey &key) const; void insertPolicy(PolicyPtr policy); void deletePolicy(const PolicyKey &key); + Policies listPolicies(const PolicyKey &filter) const; // TODO: Try to change interface, so this method is not needed void deletePolicy(std::function predicate); diff --git a/src/storage/InMemoryStorageBackend.cpp b/src/storage/InMemoryStorageBackend.cpp index a2366e8..bc3d01a 100644 --- a/src/storage/InMemoryStorageBackend.cpp +++ b/src/storage/InMemoryStorageBackend.cpp @@ -201,6 +201,16 @@ void InMemoryStorageBackend::deleteLinking(const PolicyBucketId &bucketId) { } } +PolicyBucket::Policies InMemoryStorageBackend::listPolicies(const PolicyBucketId &bucketId, + const PolicyKey &filter) const { + try { + auto &bucket = buckets().at(bucketId); + return bucket.listPolicies(filter); + } catch (const std::out_of_range &) { + throw BucketNotExistsException(bucketId); + } +} + void InMemoryStorageBackend::openFileStream(std::shared_ptr stream, const std::string &filename) { // TODO: Consider adding exceptions to streams and handling them: diff --git a/src/storage/InMemoryStorageBackend.h b/src/storage/InMemoryStorageBackend.h index d03dd6c..07f7fcd 100644 --- a/src/storage/InMemoryStorageBackend.h +++ b/src/storage/InMemoryStorageBackend.h @@ -58,6 +58,8 @@ public: virtual bool hasBucket(const PolicyBucketId &bucketId); virtual void deletePolicy(const PolicyBucketId &bucketId, const PolicyKey &key); virtual void deleteLinking(const PolicyBucketId &bucketId); + virtual PolicyBucket::Policies listPolicies(const PolicyBucketId &bucketId, + const PolicyKey &filter) const; protected: InMemoryStorageBackend() {} @@ -80,6 +82,9 @@ protected: virtual Buckets &buckets(void) { return m_buckets; } + virtual const Buckets &buckets(void) const { + return m_buckets; + } }; } /* namespace Cynara */ diff --git a/src/storage/Storage.cpp b/src/storage/Storage.cpp index aa9bc93..fafb374 100644 --- a/src/storage/Storage.cpp +++ b/src/storage/Storage.cpp @@ -154,6 +154,11 @@ void Storage::deletePolicies(const std::map