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|
26 ALL_PERMISSIONS = {read: true, write: true, manage: true}
29 "#{first_name} #{last_name}"
34 Rails.configuration.new_users_are_active ||
35 self.groups_i_can(:read).select { |x| x.match /-f+$/ }.first)
38 def groups_i_can(verb)
39 self.group_permissions.select { |uuid, mask| mask[verb] }.keys
43 actions.each do |action, target|
45 if target.respond_to? :uuid
46 target_uuid = target.uuid
48 next if target_uuid == self.uuid
49 next if (group_permissions[target_uuid] and
50 group_permissions[target_uuid][action])
51 if target.respond_to? :owner_uuid
52 next if target.owner_uuid == self.uuid
53 next if (group_permissions[target.owner_uuid] and
54 group_permissions[target.owner_uuid][action])
61 def self.invalidate_permissions_cache
62 Rails.cache.delete_matched(/^groups_for_user_/)
65 # Return a hash of {group_uuid: perm_hash} where perm_hash[:read]
66 # and perm_hash[:write] are true if this user can read and write
67 # objects owned by group_uuid.
69 Rails.cache.fetch "groups_for_user_#{self.uuid}" do
71 todo = {self.uuid => true}
74 lookup_uuids = todo.keys
75 lookup_uuids.each do |uuid| done[uuid] = true end
77 Link.where('tail_uuid in (?) and link_class = ? and head_kind = ?',
80 'arvados#group').each do |link|
81 unless done.has_key? link.head_uuid
82 todo[link.head_uuid] = true
87 link_permissions = {read:true}
89 link_permissions = {read:true,write:true}
91 link_permissions = ALL_PERMISSIONS
93 permissions_from[link.tail_uuid] ||= {}
94 permissions_from[link.tail_uuid][link.head_uuid] ||= {}
95 link_permissions.each do |k,v|
96 permissions_from[link.tail_uuid][link.head_uuid][k] ||= v
100 search_permissions(self.uuid, permissions_from)
106 def permission_to_update
107 # users must be able to update themselves (even if they are
108 # inactive) in order to create sessions
109 self == current_user or super
112 def permission_to_create
113 current_user.andand.is_admin or
114 (self == current_user and
115 self.is_active == Rails.configuration.new_users_are_active)
119 if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and not Rails.configuration.auto_admin_user.nil?
120 if current_user.email == Rails.configuration.auto_admin_user
122 self.is_active = true
127 def prevent_privilege_escalation
128 if current_user.andand.is_admin
131 if self.is_active_changed?
132 if self.is_active != self.is_active_was
133 logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
134 self.is_active = self.is_active_was
137 if self.is_admin_changed?
138 if self.is_admin != self.is_admin_was
139 logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
140 self.is_admin = self.is_admin_was
146 def prevent_inactive_admin
147 if self.is_admin and not self.is_active
148 # There is no known use case for the strange set of permissions
149 # that would result from this change. It's safest to assume it's
150 # a mistake and disallow it outright.
151 raise "Admin users cannot be inactive"
156 def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
157 nextpaths = graph[start]
158 return merged if !nextpaths
159 return merged if upstream_path.has_key? start
160 upstream_path[start] = true
161 upstream_mask ||= ALL_PERMISSIONS
162 nextpaths.each do |head, mask|
165 merged[head][k] ||= v if upstream_mask[k]
167 search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
169 upstream_path.delete start