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(repo_name: full_repo_name,
127 vm_uuid: params[:vm_uuid],
128 send_notification_email: params[:send_notification_email])
130 send_json kind: "arvados#HashList", items: @response.as_api_response(nil)
133 # delete user agreements, vm, repository, login links; set state to inactive
135 reload_object_before_update
141 if (params[:old_user_uuid] || params[:new_user_uuid])
142 if !current_user.andand.is_admin
143 return send_error("Must be admin to use old_user_uuid/new_user_uuid", status: 403)
145 if !params[:old_user_uuid] || !params[:new_user_uuid]
146 return send_error("Must supply both old_user_uuid and new_user_uuid", status: 422)
148 new_user = User.find_by_uuid(params[:new_user_uuid])
150 return send_error("User in new_user_uuid not found", status: 422)
152 @object = User.find_by_uuid(params[:old_user_uuid])
154 return send_error("User in old_user_uuid not found", status: 422)
157 if !Thread.current[:api_client].andand.is_trusted
158 return send_error("supplied API token is not from a trusted client", status: 403)
159 elsif Thread.current[:api_client_authorization].scopes != ['all']
160 return send_error("cannot merge with a scoped token", status: 403)
163 new_auth = ApiClientAuthorization.validate(token: params[:new_user_token])
165 return send_error("invalid new_user_token", status: 401)
168 if new_auth.user.uuid[0..4] == Rails.configuration.ClusterID
169 if !new_auth.api_client.andand.is_trusted
170 return send_error("supplied new_user_token is not from a trusted client", status: 403)
171 elsif new_auth.scopes != ['all']
172 return send_error("supplied new_user_token has restricted scope", status: 403)
175 new_user = new_auth.user
176 @object = current_user
179 if @object.uuid == new_user.uuid
180 return send_error("cannot merge user to self", status: 422)
183 if !params[:new_owner_uuid]
184 return send_error("missing new_owner_uuid", status: 422)
187 if !new_user.can?(write: params[:new_owner_uuid])
188 return send_error("cannot move objects into supplied new_owner_uuid: new user does not have write permission", status: 403)
191 act_as_system_user do
192 @object.merge(new_owner_uuid: params[:new_owner_uuid],
193 new_user_uuid: new_user.uuid,
194 redirect_to_new_user: params[:redirect_to_new_user])
201 def self._merge_requires_parameters
204 type: 'string', required: true,
207 type: 'string', required: false,
209 redirect_to_new_user: {
210 type: 'boolean', required: false, default: false,
213 type: 'string', required: false,
216 type: 'string', required: false,
221 def self._setup_requires_parameters
224 type: 'string', required: false,
227 type: 'object', required: false,
230 type: 'string', required: false,
233 type: 'string', required: false,
235 send_notification_email: {
236 type: 'boolean', required: false, default: false,
241 def self._update_requires_parameters
244 type: 'boolean', required: false, default: false,
249 def apply_filters(model_class=nil)
250 return super if @read_users.any?(&:is_admin)
251 if params[:uuid] != current_user.andand.uuid
252 # Non-admin index/show returns very basic information about readable users.
253 safe_attrs = ["uuid", "is_active", "is_admin", "is_invited", "email", "first_name", "last_name", "username", "can_write", "can_manage", "kind"]
255 @select = @select & safe_attrs
259 @filters += [['is_active', '=', true]]
261 # This gets called from within find_object_by_uuid.
262 # find_object_by_uuid stores the original value of @select in
263 # @preserve_select, edits the value of @select, calls
264 # find_objects_for_index, then restores @select from the value
265 # of @preserve_select. So if we want our updated value of
266 # @select here to stick, we have to set @preserve_select.
267 @preserve_select = @select
271 def nullable_attributes
272 super + [:email, :first_name, :last_name, :username]