X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/c0e2292b4ed0065448e11fe3ecd7b0ee8c87ff4b..49a6ced3c7a540a7da7155ab1c3120a5227c620c:/services/api/app/models/api_client_authorization.rb diff --git a/services/api/app/models/api_client_authorization.rb b/services/api/app/models/api_client_authorization.rb index c71d0dce4b..82dd0ec2b6 100644 --- a/services/api/app/models/api_client_authorization.rb +++ b/services/api/app/models/api_client_authorization.rb @@ -5,30 +5,34 @@ 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| - t.add :owner + api_accessible :user, extend: :common do |t| + t.add :owner_uuid t.add :user_id t.add :api_client_id t.add :api_token t.add :created_by_ip_address - t.add :default_owner + t.add :default_owner_uuid 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 - def owner + def owner_uuid self.user.andand.uuid end - def owner_was + def owner_uuid_was self.user_id_changed? ? User.find(self.user_id_was).andand.uuid : self.user.andand.uuid end - def owner_changed? + def owner_uuid_changed? self.user_id_changed? end @@ -43,21 +47,39 @@ class ApiClientAuthorization < ArvadosModel self.api_token_changed? end - def modified_by_client + def modified_by_client_uuid nil end - def modified_by_client=(x) end + def modified_by_client_uuid=(x) end - def modified_by_user + def modified_by_user_uuid nil end - def modified_by_user=(x) end + def modified_by_user_uuid=(x) end def modified_at nil 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.request_method, request.path].join(' ') + end + + def logged_attributes + attrs = attributes.dup + attrs.delete('api_token') + attrs + end + protected def permission_to_create @@ -67,6 +89,10 @@ class ApiClientAuthorization < ArvadosModel def permission_to_update (permission_to_create and not self.user_id_changed? and - not self.owner_changed?) + not self.owner_uuid_changed?) + end + + def log_update + super unless (changed - UNLOGGED_CHANGES).empty? end end