1 class Arvados::V1::ApiClientAuthorizationsController < ApplicationController
2 accept_attribute_as_json :scopes, Array
3 before_filter :current_api_client_is_trusted
4 before_filter :admin_required, :only => :create_system_auth
5 skip_before_filter :render_404_if_no_object, :only => :create_system_auth
7 def self._create_system_auth_requires_parameters
9 api_client_id: {type: 'integer', required: false},
10 scopes: {type: 'array', required: false}
13 def create_system_auth
14 @object = ApiClientAuthorization.
15 new(user_id: system_user.id,
16 api_client_id: params[:api_client_id] || current_api_client.andand.id,
17 created_by_ip_address: remote_ip,
18 scopes: Oj.load(params[:scopes] || '["all"]'))
24 if resource_attrs[:owner_uuid]
25 # The model has an owner_id attribute instead of owner_uuid, but
26 # we can't expect the client to know the local numeric ID. We
27 # translate UUID to numeric ID here.
28 resource_attrs[:user_id] =
29 User.where(uuid: resource_attrs.delete(:owner_uuid)).first.andand.id
31 resource_attrs[:api_client_id] = Thread.current[:api_client].id
37 def find_objects_for_index
38 # Here we are deliberately less helpful about searching for client
39 # authorizations. Rather than use the generic index/where/order
40 # features, we look up tokens belonging to the current user and
41 # filter by exact match on api_token (which we expect in the form
42 # of a where[uuid] parameter to make things easier for API client
44 @objects = model_class.
45 includes(:user, :api_client).
46 where('user_id=? and (? or api_token=?)', current_user.id, !@where['uuid'], @where['uuid']).
47 order('created_at desc')
50 def find_object_by_uuid
51 # Again, to make things easier for the client and our own routing,
52 # here we look for the api_token key in a "uuid" (POST) or "id"
54 @object = model_class.where('api_token=?', params[:uuid] || params[:id]).first
57 def current_api_client_is_trusted
58 unless Thread.current[:api_client].andand.is_trusted
59 render :json => { errors: ['Forbidden: this API client cannot manipulate other clients\' access tokens.'] }.to_json, status: 403