X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/fb3c02b38a24cda422de95f2f8b49002b841cc72..9f2369613436b945c1b9322cbf8b64bfabed5ce4:/services/api/app/models/user.rb diff --git a/services/api/app/models/user.rb b/services/api/app/models/user.rb index 0c3eaf1a83..e628edaa3f 100644 --- a/services/api/app/models/user.rb +++ b/services/api/app/models/user.rb @@ -1,17 +1,23 @@ -class User < OrvosModel +class User < ArvadosModel include AssignUuid include KindAndEtag include CommonApiTemplate serialize :prefs, Hash has_many :api_client_authorizations before_update :prevent_privilege_escalation + before_update :prevent_inactive_admin + before_create :check_auto_admin + after_create AdminNotifier - api_accessible :superuser, :extend => :common do |t| + has_many :authorized_keys, :foreign_key => :authorized_user_uuid, :primary_key => :uuid + + api_accessible :user, extend: :common do |t| t.add :email t.add :full_name t.add :first_name t.add :last_name t.add :identity_url + t.add :is_active t.add :is_admin t.add :prefs end @@ -35,10 +41,10 @@ class User < OrvosModel next if target_uuid == self.uuid next if (group_permissions[target_uuid] and group_permissions[target_uuid][action]) - if target.respond_to? :owner - next if target.owner == self.uuid - next if (group_permissions[target.owner] and - group_permissions[target.owner][action]) + if target.respond_to? :owner_uuid + next if target.owner_uuid == self.uuid + next if (group_permissions[target.owner_uuid] and + group_permissions[target.owner_uuid][action]) end return false end @@ -49,25 +55,9 @@ class User < OrvosModel Rails.cache.delete_matched(/^groups_for_user_/) end - protected - - def permission_to_create - Thread.current[:user] == self or - (Thread.current[:user] and Thread.current[:user].is_admin) - end - - def prevent_privilege_escalation - if self.is_admin_changed? and !current_user.is_admin - if current_user.uuid == self.uuid - if self.is_admin != self.is_admin_was - logger.warn "User #{self.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin}" - self.is_admin = self.is_admin_was - end - end - end - true - end - + # Return a hash of {group_uuid: perm_hash} where perm_hash[:read] + # and perm_hash[:write] are true if this user can read and write + # objects owned by group_uuid. def group_permissions Rails.cache.fetch "groups_for_user_#{self.uuid}" do permissions_from = {} @@ -80,7 +70,7 @@ class User < OrvosModel Link.where('tail_uuid in (?) and link_class = ? and head_kind = ?', lookup_uuids, 'permission', - 'orvos#group').each do |link| + 'arvados#group').each do |link| unless done.has_key? link.head_uuid todo[link.head_uuid] = true end @@ -104,6 +94,58 @@ class User < OrvosModel end end + protected + + def permission_to_update + # users must be able to update themselves (even if they are + # inactive) in order to create sessions + self == current_user or super + end + + def permission_to_create + current_user.andand.is_admin or + (self == current_user and + self.is_active == Rails.configuration.new_users_are_active) + end + + def check_auto_admin + if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and not Rails.configuration.auto_admin_user.nil? + if current_user.email == Rails.configuration.auto_admin_user + self.is_admin = true + self.is_active = true + end + end + end + + def prevent_privilege_escalation + if current_user.andand.is_admin + return true + end + if self.is_active_changed? + if self.is_active != self.is_active_was + logger.warn "User #{current_user.uuid} tried to change is_active from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}" + self.is_active = self.is_active_was + end + end + if self.is_admin_changed? + if self.is_admin != self.is_admin_was + logger.warn "User #{current_user.uuid} tried to change is_admin from #{self.is_admin_was} to #{self.is_admin} for #{self.uuid}" + self.is_admin = self.is_admin_was + end + end + true + end + + def prevent_inactive_admin + if self.is_admin and not self.is_active + # There is no known use case for the strange set of permissions + # that would result from this change. It's safest to assume it's + # a mistake and disallow it outright. + raise "Admin users cannot be inactive" + end + true + end + def search_permissions(start, graph, merged={}, upstream_mask=nil, upstream_path={}) nextpaths = graph[start] return merged if !nextpaths