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_uuid like ? or head_uuid like ?)',
86 Group.uuid_like_pattern,
87 User.uuid_like_pattern).each do |link|
88 newgroups << [link.tail_uuid, link.head_uuid, link.name]
90 newgroups.each do |tail_uuid, head_uuid, perm_name|
91 unless done.has_key? head_uuid
92 todo[head_uuid] = true
97 link_permissions = {read:true}
99 link_permissions = {read:true,write:true}
101 link_permissions = ALL_PERMISSIONS
103 permissions_from[tail_uuid] ||= {}
104 permissions_from[tail_uuid][head_uuid] ||= {}
105 link_permissions.each do |k,v|
106 permissions_from[tail_uuid][head_uuid][k] ||= v
110 search_permissions(self.uuid, permissions_from)
114 def self.setup(user, openid_prefix, repo_name=nil, vm_uuid=nil)
115 return user.setup_repo_vm_links(repo_name, vm_uuid, openid_prefix)
119 def setup_repo_vm_links(repo_name, vm_uuid, openid_prefix)
120 oid_login_perm = create_oid_login_perm openid_prefix
121 repo_perm = create_user_repo_link repo_name
122 vm_login_perm = create_vm_login_permission_link vm_uuid, repo_name
123 group_perm = create_user_group_link
125 return [oid_login_perm, repo_perm, vm_login_perm, group_perm, self].compact
128 # delete user signatures, login, repo, and vm perms, and mark as inactive
130 # delete oid_login_perms for this user
131 oid_login_perms = Link.where(tail_uuid: self.email,
132 link_class: 'permission',
134 oid_login_perms.each do |perm|
138 # delete repo_perms for this user
139 repo_perms = Link.where(tail_uuid: self.uuid,
140 link_class: 'permission',
142 repo_perms.each do |perm|
146 # delete vm_login_perms for this user
147 vm_login_perms = Link.where(tail_uuid: self.uuid,
148 link_class: 'permission',
150 vm_login_perms.each do |perm|
154 # delete "All users' group read permissions for this user
155 group = Group.where(name: 'All users').select do |g|
156 g[:uuid].match /-f+$/
158 group_perms = Link.where(tail_uuid: self.uuid,
159 head_uuid: group[:uuid],
160 link_class: 'permission',
162 group_perms.each do |perm|
166 # delete any signatures by this user
167 signed_uuids = Link.where(link_class: 'signature',
168 tail_uuid: self.uuid)
169 signed_uuids.each do |sign|
173 # mark the user as inactive
174 self.is_active = false
180 def permission_to_update
181 # users must be able to update themselves (even if they are
182 # inactive) in order to create sessions
183 self == current_user or super
186 def permission_to_create
187 current_user.andand.is_admin or
188 (self == current_user and
189 self.is_active == Rails.configuration.new_users_are_active)
193 if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and Rails.configuration.auto_admin_user
194 if current_user.email == Rails.configuration.auto_admin_user
196 self.is_active = true
201 def prevent_privilege_escalation
202 if current_user.andand.is_admin
205 if self.is_active_changed?
206 if self.is_active != self.is_active_was
207 logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
208 self.is_active = self.is_active_was
211 if self.is_admin_changed?
212 if self.is_admin != self.is_admin_was
213 logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
214 self.is_admin = self.is_admin_was
220 def prevent_inactive_admin
221 if self.is_admin and not self.is_active
222 # There is no known use case for the strange set of permissions
223 # that would result from this change. It's safest to assume it's
224 # a mistake and disallow it outright.
225 raise "Admin users cannot be inactive"
230 def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
231 nextpaths = graph[start]
232 return merged if !nextpaths
233 return merged if upstream_path.has_key? start
234 upstream_path[start] = true
235 upstream_mask ||= ALL_PERMISSIONS
236 nextpaths.each do |head, mask|
239 merged[head][k] ||= v if upstream_mask[k]
241 search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
243 upstream_path.delete start
247 def create_oid_login_perm (openid_prefix)
248 login_perm_props = {identity_url_prefix: openid_prefix}
250 # Check oid_login_perm
251 oid_login_perms = Link.where(tail_uuid: self.email,
252 link_class: 'permission',
253 name: 'can_login').where("head_uuid like ?", User.uuid_like_pattern)
255 if !oid_login_perms.any?
256 # create openid login permission
257 oid_login_perm = Link.create(link_class: 'permission',
259 tail_uuid: self.email,
260 head_uuid: self.uuid,
261 properties: login_perm_props
263 logger.info { "openid login permission: " + oid_login_perm[:uuid] }
265 oid_login_perm = oid_login_perms.first
268 return oid_login_perm
271 def create_user_repo_link(repo_name)
272 # repo_name is optional
274 logger.warn ("Repository name not given for #{self.uuid}.")
278 # Check for an existing repository with the same name we're about to use.
279 repo = Repository.where(name: repo_name).first
282 logger.warn "Repository exists for #{repo_name}: #{repo[:uuid]}."
284 # Look for existing repository access for this repo
285 repo_perms = Link.where(tail_uuid: self.uuid,
286 head_uuid: repo[:uuid],
287 link_class: 'permission',
290 logger.warn "User already has repository access " +
291 repo_perms.collect { |p| p[:uuid] }.inspect
292 return repo_perms.first
296 # create repo, if does not already exist
297 repo ||= Repository.create(name: repo_name)
298 logger.info { "repo uuid: " + repo[:uuid] }
300 repo_perm = Link.create(tail_uuid: self.uuid,
301 head_uuid: repo[:uuid],
302 link_class: 'permission',
304 logger.info { "repo permission: " + repo_perm[:uuid] }
308 # create login permission for the given vm_uuid, if it does not already exist
309 def create_vm_login_permission_link(vm_uuid, repo_name)
312 # vm uuid is optional
314 vm = VirtualMachine.where(uuid: vm_uuid).first
317 logger.warn "Could not find virtual machine for #{vm_uuid.inspect}"
318 raise "No vm found for #{vm_uuid}"
324 logger.info { "vm uuid: " + vm[:uuid] }
326 login_perms = Link.where(tail_uuid: self.uuid,
327 head_uuid: vm[:uuid],
328 link_class: 'permission',
332 login_perms.each do |perm|
333 if perm.properties[:username] == repo_name
340 login_perm = Link.create(tail_uuid: self.uuid,
341 head_uuid: vm[:uuid],
342 link_class: 'permission',
344 properties: {username: repo_name})
345 logger.info { "login permission: " + login_perm[:uuid] }
347 login_perm = login_perms.first
354 # add the user to the 'All users' group
355 def create_user_group_link
356 # Look up the "All users" group (we expect uuid *-*-fffffffffffffff).
357 group = Group.where(name: 'All users').select do |g|
358 g[:uuid].match /-f+$/
362 logger.warn "No 'All users' group with uuid '*-*-fffffffffffffff'."
363 raise "No 'All users' group with uuid '*-*-fffffffffffffff' is found"
365 logger.info { "\"All users\" group uuid: " + group[:uuid] }
367 group_perms = Link.where(tail_uuid: self.uuid,
368 head_uuid: group[:uuid],
369 link_class: 'permission',
373 group_perm = Link.create(tail_uuid: self.uuid,
374 head_uuid: group[:uuid],
375 link_class: 'permission',
377 logger.info { "group permission: " + group_perm[:uuid] }
379 group_perm = group_perms.first
386 # Give the special "System group" permission to manage this user and
387 # all of this user's stuff.
389 def add_system_group_permission_link
390 act_as_system_user do
391 Link.create(link_class: 'permission',
393 tail_uuid: system_group_uuid,
394 head_uuid: self.uuid)