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_/)
60 def permission_to_update
61 # users must be able to update themselves (even if they are
62 # inactive) in order to create sessions
63 self == current_user or super
66 def permission_to_create
67 current_user.andand.is_admin or
68 (self == current_user and
69 self.is_active == Rails.configuration.new_users_are_active)
73 if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and not Rails.configuration.auto_admin_user.nil?
74 if current_user.email == Rails.configuration.auto_admin_user
81 def prevent_privilege_escalation
82 if current_user.andand.is_admin
85 if self.is_active_changed?
86 if self.is_active != self.is_active_was
87 logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
88 self.is_active = self.is_active_was
91 if self.is_admin_changed?
92 if self.is_admin != self.is_admin_was
93 logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
94 self.is_admin = self.is_admin_was
100 def prevent_inactive_admin
101 if self.is_admin and not self.is_active
102 # There is no known use case for the strange set of permissions
103 # that would result from this change. It's safest to assume it's
104 # a mistake and disallow it outright.
105 raise "Admin users cannot be inactive"
110 def group_permissions
111 Rails.cache.fetch "groups_for_user_#{self.uuid}" do
112 permissions_from = {}
113 todo = {self.uuid => true}
116 lookup_uuids = todo.keys
117 lookup_uuids.each do |uuid| done[uuid] = true end
119 Link.where('tail_uuid in (?) and link_class = ? and head_kind = ?',
122 'arvados#group').each do |link|
123 unless done.has_key? link.head_uuid
124 todo[link.head_uuid] = true
126 link_permissions = {}
129 link_permissions = {read:true}
131 link_permissions = {read:true,write:true}
133 link_permissions = ALL_PERMISSIONS
135 permissions_from[link.tail_uuid] ||= {}
136 permissions_from[link.tail_uuid][link.head_uuid] ||= {}
137 link_permissions.each do |k,v|
138 permissions_from[link.tail_uuid][link.head_uuid][k] ||= v
142 search_permissions(self.uuid, permissions_from)
146 def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
147 nextpaths = graph[start]
148 return merged if !nextpaths
149 return merged if upstream_path.has_key? start
150 upstream_path[start] = true
151 upstream_mask ||= ALL_PERMISSIONS
152 nextpaths.each do |head, mask|
155 merged[head][k] ||= v if upstream_mask[k]
157 search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
159 upstream_path.delete start