changeset 272:337c506461d2

protection from rest api
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 07 Dec 2012 18:21:04 +0100
parents 98d78841066a
children d384af918264
files OrthancServer/OrthancRestApi.cpp OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h
diffstat 3 files changed, 83 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi.cpp	Fri Dec 07 15:03:31 2012 +0100
+++ b/OrthancServer/OrthancRestApi.cpp	Fri Dec 07 18:21:04 2012 +0100
@@ -607,6 +607,40 @@
   }
 
   
+  // Get information about a single patient -----------------------------------
+ 
+  static void IsProtectedPatient(RestApi::GetCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+    std::string publicId = call.GetUriComponent("id", "");
+    bool isProtected = context.GetIndex().IsProtectedPatient(publicId);
+    call.GetOutput().AnswerBuffer(isProtected ? "1" : "0", "text/plain");
+  }
+
+
+  static void SetPatientProtection(RestApi::PutCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+    std::string publicId = call.GetUriComponent("id", "");
+    std::string s = Toolbox::StripSpaces(call.GetPutBody());
+
+    if (s == "0")
+    {
+      context.GetIndex().SetProtectedPatient(publicId, false);
+      call.GetOutput().AnswerBuffer("", "text/plain");
+    }
+    else if (s == "1")
+    {
+      context.GetIndex().SetProtectedPatient(publicId, true);
+      call.GetOutput().AnswerBuffer("", "text/plain");
+    }
+    else
+    {
+      // Bad request
+    }
+  }
+
+
   // Get information about a single instance ----------------------------------
  
   static void GetInstanceFile(RestApi::GetCall& call)
@@ -845,6 +879,8 @@
     Register("/studies/{id}/archive", GetArchive<ResourceType_Study>);
     Register("/series/{id}/archive", GetArchive<ResourceType_Series>);
 
+    Register("/patients/{id}/protected", IsProtectedPatient);
+    Register("/patients/{id}/protected", SetPatientProtection);
     Register("/instances/{id}/file", GetInstanceFile);
     Register("/instances/{id}/tags", GetInstanceTags<false>);
     Register("/instances/{id}/simplified-tags", GetInstanceTags<true>);
--- a/OrthancServer/ServerIndex.cpp	Fri Dec 07 15:03:31 2012 +0100
+++ b/OrthancServer/ServerIndex.cpp	Fri Dec 07 18:21:04 2012 +0100
@@ -868,7 +868,7 @@
     StandaloneRecycling();
   }
 
-  void  ServerIndex::StandaloneRecycling()
+  void ServerIndex::StandaloneRecycling()
   {
     // WARNING: No mutex here, do not include this as a public method
     std::auto_ptr<SQLite::Transaction> t(db_->StartTransaction());
@@ -877,4 +877,46 @@
     t->Commit();
     listener_->CommitFilesToRemove();
   }
+
+
+  bool ServerIndex::IsProtectedPatient(const std::string& publicId)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+
+    // Lookup for the requested resource
+    int64_t id;
+    ResourceType type;
+    if (!db_->LookupResource(publicId, id, type) ||
+        type != ResourceType_Patient)
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+
+    return db_->IsProtectedPatient(id);
+  }
+     
+
+  void ServerIndex::SetProtectedPatient(const std::string& publicId,
+                                        bool isProtected)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+
+    // Lookup for the requested resource
+    int64_t id;
+    ResourceType type;
+    if (!db_->LookupResource(publicId, id, type) ||
+        type != ResourceType_Patient)
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+
+    // No need for a SQLite::Transaction here, as we only make 1 write to the DB
+    db_->SetProtectedPatient(id, isProtected);
+
+    if (isProtected)
+      LOG(INFO) << "Patient " << publicId << " has been protected";
+    else
+      LOG(INFO) << "Patient " << publicId << " has been unprotected";
+  }
+
 }
--- a/OrthancServer/ServerIndex.h	Fri Dec 07 15:03:31 2012 +0100
+++ b/OrthancServer/ServerIndex.h	Fri Dec 07 18:21:04 2012 +0100
@@ -137,5 +137,9 @@
 
     bool GetLastExportedResource(Json::Value& target);
 
+    bool IsProtectedPatient(const std::string& publicId);
+
+    void SetProtectedPatient(const std::string& publicId,
+                             bool isProtected);
   };
 }