Use current_user to select API response template. fixes #1594
authorTom Clegg <tom@clinicalfuture.com>
Thu, 7 Nov 2013 17:15:01 +0000 (09:15 -0800)
committerTom Clegg <tom@clinicalfuture.com>
Thu, 7 Nov 2013 17:50:11 +0000 (09:50 -0800)
24 files changed:
services/api/app/controllers/application_controller.rb
services/api/app/controllers/arvados/v1/nodes_controller.rb
services/api/app/models/api_client.rb
services/api/app/models/api_client_authorization.rb
services/api/app/models/authorized_key.rb
services/api/app/models/collection.rb
services/api/app/models/group.rb
services/api/app/models/human.rb
services/api/app/models/job.rb
services/api/app/models/job_task.rb
services/api/app/models/keep_disk.rb
services/api/app/models/link.rb
services/api/app/models/log.rb
services/api/app/models/node.rb
services/api/app/models/pipeline_instance.rb
services/api/app/models/pipeline_template.rb
services/api/app/models/repository.rb
services/api/app/models/specimen.rb
services/api/app/models/trait.rb
services/api/app/models/user.rb
services/api/app/models/virtual_machine.rb
services/api/lib/common_api_template.rb
services/api/test/fixtures/nodes.yml [new file with mode: 0644]
services/api/test/functional/arvados/v1/nodes_controller_test.rb

index 9878547f30002fd7d319e3e40fb787b5228918bf..2f07c394609a7664abec7a88630fd48c6b308c83 100644 (file)
@@ -24,7 +24,7 @@ class ApplicationController < ActionController::Base
 
   def show
     if @object
-      render json: @object.as_api_response(:superuser)
+      render json: @object.as_api_response
     else
       render_not_found("object not found")
     end
@@ -332,7 +332,7 @@ class ApplicationController < ActionController::Base
       :self_link => "",
       :next_page_token => "",
       :next_link => "",
-      :items => @objects.as_api_response(:superuser)
+      :items => @objects.as_api_response(nil)
     }
     render json: @object_list
   end
index 1f7c1b085b9e5a93e20a6fbea3f81952989f6192..33c7ea7e240102ffca6cec91cb5c934d4eeb5d7c 100644 (file)
@@ -17,4 +17,13 @@ class Arvados::V1::NodesController < ApplicationController
                    ec2_instance_id: params[:instance_id] })
     show
   end
+
+  def index
+    if current_user.andand.is_admin
+      super
+    else
+      @objects = model_class.where('last_ping_at >= ?', Time.now - 1.hours)
+      render_list
+    end
+  end
 end
index 3bda9f3d654b4e3fd5d84fe670dcb22680168c56..3e1dcd6c4a2ccecde1230c3f3b4fcfdb531db05b 100644 (file)
@@ -4,7 +4,7 @@ class ApiClient < ActiveRecord::Base
   include CommonApiTemplate
   has_many :api_client_authorizations
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :name
     t.add :url_prefix
     t.add :is_trusted
index dde15f4428de5ce802725ce0fa0429afc5bc7012..fca57dce8f4d0eb3331c05d442e9fe0fa94f399a 100644 (file)
@@ -7,7 +7,7 @@ class ApiClientAuthorization < ArvadosModel
   after_initialize :assign_random_api_token
   serialize :scopes, Array
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :owner_uuid
     t.add :user_id
     t.add :api_client_id
index d079952beed19178f8bfd99704225e56d417d268..afb33e60f8121229c15d003129a3d4492f250055 100644 (file)
@@ -7,7 +7,7 @@ class AuthorizedKey < ArvadosModel
 
   belongs_to :authorized_user, :foreign_key => :authorized_user_uuid, :class_name => 'User', :primary_key => :uuid
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :name
     t.add :key_type
     t.add :authorized_user_uuid
index 7403b9a15abfab21650f93b3779349cad063a8d1..df84b09dfb49d4050f303a9d3e33571447a11f19 100644 (file)
@@ -3,7 +3,7 @@ class Collection < ArvadosModel
   include KindAndEtag
   include CommonApiTemplate
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :data_size
     t.add :files
   end
index 94a392d5eb172ebc2a59de333bdfeb00c53283d7..9666257ea4875d4bb62d13528bb132c57a51bd14 100644 (file)
@@ -3,7 +3,7 @@ class Group < ArvadosModel
   include KindAndEtag
   include CommonApiTemplate
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :name
     t.add :description
   end
