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 after_create AdminNotifier
11 has_many :authorized_keys, :foreign_key => :authorized_user_uuid, :primary_key => :uuid
13 api_accessible :user, extend: :common do |t|
24 ALL_PERMISSIONS = {read: true, write: true, manage: true}
27 "#{first_name} #{last_name}"
30 def groups_i_can(verb)
31 self.group_permissions.select { |uuid, mask| mask[verb] }.keys
35 actions.each do |action, target|
37 if target.respond_to? :uuid
38 target_uuid = target.uuid
40 next if target_uuid == self.uuid
41 next if (group_permissions[target_uuid] and
42 group_permissions[target_uuid][action])
43 if target.respond_to? :owner_uuid
44 next if target.owner_uuid == self.uuid
45 next if (group_permissions[target.owner_uuid] and
46 group_permissions[target.owner_uuid][action])
53 def self.invalidate_permissions_cache
54 Rails.cache.delete_matched(/^groups_for_user_/)
59 def permission_to_update
60 # users must be able to update themselves (even if they are
61 # inactive) in order to create sessions
62 self == current_user or super
65 def permission_to_create
66 current_user.andand.is_admin or
67 (self == current_user and
68 self.is_active == Rails.configuration.new_users_are_active)
71 def prevent_privilege_escalation
72 if current_user.andand.is_admin
75 if self.is_active_changed?
76 if self.is_active != self.is_active_was
77 logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
78 self.is_active = self.is_active_was
81 if self.is_admin_changed?
82 if self.is_admin != self.is_admin_was
83 logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
84 self.is_admin = self.is_admin_was
90 def prevent_inactive_admin
91 if self.is_admin and not self.is_active
92 # There is no known use case for the strange set of permissions
93 # that would result from this change. It's safest to assume it's
94 # a mistake and disallow it outright.
95 raise "Admin users cannot be inactive"
100 def group_permissions
101 Rails.cache.fetch "groups_for_user_#{self.uuid}" do
102 permissions_from = {}
103 todo = {self.uuid => true}
106 lookup_uuids = todo.keys
107 lookup_uuids.each do |uuid| done[uuid] = true end
109 Link.where('tail_uuid in (?) and link_class = ? and head_kind = ?',
112 'arvados#group').each do |link|
113 unless done.has_key? link.head_uuid
114 todo[link.head_uuid] = true
116 link_permissions = {}
119 link_permissions = {read:true}
121 link_permissions = {read:true,write:true}
123 link_permissions = ALL_PERMISSIONS
125 permissions_from[link.tail_uuid] ||= {}
126 permissions_from[link.tail_uuid][link.head_uuid] ||= {}
127 link_permissions.each do |k,v|
128 permissions_from[link.tail_uuid][link.head_uuid][k] ||= v
132 search_permissions(self.uuid, permissions_from)
136 def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
137 nextpaths = graph[start]
138 return merged if !nextpaths
139 return merged if upstream_path.has_key? start
140 upstream_path[start] = true
141 upstream_mask ||= ALL_PERMISSIONS
142 nextpaths.each do |head, mask|
145 merged[head][k] ||= v if upstream_mask[k]
147 search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
149 upstream_path.delete start