1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class Arvados::V1::UsersController < ApplicationController
6 accept_attribute_as_json :prefs, Hash
7 accept_param_as_json :updates
9 skip_before_action :find_object_by_uuid, only:
10 [:activate, :current, :system, :setup, :merge, :batch_update]
11 skip_before_action :render_404_if_no_object, only:
12 [:activate, :current, :system, :setup, :merge, :batch_update]
13 before_action :admin_required, only: [:setup, :unsetup, :batch_update]
15 # Internal API used by controller to update local cache of user
16 # records from LoginCluster.
19 # update_remote_user takes a row lock on the User record, so sort
20 # the keys so we always lock them in the same order.
21 sorted = params[:updates].keys.sort
23 attrs = params[:updates][uuid]
25 u = User.update_remote_user nullify_attrs(attrs)
35 @object = current_user
38 send_error("Not logged in", status: 401)
48 if params[:id] and params[:id].match(/\D/)
49 params[:uuid] = params.delete :id
51 if current_user.andand.is_admin && params[:uuid]
52 @object = User.find_by_uuid params[:uuid]
54 @object = current_user
56 if not @object.is_active
57 if @object.uuid[0..4] == Rails.configuration.Login.LoginCluster &&
58 @object.uuid[0..4] != Rails.configuration.ClusterID
59 logger.warn "Local user #{@object.uuid} called users#activate but only LoginCluster can do that"
60 raise ArgumentError.new "cannot activate user #{@object.uuid} here, only the #{@object.uuid[0..4]} cluster can do that"
61 elsif not (current_user.is_admin or @object.is_invited)
62 logger.warn "User #{@object.uuid} called users.activate " +
64 raise ArgumentError.new "Cannot activate without being invited."
67 required_uuids = Link.where("owner_uuid = ? and link_class = ? and name = ? and tail_uuid = ? and head_uuid like ?",
72 Collection.uuid_like_pattern).
74 signed_uuids = Link.where(owner_uuid: system_user_uuid,
75 link_class: 'signature',
77 tail_uuid: @object.uuid,
78 head_uuid: required_uuids).
80 todo_uuids = required_uuids - signed_uuids
82 @object.update is_active: true
83 logger.info "User #{@object.uuid} activated"
85 logger.warn "User #{@object.uuid} called users.activate " +
86 "before signing agreements #{todo_uuids.inspect}"
87 raise ArvadosModel::PermissionDeniedError.new \
88 "Cannot activate without user agreements #{todo_uuids.inspect}."
95 # create user object and all the needed links
98 @object = User.find_by_uuid(params[:uuid])
100 return render_404_if_no_object
102 elsif !params[:user] || params[:user].empty?
103 raise ArgumentError.new "Required uuid or user"
104 elsif !params[:user]['email']
105 raise ArgumentError.new "Require user email"
107 @object = model_class.create! resource_attrs
110 # It's not always possible for the client to know the user's
111 # username when submitting this request: the username might have
112 # been assigned automatically in create!() above. If client
113 # provided a plain repository name, prefix it with the username
114 # now that we know what it is.
115 if params[:repo_name].nil?
117 elsif @object.username.nil?
119 new("cannot setup a repository because user has no username")
120 elsif params[:repo_name].index("/")
121 full_repo_name = params[:repo_name]
123 full_repo_name = "#{@object.username}/#{params[:repo_name]}"
126 @response = @object.setup(vm_uuid: params[:vm_uuid],
127 send_notification_email: params[:send_notification_email])
129 send_json kind: "arvados#HashList", items: @response.as_api_response(nil)
132 # delete user agreements, vm, repository, login links; set state to inactive
134 reload_object_before_update
140 if (params[:old_user_uuid] || params[:new_user_uuid])
141 if !current_user.andand.is_admin
142 return send_error("Must be admin to use old_user_uuid/new_user_uuid", status: 403)
144 if !params[:old_user_uuid] || !params[:new_user_uuid]
145 return send_error("Must supply both old_user_uuid and new_user_uuid", status: 422)
147 new_user = User.find_by_uuid(params[:new_user_uuid])
149 return send_error("User in new_user_uuid not found", status: 422)
151 @object = User.find_by_uuid(params[:old_user_uuid])
153 return send_error("User in old_user_uuid not found", status: 422)
156 if !Thread.current[:api_client].andand.is_trusted
157 return send_error("supplied API token is not from a trusted client", status: 403)
158 elsif Thread.current[:api_client_authorization].scopes != ['all']
159 return send_error("cannot merge with a scoped token", status: 403)
162 new_auth = ApiClientAuthorization.validate(token: params[:new_user_token])
164 return send_error("invalid new_user_token", status: 401)
167 if new_auth.user.uuid[0..4] == Rails.configuration.ClusterID
168 if !new_auth.api_client.andand.is_trusted
169 return send_error("supplied new_user_token is not from a trusted client", status: 403)
170 elsif new_auth.scopes != ['all']
171 return send_error("supplied new_user_token has restricted scope", status: 403)
174 new_user = new_auth.user
175 @object = current_user
178 if @object.uuid == new_user.uuid
179 return send_error("cannot merge user to self", status: 422)
182 if !params[:new_owner_uuid]
183 return send_error("missing new_owner_uuid", status: 422)
186 if !new_user.can?(write: params[:new_owner_uuid])
187 return send_error("cannot move objects into supplied new_owner_uuid: new user does not have write permission", status: 403)
190 act_as_system_user do
191 @object.merge(new_owner_uuid: params[:new_owner_uuid],
192 new_user_uuid: new_user.uuid,
193 redirect_to_new_user: params[:redirect_to_new_user])
200 def self._merge_requires_parameters
203 type: 'string', required: true,
206 type: 'string', required: false,
208 redirect_to_new_user: {
209 type: 'boolean', required: false, default: false,
212 type: 'string', required: false,
215 type: 'string', required: false,
220 def self._setup_requires_parameters
223 type: 'string', required: false,
226 type: 'object', required: false,
229 type: 'string', required: false,
232 type: 'string', required: false,
234 send_notification_email: {
235 type: 'boolean', required: false, default: false,
240 def self._update_requires_parameters
243 type: 'boolean', required: false, default: false,
248 def apply_filters(model_class=nil)
249 return super if @read_users.any?(&:is_admin)
250 if params[:uuid] != current_user.andand.uuid
251 # Non-admin index/show returns very basic information about readable users.
252 safe_attrs = ["uuid", "is_active", "is_admin", "is_invited", "email", "first_name", "last_name", "username", "can_write", "can_manage", "kind"]
254 @select = @select & safe_attrs
258 @filters += [['is_active', '=', true]]
260 # This gets called from within find_object_by_uuid.
261 # find_object_by_uuid stores the original value of @select in
262 # @preserve_select, edits the value of @select, calls
263 # find_objects_for_index, then restores @select from the value
264 # of @preserve_select. So if we want our updated value of
265 # @select here to stick, we have to set @preserve_select.
266 @preserve_select = @select
270 def nullable_attributes
271 super + [:email, :first_name, :last_name, :username]