1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class ApiClientAuthorization < ArvadosModel
8 include CommonApiTemplate
10 belongs_to :api_client
12 after_initialize :assign_random_api_token
13 serialize :scopes, Array
15 api_accessible :user, extend: :common do |t|
20 t.add :created_by_ip_address
21 t.add :default_owner_uuid
24 t.add :last_used_by_ip_address
28 UNLOGGED_CHANGES = ['last_used_at', 'last_used_by_ip_address', 'updated_at']
30 def assign_random_api_token
31 self.api_token ||= rand(2**256).to_s(36)
38 self.user_id_changed? ? User.where(id: self.user_id_was).first.andand.uuid : self.user.andand.uuid
40 def owner_uuid_changed?
44 def modified_by_client_uuid
47 def modified_by_client_uuid=(x) end
49 def modified_by_user_uuid
52 def modified_by_user_uuid=(x) end
57 def modified_at=(x) end
59 def scopes_allow?(req_s)
60 scopes.each do |scope|
61 return true if (scope == 'all') or (scope == req_s) or
62 ((scope.end_with? '/') and (req_s.start_with? scope))
67 def scopes_allow_request?(request)
68 method = request.request_method
70 (scopes_allow?(['HEAD', request.path].join(' ')) ||
71 scopes_allow?(['GET', request.path].join(' ')))
73 scopes_allow?([method, request.path].join(' '))
78 super.except 'api_token'
81 def self.default_orders
82 ["#{table_name}.id desc"]
87 def permission_to_create
88 current_user.andand.is_admin or (current_user.andand.id == self.user_id)
91 def permission_to_update
92 (permission_to_create and
94 not user_id_changed? and
95 not owner_uuid_changed?)
99 super unless (changed - UNLOGGED_CHANGES).empty?