user.rb revision 81da36894af70bbb8d8e24b004026ad4c5c1bc99
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher class_name: Commit.to_s, foreign_key: 'author_id'
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher class_name: Commit.to_s, foreign_key: 'committer_id'
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher class_name: Commit.to_s, foreign_key: 'pusher_id'
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher has_many :ontology_versions, through: :pushed_commits
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher attr_accessible :email, :name, :first_name, :admin, :password, :as => :admin
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher scope :email_search, ->(query) { where "email ILIKE ?", "%" << query << "%" }
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher scope :autocomplete_search, ->(query) {
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher where("name ILIKE ? OR email ILIKE ?", "%" << query << "%", query)
294e9a5521d327c5cdc49beeb9cb9e703b3134f1Jan Zeleny after_save :change_pusher_name, if: :name_changed?
4e2d9fe30bf8b692972a9654c60d2d90ed355815Stephen Gallagher before_destroy :check_remaining_admins
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher before_destroy :remove_associations_from_commits
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher validates_length_of :name, :in => 3..32
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher name? ? name : email.split("@").first
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # marks the user as deleted
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher self.encrypted_password = nil
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher self.deleted_at = Time.now
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher # nullify email fields
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher @bypass_postpone = true
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher self.email = nil
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher self.unconfirmed_email = nil
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher self.admin = false
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher save(:validate => false)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def email_required?
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher deleted_at.nil?
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if User.count < 2
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher self.admin = true
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def first_name
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher name.split(' ')[0]
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def team_permissions
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher Permission.where(subject_id: teams.pluck(:id),
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher subject_type: 'Team')
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def accessible_ids(type)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher user_permissions = permissions.where(item_type: type)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher user_permissions += team_permissions.where(item_type: type)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher user_permissions.map(&:item_id)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def owned_ids(type)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher user_permissions = permissions.where(item_type: type, role: 'owner')
07b7b76d7cd494cbd26263503ba2732c21819941Jan Zeleny user_permissions += team_permissions.where(item_type: type, role: 'owner')
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher user_permissions.map(&:item_id)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def accessible_ontologies
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher Ontology.where(id: accessible_ids('Ontology'))
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def accessible_repositories
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher Repository.active.where(id: accessible_ids('Repository'))
a4cce2c98eedecb5d3b47da62104634cae268434Stephen Gallagher def owned_deleted_repositories
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher Repository.destroying.where(id: owned_ids('Repository'))
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def check_remaining_admins
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher if self.admin && User.admin.count <= MINIMAL_ADMIN_COUNT
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher raise Permission::PowerVaccuumError, I18n.t(:admin_deletion_error_message, minimal_count: MINIMAL_ADMIN_COUNT)
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def remove_associations_from_commits
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher authored_commits.find_each do |commit|
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher commit.author = nil
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher committed_commits.find_each do |commit|
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher commit.committer = nil
ae5381b3a81ed4dee51e3ac56ddabd0bf7641c86Jakub Hrozek pushed_commits.find_each do |commit|
fae99bfe4bfc8b4a12e9c2a0ad01b3684c22f934Simo Sorce commit.pusher = nil
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher def change_pusher_name
07b7b76d7cd494cbd26263503ba2732c21819941Jan Zeleny pushed_commits.find_each do |commit|
effcbdb12c7ef892f1fd92a745cb33a08ca4ba30Stephen Gallagher commit.pusher_name = name