Added doc output directories to .gitignore
[arvados.git] / services / api / app / models / user.rb
1 class User < ArvadosModel
2   include AssignUuid
3   include KindAndEtag
4   include CommonApiTemplate
5   serialize :prefs, Hash
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
11
12   has_many :authorized_keys, :foreign_key => :authorized_user_uuid, :primary_key => :uuid
13
14   api_accessible :user, extend: :common do |t|
15     t.add :email
16     t.add :full_name
17     t.add :first_name
18     t.add :last_name
19     t.add :identity_url
20     t.add :is_active
21     t.add :is_admin
22     t.add :is_invited
23     t.add :prefs
24   end
25
26   ALL_PERMISSIONS = {read: true, write: true, manage: true}
27
28   def full_name
29     "#{first_name} #{last_name}"
30   end
31
32   def is_invited
33     (self.is_active ||
34      Rails.configuration.new_users_are_active ||
35      self.groups_i_can(:read).select { |x| x.match /-f+$/ }.first)
36   end
37
38   def groups_i_can(verb)
39     self.group_permissions.select { |uuid, mask| mask[verb] }.keys
40   end
41
42   def can?(actions)
43     actions.each do |action, target|
44       target_uuid = target
45       if target.respond_to? :uuid
46         target_uuid = target.uuid
47       end
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])
55       end
56       return false
57     end
58     true
59   end
60
61   def self.invalidate_permissions_cache
62     Rails.cache.delete_matched(/^groups_for_user_/)
63   end
64
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.
68   def group_permissions
69     Rails.cache.fetch "groups_for_user_#{self.uuid}" do
70       permissions_from = {}
71       todo = {self.uuid => true}
72       done = {}
73       while !todo.empty?
74         lookup_uuids = todo.keys
75         lookup_uuids.each do |uuid| done[uuid] = true end
76         todo = {}
77         Link.where('tail_uuid in (?) and link_class = ? and head_kind = ?',
78                    lookup_uuids,
79                    'permission',
80                    'arvados#group').each do |link|
81           unless done.has_key? link.head_uuid
82             todo[link.head_uuid] = true
83           end
84           link_permissions = {}
85           case link.name
86           when 'can_read'
87             link_permissions = {read:true}
88           when 'can_write'
89             link_permissions = {read:true,write:true}
90           when 'can_manage'
91             link_permissions = ALL_PERMISSIONS
92           end
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
97           end
98         end
99       end
100       search_permissions(self.uuid, permissions_from)
101     end
102   end
103
104   protected
105
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
110   end
111
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)
116   end
117
118   def check_auto_admin
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
121         self.is_admin = true
122         self.is_active = true
123       end
124     end
125   end
126
127   def prevent_privilege_escalation
128     if current_user.andand.is_admin
129       return true
130     end
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
135       end
136     end
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
141       end
142     end
143     true
144   end
145
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"
152     end
153     true
154   end
155
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|
163       merged[head] ||= {}
164       mask.each do |k,v|
165         merged[head][k] ||= v if upstream_mask[k]
166       end
167       search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
168     end
169     upstream_path.delete start
170     merged
171   end
172 end