From 3f6c825b3e9a0e69034ce4a608d6407075963f6e Mon Sep 17 00:00:00 2001 From: Aleksander Zdyb Date: Thu, 31 Jul 2014 14:35:39 +0200 Subject: [PATCH] Add Cynara::PolicyKeyHelpers PolicyKeyHelpers is an utility class holding helper functions to manage policy keys. Change-Id: I2f9195d62fc22809989890b0914f385b1c05d0b6 --- src/common/types/PolicyKeyHelpers.cpp | 64 +++++++++++++++++++++++++++++++++++ src/common/types/PolicyKeyHelpers.h | 42 +++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/common/types/PolicyKeyHelpers.cpp create mode 100644 src/common/types/PolicyKeyHelpers.h diff --git a/src/common/types/PolicyKeyHelpers.cpp b/src/common/types/PolicyKeyHelpers.cpp new file mode 100644 index 0000000..4d7efc6 --- /dev/null +++ b/src/common/types/PolicyKeyHelpers.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file PolicyKeyHelpers.cpp + * @author Aleksander Zdyb + * @version 1.0 + * @brief Helper functions to manage Cynara::PolicyKey + */ + +#include + +#include "PolicyKeyHelpers.h" + +namespace Cynara { + +std::string PolicyKeyHelpers::glueKey(const PolicyKey &key) { + return glueKey(key.client(), key.user(), key.privilege()); +} + +std::string PolicyKeyHelpers::glueKey(const PolicyKeyFeature &client, + const PolicyKeyFeature &user, + const PolicyKeyFeature &privilege) { + const std::string sep = ";"; + const std::string c = client.toString(); + const std::string u = user.toString(); + const std::string p = privilege.toString(); + + std::ostringstream glued; + glued << c << sep << u << sep << p << sep << c.size() << sep << u.size() << sep << p.size(); + return glued.str(); +}; + +std::vector PolicyKeyHelpers::keyVariants(const PolicyKey &key) { + const auto &client = key.client(); + const auto &user = key.user(); + const auto &privilege = key.privilege(); + const auto w = PolicyKeyFeature::createWildcard(); + + return { + glueKey(client, user, privilege), + glueKey(w, user, privilege), + glueKey(client, w, privilege), + glueKey(client, user, w), + glueKey(w, w, privilege), + glueKey(w, user, w), + glueKey(client, w, w), + glueKey(w, w, w), + }; +} + +} /* namespace Cynara */ diff --git a/src/common/types/PolicyKeyHelpers.h b/src/common/types/PolicyKeyHelpers.h new file mode 100644 index 0000000..4c4dc7f --- /dev/null +++ b/src/common/types/PolicyKeyHelpers.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file PolicyKeyHelpers.h + * @author Aleksander Zdyb + * @version 1.0 + * @brief Helper functions to manage Cynara::PolicyKey + */ +#ifndef SRC_COMMON_TYPES_POLICYKEYHELPERS_H_ +#define SRC_COMMON_TYPES_POLICYKEYHELPERS_H_ + +#include +#include + +#include + +namespace Cynara { + +class PolicyKeyHelpers { +public: + static std::string glueKey(const PolicyKey &client); + static std::string glueKey(const PolicyKeyFeature &client, const PolicyKeyFeature &user, + const PolicyKeyFeature &privilege); + static std::vector keyVariants(const PolicyKey &key); +}; + +} /* namespace Cynara */ + +#endif /* SRC_COMMON_TYPES_POLICYKEYHELPERS_H_ */ -- 2.7.4