summaryrefslogtreecommitdiff
path: root/app/models/user.rb
diff options
context:
space:
mode:
authorTimothy Andrew <mail@timothyandrew.net>2017-02-06 17:37:05 +0530
committerTimothy Andrew <mail@timothyandrew.net>2017-02-24 16:50:20 +0530
commit8e684809765fa866a125c176327825ebc565f5b3 (patch)
tree2086025a88fbdefbb355061eaf468e1db593adc9 /app/models/user.rb
parent53c34c7436112d7cac9c3887ada1d5ae630a206c (diff)
downloadgitlab-ce-8e684809765fa866a125c176327825ebc565f5b3.tar.gz
Use a `ghost` boolean to track ghost users.
Rather than using a separate `ghost` state. This lets us have the benefits of both ghost and blocked users (ghost: true, state: blocked) without having to rewrite a number of queries to include cases for `state: ghost`.
Diffstat (limited to 'app/models/user.rb')
-rw-r--r--app/models/user.rb15
1 files changed, 10 insertions, 5 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index e4adcd6cde9..f71d7e90d33 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -124,6 +124,7 @@ class User < ActiveRecord::Base
validate :unique_email, if: ->(user) { user.email_changed? }
validate :owns_notification_email, if: ->(user) { user.notification_email_changed? }
validate :owns_public_email, if: ->(user) { user.public_email_changed? }
+ validate :ghost_users_must_be_blocked
validates :avatar, file_size: { maximum: 200.kilobytes.to_i }
before_validation :generate_password, on: :create
@@ -185,8 +186,6 @@ class User < ActiveRecord::Base
"administrator if you think this is an error."
end
end
-
- state :ghost
end
mount_uploader :avatar, AvatarUploader
@@ -347,7 +346,7 @@ class User < ActiveRecord::Base
# Return (create if necessary) the ghost user. The ghost user
# owns records previously belonging to deleted users.
def ghost
- ghost_user = User.with_state(:ghost).first
+ ghost_user = User.find_by_ghost(true)
ghost_user ||
begin
@@ -359,7 +358,7 @@ class User < ActiveRecord::Base
# Recheck if a ghost user is already present (one might have been)
# added between the time we last checked (first line of this method)
# and the time we acquired the lock.
- ghost_user = User.with_state(:ghost).first
+ ghost_user = User.find_by_ghost(true)
return ghost_user if ghost_user.present?
uniquify = Uniquify.new
@@ -373,7 +372,7 @@ class User < ActiveRecord::Base
User.create(
username: username, password: Devise.friendly_token,
- email: email, name: "Ghost User", state: :ghost
+ email: email, name: "Ghost User", state: :blocked, ghost: true
)
ensure
advisory_lock.unlock
@@ -477,6 +476,12 @@ class User < ActiveRecord::Base
errors.add(:public_email, "is not an email you own") unless all_emails.include?(public_email)
end
+ def ghost_users_must_be_blocked
+ if ghost? && !blocked?
+ errors.add(:ghost, 'cannot be enabled for a user who is not blocked.')
+ end
+ end
+
def update_emails_with_primary_email
primary_email_record = emails.find_by(email: email)
if primary_email_record