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, :update_uuid, :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
32 u.update_attributes!(needupdate)
43 @object = current_user
46 send_error("Not logged in", status: 401)
56 if params[:id] and params[:id].match(/\D/)
57 params[:uuid] = params.delete :id
59 if current_user.andand.is_admin && params[:uuid]
60 @object = User.find_by_uuid params[:uuid]
62 @object = current_user
64 if not @object.is_active
65 if @object.uuid[0..4] == Rails.configuration.Login.LoginCluster &&
66 @object.uuid[0..4] != Rails.configuration.ClusterID
67 logger.warn "Local user #{@object.uuid} called users#activate but only LoginCluster can do that"
68 raise ArgumentError.new "cannot activate user #{@object.uuid} here, only the #{@object.uuid[0..4]} cluster can do that"
69 elsif not (current_user.is_admin or @object.is_invited)
70 logger.warn "User #{@object.uuid} called users.activate " +
72 raise ArgumentError.new "Cannot activate without being invited."
75 required_uuids = Link.where("owner_uuid = ? and link_class = ? and name = ? and tail_uuid = ? and head_uuid like ?",
80 Collection.uuid_like_pattern).
82 signed_uuids = Link.where(owner_uuid: system_user_uuid,
83 link_class: 'signature',
85 tail_uuid: @object.uuid,
86 head_uuid: required_uuids).
88 todo_uuids = required_uuids - signed_uuids
90 @object.update_attributes is_active: true
91 logger.info "User #{@object.uuid} activated"
93 logger.warn "User #{@object.uuid} called users.activate " +
94 "before signing agreements #{todo_uuids.inspect}"
95 raise ArvadosModel::PermissionDeniedError.new \
96 "Cannot activate without user agreements #{todo_uuids.inspect}."
103 # create user object and all the needed links
106 @object = User.find_by_uuid(params[:uuid])
108 return render_404_if_no_object
110 elsif !params[:user] || params[:user].empty?
111 raise ArgumentError.new "Required uuid or user"
112 elsif !params[:user]['email']
113 raise ArgumentError.new "Require user email"
115 @object = model_class.create! resource_attrs
118 # It's not always possible for the client to know the user's
119 # username when submitting this request: the username might have
120 # been assigned automatically in create!() above. If client
121 # provided a plain repository name, prefix it with the username
122 # now that we know what it is.
123 if params[:repo_name].nil?
125 elsif @object.username.nil?
127 new("cannot setup a repository because user has no username")
128 elsif params[:repo_name].index("/")
129 full_repo_name = params[:repo_name]
131 full_repo_name = "#{@object.username}/#{params[:repo_name]}"
134 @response = @object.setup(repo_name: full_repo_name,
135 vm_uuid: params[:vm_uuid],
136 send_notification_email: params[:send_notification_email])
138 send_json kind: "arvados#HashList", items: @response.as_api_response(nil)
141 # delete user agreements, vm, repository, login links; set state to inactive
143 reload_object_before_update
148 # Change UUID to a new (unused) uuid and transfer all owned/linked
149 # objects accordingly.
151 @object.update_uuid(new_uuid: params[:new_uuid])
156 if (params[:old_user_uuid] || params[:new_user_uuid])
157 if !current_user.andand.is_admin
158 return send_error("Must be admin to use old_user_uuid/new_user_uuid", status: 403)
160 if !params[:old_user_uuid] || !params[:new_user_uuid]
161 return send_error("Must supply both old_user_uuid and new_user_uuid", status: 422)
163 new_user = User.find_by_uuid(params[:new_user_uuid])
165 return send_error("User in new_user_uuid not found", status: 422)
167 @object = User.find_by_uuid(params[:old_user_uuid])
169 return send_error("User in old_user_uuid not found", status: 422)
172 if !Thread.current[:api_client].andand.is_trusted
173 return send_error("supplied API token is not from a trusted client", status: 403)
174 elsif Thread.current[:api_client_authorization].scopes != ['all']
175 return send_error("cannot merge with a scoped token", status: 403)
178 new_auth = ApiClientAuthorization.validate(token: params[:new_user_token])
180 return send_error("invalid new_user_token", status: 401)
183 if new_auth.user.uuid[0..4] == Rails.configuration.ClusterID
184 if !new_auth.api_client.andand.is_trusted
185 return send_error("supplied new_user_token is not from a trusted client", status: 403)
186 elsif new_auth.scopes != ['all']
187 return send_error("supplied new_user_token has restricted scope", status: 403)
190 new_user = new_auth.user
191 @object = current_user
194 if @object.uuid == new_user.uuid
195 return send_error("cannot merge user to self", status: 422)
198 if !params[:new_owner_uuid]
199 return send_error("missing new_owner_uuid", status: 422)
202 if !new_user.can?(write: params[:new_owner_uuid])
203 return send_error("cannot move objects into supplied new_owner_uuid: new user does not have write permission", status: 403)
206 act_as_system_user do
207 @object.merge(new_owner_uuid: params[:new_owner_uuid],
208 new_user_uuid: new_user.uuid,
209 redirect_to_new_user: params[:redirect_to_new_user])
216 def self._merge_requires_parameters
219 type: 'string', required: true,
222 type: 'string', required: false,
224 redirect_to_new_user: {
225 type: 'boolean', required: false, default: false,
228 type: 'string', required: false,
231 type: 'string', required: false,
236 def self._setup_requires_parameters
239 type: 'string', required: false,
242 type: 'object', required: false,
245 type: 'string', required: false,
248 type: 'string', required: false,
250 send_notification_email: {
251 type: 'boolean', required: false, default: false,
256 def self._update_requires_parameters
259 type: 'boolean', required: false, default: false,
264 def self._update_uuid_requires_parameters
267 type: 'string', required: true,
272 def apply_filters(model_class=nil)
273 return super if @read_users.any?(&:is_admin)
274 if params[:uuid] != current_user.andand.uuid
275 # Non-admin index/show returns very basic information about readable users.
276 safe_attrs = ["uuid", "is_active", "email", "first_name", "last_name", "username"]
278 @select = @select & safe_attrs
282 @filters += [['is_active', '=', true]]
287 def nullable_attributes
288 super + [:email, :first_name, :last_name, :username]