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 :send_admin_notifications
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 my_groups = self.group_permissions.select { |uuid, mask| mask[verb] }.keys
46 my_groups << anonymous_group_uuid
52 return true if is_admin
53 actions.each do |action, target|
55 if target.respond_to? :uuid
56 target_uuid = target.uuid
58 next if target_uuid == self.uuid
59 next if (group_permissions[target_uuid] and
60 group_permissions[target_uuid][action])
61 if target.respond_to? :owner_uuid
62 next if target.owner_uuid == self.uuid
63 next if (group_permissions[target.owner_uuid] and
64 group_permissions[target.owner_uuid][action])
71 def self.invalidate_permissions_cache
72 Rails.cache.delete_matched(/^groups_for_user_/)
75 # Return a hash of {group_uuid: perm_hash} where perm_hash[:read]
76 # and perm_hash[:write] are true if this user can read and write
77 # objects owned by group_uuid.
79 # The permission graph is built by repeatedly enumerating all
80 # permission links reachable from self.uuid, and then calling
83 Rails.cache.fetch "groups_for_user_#{self.uuid}" do
85 todo = {self.uuid => true}
87 # Build the equivalence class of permissions starting with
88 # self.uuid. On each iteration of this loop, todo contains
89 # the next set of uuids in the permission equivalence class
92 lookup_uuids = todo.keys
93 lookup_uuids.each do |uuid| done[uuid] = true end
96 # include all groups owned by the current set of uuids.
97 Group.where('owner_uuid in (?)', lookup_uuids).each do |group|
98 newgroups << [group.owner_uuid, group.uuid, 'can_manage']
100 # add any permission links from the current lookup_uuids to a
102 Link.where('tail_uuid in (?) and link_class = ? and (head_uuid like ? or head_uuid like ?)',
105 Group.uuid_like_pattern,
106 User.uuid_like_pattern).each do |link|
107 newgroups << [link.tail_uuid, link.head_uuid, link.name]
109 newgroups.each do |tail_uuid, head_uuid, perm_name|
110 unless done.has_key? head_uuid
111 todo[head_uuid] = true
113 link_permissions = {}
116 link_permissions = {read:true}
118 link_permissions = {read:true,write:true}
120 link_permissions = ALL_PERMISSIONS
122 permissions_from[tail_uuid] ||= {}
123 permissions_from[tail_uuid][head_uuid] ||= {}
124 link_permissions.each do |k,v|
125 permissions_from[tail_uuid][head_uuid][k] ||= v
129 search_permissions(self.uuid, permissions_from)
133 def self.setup(user, openid_prefix, repo_name=nil, vm_uuid=nil)
134 return user.setup_repo_vm_links(repo_name, vm_uuid, openid_prefix)
138 def setup_repo_vm_links(repo_name, vm_uuid, openid_prefix)
139 oid_login_perm = create_oid_login_perm openid_prefix
140 repo_perm = create_user_repo_link repo_name
141 vm_login_perm = create_vm_login_permission_link vm_uuid, repo_name
142 group_perm = create_user_group_link
144 return [oid_login_perm, repo_perm, vm_login_perm, group_perm, self].compact
147 # delete user signatures, login, repo, and vm perms, and mark as inactive
149 # delete oid_login_perms for this user
150 oid_login_perms = Link.where(tail_uuid: self.email,
151 link_class: 'permission',
153 oid_login_perms.each do |perm|
157 # delete repo_perms for this user
158 repo_perms = Link.where(tail_uuid: self.uuid,
159 link_class: 'permission',
161 repo_perms.each do |perm|
165 # delete vm_login_perms for this user
166 vm_login_perms = Link.where(tail_uuid: self.uuid,
167 link_class: 'permission',
169 vm_login_perms.each do |perm|
173 # delete "All users' group read permissions for this user
174 group = Group.where(name: 'All users').select do |g|
175 g[:uuid].match /-f+$/
177 group_perms = Link.where(tail_uuid: self.uuid,
178 head_uuid: group[:uuid],
179 link_class: 'permission',
181 group_perms.each do |perm|
185 # delete any signatures by this user
186 signed_uuids = Link.where(link_class: 'signature',
187 tail_uuid: self.uuid)
188 signed_uuids.each do |sign|
192 # mark the user as inactive
193 self.is_active = false
199 def ensure_ownership_path_leads_to_user
203 def permission_to_update
204 # users must be able to update themselves (even if they are
205 # inactive) in order to create sessions
206 self == current_user or super
209 def permission_to_create
210 current_user.andand.is_admin or
211 (self == current_user and
212 self.is_active == Rails.configuration.new_users_are_active)
216 if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and Rails.configuration.auto_admin_user
217 if current_user.email == Rails.configuration.auto_admin_user
219 self.is_active = true
224 def prevent_privilege_escalation
225 if current_user.andand.is_admin
228 if self.is_active_changed?
229 if self.is_active != self.is_active_was
230 logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
231 self.is_active = self.is_active_was
234 if self.is_admin_changed?
235 if self.is_admin != self.is_admin_was
236 logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
237 self.is_admin = self.is_admin_was
243 def prevent_inactive_admin
244 if self.is_admin and not self.is_active
245 # There is no known use case for the strange set of permissions
246 # that would result from this change. It's safest to assume it's
247 # a mistake and disallow it outright.
248 raise "Admin users cannot be inactive"
253 def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
254 nextpaths = graph[start]
255 return merged if !nextpaths
256 return merged if upstream_path.has_key? start
257 upstream_path[start] = true
258 upstream_mask ||= ALL_PERMISSIONS
259 nextpaths.each do |head, mask|
262 merged[head][k] ||= v if upstream_mask[k]
264 search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
266 upstream_path.delete start
270 def create_oid_login_perm (openid_prefix)
271 login_perm_props = { "identity_url_prefix" => openid_prefix}
273 # Check oid_login_perm
274 oid_login_perms = Link.where(tail_uuid: self.email,
275 link_class: 'permission',
276 name: 'can_login').where("head_uuid = ?", self.uuid)
278 if !oid_login_perms.any?
279 # create openid login permission
280 oid_login_perm = Link.create(link_class: 'permission',
282 tail_uuid: self.email,
283 head_uuid: self.uuid,
284 properties: login_perm_props
286 logger.info { "openid login permission: " + oid_login_perm[:uuid] }
288 oid_login_perm = oid_login_perms.first
291 return oid_login_perm
294 def create_user_repo_link(repo_name)
295 # repo_name is optional
297 logger.warn ("Repository name not given for #{self.uuid}.")
301 # Check for an existing repository with the same name we're about to use.
302 repo = Repository.where(name: repo_name).first
305 logger.warn "Repository exists for #{repo_name}: #{repo[:uuid]}."
307 # Look for existing repository access for this repo
308 repo_perms = Link.where(tail_uuid: self.uuid,
309 head_uuid: repo[:uuid],
310 link_class: 'permission',
313 logger.warn "User already has repository access " +
314 repo_perms.collect { |p| p[:uuid] }.inspect
315 return repo_perms.first
319 # create repo, if does not already exist
320 repo ||= Repository.create(name: repo_name)
321 logger.info { "repo uuid: " + repo[:uuid] }
323 repo_perm = Link.create(tail_uuid: self.uuid,
324 head_uuid: repo[:uuid],
325 link_class: 'permission',
327 logger.info { "repo permission: " + repo_perm[:uuid] }
331 # create login permission for the given vm_uuid, if it does not already exist
332 def create_vm_login_permission_link(vm_uuid, repo_name)
335 # vm uuid is optional
337 vm = VirtualMachine.where(uuid: vm_uuid).first
340 logger.warn "Could not find virtual machine for #{vm_uuid.inspect}"
341 raise "No vm found for #{vm_uuid}"
347 logger.info { "vm uuid: " + vm[:uuid] }
349 login_perms = Link.where(tail_uuid: self.uuid,
350 head_uuid: vm[:uuid],
351 link_class: 'permission',
355 login_perms.each do |perm|
356 if perm.properties['username'] == repo_name
363 login_perm = perm_exists
365 login_perm = Link.create(tail_uuid: self.uuid,
366 head_uuid: vm[:uuid],
367 link_class: 'permission',
369 properties: {'username' => repo_name})
370 logger.info { "login permission: " + login_perm[:uuid] }
377 # add the user to the 'All users' group
378 def create_user_group_link
379 # Look up the "All users" group (we expect uuid *-*-fffffffffffffff).
380 group = Group.where(name: 'All users').select do |g|
381 g[:uuid].match /-f+$/
385 logger.warn "No 'All users' group with uuid '*-*-fffffffffffffff'."
386 raise "No 'All users' group with uuid '*-*-fffffffffffffff' is found"
388 logger.info { "\"All users\" group uuid: " + group[:uuid] }
390 group_perms = Link.where(tail_uuid: self.uuid,
391 head_uuid: group[:uuid],
392 link_class: 'permission',
396 group_perm = Link.create(tail_uuid: self.uuid,
397 head_uuid: group[:uuid],
398 link_class: 'permission',
400 logger.info { "group permission: " + group_perm[:uuid] }
402 group_perm = group_perms.first
409 # Give the special "System group" permission to manage this user and
410 # all of this user's stuff.
412 def add_system_group_permission_link
413 act_as_system_user do
414 Link.create(link_class: 'permission',
416 tail_uuid: system_group_uuid,
417 head_uuid: self.uuid)
421 # Send admin notifications
422 def send_admin_notifications
423 AdminNotifier.new_user(self).deliver
424 if not self.is_active then
425 AdminNotifier.new_inactive_user(self).deliver