Simplify service plugin 99/135299/2
authorZofia Abramowska <z.abramowska@samsung.com>
Wed, 21 Jun 2017 16:31:00 +0000 (18:31 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Mon, 26 Jun 2017 14:12:13 +0000 (16:12 +0200)
Service side plugin supports Ask user policy and translates
it to DENY.

Change-Id: If9a8601e18da68d59b116a2b5d8f6f0872748a4e

src/plugin/service/ServicePlugin.cpp

index f557176c42d10be5ae8ea42f0cc45ae3a4aec5ca..e9bbc3aa29ab54f12ada280ad0858e958a29dab3 100644 (file)
 #include <ostream>
 #include <cynara-plugin.h>
 
+#include <attributes/attributes.h>
 #include <types/PolicyDescription.h>
 #include <types/SupportedTypes.h>
-#include <translator/Translator.h>
-
-#include "CapacityCache.h"
 
 using namespace Cynara;
 
-typedef std::tuple<std::string, std::string, std::string> Key;
-std::ostream &operator<<(std::ostream &os, const Key &key) {
-    os << "client: " << std::get<0>(key)
-       << ", user: " << std::get<1>(key)
-       << ", privilege: " << std::get<2>(key);
-    return os;
-}
-
-std::ostream &operator<<(std::ostream &os, const PolicyResult &result) {
-    os << "type: " << result.policyType()
-       << ", metadata: " << result.metadata();
-    return os;
-}
-
 namespace AskUser {
 
-std::function<std::string(const Key&)> hasher = [](const Key &key) {
-    const char separator = '\1';
-    const auto &client = std::get<0>(key);
-    const auto &user = std::get<1>(key);
-    const auto &privilege = std::get<2>(key);
-    return client + user + privilege + separator +
-            std::to_string(client.size()) + separator +
-            std::to_string(user.size()) + separator +
-            std::to_string(privilege.size());
-};
-
 const std::vector<PolicyDescription> serviceDescriptions = {
     { SupportedTypes::Service::ASK_USER, "Ask user" }
 };
 
 class AskUserPlugin : public ServicePluginInterface {
 public:
-    AskUserPlugin()
-        : m_cache(hasher)
-    {}
+    AskUserPlugin() {}
     const std::vector<PolicyDescription> &getSupportedPolicyDescr() {
         return serviceDescriptions;
     }
 
-    PluginStatus check(const std::string &client,
-                       const std::string &user,
-                       const std::string &privilege,
+    PluginStatus check(UNUSED const std::string &client,
+                       UNUSED const std::string &user,
+                       UNUSED const std::string &privilege,
                        PolicyResult &result,
-                       AgentType &requiredAgent,
-                       PluginData &pluginData) noexcept
+                       UNUSED AgentType &requiredAgent,
+                       UNUSED PluginData &pluginData) noexcept
     {
-        try {
-            if (!m_cache.get(Key(client, user, privilege), result)) {
-                pluginData = Translator::Plugin::requestToData(client, user, privilege);
-                requiredAgent = AgentType(SupportedTypes::Agent::AgentType);
-                return PluginStatus::ANSWER_NOTREADY;
-            }
-            if (result.policyType() == SupportedTypes::Client::ALLOW_PER_LIFE)
-                result = PolicyResult(PredefinedPolicyType::ALLOW);
-            else
-                result = PolicyResult(PredefinedPolicyType::DENY);
-            return PluginStatus::ANSWER_READY;
-        } catch (const Translator::TranslateErrorException &e) {
-            LOGE("Error translating request to data : " << e.what());
-        } catch (const std::exception &e) {
-            LOGE("Failed with std exception: " << e.what());
-        } catch (...) {
-            LOGE("Failed with unknown exception: ");
-        }
-        return PluginStatus::ERROR;
+        result = PolicyResult(PredefinedPolicyType::DENY);
+        return PluginStatus::ANSWER_READY;
     }
 
-    PluginStatus update(const std::string &client,
-                        const std::string &user,
-                        const std::string &privilege,
-                        const PluginData &agentData,
+    PluginStatus update(UNUSED const std::string &client,
+                        UNUSED const std::string &user,
+                        UNUSED const std::string &privilege,
+                        UNUSED const PluginData &agentData,
                         PolicyResult &result) noexcept
     {
-        try {
-            PolicyType resultType = Translator::Plugin::dataToAnswer(agentData);
-            result = PolicyResult(resultType);
-
-            if (resultType == SupportedTypes::Client::ALLOW_PER_LIFE) {
-                m_cache.update(Key(client, user, privilege), PolicyResult(resultType));
-                result = PolicyResult(PredefinedPolicyType::ALLOW);
-            } else if (resultType == SupportedTypes::Client::DENY_PER_LIFE) {
-                m_cache.update(Key(client, user, privilege), PolicyResult(resultType));
-                result = PolicyResult(PredefinedPolicyType::DENY);
-            }
-
-            return PluginStatus::SUCCESS;
-        } catch (const Translator::TranslateErrorException &e) {
-            LOGE("Error translating data to answer : " << e.what());
-        } catch (const std::exception &e) {
-            LOGE("Failed with std exception: " << e.what());
-        } catch (...) {
-            LOGE("Failed with unknown exception: ");
-        }
-        return PluginStatus::ERROR;
-    }
-
-    void invalidate() {
-        m_cache.clear();
+        result = PolicyResult(PredefinedPolicyType::DENY);
+        return PluginStatus::SUCCESS;
     }
 
-private:
-    Plugin::CapacityCache<Key, PolicyResult> m_cache;
+    void invalidate() {}
 };
 
 } // namespace AskUser