index f031915a8f8710d6661033ec94915074bab2c287..3717f81c8f20faf5c2a867f9e3fc7a30b069d0d8 100644 (file)
@@ -4,7 +4,7 @@ class Human < ArvadosModel
   include CommonApiTemplate
   serialize :properties, Hash
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :properties
   end
 end
index 154e2724db2e2aeab745ad2a1219ef9371eb59b2..d34d6df6b6fd35c9cd974817fa86ff62abf68b5c 100644 (file)
@@ -14,7 +14,7 @@ class Job < ArvadosModel
   class SubmitIdReused < StandardError
   end
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :submit_id
     t.add :priority
     t.add :script
index e2f7930087bbc4e6e131c33fbde9aada5966b7df..7d568e952a6c20eb69781708db5d0627bab11080 100644 (file)
@@ -6,7 +6,7 @@ class JobTask < ArvadosModel
   after_update :delete_created_job_tasks_if_failed
   after_update :assign_created_job_tasks_qsequence_if_succeeded
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :job_uuid
     t.add :created_by_job_task_uuid
     t.add :sequence
index 3913077846e44665a01f85cb5ffee1bd026e27f4..ee285d969af0a9754cfe52f06b9417f9e8662f97 100644 (file)
@@ -4,7 +4,7 @@ class KeepDisk < ArvadosModel
   include CommonApiTemplate
   before_validation :ensure_ping_secret
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :node_uuid
     t.add :filesystem_uuid
     t.add :ping_secret
index af7226b6df2dbc9d20d258ac7ccd306de452a22e..1d4e13d18618eddb14a98aac80aa60f591b5c58a 100644 (file)
@@ -12,7 +12,7 @@ class Link < ArvadosModel
   attr_accessor :head
   attr_accessor :tail
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :tail_kind
     t.add :tail_uuid
     t.add :link_class
index d856455f4dadc5b9ce20d9a91a9d1f09223482fe..29efc9dc1136427e471d63ce3aedd0caaf58d193 100644 (file)
@@ -6,7 +6,7 @@ class Log < ArvadosModel
   before_validation :set_default_event_at
   attr_accessor :object
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :object_kind
     t.add :object_uuid
     t.add :object, :if => :object
index cf1dd06693dee078507e5dd452b09e60c4fa6a23..8e17a8765d4aff9d55aea61602559229e15728ed 100644 (file)
@@ -18,15 +18,17 @@ class Node < ArvadosModel
   @@domain = Rails.configuration.compute_node_domain rescue `hostname --domain`.strip
   @@nameservers = Rails.configuration.compute_node_nameservers
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, :extend => :common do |t|
     t.add :hostname
     t.add :domain
     t.add :ip_address
-    t.add :first_ping_at
     t.add :last_ping_at
-    t.add :info
     t.add :slot_number
     t.add :status
+  end
+  api_accessible :superuser, :extend => :user do |t|
+    t.add :first_ping_at
+    t.add :info
     t.add lambda { |x| @@nameservers }, :as => :nameservers
   end
 
index c73d059661f495bffbd378718dae2143c1400e9e..43497da6f4b4e133865e0492b57e73ce918d2c77 100644 (file)
@@ -10,7 +10,7 @@ class PipelineInstance < ArvadosModel
   before_validation :bootstrap_components
   before_validation :update_success
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :pipeline_template_uuid
     t.add :pipeline_template, :if => :pipeline_template
     t.add :name
index 205ce696faddfe80d0fbf6ec5aa88611af4b21de..3b099ed79403ae2bcef0d9d5580416bd2bc39095 100644 (file)
@@ -4,7 +4,7 @@ class PipelineTemplate < ArvadosModel
   include CommonApiTemplate
   serialize :components, Hash
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :name
     t.add :components
   end
index 47e0ad4f0d3473930a1dc7a920988a3b910e7458..6b027d18899caf8297e7ce8a740c6db9ef965ac6 100644 (file)
@@ -3,7 +3,7 @@ class Repository < ArvadosModel
   include KindAndEtag
   include CommonApiTemplate
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :name
     t.add :fetch_url
     t.add :push_url
index ea97fa012aee74c5c258a1e9ea15cc484c522759..bcfcd7a5f2be49f5de232c6b076e17c38a0744bf 100644 (file)
@@ -4,7 +4,7 @@ class Specimen < ArvadosModel
   include CommonApiTemplate
   serialize :properties, Hash
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :material
     t.add :properties
   end
index d16e7d07a96b55b66b8134aa41a6145548bb1e76..85ab2368a837182cab81ec3db4ede5b166464519 100644 (file)
@@ -4,7 +4,7 @@ class Trait < ArvadosModel
   include CommonApiTemplate
   serialize :properties, Hash
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :name
     t.add :properties
   end
