diff Plugin/CachedAuthorizationService.cpp @ 115:0eed78c1e177

cache the UserProfile + updated http filter logic
author Alain Mazy <am@osimis.io>
date Fri, 08 Sep 2023 09:52:21 +0200
parents 43154740ea2e
children 9be1ee2b8fe1
line wrap: on
line diff
--- a/Plugin/CachedAuthorizationService.cpp	Wed Sep 06 17:02:41 2023 +0200
+++ b/Plugin/CachedAuthorizationService.cpp	Fri Sep 08 09:52:21 2023 +0200
@@ -17,6 +17,7 @@
  **/
 
 #include "CachedAuthorizationService.h"
+#include "AuthorizationWebService.h"
 
 #include <OrthancException.h>
 #include <Toolbox.h>
@@ -112,13 +113,49 @@
   }
 
   
-  bool CachedAuthorizationService::GetUserProfileInternal(unsigned int& validity,
+  bool CachedAuthorizationService::GetUserProfileInternal(unsigned int& validityNotUsed,
                                                           UserProfile& profile /* out */,
                                                           const Token* token,
                                                           const std::string& tokenValue)
   {
-    // no cache used when retrieving the full user profile
-    return decorated_->GetUserProfileInternal(validity, profile, token, tokenValue);
+    assert(decorated_.get() != NULL);
+
+    std::string key = ComputeKey("user-profile", token, tokenValue);
+    std::string serializedProfile;
+
+    if (cache_->Retrieve(serializedProfile, key))
+    {
+      // Return the previously cached profile
+      Json::Value jsonProfile;
+      
+      Orthanc::Toolbox::ReadJson(jsonProfile, serializedProfile);
+      
+      AuthorizationWebService::FromJson(profile, jsonProfile);
+
+      profile.tokenKey = token->GetKey();
+      profile.tokenType = token->GetType();
+      profile.tokenValue = tokenValue;
+
+      return true;
+    }        
+    else
+    {
+      unsigned int validity;
+
+      if (decorated_->GetUserProfileInternal(validity, profile, token, tokenValue))
+      {
+        Json::Value jsonProfile;
+
+        AuthorizationWebService::ToJson(jsonProfile, profile);
+        Orthanc::Toolbox::WriteFastJson(serializedProfile, jsonProfile);
+
+        cache_->Store(key, serializedProfile, validity);
+        
+        return true;
+      }
+    }
+
+    return false;
   }
 
   bool CachedAuthorizationService::HasUserPermissionInternal(unsigned int& validity,