Merge remote-tracking branch 'origin/master' into 2044-share-button
[arvados.git] / services / api / app / controllers / arvados / v1 / api_client_authorizations_controller.rb
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
6
7   def self._create_system_auth_requires_parameters
8     {
9       api_client_id: {type: 'integer', required: false},
10       scopes: {type: 'array', required: false}
11     }
12   end
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"]'))
19     @object.save!
20     show
21   end
22
23   def create
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
30     elsif not resource_attrs[:user_id]
31       resource_attrs[:user_id] = current_user.id
32     end
33     resource_attrs[:api_client_id] = Thread.current[:api_client].id
34     super
35   end
36
37   protected
38
39   def default_orders
40     ["#{table_name}.created_at desc"]
41   end
42
43   def find_objects_for_index
44     # Here we are deliberately less helpful about searching for client
45     # authorizations.  We look up tokens belonging to the current user
46     # and filter by exact matches on api_token and scopes.
47     wanted_scopes = []
48     if @filters
49       wanted_scopes.concat(@filters.map { |attr, operator, operand|
50         ((attr == 'scopes') and (operator == '=')) ? operand : nil
51       })
52       @filters.select! { |attr, operator, operand|
53         (attr == 'uuid') and (operator == '=')
54       }
55     end
56     if @where
57       wanted_scopes << @where['scopes']
58       @where.select! { |attr, val| attr == 'uuid' }
59     end
60     @objects = model_class.
61       includes(:user, :api_client).
62       where('user_id=?', current_user.id)
63     super
64     wanted_scopes.compact.each do |scope_list|
65       sorted_scopes = scope_list.sort
66       @objects = @objects.select { |auth| auth.scopes.sort == sorted_scopes }
67     end
68   end
69
70   def find_object_by_uuid
71     # Again, to make things easier for the client and our own routing,
72     # here we look for the api_token key in a "uuid" (POST) or "id"
73     # (GET) parameter.
74     @object = model_class.where('api_token=?', params[:uuid] || params[:id]).first
75   end
76
77   def current_api_client_is_trusted
78     unless Thread.current[:api_client].andand.is_trusted
79       render :json => { errors: ['Forbidden: this API client cannot manipulate other clients\' access tokens.'] }.to_json, status: 403
80     end
81   end
82 end