Merge branch '19929-fill-discovery-document'
[arvados.git] / services / api / app / controllers / arvados / v1 / user_agreements_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class Arvados::V1::UserAgreementsController < ApplicationController
6   before_action :admin_required, except: [:index, :sign, :signatures]
7   skip_before_action :find_object_by_uuid, only: [:sign, :signatures]
8   skip_before_action :render_404_if_no_object, only: [:sign, :signatures]
9
10   def model_class
11     Link
12   end
13
14   def table_name
15     'links'
16   end
17
18   def index
19     if not current_user.is_invited
20       # New users cannot see user agreements until/unless invited to
21       # use this installation.
22       @objects = []
23     else
24       act_as_system_user do
25         uuids = Link.where("owner_uuid = ? and link_class = ? and name = ? and tail_uuid = ? and head_uuid like ?",
26                            system_user_uuid,
27                            'signature',
28                            'require',
29                            system_user_uuid,
30                            Collection.uuid_like_pattern).
31           collect(&:head_uuid)
32         @objects = Collection.where('uuid in (?)', uuids)
33       end
34     end
35     @response_resource_name = 'collection'
36     super
37   end
38
39   def self._signatures_method_description
40     "List all user agreement signature links from a user."
41   end
42
43   def signatures
44     current_user_uuid = (current_user.andand.is_admin && params[:uuid]) ||
45       current_user.uuid
46     act_as_system_user do
47       @objects = Link.where("owner_uuid = ? and link_class = ? and name = ? and tail_uuid = ? and head_uuid like ?",
48                             system_user_uuid,
49                             'signature',
50                             'click',
51                             current_user_uuid,
52                             Collection.uuid_like_pattern)
53     end
54     @response_resource_name = 'link'
55     render_list
56   end
57
58   def self._sign_method_description
59     "Create a signature link from the current user for a given user agreement."
60   end
61
62   def sign
63     current_user_uuid = current_user.uuid
64     act_as_system_user do
65       @object = Link.create(link_class: 'signature',
66                             name: 'click',
67                             tail_uuid: current_user_uuid,
68                             head_uuid: params[:uuid])
69     end
70     show
71   end
72
73   def create
74     usage_error
75   end
76
77   def update
78     usage_error
79   end
80
81   def destroy
82     usage_error
83   end
84
85   protected
86   def usage_error
87     raise ArgumentError.new \
88     "Manage user agreements via Collections and Links instead."
89   end
90   
91 end