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 AdminNotifier
17 has_many :authorized_keys, :foreign_key => :authorized_user_uuid, :primary_key => :uuid
19 api_accessible :user, extend: :common do |t|
31 ALL_PERMISSIONS = {read: true, write: true, manage: true}
34 "#{first_name} #{last_name}".strip
39 Rails.configuration.new_users_are_active ||
40 self.groups_i_can(:read).select { |x| x.match /-f+$/ }.first)
43 def groups_i_can(verb)
44 self.group_permissions.select { |uuid, mask| mask[verb] }.keys
48 return true if is_admin
49 actions.each do |action, target|
51 if target.respond_to? :uuid
52 target_uuid = target.uuid
54 next if target_uuid == self.uuid
55 next if (group_permissions[target_uuid] and
56 group_permissions[target_uuid][action])
57 if target.respond_to? :owner_uuid
58 next if target.owner_uuid == self.uuid
59 next if (group_permissions[target.owner_uuid] and
60 group_permissions[target.owner_uuid][action])
67 def self.invalidate_permissions_cache
68 Rails.cache.delete_matched(/^groups_for_user_/)
71 # Return a hash of {group_uuid: perm_hash} where perm_hash[:read]
72 # and perm_hash[:write] are true if this user can read and write
73 # objects owned by group_uuid.
75 Rails.cache.fetch "groups_for_user_#{self.uuid}" do
77 todo = {self.uuid => true}
80 lookup_uuids = todo.keys
81 lookup_uuids.each do |uuid| done[uuid] = true end
84 Group.where('owner_uuid in (?)', lookup_uuids).each do |group|
85 newgroups << [group.owner_uuid, group.uuid, 'can_manage']
87 Link.where('tail_uuid in (?) and link_class = ? and (head_uuid like ? or head_uuid like ?)',
90 Group.uuid_like_pattern,
91 User.uuid_like_pattern).each do |link|
92 newgroups << [link.tail_uuid, link.head_uuid, link.name]
94 newgroups.each do |tail_uuid, head_uuid, perm_name|
95 unless done.has_key? head_uuid
96 todo[head_uuid] = true
101 link_permissions = {read:true}
103 link_permissions = {read:true,write:true}
105 link_permissions = ALL_PERMISSIONS
107 permissions_from[tail_uuid] ||= {}
108 permissions_from[tail_uuid][head_uuid] ||= {}
109 link_permissions.each do |k,v|
110 permissions_from[tail_uuid][head_uuid][k] ||= v
114 search_permissions(self.uuid, permissions_from)
118 def self.setup(user, openid_prefix, repo_name=nil, vm_uuid=nil)
119 return user.setup_repo_vm_links(repo_name, vm_uuid, openid_prefix)
123 def setup_repo_vm_links(repo_name, vm_uuid, openid_prefix)
124 oid_login_perm = create_oid_login_perm openid_prefix
125 repo_perm = create_user_repo_link repo_name
126 vm_login_perm = create_vm_login_permission_link vm_uuid, repo_name
127 group_perm = create_user_group_link
129 return [oid_login_perm, repo_perm, vm_login_perm, group_perm, self].compact
132 # delete user signatures, login, repo, and vm perms, and mark as inactive
134 # delete oid_login_perms for this user
135 oid_login_perms = Link.where(tail_uuid: self.email,
136 link_class: 'permission',
138 oid_login_perms.each do |perm|
142 # delete repo_perms for this user
143 repo_perms = Link.where(tail_uuid: self.uuid,
144 link_class: 'permission',
146 repo_perms.each do |perm|
150 # delete vm_login_perms for this user
151 vm_login_perms = Link.where(tail_uuid: self.uuid,
152 link_class: 'permission',
154 vm_login_perms.each do |perm|
158 # delete "All users' group read permissions for this user
159 group = Group.where(name: 'All users').select do |g|
160 g[:uuid].match /-f+$/
162 group_perms = Link.where(tail_uuid: self.uuid,
163 head_uuid: group[:uuid],
164 link_class: 'permission',
166 group_perms.each do |perm|
170 # delete any signatures by this user
171 signed_uuids = Link.where(link_class: 'signature',
172 tail_uuid: self.uuid)
173 signed_uuids.each do |sign|
177 # mark the user as inactive
178 self.is_active = false
184 def ensure_ownership_path_leads_to_user
188 def permission_to_update
189 # users must be able to update themselves (even if they are
190 # inactive) in order to create sessions
191 self == current_user or super
194 def permission_to_create
195 current_user.andand.is_admin or
196 (self == current_user and
197 self.is_active == Rails.configuration.new_users_are_active)
201 if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and Rails.configuration.auto_admin_user
202 if current_user.email == Rails.configuration.auto_admin_user
204 self.is_active = true
209 def prevent_privilege_escalation
210 if current_user.andand.is_admin
213 if self.is_active_changed?
214 if self.is_active != self.is_active_was
215 logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
216 self.is_active = self.is_active_was
219 if self.is_admin_changed?
220 if self.is_admin != self.is_admin_was
221 logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
222 self.is_admin = self.is_admin_was
228 def prevent_inactive_admin
229 if self.is_admin and not self.is_active
230 # There is no known use case for the strange set of permissions
231 # that would result from this change. It's safest to assume it's
232 # a mistake and disallow it outright.
233 raise "Admin users cannot be inactive"
238 def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
239 nextpaths = graph[start]
240 return merged if !nextpaths
241 return merged if upstream_path.has_key? start
242 upstream_path[start] = true
243 upstream_mask ||= ALL_PERMISSIONS
244 nextpaths.each do |head, mask|
247 merged[head][k] ||= v if upstream_mask[k]
249 search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
251 upstream_path.delete start
255 def create_oid_login_perm (openid_prefix)
256 login_perm_props = { "identity_url_prefix" => openid_prefix}
258 # Check oid_login_perm
259 oid_login_perms = Link.where(tail_uuid: self.email,
260 link_class: 'permission',
261 name: 'can_login').where("head_uuid = ?", self.uuid)
263 if !oid_login_perms.any?
264 # create openid login permission
265 oid_login_perm = Link.create(link_class: 'permission',
267 tail_uuid: self.email,
268 head_uuid: self.uuid,
269 properties: login_perm_props
271 logger.info { "openid login permission: " + oid_login_perm[:uuid] }
273 oid_login_perm = oid_login_perms.first
276 return oid_login_perm
279 def create_user_repo_link(repo_name)
280 # repo_name is optional
282 logger.warn ("Repository name not given for #{self.uuid}.")
286 # Check for an existing repository with the same name we're about to use.
287 repo = Repository.where(name: repo_name).first
290 logger.warn "Repository exists for #{repo_name}: #{repo[:uuid]}."
292 # Look for existing repository access for this repo
293 repo_perms = Link.where(tail_uuid: self.uuid,
294 head_uuid: repo[:uuid],
295 link_class: 'permission',
298 logger.warn "User already has repository access " +
299 repo_perms.collect { |p| p[:uuid] }.inspect
300 return repo_perms.first
304 # create repo, if does not already exist
305 repo ||= Repository.create(name: repo_name)
306 logger.info { "repo uuid: " + repo[:uuid] }
308 repo_perm = Link.create(tail_uuid: self.uuid,
309 head_uuid: repo[:uuid],
310 link_class: 'permission',
312 logger.info { "repo permission: " + repo_perm[:uuid] }
316 # create login permission for the given vm_uuid, if it does not already exist
317 def create_vm_login_permission_link(vm_uuid, repo_name)
320 # vm uuid is optional
322 vm = VirtualMachine.where(uuid: vm_uuid).first
325 logger.warn "Could not find virtual machine for #{vm_uuid.inspect}"
326 raise "No vm found for #{vm_uuid}"
332 logger.info { "vm uuid: " + vm[:uuid] }
334 login_perms = Link.where(tail_uuid: self.uuid,
335 head_uuid: vm[:uuid],
336 link_class: 'permission',
340 login_perms.each do |perm|
341 if perm.properties['username'] == repo_name
348 login_perm = perm_exists
350 login_perm = Link.create(tail_uuid: self.uuid,
351 head_uuid: vm[:uuid],
352 link_class: 'permission',
354 properties: {'username' => repo_name})
355 logger.info { "login permission: " + login_perm[:uuid] }
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 link_class: 'permission',
381 group_perm = Link.create(tail_uuid: self.uuid,
382 head_uuid: group[:uuid],
383 link_class: 'permission',
385 logger.info { "group permission: " + group_perm[:uuid] }
387 group_perm = group_perms.first
394 # Give the special "System group" permission to manage this user and
395 # all of this user's stuff.
397 def add_system_group_permission_link
398 act_as_system_user do
399 Link.create(link_class: 'permission',
401 tail_uuid: system_group_uuid,
402 head_uuid: self.uuid)