Commit d8b9de52 authored by Semyon Pupkov's avatar Semyon Pupkov

Remove unnecessary self from user model

parent 694b55c8
...@@ -227,19 +227,19 @@ class User < ActiveRecord::Base ...@@ -227,19 +227,19 @@ class User < ActiveRecord::Base
def filter(filter_name) def filter(filter_name)
case filter_name case filter_name
when 'admins' when 'admins'
self.admins admins
when 'blocked' when 'blocked'
self.blocked blocked
when 'two_factor_disabled' when 'two_factor_disabled'
self.without_two_factor without_two_factor
when 'two_factor_enabled' when 'two_factor_enabled'
self.with_two_factor with_two_factor
when 'wop' when 'wop'
self.without_projects without_projects
when 'external' when 'external'
self.external external
else else
self.active active
end end
end end
...@@ -337,7 +337,7 @@ class User < ActiveRecord::Base ...@@ -337,7 +337,7 @@ class User < ActiveRecord::Base
end end
def generate_password def generate_password
if self.force_random_password if force_random_password
self.password = self.password_confirmation = Devise.friendly_token.first(Devise.password_length.min) self.password = self.password_confirmation = Devise.friendly_token.first(Devise.password_length.min)
end end
end end
...@@ -378,56 +378,55 @@ class User < ActiveRecord::Base ...@@ -378,56 +378,55 @@ class User < ActiveRecord::Base
end end
def two_factor_otp_enabled? def two_factor_otp_enabled?
self.otp_required_for_login? otp_required_for_login?
end end
def two_factor_u2f_enabled? def two_factor_u2f_enabled?
self.u2f_registrations.exists? u2f_registrations.exists?
end end
def namespace_uniq def namespace_uniq
# Return early if username already failed the first uniqueness validation # Return early if username already failed the first uniqueness validation
return if self.errors.key?(:username) && return if errors.key?(:username) &&
self.errors[:username].include?('has already been taken') errors[:username].include?('has already been taken')
namespace_name = self.username existing_namespace = Namespace.by_path(username)
existing_namespace = Namespace.by_path(namespace_name) if existing_namespace && existing_namespace != namespace
if existing_namespace && existing_namespace != self.namespace errors.add(:username, 'has already been taken')
self.errors.add(:username, 'has already been taken')
end end
end end
def avatar_type def avatar_type
unless self.avatar.image? unless avatar.image?
self.errors.add :avatar, "only images allowed" errors.add :avatar, "only images allowed"
end end
end end
def unique_email def unique_email
if !self.emails.exists?(email: self.email) && Email.exists?(email: self.email) if !emails.exists?(email: email) && Email.exists?(email: email)
self.errors.add(:email, 'has already been taken') errors.add(:email, 'has already been taken')
end end
end end
def owns_notification_email def owns_notification_email
return if self.temp_oauth_email? return if temp_oauth_email?
self.errors.add(:notification_email, "is not an email you own") unless self.all_emails.include?(self.notification_email) errors.add(:notification_email, "is not an email you own") unless all_emails.include?(notification_email)
end end
def owns_public_email def owns_public_email
return if self.public_email.blank? return if public_email.blank?
self.errors.add(:public_email, "is not an email you own") unless self.all_emails.include?(self.public_email) errors.add(:public_email, "is not an email you own") unless all_emails.include?(public_email)
end end
def update_emails_with_primary_email def update_emails_with_primary_email
primary_email_record = self.emails.find_by(email: self.email) primary_email_record = emails.find_by(email: email)
if primary_email_record if primary_email_record
primary_email_record.destroy primary_email_record.destroy
self.emails.create(email: self.email_was) emails.create(email: email_was)
self.update_secondary_emails! update_secondary_emails!
end end
end end
...@@ -581,7 +580,7 @@ class User < ActiveRecord::Base ...@@ -581,7 +580,7 @@ class User < ActiveRecord::Base
end end
def project_deploy_keys def project_deploy_keys
DeployKey.unscoped.in_projects(self.authorized_projects.pluck(:id)).distinct(:id) DeployKey.unscoped.in_projects(authorized_projects.pluck(:id)).distinct(:id)
end end
def accessible_deploy_keys def accessible_deploy_keys
...@@ -597,38 +596,38 @@ class User < ActiveRecord::Base ...@@ -597,38 +596,38 @@ class User < ActiveRecord::Base
end end
def sanitize_attrs def sanitize_attrs
%w(name username skype linkedin twitter).each do |attr| %w[name username skype linkedin twitter].each do |attr|
value = self.send(attr) value = public_send(attr)
self.send("#{attr}=", Sanitize.clean(value)) if value.present? public_send("#{attr}=", Sanitize.clean(value)) if value.present?
end end
end end
def set_notification_email def set_notification_email
if self.notification_email.blank? || !self.all_emails.include?(self.notification_email) if notification_email.blank? || !all_emails.include?(notification_email)
self.notification_email = self.email self.notification_email = email
end end
end end
def set_public_email def set_public_email
if self.public_email.blank? || !self.all_emails.include?(self.public_email) if public_email.blank? || !all_emails.include?(public_email)
self.public_email = '' self.public_email = ''
end end
end end
def update_secondary_emails! def update_secondary_emails!
self.set_notification_email set_notification_email
self.set_public_email set_public_email
self.save if self.notification_email_changed? || self.public_email_changed? save if notification_email_changed? || public_email_changed?
end end
def set_projects_limit def set_projects_limit
# `User.select(:id)` raises # `User.select(:id)` raises
# `ActiveModel::MissingAttributeError: missing attribute: projects_limit` # `ActiveModel::MissingAttributeError: missing attribute: projects_limit`
# without this safeguard! # without this safeguard!
return unless self.has_attribute?(:projects_limit) return unless has_attribute?(:projects_limit)
connection_default_value_defined = new_record? && !projects_limit_changed? connection_default_value_defined = new_record? && !projects_limit_changed?
return unless self.projects_limit.nil? || connection_default_value_defined return unless projects_limit.nil? || connection_default_value_defined
self.projects_limit = current_application_settings.default_projects_limit self.projects_limit = current_application_settings.default_projects_limit
end end
...@@ -658,7 +657,7 @@ class User < ActiveRecord::Base ...@@ -658,7 +657,7 @@ class User < ActiveRecord::Base
def with_defaults def with_defaults
User.defaults.each do |k, v| User.defaults.each do |k, v|
self.send("#{k}=", v) public_send("#{k}=", v)
end end
self self
...@@ -678,7 +677,7 @@ class User < ActiveRecord::Base ...@@ -678,7 +677,7 @@ class User < ActiveRecord::Base
# Thus it will automatically generate a new fragment # Thus it will automatically generate a new fragment
# when the event is updated because the key changes. # when the event is updated because the key changes.
def reset_events_cache def reset_events_cache
Event.where(author_id: self.id). Event.where(author_id: id).
order('id DESC').limit(1000). order('id DESC').limit(1000).
update_all(updated_at: Time.now) update_all(updated_at: Time.now)
end end
...@@ -711,8 +710,8 @@ class User < ActiveRecord::Base ...@@ -711,8 +710,8 @@ class User < ActiveRecord::Base
def all_emails def all_emails
all_emails = [] all_emails = []
all_emails << self.email unless self.temp_oauth_email? all_emails << email unless temp_oauth_email?
all_emails.concat(self.emails.map(&:email)) all_emails.concat(emails.map(&:email))
all_emails all_emails
end end
...@@ -726,21 +725,21 @@ class User < ActiveRecord::Base ...@@ -726,21 +725,21 @@ class User < ActiveRecord::Base
def ensure_namespace_correct def ensure_namespace_correct
# Ensure user has namespace # Ensure user has namespace
self.create_namespace!(path: self.username, name: self.username) unless self.namespace create_namespace!(path: username, name: username) unless namespace
if self.username_changed? if username_changed?
self.namespace.update_attributes(path: self.username, name: self.username) namespace.update_attributes(path: username, name: username)
end end
end end
def post_create_hook def post_create_hook
log_info("User \"#{self.name}\" (#{self.email}) was created") log_info("User \"#{name}\" (#{email}) was created")
notification_service.new_user(self, @reset_token) if self.created_by_id notification_service.new_user(self, @reset_token) if created_by_id
system_hook_service.execute_hooks_for(self, :create) system_hook_service.execute_hooks_for(self, :create)
end end
def post_destroy_hook def post_destroy_hook
log_info("User \"#{self.name}\" (#{self.email}) was removed") log_info("User \"#{name}\" (#{email}) was removed")
system_hook_service.execute_hooks_for(self, :destroy) system_hook_service.execute_hooks_for(self, :destroy)
end end
...@@ -784,7 +783,7 @@ class User < ActiveRecord::Base ...@@ -784,7 +783,7 @@ class User < ActiveRecord::Base
end end
def oauth_authorized_tokens def oauth_authorized_tokens
Doorkeeper::AccessToken.where(resource_owner_id: self.id, revoked_at: nil) Doorkeeper::AccessToken.where(resource_owner_id: id, revoked_at: nil)
end end
# Returns the projects a user contributed to in the last year. # Returns the projects a user contributed to in the last year.
...@@ -917,7 +916,7 @@ class User < ActiveRecord::Base ...@@ -917,7 +916,7 @@ class User < ActiveRecord::Base
end end
def ensure_external_user_rights def ensure_external_user_rights
return unless self.external? return unless external?
self.can_create_group = false self.can_create_group = false
self.projects_limit = 0 self.projects_limit = 0
...@@ -929,7 +928,7 @@ class User < ActiveRecord::Base ...@@ -929,7 +928,7 @@ class User < ActiveRecord::Base
if current_application_settings.domain_blacklist_enabled? if current_application_settings.domain_blacklist_enabled?
blocked_domains = current_application_settings.domain_blacklist blocked_domains = current_application_settings.domain_blacklist
if domain_matches?(blocked_domains, self.email) if domain_matches?(blocked_domains, email)
error = 'is not from an allowed domain.' error = 'is not from an allowed domain.'
valid = false valid = false
end end
...@@ -937,7 +936,7 @@ class User < ActiveRecord::Base ...@@ -937,7 +936,7 @@ class User < ActiveRecord::Base
allowed_domains = current_application_settings.domain_whitelist allowed_domains = current_application_settings.domain_whitelist
unless allowed_domains.blank? unless allowed_domains.blank?
if domain_matches?(allowed_domains, self.email) if domain_matches?(allowed_domains, email)
valid = true valid = true
else else
error = "domain is not authorized for sign-up" error = "domain is not authorized for sign-up"
...@@ -945,7 +944,7 @@ class User < ActiveRecord::Base ...@@ -945,7 +944,7 @@ class User < ActiveRecord::Base
end end
end end
self.errors.add(:email, error) unless valid errors.add(:email, error) unless valid
valid valid
end end
......
---
title: Remove unnecessary self from user model
merge_request: 7551
author: Semyon Pupkov
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment