1 require 'can_be_an_owner'
3 class User < ArvadosModel
6 include CommonApiTemplate
10 has_many :api_client_authorizations
11 before_update :prevent_privilege_escalation
12 before_update :prevent_inactive_admin
13 before_create :check_auto_admin
14 after_create :add_system_group_permission_link
15 after_create :auto_setup_new_user
16 after_create :send_admin_notifications
17 after_update :send_profile_created_notification
20 has_many :authorized_keys, :foreign_key => :authorized_user_uuid, :primary_key => :uuid
22 api_accessible :user, extend: :common do |t|
34 ALL_PERMISSIONS = {read: true, write: true, manage: true}
37 "#{first_name} #{last_name}".strip
42 Rails.configuration.new_users_are_active ||
43 self.groups_i_can(:read).select { |x| x.match /-f+$/ }.first)
46 def groups_i_can(verb)
47 my_groups = self.group_permissions.select { |uuid, mask| mask[verb] }.keys
49 my_groups << anonymous_group_uuid
55 return true if is_admin
56 actions.each do |action, target|
58 if target.respond_to? :uuid
59 target_uuid = target.uuid
62 target = ArvadosModel.find_by_uuid(target_uuid)
65 next if target_uuid == self.uuid
66 next if (group_permissions[target_uuid] and
67 group_permissions[target_uuid][action])
68 if target.respond_to? :owner_uuid
69 next if target.owner_uuid == self.uuid
70 next if (group_permissions[target.owner_uuid] and
71 group_permissions[target.owner_uuid][action])
78 def self.invalidate_permissions_cache
79 Rails.cache.delete_matched(/^groups_for_user_/)
82 # Return a hash of {group_uuid: perm_hash} where perm_hash[:read]
83 # and perm_hash[:write] are true if this user can read and write
84 # objects owned by group_uuid.
86 # The permission graph is built by repeatedly enumerating all
87 # permission links reachable from self.uuid, and then calling
90 Rails.cache.fetch "groups_for_user_#{self.uuid}" do
92 todo = {self.uuid => true}
94 # Build the equivalence class of permissions starting with
95 # self.uuid. On each iteration of this loop, todo contains
96 # the next set of uuids in the permission equivalence class
99 lookup_uuids = todo.keys
100 lookup_uuids.each do |uuid| done[uuid] = true end
103 # include all groups owned by the current set of uuids.
104 Group.where('owner_uuid in (?)', lookup_uuids).each do |group|
105 newgroups << [group.owner_uuid, group.uuid, 'can_manage']
107 # add any permission links from the current lookup_uuids to a
109 Link.where('tail_uuid in (?) and link_class = ? and (head_uuid like ? or head_uuid like ?)',
112 Group.uuid_like_pattern,
113 User.uuid_like_pattern).each do |link|
114 newgroups << [link.tail_uuid, link.head_uuid, link.name]
116 newgroups.each do |tail_uuid, head_uuid, perm_name|
117 unless done.has_key? head_uuid
118 todo[head_uuid] = true
120 link_permissions = {}
123 link_permissions = {read:true}
125 link_permissions = {read:true,write:true}
127 link_permissions = ALL_PERMISSIONS
129 permissions_from[tail_uuid] ||= {}
130 permissions_from[tail_uuid][head_uuid] ||= {}
131 link_permissions.each do |k,v|
132 permissions_from[tail_uuid][head_uuid][k] ||= v
136 search_permissions(self.uuid, permissions_from)
140 def self.setup(user, openid_prefix, repo_name=nil, vm_uuid=nil)
141 return user.setup_repo_vm_links(repo_name, vm_uuid, openid_prefix)
145 def setup_repo_vm_links(repo_name, vm_uuid, openid_prefix)
146 oid_login_perm = create_oid_login_perm openid_prefix
147 repo_perm = create_user_repo_link repo_name
148 vm_login_perm = create_vm_login_permission_link vm_uuid, repo_name
149 group_perm = create_user_group_link
151 return [oid_login_perm, repo_perm, vm_login_perm, group_perm, self].compact
154 # delete user signatures, login, repo, and vm perms, and mark as inactive
156 # delete oid_login_perms for this user
157 Link.destroy_all(tail_uuid: self.email,
158 link_class: 'permission',
161 # delete repo_perms for this user
162 Link.destroy_all(tail_uuid: self.uuid,
163 link_class: 'permission',
166 # delete vm_login_perms for this user
167 Link.destroy_all(tail_uuid: self.uuid,
168 link_class: 'permission',
171 # delete "All users" group read permissions for this user
172 group = Group.where(name: 'All users').select do |g|
173 g[:uuid].match /-f+$/
175 Link.destroy_all(tail_uuid: self.uuid,
176 head_uuid: group[:uuid],
177 link_class: 'permission',
180 # delete any signatures by this user
181 Link.destroy_all(link_class: 'signature',
182 tail_uuid: self.uuid)
184 # delete user preferences (including profile)
187 # mark the user as inactive
188 self.is_active = false
194 def ensure_ownership_path_leads_to_user
198 def permission_to_update
199 # users must be able to update themselves (even if they are
200 # inactive) in order to create sessions
201 self == current_user or super
204 def permission_to_create
205 current_user.andand.is_admin or
206 (self == current_user and
207 self.is_active == Rails.configuration.new_users_are_active)
211 if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and Rails.configuration.auto_admin_user
212 if self.email == Rails.configuration.auto_admin_user
214 self.is_active = true
219 def prevent_privilege_escalation
220 if current_user.andand.is_admin
223 if self.is_active_changed?
224 if self.is_active != self.is_active_was
225 logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
226 self.is_active = self.is_active_was
229 if self.is_admin_changed?
230 if self.is_admin != self.is_admin_was
231 logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
232 self.is_admin = self.is_admin_was
238 def prevent_inactive_admin
239 if self.is_admin and not self.is_active
240 # There is no known use case for the strange set of permissions
241 # that would result from this change. It's safest to assume it's
242 # a mistake and disallow it outright.
243 raise "Admin users cannot be inactive"
248 def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
249 nextpaths = graph[start]
250 return merged if !nextpaths
251 return merged if upstream_path.has_key? start
252 upstream_path[start] = true
253 upstream_mask ||= ALL_PERMISSIONS
254 nextpaths.each do |head, mask|
257 merged[head][k] ||= v if upstream_mask[k]
259 search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
261 upstream_path.delete start
265 def create_oid_login_perm (openid_prefix)
266 login_perm_props = { "identity_url_prefix" => openid_prefix}
268 # Check oid_login_perm
269 oid_login_perms = Link.where(tail_uuid: self.email,
270 link_class: 'permission',
271 name: 'can_login').where("head_uuid = ?", self.uuid)
273 if !oid_login_perms.any?
274 # create openid login permission
275 oid_login_perm = Link.create(link_class: 'permission',
277 tail_uuid: self.email,
278 head_uuid: self.uuid,
279 properties: login_perm_props
281 logger.info { "openid login permission: " + oid_login_perm[:uuid] }
283 oid_login_perm = oid_login_perms.first
286 return oid_login_perm
289 def create_user_repo_link(repo_name)
290 # repo_name is optional
292 logger.warn ("Repository name not given for #{self.uuid}.")
296 # Check for an existing repository with the same name we're about to use.
297 repo = Repository.where(name: repo_name).first
300 logger.warn "Repository exists for #{repo_name}: #{repo[:uuid]}."
302 # Look for existing repository access for this repo
303 repo_perms = Link.where(tail_uuid: self.uuid,
304 head_uuid: repo[:uuid],
305 link_class: 'permission',
308 logger.warn "User already has repository access " +
309 repo_perms.collect { |p| p[:uuid] }.inspect
310 return repo_perms.first
314 # create repo, if does not already exist
315 repo ||= Repository.create(name: repo_name)
316 logger.info { "repo uuid: " + repo[:uuid] }
318 repo_perm = Link.create(tail_uuid: self.uuid,
319 head_uuid: repo[:uuid],
320 link_class: 'permission',
322 logger.info { "repo permission: " + repo_perm[:uuid] }
326 # create login permission for the given vm_uuid, if it does not already exist
327 def create_vm_login_permission_link(vm_uuid, repo_name)
330 # vm uuid is optional
332 vm = VirtualMachine.where(uuid: vm_uuid).first
335 logger.warn "Could not find virtual machine for #{vm_uuid.inspect}"
336 raise "No vm found for #{vm_uuid}"
342 logger.info { "vm uuid: " + vm[:uuid] }
344 login_perms = Link.where(tail_uuid: self.uuid,
345 head_uuid: vm[:uuid],
346 link_class: 'permission',
350 login_perms.each do |perm|
351 if perm.properties['username'] == repo_name
358 login_perm = perm_exists
360 login_perm = Link.create(tail_uuid: self.uuid,
361 head_uuid: vm[:uuid],
362 link_class: 'permission',
364 properties: {'username' => repo_name})
365 logger.info { "login permission: " + login_perm[:uuid] }
372 # add the user to the 'All users' group
373 def create_user_group_link
374 # Look up the "All users" group (we expect uuid *-*-fffffffffffffff).
375 group = Group.where(name: 'All users').select do |g|
376 g[:uuid].match /-f+$/
380 logger.warn "No 'All users' group with uuid '*-*-fffffffffffffff'."
381 raise "No 'All users' group with uuid '*-*-fffffffffffffff' is found"
383 logger.info { "\"All users\" group uuid: " + group[:uuid] }
385 group_perms = Link.where(tail_uuid: self.uuid,
386 head_uuid: group[:uuid],
387 link_class: 'permission',
391 group_perm = Link.create(tail_uuid: self.uuid,
392 head_uuid: group[:uuid],
393 link_class: 'permission',
395 logger.info { "group permission: " + group_perm[:uuid] }
397 group_perm = group_perms.first
404 # Give the special "System group" permission to manage this user and
405 # all of this user's stuff.
407 def add_system_group_permission_link
408 act_as_system_user do
409 Link.create(link_class: 'permission',
411 tail_uuid: system_group_uuid,
412 head_uuid: self.uuid)
416 # Send admin notifications
417 def send_admin_notifications
418 AdminNotifier.new_user(self).deliver
419 if not self.is_active then
420 AdminNotifier.new_inactive_user(self).deliver
424 # Automatically setup new user during creation
425 def auto_setup_new_user
426 return true if !Rails.configuration.auto_setup_new_users
427 return true if !self.email
429 if Rails.configuration.auto_setup_new_users_with_vm_uuid ||
430 Rails.configuration.auto_setup_new_users_with_repository
431 username = self.email.partition('@')[0] if self.email
432 return true if !username
434 blacklisted_usernames = Rails.configuration.auto_setup_name_blacklist
435 if blacklisted_usernames.include?(username)
437 elsif !(/^[a-zA-Z][-._a-zA-Z0-9]{0,30}[a-zA-Z0-9]$/.match(username))
440 username = derive_unique_username username
444 if !Rails.configuration.auto_setup_new_users_with_vm_uuid &&
445 !Rails.configuration.auto_setup_new_users_with_repository
446 oid_login_perm = create_oid_login_perm Rails.configuration.default_openid_prefix
447 group_perm = create_user_group_link
449 setup_repo_vm_links(username,
450 Rails.configuration.auto_setup_new_users_with_vm_uuid,
451 Rails.configuration.default_openid_prefix)
455 # Find a username that starts with the given string and does not collide
456 # with any existing repository name or VM login name
457 def derive_unique_username username
458 vm_uuid = Rails.configuration.auto_setup_new_users_with_vm_uuid
460 if Repository.where(name: username).empty?
461 login_collisions = Link.where(head_uuid: vm_uuid,
462 link_class: 'permission',
463 name: 'can_login').select do |perm|
464 perm.properties['username'] == username
466 if login_collisions.empty?
470 username = username + SecureRandom.random_number(100).to_s
474 # Send notification if the user saved profile for the first time
475 def send_profile_created_notification
476 if self.prefs_changed?
477 if self.prefs_was.andand.empty? || !self.prefs_was.andand['profile']
478 profile_notification_address = Rails.configuration.user_profile_notification_address
479 ProfileNotifier.profile_created(self, profile_notification_address).deliver if profile_notification_address