X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/eba617baa5f8f180f9a55777beeb228849b36a9e..0eb72b526bf8bbb011551ecf019f604e17a534f1:/services/api/app/models/user.rb diff --git a/services/api/app/models/user.rb b/services/api/app/models/user.rb index 446a61f255..26c714eb4a 100644 --- a/services/api/app/models/user.rb +++ b/services/api/app/models/user.rb @@ -1,3 +1,7 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: AGPL-3.0 + require 'can_be_an_owner' class User < ArvadosModel @@ -8,19 +12,43 @@ class User < ArvadosModel serialize :prefs, Hash has_many :api_client_authorizations + validates(:username, + format: { + with: /\A[A-Za-z][A-Za-z0-9]*\z/, + message: "must begin with a letter and contain only alphanumerics", + }, + uniqueness: true, + allow_nil: true) before_update :prevent_privilege_escalation before_update :prevent_inactive_admin + before_update :verify_repositories_empty, :if => Proc.new { |user| + user.username.nil? and user.username_changed? + } + before_update :setup_on_activate before_create :check_auto_admin + before_create :set_initial_username, :if => Proc.new { |user| + user.username.nil? and user.email + } after_create :add_system_group_permission_link - after_create :auto_setup_new_user + after_create :auto_setup_new_user, :if => Proc.new { |user| + Rails.configuration.auto_setup_new_users and + (user.uuid != system_user_uuid) and + (user.uuid != anonymous_user_uuid) + } after_create :send_admin_notifications after_update :send_profile_created_notification - + after_update :sync_repository_names, :if => Proc.new { |user| + (user.uuid != system_user_uuid) and + user.username_changed? and + (not user.username_was.nil?) + } has_many :authorized_keys, :foreign_key => :authorized_user_uuid, :primary_key => :uuid + has_many :repositories, foreign_key: :owner_uuid, primary_key: :uuid api_accessible :user, extend: :common do |t| t.add :email + t.add :username t.add :full_name t.add :first_name t.add :last_name @@ -29,10 +57,19 @@ class User < ArvadosModel t.add :is_admin t.add :is_invited t.add :prefs + t.add :writable_by end ALL_PERMISSIONS = {read: true, write: true, manage: true} + # Map numeric permission levels (see lib/create_permission_view.sql) + # back to read/write/manage flags. + PERMS_FOR_VAL = + [{}, + {read: true}, + {read: true, write: true}, + {read: true, write: true, manage: true}] + def full_name "#{first_name} #{last_name}".strip end @@ -40,7 +77,7 @@ class User < ArvadosModel def is_invited !!(self.is_active || Rails.configuration.new_users_are_active || - self.groups_i_can(:read).select { |x| x.match /-f+$/ }.first) + self.groups_i_can(:read).select { |x| x.match(/-f+$/) }.first) end def groups_i_can(verb) @@ -70,83 +107,109 @@ class User < ArvadosModel next if (group_permissions[target.owner_uuid] and group_permissions[target.owner_uuid][action]) end + sufficient_perms = case action + when :manage + ['can_manage'] + when :write + ['can_manage', 'can_write'] + when :read + ['can_manage', 'can_write', 'can_read'] + else + # (Skip this kind of permission opportunity + # if action is an unknown permission type) + end + if sufficient_perms + # Check permission links with head_uuid pointing directly at + # the target object. If target is a Group, this is redundant + # and will fail except [a] if permission caching is broken or + # [b] during a race condition, where a permission link has + # *just* been added. + if Link.where(link_class: 'permission', + name: sufficient_perms, + tail_uuid: groups_i_can(action) + [self.uuid], + head_uuid: target_uuid).any? + next + end + end return false end true end - def self.invalidate_permissions_cache - Rails.cache.delete_matched(/^groups_for_user_/) + def self.invalidate_permissions_cache(timestamp=nil) + if Rails.configuration.async_permissions_update + timestamp = DbCurrentTime::db_current_time.to_i if timestamp.nil? + connection.execute "NOTIFY invalidate_permissions_cache, '#{timestamp}'" + else + Rails.cache.delete_matched(/^groups_for_user_/) + end + end + + # Return a hash of {user_uuid: group_perms} + def self.all_group_permissions + install_view('permission') + all_perms = {} + ActiveRecord::Base.connection. + exec_query('SELECT user_uuid, target_owner_uuid, max(perm_level) + FROM permission_view + WHERE target_owner_uuid IS NOT NULL + GROUP BY user_uuid, target_owner_uuid', + # "name" arg is a query label that appears in logs: + "all_group_permissions", + ).rows.each do |user_uuid, group_uuid, max_p_val| + all_perms[user_uuid] ||= {} + all_perms[user_uuid][group_uuid] = PERMS_FOR_VAL[max_p_val.to_i] + end + all_perms + 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 calculate_group_permissions + self.class.install_view('permission') + + group_perms = {} + ActiveRecord::Base.connection. + exec_query('SELECT target_owner_uuid, max(perm_level) + FROM permission_view + WHERE user_uuid = $1 + AND target_owner_uuid IS NOT NULL + GROUP BY target_owner_uuid', + # "name" arg is a query label that appears in logs: + "group_permissions for #{uuid}", + # "binds" arg is an array of [col_id, value] for '$1' vars: + [[nil, uuid]], + ).rows.each do |group_uuid, max_p_val| + group_perms[group_uuid] = PERMS_FOR_VAL[max_p_val.to_i] + end + Rails.cache.write "groups_for_user_#{self.uuid}", group_perms + group_perms 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. - # - # The permission graph is built by repeatedly enumerating all - # permission links reachable from self.uuid, and then calling - # search_permissions def group_permissions - Rails.cache.fetch "groups_for_user_#{self.uuid}" do - permissions_from = {} - todo = {self.uuid => true} - done = {} - # Build the equivalence class of permissions starting with - # self.uuid. On each iteration of this loop, todo contains - # the next set of uuids in the permission equivalence class - # to evaluate. - while !todo.empty? - lookup_uuids = todo.keys - lookup_uuids.each do |uuid| done[uuid] = true end - todo = {} - newgroups = [] - # include all groups owned by the current set of uuids. - Group.where('owner_uuid in (?)', lookup_uuids).each do |group| - newgroups << [group.owner_uuid, group.uuid, 'can_manage'] - end - # add any permission links from the current lookup_uuids to a Group. - Link.where('link_class = ? and tail_uuid in (?) and ' \ - '(head_uuid like ? or (name = ? and head_uuid like ?))', - 'permission', - lookup_uuids, - Group.uuid_like_pattern, - 'can_manage', - User.uuid_like_pattern).each do |link| - newgroups << [link.tail_uuid, link.head_uuid, link.name] - end - newgroups.each do |tail_uuid, head_uuid, perm_name| - unless done.has_key? head_uuid - todo[head_uuid] = true - end - link_permissions = {} - case perm_name - when 'can_read' - link_permissions = {read:true} - when 'can_write' - link_permissions = {read:true,write:true} - when 'can_manage' - link_permissions = ALL_PERMISSIONS - end - permissions_from[tail_uuid] ||= {} - permissions_from[tail_uuid][head_uuid] ||= {} - link_permissions.each do |k,v| - permissions_from[tail_uuid][head_uuid][k] ||= v - end + r = Rails.cache.read "groups_for_user_#{self.uuid}" + if r.nil? + if Rails.configuration.async_permissions_update + while r.nil? + sleep(0.1) + r = Rails.cache.read "groups_for_user_#{self.uuid}" end + else + r = calculate_group_permissions end - search_permissions(self.uuid, permissions_from) end - end - - def self.setup(user, openid_prefix, repo_name=nil, vm_uuid=nil) - return user.setup_repo_vm_links(repo_name, vm_uuid, openid_prefix) + r end # create links - def setup_repo_vm_links(repo_name, vm_uuid, openid_prefix) + def setup(openid_prefix:, repo_name: nil, vm_uuid: nil) oid_login_perm = create_oid_login_perm openid_prefix repo_perm = create_user_repo_link repo_name - vm_login_perm = create_vm_login_permission_link vm_uuid, repo_name + vm_login_perm = create_vm_login_permission_link(vm_uuid, username) if vm_uuid group_perm = create_user_group_link return [oid_login_perm, repo_perm, vm_login_perm, group_perm, self].compact @@ -171,7 +234,7 @@ class User < ArvadosModel # delete "All users" group read permissions for this user group = Group.where(name: 'All users').select do |g| - g[:uuid].match /-f+$/ + g[:uuid].match(/-f+$/) end.first Link.destroy_all(tail_uuid: self.uuid, head_uuid: group[:uuid], @@ -190,6 +253,25 @@ class User < ArvadosModel self.save! end + def set_initial_username(requested: false) + if !requested.is_a?(String) || requested.empty? + email_parts = email.partition("@") + local_parts = email_parts.first.partition("+") + if email_parts.any?(&:empty?) + return + elsif not local_parts.first.empty? + requested = local_parts.first + else + requested = email_parts.first + end + end + requested.sub!(/^[^A-Za-z]+/, "") + requested.gsub!(/[^A-Za-z0-9]/, "") + unless requested.empty? + self.username = find_usable_username_from(requested) + end + end + protected def ensure_ownership_path_leads_to_user @@ -197,9 +279,13 @@ class User < ArvadosModel end 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 + if username_changed? + current_user.andand.is_admin + else + # users must be able to update themselves (even if they are + # inactive) in order to create sessions + self == current_user or super + end end def permission_to_create @@ -209,12 +295,46 @@ class User < ArvadosModel end def check_auto_admin - if User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and Rails.configuration.auto_admin_user - if self.email == Rails.configuration.auto_admin_user - self.is_admin = true - self.is_active = true + return if self.uuid.end_with?('anonymouspublic') + if (User.where("email = ?",self.email).where(:is_admin => true).count == 0 and + Rails.configuration.auto_admin_user and self.email == Rails.configuration.auto_admin_user) or + (User.where("uuid not like '%-000000000000000'").where(:is_admin => true).count == 0 and + Rails.configuration.auto_admin_first_user) + self.is_admin = true + self.is_active = true + end + end + + def find_usable_username_from(basename) + # If "basename" is a usable username, return that. + # Otherwise, find a unique username "basenameN", where N is the + # smallest integer greater than 1, and return that. + # Return nil if a unique username can't be found after reasonable + # searching. + quoted_name = self.class.connection.quote_string(basename) + next_username = basename + next_suffix = 1 + while Rails.configuration.auto_setup_name_blacklist.include?(next_username) + next_suffix += 1 + next_username = "%s%i" % [basename, next_suffix] + end + 0.upto(6).each do |suffix_len| + pattern = "%s%s" % [quoted_name, "_" * suffix_len] + self.class. + where("username like '#{pattern}'"). + select(:username). + order('username asc'). + each do |other_user| + if other_user.username > next_username + break + elsif other_user.username == next_username + next_suffix += 1 + next_username = "%s%i" % [basename, next_suffix] + end end + return next_username if (next_username.size <= pattern.size) end + nil end def prevent_privilege_escalation @@ -263,13 +383,12 @@ class User < ArvadosModel merged end - def create_oid_login_perm (openid_prefix) - login_perm_props = { "identity_url_prefix" => openid_prefix} - + def create_oid_login_perm(openid_prefix) # Check oid_login_perm oid_login_perms = Link.where(tail_uuid: self.email, - link_class: 'permission', - name: 'can_login').where("head_uuid = ?", self.uuid) + head_uuid: self.uuid, + link_class: 'permission', + name: 'can_login') if !oid_login_perms.any? # create openid login permission @@ -277,8 +396,9 @@ class User < ArvadosModel name: 'can_login', tail_uuid: self.email, head_uuid: self.uuid, - properties: login_perm_props - ) + properties: { + "identity_url_prefix" => openid_prefix, + }) logger.info { "openid login permission: " + oid_login_perm[:uuid] } else oid_login_perm = oid_login_perms.first @@ -294,118 +414,60 @@ class User < ArvadosModel return end - # Check for an existing repository with the same name we're about to use. - repo = Repository.where(name: repo_name).first - - if repo - logger.warn "Repository exists for #{repo_name}: #{repo[:uuid]}." - - # Look for existing repository access for this repo - repo_perms = Link.where(tail_uuid: self.uuid, - head_uuid: repo[:uuid], - link_class: 'permission', - name: 'can_manage') - if repo_perms.any? - logger.warn "User already has repository access " + - repo_perms.collect { |p| p[:uuid] }.inspect - return repo_perms.first - end - end - - # create repo, if does not already exist - repo ||= Repository.create(name: repo_name) + repo = Repository.where(owner_uuid: uuid, name: repo_name).first_or_create! logger.info { "repo uuid: " + repo[:uuid] } - - repo_perm = Link.create(tail_uuid: self.uuid, - head_uuid: repo[:uuid], - link_class: 'permission', - name: 'can_manage') + repo_perm = Link.where(tail_uuid: uuid, head_uuid: repo.uuid, + link_class: "permission", + name: "can_manage").first_or_create! logger.info { "repo permission: " + repo_perm[:uuid] } return repo_perm end # create login permission for the given vm_uuid, if it does not already exist def create_vm_login_permission_link(vm_uuid, repo_name) - begin - - # vm uuid is optional - if vm_uuid - vm = VirtualMachine.where(uuid: vm_uuid).first - - if not vm - logger.warn "Could not find virtual machine for #{vm_uuid.inspect}" - raise "No vm found for #{vm_uuid}" - end - else - return - end + # vm uuid is optional + return if !vm_uuid - logger.info { "vm uuid: " + vm[:uuid] } + vm = VirtualMachine.where(uuid: vm_uuid).first + if !vm + logger.warn "Could not find virtual machine for #{vm_uuid.inspect}" + raise "No vm found for #{vm_uuid}" + end - login_perms = Link.where(tail_uuid: self.uuid, - head_uuid: vm[:uuid], - link_class: 'permission', - name: 'can_login') + logger.info { "vm uuid: " + vm[:uuid] } + login_attrs = { + tail_uuid: uuid, head_uuid: vm.uuid, + link_class: "permission", name: "can_login", + } - perm_exists = false - login_perms.each do |perm| - if perm.properties['username'] == repo_name - perm_exists = perm - break - end - end + login_perm = Link. + where(login_attrs). + select { |link| link.properties["username"] == repo_name }. + first - if perm_exists - login_perm = perm_exists - else - login_perm = Link.create(tail_uuid: self.uuid, - head_uuid: vm[:uuid], - link_class: 'permission', - name: 'can_login', - properties: {'username' => repo_name}) - logger.info { "login permission: " + login_perm[:uuid] } - end + login_perm ||= Link. + create(login_attrs.merge(properties: {"username" => repo_name})) - return login_perm - end + logger.info { "login permission: " + login_perm[:uuid] } + login_perm end # add the user to the 'All users' group def create_user_group_link - # Look up the "All users" group (we expect uuid *-*-fffffffffffffff). - group = Group.where(name: 'All users').select do |g| - g[:uuid].match /-f+$/ - end.first - - if not group - logger.warn "No 'All users' group with uuid '*-*-fffffffffffffff'." - raise "No 'All users' group with uuid '*-*-fffffffffffffff' is found" - else - logger.info { "\"All users\" group uuid: " + group[:uuid] } - - group_perms = Link.where(tail_uuid: self.uuid, - head_uuid: group[:uuid], - link_class: 'permission', - name: 'can_read') - - if !group_perms.any? - group_perm = Link.create(tail_uuid: self.uuid, - head_uuid: group[:uuid], - link_class: 'permission', - name: 'can_read') - logger.info { "group permission: " + group_perm[:uuid] } - else - group_perm = group_perms.first - end - - return group_perm - end + return (Link.where(tail_uuid: self.uuid, + head_uuid: all_users_group[:uuid], + link_class: 'permission', + name: 'can_read').first or + Link.create(tail_uuid: self.uuid, + head_uuid: all_users_group[:uuid], + link_class: 'permission', + name: 'can_read')) end # Give the special "System group" permission to manage this user and # all of this user's stuff. - # def add_system_group_permission_link + return true if uuid == system_user_uuid act_as_system_user do Link.create(link_class: 'permission', name: 'can_manage', @@ -416,50 +478,33 @@ class User < ArvadosModel # Send admin notifications def send_admin_notifications - AdminNotifier.new_user(self).deliver + AdminNotifier.new_user(self).deliver_now if not self.is_active then - AdminNotifier.new_inactive_user(self).deliver + AdminNotifier.new_inactive_user(self).deliver_now end end - # Automatically setup new user during creation - def auto_setup_new_user - return true if !Rails.configuration.auto_setup_new_users - return true if !self.email - - if Rails.configuration.auto_setup_new_users_with_vm_uuid || - Rails.configuration.auto_setup_new_users_with_repository - username = self.email.partition('@')[0] if self.email - return true if !username - - blacklisted_usernames = Rails.configuration.auto_setup_name_blacklist - if blacklisted_usernames.include?(username) - return true - elsif !(/^[a-zA-Z][-._a-zA-Z0-9]{0,30}[a-zA-Z0-9]$/.match(username)) - return true - else - return true if !(username = derive_unique_username username) - end + # Automatically setup if is_active flag turns on + def setup_on_activate + return if [system_user_uuid, anonymous_user_uuid].include?(self.uuid) + if is_active && (new_record? || is_active_changed?) + setup(openid_prefix: Rails.configuration.default_openid_prefix) end - - # setup user - setup_repo_vm_links(username, - Rails.configuration.auto_setup_new_users_with_vm_uuid, - Rails.configuration.default_openid_prefix) end - # Find a username that starts with the given string and does not collide - # with any existing repository name or VM login name - def derive_unique_username username - while true - if Repository.where(name: username).empty? - login_collisions = Link.where(link_class: 'permission', - name: 'can_login').select do |perm| - perm.properties['username'] == username - end - return username if login_collisions.empty? + # Automatically setup new user during creation + def auto_setup_new_user + setup(openid_prefix: Rails.configuration.default_openid_prefix) + if username + create_vm_login_permission_link(Rails.configuration.auto_setup_new_users_with_vm_uuid, + username) + repo_name = "#{username}/#{username}" + if Rails.configuration.auto_setup_new_users_with_repository and + Repository.where(name: repo_name).first.nil? + repo = Repository.create!(name: repo_name, owner_uuid: uuid) + Link.create!(tail_uuid: uuid, head_uuid: repo.uuid, + link_class: "permission", name: "can_manage") end - username = username + SecureRandom.random_number(10).to_s end end @@ -468,9 +513,24 @@ class User < ArvadosModel if self.prefs_changed? if self.prefs_was.andand.empty? || !self.prefs_was.andand['profile'] profile_notification_address = Rails.configuration.user_profile_notification_address - ProfileNotifier.profile_created(self, profile_notification_address).deliver if profile_notification_address + ProfileNotifier.profile_created(self, profile_notification_address).deliver_now if profile_notification_address end end end + def verify_repositories_empty + unless repositories.first.nil? + errors.add(:username, "can't be unset when the user owns repositories") + false + end + end + + def sync_repository_names + old_name_re = /^#{Regexp.escape(username_was)}\// + name_sub = "#{username}/" + repositories.find_each do |repo| + repo.name = repo.name.sub(old_name_re, name_sub) + repo.save! + end + end end