add per-user and per-session default_owner. refs #1415
authorTom Clegg <tom@clinicalfuture.com>
Wed, 20 Mar 2013 00:18:03 +0000 (17:18 -0700)
committerTom Clegg <tom@clinicalfuture.com>
Wed, 20 Mar 2013 00:18:03 +0000 (17:18 -0700)
app/controllers/application_controller.rb
app/models/orvos_model.rb
db/migrate/20130319235957_add_default_owner_to_users.rb [new file with mode: 0644]
db/migrate/20130320000107_add_default_owner_to_api_client_authorizations.rb [new file with mode: 0644]
db/schema.rb
lib/current_api_client.rb

index 3d96706d3b16d9737e7e3e6e8eff3fb36028ca73..86bebcd103d1da8a00f9540f3c7f12e9c73f2504 100644 (file)
@@ -168,6 +168,7 @@ class ApplicationController < ActionController::Base
         if api_client_auth
           session[:user_id] = api_client_auth.user.id
           session[:api_client_uuid] = api_client_auth.api_client.uuid
+          session[:api_client_authorization_id] = api_client_auth.id
           user = api_client_auth.user
           api_client = api_client_auth.api_client
         end
@@ -176,16 +177,22 @@ class ApplicationController < ActionController::Base
         api_client = ApiClient.
           where('uuid=?',session[:api_client_uuid]).
           first rescue nil
+        api_client_auth = ApiClientAuthorization.
+          find session[:api_client_authorization_id]
       end
       Thread.current[:api_client_trusted] = session[:api_client_trusted]
       Thread.current[:api_client_ip_address] = remote_ip
+      Thread.current[:api_client_authorization] = api_client_auth
+      Thread.current[:api_client_uuid] = api_client.uuid
       Thread.current[:api_client] = api_client
       Thread.current[:user] = user
       yield
     ensure
       Thread.current[:api_client_trusted] = nil
       Thread.current[:api_client_ip_address] = nil
+      Thread.current[:api_client_authorization] = nil
       Thread.current[:api_client_uuid] = nil
+      Thread.current[:api_client] = nil
       Thread.current[:user] = nil
     end
   end
index 262d755426e8d8a7bd52dc75cffd90f6e5d05149..adaaf35c8e02c3e9ee4505bbea67f4c041f822bc 100644 (file)
@@ -83,7 +83,7 @@ class OrvosModel < ActiveRecord::Base
 
   def update_modified_by_fields
     self.created_at ||= Time.now
-    self.owner ||= current_user.uuid if current_user
+    self.owner ||= current_default_owner
     self.modified_at = Time.now
     self.modified_by_user = current_user ? current_user.uuid : nil
     self.modified_by_client = current_api_client ? current_api_client.uuid : nil
diff --git a/db/migrate/20130319235957_add_default_owner_to_users.rb b/db/migrate/20130319235957_add_default_owner_to_users.rb
new file mode 100644 (file)
index 0000000..712267a
--- /dev/null
@@ -0,0 +1,5 @@
+class AddDefaultOwnerToUsers < ActiveRecord::Migration
+  def change
+    add_column :users, :default_owner, :string
+  end
+end
diff --git a/db/migrate/20130320000107_add_default_owner_to_api_client_authorizations.rb b/db/migrate/20130320000107_add_default_owner_to_api_client_authorizations.rb
new file mode 100644 (file)
index 0000000..43577bc
--- /dev/null
@@ -0,0 +1,5 @@
+class AddDefaultOwnerToApiClientAuthorizations < ActiveRecord::Migration
+  def change
+    add_column :api_client_authorizations, :default_owner, :string
+  end
+end
index 488925669049a7db08fbf7e4182b62f5d2c46abf..dc4f210e3ef1eb62afe10961102a2d7adae98e3c 100644 (file)
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended to check this file into your version control system.
 
-ActiveRecord::Schema.define(:version => 20130319201431) do
+ActiveRecord::Schema.define(:version => 20130320000107) do
 
   create_table "api_client_authorizations", :force => true do |t|
     t.string   "api_token",               :null => false
@@ -23,6 +23,7 @@ ActiveRecord::Schema.define(:version => 20130319201431) do
     t.datetime "expires_at"
     t.datetime "created_at"
     t.datetime "updated_at"
+    t.string   "default_owner"
   end
 
   add_index "api_client_authorizations", ["api_client_id"], :name => "index_api_client_authorizations_on_api_client_id"
@@ -282,6 +283,7 @@ ActiveRecord::Schema.define(:version => 20130319201431) do
     t.boolean  "is_admin"
     t.text     "prefs"
     t.datetime "updated_at"
+    t.string   "default_owner"
   end
 
   add_index "users", ["created_at"], :name => "index_users_on_created_at"
index bf62b7f68915fb1ec4d8d2d00779af08b660b115..0e578a39c56014b0123472d911e405581bd8f7cc 100644 (file)
@@ -7,6 +7,22 @@ module CurrentApiClient
     Thread.current[:api_client]
   end
 
+  def current_api_client_authorization
+    Thread.current[:api_client_authorization]
+  end
+
+  def current_default_owner
+    # owner uuid for newly created objects
+    ((current_api_client_authorization &&
+      current_api_client_authorization.default_owner)
+     ||
+     (current_user && current_user.default_owner)
+     ||
+     (current_user && current_user.uuid)
+     ||
+     nil)
+  end
+
   # Where is the client connecting from?
   def current_api_client_ip_address
     Thread.current[:api_client_ip_address]