user.rb revision ce4376cf83941a8eed6a14acc1d446eec52b2e83
6bdda696b3ea703c47e87fea61017ec655f91d92ndclass User < ActiveRecord::Base
6bdda696b3ea703c47e87fea61017ec655f91d92nd MINIMAL_ADMIN_COUNT = 1
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd include User::Authentication
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd has_many :comments
6bdda696b3ea703c47e87fea61017ec655f91d92nd has_many :ontology_versions
6bdda696b3ea703c47e87fea61017ec655f91d92nd has_many :team_users
6bdda696b3ea703c47e87fea61017ec655f91d92nd has_many :teams, :through => :team_users
6bdda696b3ea703c47e87fea61017ec655f91d92nd has_many :metadata
0662ed52e814f8f08ef0e09956413a792584eddffuankg has_many :permissions, :as => :subject
6bdda696b3ea703c47e87fea61017ec655f91d92nd has_many :keys
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd attr_accessible :email, :name, :first_name, :admin, :password, :as => :admin
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd strip_attributes :only => [:name, :email]
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd scope :admin, where(:admin => true)
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd scope :email_search, ->(query) { where "email ILIKE ?", "%" << query << "%" }
16b55a35cff91315d261d1baa776138af465c4e4fuankg
16b55a35cff91315d261d1baa776138af465c4e4fuankg scope :autocomplete_search, ->(query) {
16b55a35cff91315d261d1baa776138af465c4e4fuankg where("name ILIKE ? OR email ILIKE ?", "%" << query << "%", query)
6bdda696b3ea703c47e87fea61017ec655f91d92nd }
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd before_destroy :check_remaining_admins
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd validates_length_of :name, :in => 3..32
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def to_s
6bdda696b3ea703c47e87fea61017ec655f91d92nd name? ? name : email.split("@").first
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd # marks the user as deleted
6bdda696b3ea703c47e87fea61017ec655f91d92nd def delete
6bdda696b3ea703c47e87fea61017ec655f91d92nd self.encrypted_password = nil
6bdda696b3ea703c47e87fea61017ec655f91d92nd self.deleted_at = Time.now
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd # nullify email fields
6bdda696b3ea703c47e87fea61017ec655f91d92nd @bypass_postpone = true
6bdda696b3ea703c47e87fea61017ec655f91d92nd self.email = nil
6bdda696b3ea703c47e87fea61017ec655f91d92nd self.unconfirmed_email = nil
6bdda696b3ea703c47e87fea61017ec655f91d92nd self.admin = false
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd save(:validate => false)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def email_required?
6bdda696b3ea703c47e87fea61017ec655f91d92nd deleted_at.nil?
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def confirm!
6bdda696b3ea703c47e87fea61017ec655f91d92nd super
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd if User.count < 2
6bdda696b3ea703c47e87fea61017ec655f91d92nd self.admin = true
6bdda696b3ea703c47e87fea61017ec655f91d92nd self.save
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def first_name
6bdda696b3ea703c47e87fea61017ec655f91d92nd name.split(' ')[0]
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def team_permissions
6bdda696b3ea703c47e87fea61017ec655f91d92nd Permission.where(subject_id: teams.pluck(:id),
6bdda696b3ea703c47e87fea61017ec655f91d92nd subject_type: 'Team')
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def accessible_ids(type)
6bdda696b3ea703c47e87fea61017ec655f91d92nd user_permissions = permissions.where(item_type: type)
6bdda696b3ea703c47e87fea61017ec655f91d92nd user_permissions += team_permissions.where(item_type: type)
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg user_permissions.map(&:item_id)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def owned_ids(type)
6bdda696b3ea703c47e87fea61017ec655f91d92nd user_permissions = permissions.where(item_type: type, role: 'owner')
6bdda696b3ea703c47e87fea61017ec655f91d92nd user_permissions += team_permissions.where(item_type: type, role: 'owner')
6bdda696b3ea703c47e87fea61017ec655f91d92nd user_permissions.map(&:item_id)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def accessible_ontologies
6bdda696b3ea703c47e87fea61017ec655f91d92nd Ontology.where(id: accessible_ids('Ontology'))
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def accessible_repositories
6bdda696b3ea703c47e87fea61017ec655f91d92nd Repository.active.where(id: accessible_ids('Repository'))
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd def owned_deleted_repositories
6bdda696b3ea703c47e87fea61017ec655f91d92nd Repository.destroying.where(id: owned_ids('Repository'))
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92nd protected
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
6bdda696b3ea703c47e87fea61017ec655f91d92nd def check_remaining_admins
6bdda696b3ea703c47e87fea61017ec655f91d92nd if self.admin && User.admin.count <= MINIMAL_ADMIN_COUNT
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg raise Permission::PowerVaccuumError, I18n.t(:admin_deletion_error_message, minimal_count: MINIMAL_ADMIN_COUNT)
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd end
6bdda696b3ea703c47e87fea61017ec655f91d92nd
6bdda696b3ea703c47e87fea61017ec655f91d92ndend
6bdda696b3ea703c47e87fea61017ec655f91d92nd