index 2fb3df392698015eb38f139e89fc5baac99f4827..6bb562be8ea260ee1fa7c47cc214e3d010476848 100644 (file)
@@ -10,7 +10,7 @@ class User < ArvadosModel
 
   has_many :authorized_keys, :foreign_key => :authorized_user_uuid, :primary_key => :uuid
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :email
     t.add :full_name
     t.add :first_name
index 9b36ab5ad7c18126543fc90c3dd9816f44023944..d2830cfcc2afd22c8e42970c1f75d8500b747a71 100644 (file)
@@ -5,7 +5,7 @@ class VirtualMachine < ArvadosModel
 
   has_many :login_permissions, :foreign_key => :head_uuid, :class_name => 'Link', :primary_key => :uuid, :conditions => "link_class = 'permission' and name = 'can_login'"
 
-  api_accessible :superuser, :extend => :common do |t|
+  api_accessible :user, extend: :common do |t|
     t.add :hostname
   end
 
index 5426d13c2375fc9560e9ecadb275029068dd85fb..d474581ef369444199eef972f554c00fdf33dac9 100644 (file)
@@ -1,7 +1,11 @@
 module CommonApiTemplate
   def self.included(base)
-    base.extend(ClassMethods)
     base.acts_as_api
+    base.class_eval do
+      alias_method :as_api_response_orig, :as_api_response
+      include InstanceMethods
+    end
+    base.extend(ClassMethods)
     base.api_accessible :common do |t|
       t.add :href
       t.add :kind
@@ -16,6 +20,21 @@ module CommonApiTemplate
     end
   end
 
+  module InstanceMethods
+    # choose template based on opts[:for_user]
+    def as_api_response(template=nil, opts={})
+      if template.nil?
+        user = opts[:for_user] || current_user
+        if user.is_admin and self.respond_to? :api_accessible_superuser
+          template = :superuser
+        else
+          template = :user
+        end
+      end
+      self.as_api_response_orig(template, opts)
+    end
+  end
+
   module ClassMethods
   end
 end
diff --git a/services/api/test/fixtures/nodes.yml b/services/api/test/fixtures/nodes.yml
new file mode 100644 (file)
index 0000000..780c3db
--- /dev/null
@@ -0,0 +1,19 @@
+running:
+  uuid: zzzzz-7ekkf-53y36l1lu5ijveb
+  owner_uuid: zzzzz-tpzed-d9tiejq69daie8f
+  hostname: compute0
+  domain: ""
+  ip_address: 172.17.2.172
+  last_ping_at: <%= 1.minute.ago.to_s(:db) %>
+  first_ping_at: <%= 23.hour.ago.to_s(:db) %>
+  info: {ping_secret: "48dpm3b8ijyj3jkr2yczxw0844dqd2752bhll7klodvgz9bg80"}
+
+down:
+  uuid: zzzzz-7ekkf-2vbompg3ecc6e2s
+  owner_uuid: zzzzz-tpzed-d9tiejq69daie8f
+  hostname: compute1
+  domain: ""
+  ip_address: 172.17.2.173
+  last_ping_at: <%= 1.hour.ago.to_s(:db) %>
+  first_ping_at: <%= 23.hour.ago.to_s(:db) %>
+  info: {ping_secret: "2k3i71depad36ugwmlgzilbi4e8n0illb2r8l4efg9mzkb3a1k"}
index 76915eddc33314a408d953bb0623facffb9fb12e..49d3a30d201188faf9cced3c55890b0482dd5048 100644 (file)
@@ -2,19 +2,24 @@ require 'test_helper'
 
 class Arvados::V1::NodesControllerTest < ActionController::TestCase
 
-  test "should get index" do
+  test "should get index with ping_secret" do
     authorize_with :admin
     get :index
     assert_response :success
     assert_not_nil assigns(:objects)
+    node_items = JSON.parse(@response.body)['items']
+    assert_not_equal 0, node_items.size
+    assert_not_nil node_items[0]['info'].andand['ping_secret']
   end
 
   # inactive user should not see any nodes
-  test "should get empty index" do
+  test "should get index without ping_secret" do
     authorize_with :inactive
     get :index
     assert_response :success
-    assert_equal 0, JSON.parse(@response.body)['items'].size
+    node_items = JSON.parse(@response.body)['items']
+    assert_not_equal 0, node_items.size
+    assert_nil node_items[0]['info'].andand['ping_secret']
   end
 
 end