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_kind in (?)',
86 ['arvados#group', 'arvados#user']).each do |link|
87 newgroups << [link.tail_uuid, link.head_uuid, link.name]
89 newgroups.each do |tail_uuid, head_uuid, perm_name|
90 unless done.has_key? head_uuid
91 todo[head_uuid] = true
96 link_permissions = {read:true}
98 link_permissions = {read:true,write:true}
100 link_permissions = ALL_PERMISSIONS
102 permissions_from[tail_uuid] ||= {}
103 permissions_from[tail_uuid][head_uuid] ||= {}
104 link_permissions.each do |k,v|
105 permissions_from[tail_uuid][head_uuid][k] ||= v
109 search_permissions(self.uuid, permissions_from)
113 def self.setup(user, openid_prefix, repo_name=nil, vm_uuid=nil)
114 login_perm_props = {identity_url_prefix: openid_prefix}
116 # Check oid_login_perm
117 oid_login_perms = Link.where(tail_uuid: user.email,
118 head_kind: 'arvados#user',
119 link_class: 'permission',
122 if !oid_login_perms.any?
123 # create openid login permission
124 oid_login_perm = Link.create(link_class: 'permission',
127 tail_uuid: user.email,
128 head_kind: 'arvados#user',
129 head_uuid: user.uuid,
130 properties: login_perm_props
132 logger.info { "openid login permission: " + oid_login_perm[:uuid] }
134 oid_login_perm = oid_login_perms.first
137 return [oid_login_perm] + user.setup_repo_vm_links(repo_name, vm_uuid)
141 def setup_repo_vm_links(repo_name, vm_uuid)
142 repo_perm = create_user_repo_link repo_name
143 vm_login_perm = create_vm_login_permission_link vm_uuid, repo_name
144 group_perm = create_user_group_link
146 return [repo_perm, vm_login_perm, group_perm, self].compact
149 # delete user signatures, login, repo, and vm perms, and mark as inactive
151 # delete oid_login_perms for this user
152 oid_login_perms = Link.where(tail_uuid: self.email,
153 head_kind: 'arvados#user',
154 link_class: 'permission',
156 oid_login_perms.each do |perm|
160 # delete repo_perms for this user
161 repo_perms = Link.where(tail_uuid: self.uuid,
162 head_kind: 'arvados#repository',
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 head_kind: 'arvados#virtualMachine',
172 link_class: 'permission',
174 vm_login_perms.each do |perm|
178 # delete any signatures by this user
179 signed_uuids = Link.where(link_class: 'signature',
180 tail_kind: 'arvados#user',
181 tail_uuid: self.uuid)
182 signed_uuids.each do |sign|
186 # mark the user as inactive
187 self.is_active = false
193 def permission_to_update
194 # users must be able to update themselves (even if they are
195 # inactive) in order to create sessions
196 self == current_user or super
199 def permission_to_create
200 current_user.andand.is_admin or
201 (self == current_user and
202 self.is_active == Rails.configuration.new_users_are_active)
206 if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and Rails.configuration.auto_admin_user
207 if current_user.email == Rails.configuration.auto_admin_user
209 self.is_active = true
214 def prevent_privilege_escalation
215 if current_user.andand.is_admin
218 if self.is_active_changed?
219 if self.is_active != self.is_active_was
220 logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
221 self.is_active = self.is_active_was
224 if self.is_admin_changed?
225 if self.is_admin != self.is_admin_was
226 logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
227 self.is_admin = self.is_admin_was
233 def prevent_inactive_admin
234 if self.is_admin and not self.is_active
235 # There is no known use case for the strange set of permissions
236 # that would result from this change. It's safest to assume it's
237 # a mistake and disallow it outright.
238 raise "Admin users cannot be inactive"
243 def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
244 nextpaths = graph[start]
245 return merged if !nextpaths
246 return merged if upstream_path.has_key? start
247 upstream_path[start] = true
248 upstream_mask ||= ALL_PERMISSIONS
249 nextpaths.each do |head, mask|
252 merged[head][k] ||= v if upstream_mask[k]
254 search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
256 upstream_path.delete start
260 def create_user_repo_link(repo_name)
261 # repo_name is optional
263 logger.warn ("Repository name not given for #{self.uuid}.")
267 # Check for an existing repository with the same name we're about to use.
268 repo = Repository.where(name: repo_name).first
271 logger.warn "Repository exists for #{repo_name}: #{repo[:uuid]}."
273 # Look for existing repository access for this repo
274 repo_perms = Link.where(tail_uuid: self.uuid,
275 head_kind: 'arvados#repository',
276 head_uuid: repo[:uuid],
277 link_class: 'permission',
280 logger.warn "User already has repository access " +
281 repo_perms.collect { |p| p[:uuid] }.inspect
282 return repo_perms.first
286 # create repo, if does not already exist
287 repo ||= Repository.create(name: repo_name)
288 logger.info { "repo uuid: " + repo[:uuid] }
290 repo_perm = Link.create(tail_kind: 'arvados#user',
291 tail_uuid: self.uuid,
292 head_kind: 'arvados#repository',
293 head_uuid: repo[:uuid],
294 link_class: 'permission',
296 logger.info { "repo permission: " + repo_perm[:uuid] }
300 # create login permission for the given vm_uuid, if it does not already exist
301 def create_vm_login_permission_link(vm_uuid, repo_name)
304 # vm uuid is optional
306 vm = VirtualMachine.where(uuid: vm_uuid).first
309 logger.warn "Could not find virtual machine for #{vm_uuid.inspect}"
310 raise "No vm found for #{vm_uuid}"
316 logger.info { "vm uuid: " + vm[:uuid] }
318 login_perms = Link.where(tail_uuid: self.uuid,
319 head_uuid: vm[:uuid],
320 head_kind: 'arvados#virtualMachine',
321 link_class: 'permission',
324 login_perm = Link.create(tail_kind: 'arvados#user',
325 tail_uuid: self.uuid,
326 head_kind: 'arvados#virtualMachine',
327 head_uuid: vm[:uuid],
328 link_class: 'permission',
330 properties: {username: repo_name})
331 logger.info { "login permission: " + login_perm[:uuid] }
333 login_perm = login_perms.first
340 # add the user to the 'All users' group
341 def create_user_group_link
342 # Look up the "All users" group (we expect uuid *-*-fffffffffffffff).
343 group = Group.where(name: 'All users').select do |g|
344 g[:uuid].match /-f+$/
348 logger.warn "No 'All users' group with uuid '*-*-fffffffffffffff'."
349 raise "No 'All users' group with uuid '*-*-fffffffffffffff' is found"
351 logger.info { "\"All users\" group uuid: " + group[:uuid] }
353 group_perms = Link.where(tail_uuid: self.uuid,
354 head_uuid: group[:uuid],
355 head_kind: 'arvados#group',
356 link_class: 'permission',
360 group_perm = Link.create(tail_kind: 'arvados#user',
361 tail_uuid: self.uuid,
362 head_kind: 'arvados#group',
363 head_uuid: group[:uuid],
364 link_class: 'permission',
366 logger.info { "group permission: " + group_perm[:uuid] }
368 group_perm = group_perms.first
375 # Give the special "System group" permission to manage this user and
376 # all of this user's stuff.
378 def add_system_group_permission_link
379 Link.create(link_class: 'permission',
381 tail_kind: 'arvados#group',
382 tail_uuid: system_group_uuid,
383 head_kind: 'arvados#user',
384 head_uuid: self.uuid)