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 params[:updates].andand.each do |uuid, attrs|
21 u = User.find_or_create_by(uuid: uuid)
22 rescue ActiveRecord::RecordNotUnique
26 nullify_attrs(attrs).each do |k,v|
27 if !v.nil? && u.send(k) != v
31 if needupdate.length > 0
34 rescue ActiveRecord::RecordInvalid
35 loginCluster = Rails.configuration.Login.LoginCluster
36 if u.uuid[0..4] == loginCluster && !needupdate[:username].nil?
37 local_user = User.find_by_username(needupdate[:username])
38 # The username of this record conflicts with an existing,
39 # different user record. This can happen because the
40 # username changed upstream on the login cluster, or
41 # because we're federated with another cluster with a user
42 # by the same username. The login cluster is the source
43 # of truth, so change the username on the conflicting
44 # record and retry the update operation.
45 if local_user.uuid != u.uuid
46 new_username = "#{needupdate[:username]}#{rand(99999999)}"
47 Rails.logger.warn("cached username '#{needupdate[:username]}' collision with user '#{local_user.uuid}' - renaming to '#{new_username}' before retrying")
48 local_user.update!({username: new_username})
52 raise # Not the issue we're handling above
64 @object = current_user
67 send_error("Not logged in", status: 401)
77 if params[:id] and params[:id].match(/\D/)
78 params[:uuid] = params.delete :id
80 if current_user.andand.is_admin && params[:uuid]
81 @object = User.find_by_uuid params[:uuid]
83 @object = current_user
85 if not @object.is_active
86 if @object.uuid[0..4] == Rails.configuration.Login.LoginCluster &&
87 @object.uuid[0..4] != Rails.configuration.ClusterID
88 logger.warn "Local user #{@object.uuid} called users#activate but only LoginCluster can do that"
89 raise ArgumentError.new "cannot activate user #{@object.uuid} here, only the #{@object.uuid[0..4]} cluster can do that"
90 elsif not (current_user.is_admin or @object.is_invited)
91 logger.warn "User #{@object.uuid} called users.activate " +
93 raise ArgumentError.new "Cannot activate without being invited."
96 required_uuids = Link.where("owner_uuid = ? and link_class = ? and name = ? and tail_uuid = ? and head_uuid like ?",
101 Collection.uuid_like_pattern).
103 signed_uuids = Link.where(owner_uuid: system_user_uuid,
104 link_class: 'signature',
106 tail_uuid: @object.uuid,
107 head_uuid: required_uuids).
109 todo_uuids = required_uuids - signed_uuids
111 @object.update is_active: true
112 logger.info "User #{@object.uuid} activated"
114 logger.warn "User #{@object.uuid} called users.activate " +
115 "before signing agreements #{todo_uuids.inspect}"
116 raise ArvadosModel::PermissionDeniedError.new \
117 "Cannot activate without user agreements #{todo_uuids.inspect}."
124 # create user object and all the needed links
127 @object = User.find_by_uuid(params[:uuid])
129 return render_404_if_no_object
131 elsif !params[:user] || params[:user].empty?
132 raise ArgumentError.new "Required uuid or user"
133 elsif !params[:user]['email']
134 raise ArgumentError.new "Require user email"
136 @object = model_class.create! resource_attrs
139 # It's not always possible for the client to know the user's
140 # username when submitting this request: the username might have
141 # been assigned automatically in create!() above. If client
142 # provided a plain repository name, prefix it with the username
143 # now that we know what it is.
144 if params[:repo_name].nil?
146 elsif @object.username.nil?
148 new("cannot setup a repository because user has no username")
149 elsif params[:repo_name].index("/")
150 full_repo_name = params[:repo_name]
152 full_repo_name = "#{@object.username}/#{params[:repo_name]}"
155 @response = @object.setup(repo_name: full_repo_name,
156 vm_uuid: params[:vm_uuid],
157 send_notification_email: params[:send_notification_email])
159 send_json kind: "arvados#HashList", items: @response.as_api_response(nil)
162 # delete user agreements, vm, repository, login links; set state to inactive
164 reload_object_before_update
170 if (params[:old_user_uuid] || params[:new_user_uuid])
171 if !current_user.andand.is_admin
172 return send_error("Must be admin to use old_user_uuid/new_user_uuid", status: 403)
174 if !params[:old_user_uuid] || !params[:new_user_uuid]
175 return send_error("Must supply both old_user_uuid and new_user_uuid", status: 422)
177 new_user = User.find_by_uuid(params[:new_user_uuid])
179 return send_error("User in new_user_uuid not found", status: 422)
181 @object = User.find_by_uuid(params[:old_user_uuid])
183 return send_error("User in old_user_uuid not found", status: 422)
186 if !Thread.current[:api_client].andand.is_trusted
187 return send_error("supplied API token is not from a trusted client", status: 403)
188 elsif Thread.current[:api_client_authorization].scopes != ['all']
189 return send_error("cannot merge with a scoped token", status: 403)
192 new_auth = ApiClientAuthorization.validate(token: params[:new_user_token])
194 return send_error("invalid new_user_token", status: 401)
197 if new_auth.user.uuid[0..4] == Rails.configuration.ClusterID
198 if !new_auth.api_client.andand.is_trusted
199 return send_error("supplied new_user_token is not from a trusted client", status: 403)
200 elsif new_auth.scopes != ['all']
201 return send_error("supplied new_user_token has restricted scope", status: 403)
204 new_user = new_auth.user
205 @object = current_user
208 if @object.uuid == new_user.uuid
209 return send_error("cannot merge user to self", status: 422)
212 if !params[:new_owner_uuid]
213 return send_error("missing new_owner_uuid", status: 422)
216 if !new_user.can?(write: params[:new_owner_uuid])
217 return send_error("cannot move objects into supplied new_owner_uuid: new user does not have write permission", status: 403)
220 act_as_system_user do
221 @object.merge(new_owner_uuid: params[:new_owner_uuid],
222 new_user_uuid: new_user.uuid,
223 redirect_to_new_user: params[:redirect_to_new_user])
230 def self._merge_requires_parameters
233 type: 'string', required: true,
236 type: 'string', required: false,
238 redirect_to_new_user: {
239 type: 'boolean', required: false, default: false,
242 type: 'string', required: false,
245 type: 'string', required: false,
250 def self._setup_requires_parameters
253 type: 'string', required: false,
256 type: 'object', required: false,
259 type: 'string', required: false,
262 type: 'string', required: false,
264 send_notification_email: {
265 type: 'boolean', required: false, default: false,
270 def self._update_requires_parameters
273 type: 'boolean', required: false, default: false,
278 def apply_filters(model_class=nil)
279 return super if @read_users.any?(&:is_admin)
280 if params[:uuid] != current_user.andand.uuid
281 # Non-admin index/show returns very basic information about readable users.
282 safe_attrs = ["uuid", "is_active", "email", "first_name", "last_name", "username", "can_write", "can_manage", "kind"]
284 @select = @select & safe_attrs
288 @filters += [['is_active', '=', true]]
290 # This gets called from within find_object_by_uuid.
291 # find_object_by_uuid stores the original value of @select in
292 # @preserve_select, edits the value of @select, calls
293 # find_objects_for_index, then restores @select from the value
294 # of @preserve_select. So if we want our updated value of
295 # @select here to stick, we have to set @preserve_select.
296 @preserve_select = @select
300 def nullable_attributes
301 super + [:email, :first_name, :last_name, :username]