Merge branch '1646-arv-put'
[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 :prefs
23   end
24
25   ALL_PERMISSIONS = {read: true, write: true, manage: true}
26
27   def full_name
28     "#{first_name} #{last_name}"
29   end
30
31   def groups_i_can(verb)
32     self.group_permissions.select { |uuid, mask| mask[verb] }.keys
33   end
34
35   def can?(actions)
36     actions.each do |action, target|
37       target_uuid = target
38       if target.respond_to? :uuid
39         target_uuid = target.uuid
40       end
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])
48       end
49       return false
50     end
51     true
52   end
53
54   def self.invalidate_permissions_cache
55     Rails.cache.delete_matched(/^groups_for_user_/)
56   end
57
58   # Return a hash of {group_uuid: perm_hash} where perm_hash[:read]
59   # and perm_hash[:write] are true if this user can read and write
60   # objects owned by group_uuid.
61   def group_permissions
62     Rails.cache.fetch "groups_for_user_#{self.uuid}" do
63       permissions_from = {}
64       todo = {self.uuid => true}
65       done = {}
66       while !todo.empty?
67         lookup_uuids = todo.keys
68         lookup_uuids.each do |uuid| done[uuid] = true end
69         todo = {}
70         Link.where('tail_uuid in (?) and link_class = ? and head_kind = ?',
71                    lookup_uuids,
72                    'permission',
73                    'arvados#group').each do |link|
74           unless done.has_key? link.head_uuid
75             todo[link.head_uuid] = true
76           end
77           link_permissions = {}
78           case link.name
79           when 'can_read'
80             link_permissions = {read:true}
81           when 'can_write'
82             link_permissions = {read:true,write:true}
83           when 'can_manage'
84             link_permissions = ALL_PERMISSIONS
85           end
86           permissions_from[link.tail_uuid] ||= {}
87           permissions_from[link.tail_uuid][link.head_uuid] ||= {}
88           link_permissions.each do |k,v|
89             permissions_from[link.tail_uuid][link.head_uuid][k] ||= v
90           end
91         end
92       end
93       search_permissions(self.uuid, permissions_from)
94     end
95   end
96
97   protected
98
99   def permission_to_update
100     # users must be able to update themselves (even if they are
101     # inactive) in order to create sessions
102     self == current_user or super
103   end
104
105   def permission_to_create
106     current_user.andand.is_admin or
107       (self == current_user and
108        self.is_active == Rails.configuration.new_users_are_active)
109   end
110
111   def check_auto_admin
112     if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and not Rails.configuration.auto_admin_user.nil?
113       if current_user.email == Rails.configuration.auto_admin_user
114         self.is_admin = true
115         self.is_active = true
116       end
117     end
118   end
119
120   def prevent_privilege_escalation
121     if current_user.andand.is_admin
122       return true
123     end
124     if self.is_active_changed?
125       if self.is_active != self.is_active_was
126         logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
127         self.is_active = self.is_active_was
128       end
129     end
130     if self.is_admin_changed?
131       if self.is_admin != self.is_admin_was
132         logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}"
133         self.is_admin = self.is_admin_was
134       end
135     end
136     true
137   end
138
139   def prevent_inactive_admin
140     if self.is_admin and not self.is_active
141       # There is no known use case for the strange set of permissions
142       # that would result from this change. It's safest to assume it's
143       # a mistake and disallow it outright.
144       raise "Admin users cannot be inactive"
145     end
146     true
147   end
148
149   def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={})
150     nextpaths = graph[start]
151     return merged if !nextpaths
152     return merged if upstream_path.has_key? start
153     upstream_path[start] = true
154     upstream_mask ||= ALL_PERMISSIONS
155     nextpaths.each do |head, mask|
156       merged[head] ||= {}
157       mask.each do |k,v|
158         merged[head][k] ||= v if upstream_mask[k]
159       end
160       search_permissions(head, graph, merged, upstream_mask.select { |k,v| v && merged[head][k] }, upstream_path)
161     end
162     upstream_path.delete start
163     merged
164   end
165 end