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
59 target = ArvadosModel.find_by_uuid(target_uuid)
62 next if target_uuid == self.uuid
63 next if (group_permissions[target_uuid] and
64 group_permissions[target_uuid][action])
65 if target.respond_to? :owner_uuid
66 next if target.owner_uuid == self.uuid
67 next if (group_permissions[target.owner_uuid] and
68 group_permissions[target.owner_uuid][action])
75 def self.invalidate_permissions_cache
76 Rails.cache.delete_matched(/^groups_for_user_/)
79 # Return a hash of {group_uuid: perm_hash} where perm_hash[:read]
80 # and perm_hash[:write] are true if this user can read and write
81 # objects owned by group_uuid.
83 # The permission graph is built by repeatedly enumerating all
84 # permission links reachable from self.uuid, and then calling
87 Rails.cache.fetch "groups_for_user_#{self.uuid}" do
89 todo = {self.uuid => true}
91 # Build the equivalence class of permissions starting with
92 # self.uuid. On each iteration of this loop, todo contains
93 # the next set of uuids in the permission equivalence class
96 lookup_uuids = todo.keys
97 lookup_uuids.each do |uuid| done[uuid] = true end
100 # include all groups owned by the current set of uuids.
101 Group.where('owner_uuid in (?)', lookup_uuids).each do |group|
102 newgroups << [group.owner_uuid, group.uuid, 'can_manage']
104 # add any permission links from the current lookup_uuids to a
106 Link.where('tail_uuid in (?) and link_class = ? and (head_uuid like ? or head_uuid like ?)',
109 Group.uuid_like_pattern,
110 User.uuid_like_pattern).each do |link|
111 newgroups << [link.tail_uuid, link.head_uuid, link.name]
113 newgroups.each do |tail_uuid, head_uuid, perm_name|
114 unless done.has_key? head_uuid
115 todo[head_uuid] = true
117 link_permissions = {}
120 link_permissions = {read:true}
122 link_permissions = {read:true,write:true}
124 link_permissions = ALL_PERMISSIONS
126 permissions_from[tail_uuid] ||= {}
127 permissions_from[tail_uuid][head_uuid] ||= {}
128 link_permissions.each do |k,v|
129 permissions_from[tail_uuid][head_uuid][k] ||= v
133 search_permissions(self.uuid, permissions_from)
137 def self.setup(user, openid_prefix, repo_name=nil, vm_uuid=nil)
138 return user.setup_repo_vm_links(repo_name, vm_uuid, openid_prefix)
142 def setup_repo_vm_links(repo_name, vm_uuid, openid_prefix)
143 oid_login_perm = create_oid_login_perm openid_prefix
144 repo_perm = create_user_repo_link repo_name
145 vm_login_perm = create_vm_login_permission_link vm_uuid, repo_name
146 group_perm = create_user_group_link
148 return [oid_login_perm, repo_perm, vm_login_perm, group_perm, self].compact
151 # delete user signatures, login, repo, and vm perms, and mark as inactive
153 # delete oid_login_perms for this user
154 oid_login_perms = Link.where(tail_uuid: self.email,
155 link_class: 'permission',
157 oid_login_perms.each do |perm|
161 # delete repo_perms for this user
162 repo_perms = Link.where(tail_uuid: self.uuid,
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 link_class: 'permission',
173 vm_login_perms.each do |perm|
177 # delete "All users' group read permissions for this user
178 group = Group.where(name: 'All users').select do |g|
179 g[:uuid].match /-f+$/
181 group_perms = Link.where(tail_uuid: self.uuid,
182 head_uuid: group[:uuid],
183 link_class: 'permission',
185 group_perms.each do |perm|
189 # delete any signatures by this user
190 signed_uuids = Link.where(link_class: 'signature',
191 tail_uuid: self.uuid)
192 signed_uuids.each do |sign|
196 # mark the user as inactive
197 self.is_active = false
203 def ensure_ownership_path_leads_to_user
207 def permission_to_update
208 # users must be able to update themselves (even if they are
209 # inactive) in order to create sessions
210 self == current_user or super
213 def permission_to_create
214 current_user.andand.is_admin or
215 (self == current_user and
216 self.is_active == Rails.configuration.new_users_are_active)
220 if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and Rails.configuration.auto_admin_user
221 if self.email == Rails.configuration.auto_admin_user
223 self.is_active = true
228 def prevent_privilege_escalation
229 if current_user.andand.is_admin
232 if self.is_active_changed?
233 if self.is_active != self.is_active_was
234 logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
235 self.is_active = self.is_active_was
238 if self.is_admin_changed?
239 if self.is_admin != self.is_admin_was
240 logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
241 self.is_admin = self.is_admin_was
247 def prevent_inactive_admin
248 if self.is_admin and not self.is_active
249 # There is no known use case for the strange set of permissions
250 # that would result from this change. It's safest to assume it's
251 # a mistake and disallow it outright.
252 raise "Admin users cannot be inactive"
257 def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
258 nextpaths = graph[start]
259 return merged if !nextpaths
260 return merged if upstream_path.has_key? start
261 upstream_path[start] = true
262 upstream_mask ||= ALL_PERMISSIONS
263 nextpaths.each do |head, mask|
266 merged[head][k] ||= v if upstream_mask[k]
268 search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
270 upstream_path.delete start
274 def create_oid_login_perm (openid_prefix)
275 login_perm_props = { "identity_url_prefix" => openid_prefix}
277 # Check oid_login_perm
278 oid_login_perms = Link.where(tail_uuid: self.email,
279 link_class: 'permission',
280 name: 'can_login').where("head_uuid = ?", self.uuid)
282 if !oid_login_perms.any?
283 # create openid login permission
284 oid_login_perm = Link.create(link_class: 'permission',
286 tail_uuid: self.email,
287 head_uuid: self.uuid,
288 properties: login_perm_props
290 logger.info { "openid login permission: " + oid_login_perm[:uuid] }
292 oid_login_perm = oid_login_perms.first
295 return oid_login_perm
298 def create_user_repo_link(repo_name)
299 # repo_name is optional
301 logger.warn ("Repository name not given for #{self.uuid}.")
305 # Check for an existing repository with the same name we're about to use.
306 repo = Repository.where(name: repo_name).first
309 logger.warn "Repository exists for #{repo_name}: #{repo[:uuid]}."
311 # Look for existing repository access for this repo
312 repo_perms = Link.where(tail_uuid: self.uuid,
313 head_uuid: repo[:uuid],
314 link_class: 'permission',
317 logger.warn "User already has repository access " +
318 repo_perms.collect { |p| p[:uuid] }.inspect
319 return repo_perms.first
323 # create repo, if does not already exist
324 repo ||= Repository.create(name: repo_name)
325 logger.info { "repo uuid: " + repo[:uuid] }
327 repo_perm = Link.create(tail_uuid: self.uuid,
328 head_uuid: repo[:uuid],
329 link_class: 'permission',
331 logger.info { "repo permission: " + repo_perm[:uuid] }
335 # create login permission for the given vm_uuid, if it does not already exist
336 def create_vm_login_permission_link(vm_uuid, repo_name)
339 # vm uuid is optional
341 vm = VirtualMachine.where(uuid: vm_uuid).first
344 logger.warn "Could not find virtual machine for #{vm_uuid.inspect}"
345 raise "No vm found for #{vm_uuid}"
351 logger.info { "vm uuid: " + vm[:uuid] }
353 login_perms = Link.where(tail_uuid: self.uuid,
354 head_uuid: vm[:uuid],
355 link_class: 'permission',
359 login_perms.each do |perm|
360 if perm.properties['username'] == repo_name
367 login_perm = perm_exists
369 login_perm = Link.create(tail_uuid: self.uuid,
370 head_uuid: vm[:uuid],
371 link_class: 'permission',
373 properties: {'username' => repo_name})
374 logger.info { "login permission: " + login_perm[:uuid] }
381 # add the user to the 'All users' group
382 def create_user_group_link
383 # Look up the "All users" group (we expect uuid *-*-fffffffffffffff).
384 group = Group.where(name: 'All users').select do |g|
385 g[:uuid].match /-f+$/
389 logger.warn "No 'All users' group with uuid '*-*-fffffffffffffff'."
390 raise "No 'All users' group with uuid '*-*-fffffffffffffff' is found"
392 logger.info { "\"All users\" group uuid: " + group[:uuid] }
394 group_perms = Link.where(tail_uuid: self.uuid,
395 head_uuid: group[:uuid],
396 link_class: 'permission',
400 group_perm = Link.create(tail_uuid: self.uuid,
401 head_uuid: group[:uuid],
402 link_class: 'permission',
404 logger.info { "group permission: " + group_perm[:uuid] }
406 group_perm = group_perms.first
413 # Give the special "System group" permission to manage this user and
414 # all of this user's stuff.
416 def add_system_group_permission_link
417 act_as_system_user do
418 Link.create(link_class: 'permission',
420 tail_uuid: system_group_uuid,
421 head_uuid: self.uuid)
425 # Send admin notifications
426 def send_admin_notifications
427 AdminNotifier.new_user(self).deliver
428 if not self.is_active then
429 AdminNotifier.new_inactive_user(self).deliver