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 AdminNotifier
12 has_many :authorized_keys, :foreign_key => :authorized_user_uuid, :primary_key => :uuid
14 api_accessible :user, extend: :common do |t|
25 ALL_PERMISSIONS = {read: true, write: true, manage: true}
28 "#{first_name} #{last_name}"
31 def groups_i_can(verb)
32 self.group_permissions.select { |uuid, mask| mask[verb] }.keys
36 actions.each do |action, target|
38 if target.respond_to? :uuid
39 target_uuid = target.uuid
41 next if target_uuid == self.uuid
42 next if (group_permissions[target_uuid] and
43 group_permissions[target_uuid][action])
44 if target.respond_to? :owner_uuid
45 next if target.owner_uuid == self.uuid
46 next if (group_permissions[target.owner_uuid] and
47 group_permissions[target.owner_uuid][action])
54 def self.invalidate_permissions_cache
55 Rails.cache.delete_matched(/^groups_for_user_/)
58 # Return a hash of {group_uuid: perm_hash} where perm_hash[:read]
59 # and perm_hash[:write] are true if this user can read and write
60 # objects owned by group_uuid.
62 Rails.cache.fetch "groups_for_user_#{self.uuid}" do
64 todo = {self.uuid => true}
67 lookup_uuids = todo.keys
68 lookup_uuids.each do |uuid| done[uuid] = true end
70 Link.where('tail_uuid in (?) and link_class = ? and head_kind = ?',
73 'arvados#group').each do |link|
74 unless done.has_key? link.head_uuid
75 todo[link.head_uuid] = true
80 link_permissions = {read:true}
82 link_permissions = {read:true,write:true}
84 link_permissions = ALL_PERMISSIONS
86 permissions_from[link.tail_uuid] ||= {}
87 permissions_from[link.tail_uuid][link.head_uuid] ||= {}
88 link_permissions.each do |k,v|
89 permissions_from[link.tail_uuid][link.head_uuid][k] ||= v
93 search_permissions(self.uuid, permissions_from)
99 def permission_to_update
100 # users must be able to update themselves (even if they are
101 # inactive) in order to create sessions
102 self == current_user or super
105 def permission_to_create
106 current_user.andand.is_admin or
107 (self == current_user and
108 self.is_active == Rails.configuration.new_users_are_active)
112 if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and not Rails.configuration.auto_admin_user.nil?
113 if current_user.email == Rails.configuration.auto_admin_user
115 self.is_active = true
120 def prevent_privilege_escalation
121 if current_user.andand.is_admin
124 if self.is_active_changed?
125 if self.is_active != self.is_active_was
126 logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
127 self.is_active = self.is_active_was
130 if self.is_admin_changed?
131 if self.is_admin != self.is_admin_was
132 logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
133 self.is_admin = self.is_admin_was
139 def prevent_inactive_admin
140 if self.is_admin and not self.is_active
141 # There is no known use case for the strange set of permissions
142 # that would result from this change. It's safest to assume it's
143 # a mistake and disallow it outright.
144 raise "Admin users cannot be inactive"
149 def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
150 nextpaths = graph[start]
151 return merged if !nextpaths
152 return merged if upstream_path.has_key? start
153 upstream_path[start] = true
154 upstream_mask ||= ALL_PERMISSIONS
155 nextpaths.each do |head, mask|
158 merged[head][k] ||= v if upstream_mask[k]
160 search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
162 upstream_path.delete start