Implement Logic::check() method using SocketClient
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Mon, 30 Jun 2014 17:59:07 +0000 (19:59 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 3 Jul 2014 12:19:10 +0000 (14:19 +0200)
Implementation always checks privilege by sending request to Cynara
service.
Features not implemented:
* No cache is used;
* Session parameter is unused;
* No plugin support is implemented.

Change-Id: Ifdfdd56cceb967a8490f383ae396c5066e8a97fd

src/client/logic/Logic.cpp
src/client/logic/Logic.h
src/common/exceptions/UnexpectedErrorException.h

index cebfb28..4a46c33 100644 (file)
  * @brief       This file contains implementation of Logic class - main libcynara-client class
  */
 
+#include <memory>
+
 #include <common.h>
+#include <exceptions/ServerConnectionErrorException.h>
+#include <exceptions/UnexpectedErrorException.h>
+#include <log/log.h>
+#include <request/CheckRequest.h>
+#include <request/pointers.h>
+#include <response/CheckResponse.h>
+#include <response/pointers.h>
+#include <types/PolicyKey.h>
+#include <types/PolicyResult.h>
 
+#include <sockets/SocketClient.h>
 #include "Logic.h"
 
 namespace Cynara {
 
-cynara_api_result Logic::check(const std::string &client UNUSED, const std::string &session UNUSED,
-                               const std::string &user UNUSED, const std::string &privilege UNUSED)
+Logic::Logic() {
+    m_socketClient = std::make_shared<SocketClient>();
+}
+
+cynara_api_result Logic::check(const std::string &client, const std::string &session UNUSED,
+                               const std::string &user, const std::string &privilege) noexcept
 {
-    //todo - this is a stub
-    return cynara_api_result::CYNARA_API_ACCESS_DENIED;
+    //todo Handle session parameter.
+    //todo Check if answer can be get from cache. Update cache.
+
+    //Ask cynara service
+    PolicyResult result(PredefinedPolicyType::DENY);
+    try {
+        RequestPtr request = std::make_shared<CheckRequest>(PolicyKey(client, user, privilege));
+        ResponsePtr response = m_socketClient->askCynaraServer(request);
+        if (!response) {
+            LOGW("Disconnected by cynara server.");
+            onDisconnected();
+            return cynara_api_result::CYNARA_API_SERVICE_NOT_AVAILABLE;
+        }
+        CheckResponsePtr checkResponse = std::dynamic_pointer_cast<CheckResponse>(response);
+        if (!checkResponse) {
+            LOGC("Critical error. Casting Response to CheckResponse failed.");
+            throw UnexpectedErrorException("Error casting Response to CheckResponse");
+        }
+        result = checkResponse->m_resultRef;
+    } catch (const ServerConnectionErrorException &ex) {
+        LOGE("Cynara service not available.");
+        onDisconnected();
+        return cynara_api_result::CYNARA_API_SERVICE_NOT_AVAILABLE;
+    } catch (const std::exception &ex) {
+        LOGE("Error during check of privilege: %s", ex.what());
+        return cynara_api_result::CYNARA_API_ACCESS_DENIED;
+    }
+
+    //todo Interprete result.
+    //todo Update cache.
+
+    //todo return result after more detailed interpretation.
+    if (result.policyType() == PredefinedPolicyType::ALLOW)
+        return cynara_api_result::CYNARA_API_SUCCESS;
+    else
+        return cynara_api_result::CYNARA_API_ACCESS_DENIED;
+}
+
+void Logic::onDisconnected(void) {
+    //todo run special actions when disconnected from cynara service
+    //     like cleaning cache
 }
 
 } // namespace Cynara
index 445d940..512b313 100644 (file)
 #define SRC_CLIENT_LOGIC_LOGIC_H_
 
 #include <string>
+
 #include <api/ApiInterface.h>
+#include <sockets/SocketClient.h>
 
 namespace Cynara {
 
 class Logic : public ApiInterface {
+private:
+    SocketClientPtr m_socketClient;
+
+    void onDisconnected(void);
+
 public:
-    Logic() = default;
+    Logic();
     virtual ~Logic() = default;
 
     virtual cynara_api_result check(const std::string &client, const std::string &session,
-        const std::string &user, const std::string &privilege);
+        const std::string &user, const std::string &privilege) noexcept;
 };
 
 } // namespace Cynara
index 9d858f4..0b85ad4 100644 (file)
@@ -42,6 +42,12 @@ public:
         stream << errorMsg << ">";
         m_whatMessage = stream.str();
     }
+    UnexpectedErrorException(const char *errorMsg) {
+        std::ostringstream stream;
+        stream << "UnexpectedErrorException with message <";
+        stream << errorMsg << ">";
+        m_whatMessage = stream.str();
+    }
 
     virtual ~UnexpectedErrorException() = default;