1 class User < ArvadosModel
4 include CommonApiTemplate
6 has_many :api_client_authorizations
7 before_update :prevent_privilege_escalation
8 before_update :prevent_inactive_admin
9 before_create :check_auto_admin
10 after_create :add_system_group_permission_link
11 after_create AdminNotifier
13 has_many :authorized_keys, :foreign_key => :authorized_user_uuid, :primary_key => :uuid
15 api_accessible :user, extend: :common do |t|
27 ALL_PERMISSIONS = {read: true, write: true, manage: true}
30 "#{first_name} #{last_name}"
35 Rails.configuration.new_users_are_active ||
36 self.groups_i_can(:read).select { |x| x.match /-f+$/ }.first)
39 def groups_i_can(verb)
40 self.group_permissions.select { |uuid, mask| mask[verb] }.keys
44 return true if is_admin
45 actions.each do |action, target|
47 if target.respond_to? :uuid
48 target_uuid = target.uuid
50 next if target_uuid == self.uuid
51 next if (group_permissions[target_uuid] and
52 group_permissions[target_uuid][action])
53 if target.respond_to? :owner_uuid
54 next if target.owner_uuid == self.uuid
55 next if (group_permissions[target.owner_uuid] and
56 group_permissions[target.owner_uuid][action])
63 def self.invalidate_permissions_cache
64 Rails.cache.delete_matched(/^groups_for_user_/)
67 # Return a hash of {group_uuid: perm_hash} where perm_hash[:read]
68 # and perm_hash[:write] are true if this user can read and write
69 # objects owned by group_uuid.
71 Rails.cache.fetch "groups_for_user_#{self.uuid}" do
73 todo = {self.uuid => true}
76 lookup_uuids = todo.keys
77 lookup_uuids.each do |uuid| done[uuid] = true end
80 Group.where('owner_uuid in (?)', lookup_uuids).each do |group|
81 newgroups << [group.owner_uuid, group.uuid, 'can_manage']
83 Link.where('tail_uuid in (?) and link_class = ? and head_kind in (?)',
86 ['arvados#group', 'arvados#user']).each do |link|
87 newgroups << [link.tail_uuid, link.head_uuid, link.name]
89 newgroups.each do |tail_uuid, head_uuid, perm_name|
90 unless done.has_key? head_uuid
91 todo[head_uuid] = true
96 link_permissions = {read:true}
98 link_permissions = {read:true,write:true}
100 link_permissions = ALL_PERMISSIONS
102 permissions_from[tail_uuid] ||= {}
103 permissions_from[tail_uuid][head_uuid] ||= {}
104 link_permissions.each do |k,v|
105 permissions_from[tail_uuid][head_uuid][k] ||= v
109 search_permissions(self.uuid, permissions_from)
113 def self.setup(user, openid_prefix, repo_name=nil, vm_uuid=nil)
114 login_perm_props = {identity_url_prefix: openid_prefix}
116 # Check oid_login_perm
117 oid_login_perms = Link.where(tail_uuid: user.email,
118 head_kind: 'arvados#user',
119 link_class: 'permission',
122 if !oid_login_perms.any?
123 # create openid login permission
124 oid_login_perm = Link.create(link_class: 'permission',
127 tail_uuid: user.email,
128 head_kind: 'arvados#user',
129 head_uuid: user.uuid,
130 properties: login_perm_props
132 logger.info { "openid login permission: " + oid_login_perm[:uuid] }
134 oid_login_perm = oid_login_perms.first
137 return [oid_login_perm] + user.setup_repo_vm_links(repo_name, vm_uuid)
141 def setup_repo_vm_links(repo_name, vm_uuid)
142 repo_perm = create_user_repo_link repo_name
143 vm_login_perm = create_vm_login_permission_link vm_uuid, repo_name
144 group_perm = create_user_group_link
146 return [repo_perm, vm_login_perm, group_perm, self].compact
149 # delete user signatures, login, repo, and vm perms, and mark as inactive
151 # delete oid_login_perms for this user
152 oid_login_perms = Link.where(tail_uuid: self.email,
153 head_kind: 'arvados#user',
154 link_class: 'permission',
156 oid_login_perms.each do |perm|
160 # delete repo_perms for this user
161 repo_perms = Link.where(tail_uuid: self.uuid,
162 head_kind: 'arvados#repository',
163 link_class: 'permission',
165 repo_perms.each do |perm|
169 # delete vm_login_perms for this user
170 vm_login_perms = Link.where(tail_uuid: self.uuid,
171 head_kind: 'arvados#virtualMachine',
172 link_class: 'permission',
174 vm_login_perms.each do |perm|
178 # delete "All users' group read permissions for this user
179 group = Group.where(name: 'All users').select do |g|
180 g[:uuid].match /-f+$/
182 group_perms = Link.where(tail_uuid: self.uuid,
183 head_uuid: group[:uuid],
184 head_kind: 'arvados#group',
185 link_class: 'permission',
187 group_perms.each do |perm|
191 # delete any signatures by this user
192 signed_uuids = Link.where(link_class: 'signature',
193 tail_kind: 'arvados#user',
194 tail_uuid: self.uuid)
195 signed_uuids.each do |sign|
199 # mark the user as inactive
200 self.is_active = false
206 def permission_to_update
207 # users must be able to update themselves (even if they are
208 # inactive) in order to create sessions
209 self == current_user or super
212 def permission_to_create
213 current_user.andand.is_admin or
214 (self == current_user and
215 self.is_active == Rails.configuration.new_users_are_active)
219 if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and Rails.configuration.auto_admin_user
220 if current_user.email == Rails.configuration.auto_admin_user
222 self.is_active = true
227 def prevent_privilege_escalation
228 if current_user.andand.is_admin
231 if self.is_active_changed?
232 if self.is_active != self.is_active_was
233 logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
234 self.is_active = self.is_active_was
237 if self.is_admin_changed?
238 if self.is_admin != self.is_admin_was
239 logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
240 self.is_admin = self.is_admin_was
246 def prevent_inactive_admin
247 if self.is_admin and not self.is_active
248 # There is no known use case for the strange set of permissions
249 # that would result from this change. It's safest to assume it's
250 # a mistake and disallow it outright.
251 raise "Admin users cannot be inactive"
256 def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
257 nextpaths = graph[start]
258 return merged if !nextpaths
259 return merged if upstream_path.has_key? start
260 upstream_path[start] = true
261 upstream_mask ||= ALL_PERMISSIONS
262 nextpaths.each do |head, mask|
265 merged[head][k] ||= v if upstream_mask[k]
267 search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
269 upstream_path.delete start
273 def create_user_repo_link(repo_name)
274 # repo_name is optional
276 logger.warn ("Repository name not given for #{self.uuid}.")
280 # Check for an existing repository with the same name we're about to use.
281 repo = Repository.where(name: repo_name).first
284 logger.warn "Repository exists for #{repo_name}: #{repo[:uuid]}."
286 # Look for existing repository access for this repo
287 repo_perms = Link.where(tail_uuid: self.uuid,
288 head_kind: 'arvados#repository',
289 head_uuid: repo[:uuid],
290 link_class: 'permission',
293 logger.warn "User already has repository access " +
294 repo_perms.collect { |p| p[:uuid] }.inspect
295 return repo_perms.first
299 # create repo, if does not already exist
300 repo ||= Repository.create(name: repo_name)
301 logger.info { "repo uuid: " + repo[:uuid] }
303 repo_perm = Link.create(tail_kind: 'arvados#user',
304 tail_uuid: self.uuid,
305 head_kind: 'arvados#repository',
306 head_uuid: repo[:uuid],
307 link_class: 'permission',
309 logger.info { "repo permission: " + repo_perm[:uuid] }
313 # create login permission for the given vm_uuid, if it does not already exist
314 def create_vm_login_permission_link(vm_uuid, repo_name)
317 # vm uuid is optional
319 vm = VirtualMachine.where(uuid: vm_uuid).first
322 logger.warn "Could not find virtual machine for #{vm_uuid.inspect}"
323 raise "No vm found for #{vm_uuid}"
329 logger.info { "vm uuid: " + vm[:uuid] }
331 login_perms = Link.where(tail_uuid: self.uuid,
332 head_uuid: vm[:uuid],
333 head_kind: 'arvados#virtualMachine',
334 link_class: 'permission',
338 login_perms.each do |perm|
339 if perm.properties[:username] == repo_name
346 login_perm = Link.create(tail_kind: 'arvados#user',
347 tail_uuid: self.uuid,
348 head_kind: 'arvados#virtualMachine',
349 head_uuid: vm[:uuid],
350 link_class: 'permission',
352 properties: {username: repo_name})
353 logger.info { "login permission: " + login_perm[:uuid] }
355 login_perm = login_perms.first
362 # add the user to the 'All users' group
363 def create_user_group_link
364 # Look up the "All users" group (we expect uuid *-*-fffffffffffffff).
365 group = Group.where(name: 'All users').select do |g|
366 g[:uuid].match /-f+$/
370 logger.warn "No 'All users' group with uuid '*-*-fffffffffffffff'."
371 raise "No 'All users' group with uuid '*-*-fffffffffffffff' is found"
373 logger.info { "\"All users\" group uuid: " + group[:uuid] }
375 group_perms = Link.where(tail_uuid: self.uuid,
376 head_uuid: group[:uuid],
377 head_kind: 'arvados#group',
378 link_class: 'permission',
382 group_perm = Link.create(tail_kind: 'arvados#user',
383 tail_uuid: self.uuid,
384 head_kind: 'arvados#group',
385 head_uuid: group[:uuid],
386 link_class: 'permission',
388 logger.info { "group permission: " + group_perm[:uuid] }
390 group_perm = group_perms.first
397 # Give the special "System group" permission to manage this user and
398 # all of this user's stuff.
400 def add_system_group_permission_link
401 Link.create(link_class: 'permission',
403 tail_kind: 'arvados#group',
404 tail_uuid: system_group_uuid,
405 head_kind: 'arvados#user',
406 head_uuid: self.uuid)