Add Cynara::PolicyKeyHelpers 20/25220/4
authorAleksander Zdyb <a.zdyb@partner.samsung.com>
Thu, 31 Jul 2014 12:35:39 +0000 (14:35 +0200)
committerAleksander Zdyb <a.zdyb@partner.samsung.com>
Thu, 31 Jul 2014 12:35:39 +0000 (14:35 +0200)
PolicyKeyHelpers is an utility class holding
helper functions to manage policy keys.

Change-Id: I2f9195d62fc22809989890b0914f385b1c05d0b6

src/common/types/PolicyKeyHelpers.cpp [new file with mode: 0644]
src/common/types/PolicyKeyHelpers.h [new file with mode: 0644]

diff --git a/src/common/types/PolicyKeyHelpers.cpp b/src/common/types/PolicyKeyHelpers.cpp
new file mode 100644 (file)
index 0000000..4d7efc6
--- /dev/null
@@ -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 <a.zdyb@partner.samsung.com>
+ * @version     1.0
+ * @brief       Helper functions to manage Cynara::PolicyKey
+ */
+
+#include <sstream>
+
+#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<std::string> 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 (file)
index 0000000..4c4dc7f
--- /dev/null
@@ -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 <a.zdyb@partner.samsung.com>
+ * @version     1.0
+ * @brief       Helper functions to manage Cynara::PolicyKey
+ */
+#ifndef SRC_COMMON_TYPES_POLICYKEYHELPERS_H_
+#define SRC_COMMON_TYPES_POLICYKEYHELPERS_H_
+
+#include <string>
+#include <vector>
+
+#include <types/PolicyKey.h>
+
+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<std::string> keyVariants(const PolicyKey &key);
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_TYPES_POLICYKEYHELPERS_H_ */