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 return true if is_admin
44 actions.each do |action, target|
46 if target.respond_to? :uuid
47 target_uuid = target.uuid
49 next if target_uuid == self.uuid
50 next if (group_permissions[target_uuid] and
51 group_permissions[target_uuid][action])
52 if target.respond_to? :owner_uuid
53 next if target.owner_uuid == self.uuid
54 next if (group_permissions[target.owner_uuid] and
55 group_permissions[target.owner_uuid][action])
62 def self.invalidate_permissions_cache
63 Rails.cache.delete_matched(/^groups_for_user_/)
66 # Return a hash of {group_uuid: perm_hash} where perm_hash[:read]
67 # and perm_hash[:write] are true if this user can read and write
68 # objects owned by group_uuid.
70 Rails.cache.fetch "groups_for_user_#{self.uuid}" do
72 todo = {self.uuid => true}
75 lookup_uuids = todo.keys
76 lookup_uuids.each do |uuid| done[uuid] = true end
79 Group.where('owner_uuid in (?)', lookup_uuids).each do |group|
80 newgroups << [group.owner_uuid, group.uuid, 'can_manage']
82 Link.where('tail_uuid in (?) and link_class = ? and head_uuid like ?',
85 Group.uuid_like_pattern).each do |link|
86 newgroups << [link.tail_uuid, link.head_uuid, link.name]
88 newgroups.each do |tail_uuid, head_uuid, perm_name|
89 unless done.has_key? head_uuid
90 todo[head_uuid] = true
95 link_permissions = {read:true}
97 link_permissions = {read:true,write:true}
99 link_permissions = ALL_PERMISSIONS
101 permissions_from[tail_uuid] ||= {}
102 permissions_from[tail_uuid][head_uuid] ||= {}
103 link_permissions.each do |k,v|
104 permissions_from[tail_uuid][head_uuid][k] ||= v
108 search_permissions(self.uuid, permissions_from)
114 def permission_to_update
115 # users must be able to update themselves (even if they are
116 # inactive) in order to create sessions
117 self == current_user or super
120 def permission_to_create
121 current_user.andand.is_admin or
122 (self == current_user and
123 self.is_active == Rails.configuration.new_users_are_active)
127 if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and Rails.configuration.auto_admin_user
128 if current_user.email == Rails.configuration.auto_admin_user
130 self.is_active = true
135 def prevent_privilege_escalation
136 if current_user.andand.is_admin
139 if self.is_active_changed?
140 if self.is_active != self.is_active_was
141 logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
142 self.is_active = self.is_active_was
145 if self.is_admin_changed?
146 if self.is_admin != self.is_admin_was
147 logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
148 self.is_admin = self.is_admin_was
154 def prevent_inactive_admin
155 if self.is_admin and not self.is_active
156 # There is no known use case for the strange set of permissions
157 # that would result from this change. It's safest to assume it's
158 # a mistake and disallow it outright.
159 raise "Admin users cannot be inactive"
164 def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
165 nextpaths = graph[start]
166 return merged if !nextpaths
167 return merged if upstream_path.has_key? start
168 upstream_path[start] = true
169 upstream_mask ||= ALL_PERMISSIONS
170 nextpaths.each do |head, mask|
173 merged[head][k] ||= v if upstream_mask[k]
175 search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
177 upstream_path.delete start