`human?` helper method on a user

This is cleaner than hard coding `id > 0` in ruby code.
This commit is contained in:
Robin Ward 2019-02-08 13:34:54 -05:00
parent fb18c57372
commit c719658f9f
5 changed files with 17 additions and 3 deletions

View File

@ -329,7 +329,7 @@ class SessionController < ApplicationController
RateLimiter.new(nil, "forgot-password-login-min-#{params[:login].to_s[0..100]}", 3, 1.minute).performed!
user = User.find_by_username_or_email(params[:login])
user_presence = user.present? && user.id > 0 && !user.staged
user_presence = user.present? && user.human? && !user.staged
if user_presence
email_token = user.email_tokens.create(email: user.email)
Jobs.enqueue(:critical_user_email, type: :forgot_password, user_id: user.id, email_token: email_token.token)

View File

@ -291,6 +291,10 @@ class User < ActiveRecord::Base
fields.uniq
end
def human?
self.id > 0
end
def effective_locale
if SiteSetting.allow_user_locale && self.locale.present?
self.locale

View File

@ -226,7 +226,7 @@ class PostDestroyer
end
def agree_with_flags
if @post.has_active_flag? && @user.id > 0 && @user.staff?
if @post.has_active_flag? && @user.human? && @user.staff?
Jobs.enqueue(
:send_system_message,
user_id: @post.user_id,

View File

@ -154,7 +154,7 @@ after_initialize do
self.add_to_class(:user, :enqueue_narrative_bot_job?) do
SiteSetting.discourse_narrative_bot_enabled &&
self.id > 0 &&
self.human? &&
!self.anonymous? &&
!self.staged &&
!SiteSetting.discourse_narrative_bot_ignored_usernames.split('|'.freeze).include?(self.username)

View File

@ -1989,4 +1989,14 @@ describe User do
expect(PostAction.with_deleted.where(user_id: user.id).length).to eq(0)
end
end
context "human?" do
it "returns true for a regular user" do
expect(Fabricate(:user)).to be_human
end
it "returns false for the system user" do
expect(Discourse.system_user).not_to be_human
end
end
end