Merge branch '2640-folder-api' into 1970-folder-view
[arvados.git] / services / api / app / models / api_client_authorization.rb
index e90d7980cd682192281f921fdab0b4ee2a7075eb..2488d8322fbc97dfb9d3ee3446bf902bf581a9ce 100644 (file)
@@ -5,8 +5,9 @@ class ApiClientAuthorization < ArvadosModel
   belongs_to :api_client
   belongs_to :user
   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
@@ -16,8 +17,11 @@ class ApiClientAuthorization < ArvadosModel
     t.add :expires_at
     t.add :last_used_at
     t.add :last_used_by_ip_address
+    t.add :scopes
   end
 
+  UNLOGGED_CHANGES = ['last_used_at', 'last_used_by_ip_address', 'updated_at']
+
   def assign_random_api_token
     self.api_token ||= rand(2**256).to_s(36)
   end
@@ -58,6 +62,24 @@ class ApiClientAuthorization < ArvadosModel
   end
   def modified_at=(x) end
 
+  def scopes_allow?(req_s)
+    scopes.each do |scope|
+      return true if (scope == 'all') or (scope == req_s) or
+        ((scope.end_with? '/') and (req_s.start_with? scope))
+    end
+    false
+  end
+
+  def scopes_allow_request?(request)
+    scopes_allow? [request.method, request.path].join(' ')
+  end
+
+  def logged_attributes
+    attrs = attributes.dup
+    attrs.delete('api_token')
+    attrs
+  end
+
   protected
 
   def permission_to_create
@@ -69,4 +91,8 @@ class ApiClientAuthorization < ArvadosModel
      not self.user_id_changed? and
      not self.owner_uuid_changed?)
   end
+
+  def log_update
+    super unless (changed - UNLOGGED_CHANGES).empty?
+  end
 end