Advertise filters param in discovery doc.
[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     return true if is_admin
44     actions.each do |action, target|
45       target_uuid = target
46       if target.respond_to? :uuid
47         target_uuid = target.uuid
48       end
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])
56       end
57       return false
58     end
59     true
60   end
61
62   def self.invalidate_permissions_cache
63     Rails.cache.delete_matched(/^groups_for_user_/)
64   end
65
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.
69   def group_permissions
70     Rails.cache.fetch "groups_for_user_#{self.uuid}" do
71       permissions_from = {}
72       todo = {self.uuid => true}
73       done = {}
74       while !todo.empty?
75         lookup_uuids = todo.keys
76         lookup_uuids.each do |uuid| done[uuid] = true end
77         todo = {}
78         newgroups = []
79         Group.where('owner_uuid in (?)', lookup_uuids).each do |group|
80           newgroups << [group.owner_uuid, group.uuid, 'can_manage']
81         end
82         Link.where('tail_uuid in (?) and link_class = ? and head_kind = ?',
83                    lookup_uuids,
84                    'permission',
85                    'arvados#group').each do |link|
86           newgroups << [link.tail_uuid, link.head_uuid, link.name]
87         end
88         newgroups.each do |tail_uuid, head_uuid, perm_name|
89           unless done.has_key? head_uuid
90             todo[head_uuid] = true
91           end
92           link_permissions = {}
93           case perm_name
94           when 'can_read'
95             link_permissions = {read:true}
96           when 'can_write'
97             link_permissions = {read:true,write:true}
98           when 'can_manage'
99             link_permissions = ALL_PERMISSIONS
100           end
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
105           end
106         end
107       end
108       search_permissions(self.uuid, permissions_from)
109     end
110   end
111
112   protected
113
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
118   end
119
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)
124   end
125
126   def check_auto_admin
127     if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and not Rails.configuration.auto_admin_user.nil?
128       if current_user.email == Rails.configuration.auto_admin_user
129         self.is_admin = true
130         self.is_active = true
131       end
132     end
133   end
134
135   def prevent_privilege_escalation
136     if current_user.andand.is_admin
137       return true
138     end
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
143       end
144     end
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
149       end
150     end
151     true
152   end
153
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"
160     end
161     true
162   end
163
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|
171       merged[head] ||= {}
172       mask.each do |k,v|
173         merged[head][k] ||= v if upstream_mask[k]
174       end
175       search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
176     end
177     upstream_path.delete start
178     merged
179   end
180 end