2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-15 13:02:11 +08:00
|
|
|
require_dependency 'jobs/base'
|
2013-04-19 02:27:04 +08:00
|
|
|
require_dependency 'email'
|
2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'email_token'
|
2016-12-21 17:00:45 +08:00
|
|
|
require_dependency 'email_validator'
|
2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'trust_level'
|
2013-03-06 20:12:16 +08:00
|
|
|
require_dependency 'pbkdf2'
|
2013-05-11 04:58:23 +08:00
|
|
|
require_dependency 'discourse'
|
2013-06-06 04:00:45 +08:00
|
|
|
require_dependency 'post_destroyer'
|
2013-06-06 22:40:10 +08:00
|
|
|
require_dependency 'user_name_suggester'
|
2013-06-23 12:32:46 +08:00
|
|
|
require_dependency 'pretty_text'
|
2013-11-23 02:18:45 +08:00
|
|
|
require_dependency 'url_helper'
|
2014-05-30 12:17:35 +08:00
|
|
|
require_dependency 'letter_avatar'
|
2014-06-17 08:46:30 +08:00
|
|
|
require_dependency 'promotion'
|
2017-09-05 09:39:56 +08:00
|
|
|
require_dependency 'password_validator'
|
2017-09-15 13:02:11 +08:00
|
|
|
require_dependency 'notification_serializer'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
class User < ActiveRecord::Base
|
2017-08-15 23:46:57 +08:00
|
|
|
include Searchable
|
2013-06-07 06:07:59 +08:00
|
|
|
include Roleable
|
2014-04-28 16:31:51 +08:00
|
|
|
include HasCustomFields
|
2018-02-20 14:44:51 +08:00
|
|
|
include SecondFactorManager
|
2018-10-05 16:53:59 +08:00
|
|
|
include HasDestroyedWebHook
|
2013-06-07 06:07:59 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
has_many :posts
|
2019-04-26 20:26:31 +08:00
|
|
|
has_many :notifications, dependent: :delete_all
|
|
|
|
has_many :topic_users, dependent: :delete_all
|
2015-09-03 02:43:15 +08:00
|
|
|
has_many :category_users, dependent: :destroy
|
2016-05-05 02:02:47 +08:00
|
|
|
has_many :tag_users, dependent: :destroy
|
2016-08-16 15:06:33 +08:00
|
|
|
has_many :user_api_keys, dependent: :destroy
|
2013-02-06 03:16:51 +08:00
|
|
|
has_many :topics
|
2013-04-12 04:04:20 +08:00
|
|
|
has_many :user_open_ids, dependent: :destroy
|
2019-01-17 08:19:37 +08:00
|
|
|
|
2019-01-17 09:40:30 +08:00
|
|
|
# dependent deleting handled via before_destroy
|
|
|
|
has_many :user_actions
|
|
|
|
has_many :post_actions
|
2019-01-17 08:19:37 +08:00
|
|
|
|
2015-09-03 02:43:15 +08:00
|
|
|
has_many :user_badges, -> { where('user_badges.badge_id IN (SELECT id FROM badges WHERE enabled)') }, dependent: :destroy
|
2014-07-09 13:31:49 +08:00
|
|
|
has_many :badges, through: :user_badges
|
2014-11-29 03:20:43 +08:00
|
|
|
has_many :email_logs, dependent: :delete_all
|
2016-04-19 04:58:30 +08:00
|
|
|
has_many :incoming_emails, dependent: :delete_all
|
2013-02-06 03:16:51 +08:00
|
|
|
has_many :post_timings
|
2013-09-04 05:19:29 +08:00
|
|
|
has_many :topic_allowed_users, dependent: :destroy
|
2013-02-06 03:16:51 +08:00
|
|
|
has_many :topics_allowed, through: :topic_allowed_users, source: :topic
|
2013-09-04 05:19:29 +08:00
|
|
|
has_many :email_tokens, dependent: :destroy
|
|
|
|
has_many :user_visits, dependent: :destroy
|
|
|
|
has_many :invites, dependent: :destroy
|
|
|
|
has_many :topic_links, dependent: :destroy
|
2013-11-23 01:29:07 +08:00
|
|
|
has_many :uploads
|
2017-04-15 12:11:02 +08:00
|
|
|
has_many :user_warnings
|
2015-12-23 08:09:17 +08:00
|
|
|
has_many :user_archived_messages, dependent: :destroy
|
2016-03-08 03:40:11 +08:00
|
|
|
has_many :email_change_requests, dependent: :destroy
|
2019-04-26 16:11:39 +08:00
|
|
|
|
|
|
|
# see before_destroy
|
|
|
|
has_many :directory_items
|
2017-02-01 06:21:37 +08:00
|
|
|
has_many :user_auth_tokens, dependent: :destroy
|
2018-08-31 16:18:06 +08:00
|
|
|
has_many :user_auth_token_logs, dependent: :destroy
|
2018-03-15 03:11:35 +08:00
|
|
|
|
|
|
|
has_many :group_users, dependent: :destroy
|
|
|
|
has_many :groups, through: :group_users
|
2019-03-27 19:30:59 +08:00
|
|
|
has_many :group_requests, dependent: :destroy
|
2018-03-15 03:11:35 +08:00
|
|
|
has_many :secure_categories, through: :groups, source: :categories
|
|
|
|
|
2018-09-20 13:33:10 +08:00
|
|
|
has_many :user_uploads, dependent: :destroy
|
2017-04-27 02:47:36 +08:00
|
|
|
has_many :user_emails, dependent: :destroy
|
2015-12-23 08:09:17 +08:00
|
|
|
|
2017-07-25 23:44:46 +08:00
|
|
|
has_one :primary_email, -> { where(primary: true) }, class_name: 'UserEmail', dependent: :destroy
|
2013-05-11 04:58:23 +08:00
|
|
|
|
2016-02-17 12:46:19 +08:00
|
|
|
has_one :user_option, dependent: :destroy
|
2014-05-22 15:37:02 +08:00
|
|
|
has_one :user_avatar, dependent: :destroy
|
2018-11-28 23:49:24 +08:00
|
|
|
has_many :user_associated_accounts, dependent: :destroy
|
2013-04-12 04:04:20 +08:00
|
|
|
has_one :github_user_info, dependent: :destroy
|
2018-07-23 23:51:57 +08:00
|
|
|
has_many :oauth2_user_infos, dependent: :destroy
|
2018-03-01 19:10:27 +08:00
|
|
|
has_one :instagram_user_info, dependent: :destroy
|
2018-06-28 16:12:32 +08:00
|
|
|
has_many :user_second_factors, dependent: :destroy
|
2019-03-15 14:59:16 +08:00
|
|
|
|
|
|
|
has_many :totps, -> {
|
|
|
|
where(method: UserSecondFactor.methods[:totp], enabled: true)
|
|
|
|
}, class_name: "UserSecondFactor"
|
|
|
|
|
2019-05-29 12:26:06 +08:00
|
|
|
has_one :anonymous_user_master, class_name: 'AnonymousUser'
|
|
|
|
has_one :anonymous_user_shadow, ->(record) { where(active: true) }, foreign_key: :master_user_id, class_name: 'AnonymousUser'
|
|
|
|
|
|
|
|
has_one :master_user, through: :anonymous_user_master
|
|
|
|
has_one :shadow_user, through: :anonymous_user_shadow, source: :user
|
|
|
|
|
2013-09-12 02:50:26 +08:00
|
|
|
has_one :user_stat, dependent: :destroy
|
2014-06-10 13:19:08 +08:00
|
|
|
has_one :user_profile, dependent: :destroy, inverse_of: :user
|
2019-04-29 11:58:52 +08:00
|
|
|
has_one :profile_background_upload, through: :user_profile
|
|
|
|
has_one :card_background_upload, through: :user_profile
|
2014-02-25 11:30:49 +08:00
|
|
|
has_one :single_sign_on_record, dependent: :destroy
|
2013-02-06 03:16:51 +08:00
|
|
|
belongs_to :approved_by, class_name: 'User'
|
2014-04-24 10:42:04 +08:00
|
|
|
belongs_to :primary_group, class_name: 'Group'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-03-24 08:55:22 +08:00
|
|
|
has_many :muted_user_records, class_name: 'MutedUser'
|
|
|
|
has_many :muted_users, through: :muted_user_records
|
|
|
|
|
2013-10-23 03:53:08 +08:00
|
|
|
has_one :api_key, dependent: :destroy
|
2013-05-23 03:33:33 +08:00
|
|
|
|
2018-05-05 06:31:48 +08:00
|
|
|
has_many :push_subscriptions, dependent: :destroy
|
|
|
|
|
2014-05-22 15:37:02 +08:00
|
|
|
belongs_to :uploaded_avatar, class_name: 'Upload'
|
2013-08-14 04:08:29 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
has_many :acting_group_histories, dependent: :destroy, foreign_key: :acting_user_id, class_name: 'GroupHistory'
|
|
|
|
has_many :targeted_group_histories, dependent: :destroy, foreign_key: :target_user_id, class_name: 'GroupHistory'
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
has_many :reviewable_scores, dependent: :destroy
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
delegate :last_sent_email_address, to: :email_logs
|
2013-11-15 23:27:43 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
validates_presence_of :username
|
2017-08-31 12:06:56 +08:00
|
|
|
validate :username_validator, if: :will_save_change_to_username?
|
2013-02-06 03:16:51 +08:00
|
|
|
validate :password_validator
|
2019-05-14 04:43:19 +08:00
|
|
|
validate :name_validator, if: :will_save_change_to_name?
|
2017-08-31 12:06:56 +08:00
|
|
|
validates :name, user_full_name: true, if: :will_save_change_to_name?, length: { maximum: 255 }
|
2017-07-28 09:20:09 +08:00
|
|
|
validates :ip_address, allowed_ip_address: { on: :create, message: :signup_not_allowed }
|
2017-07-31 10:45:18 +08:00
|
|
|
validates :primary_email, presence: true
|
2017-09-12 01:22:04 +08:00
|
|
|
validates_associated :primary_email, message: -> (_, user_email) { user_email[:value]&.errors[:email]&.first }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
after_initialize :add_trust_level
|
2015-08-22 02:39:21 +08:00
|
|
|
|
2017-10-25 13:02:18 +08:00
|
|
|
before_validation :set_skip_validate_email
|
2017-08-09 10:56:08 +08:00
|
|
|
|
2013-02-06 10:44:49 +08:00
|
|
|
after_create :create_email_token
|
2013-09-12 02:50:26 +08:00
|
|
|
after_create :create_user_stat
|
2016-02-17 12:46:19 +08:00
|
|
|
after_create :create_user_option
|
2014-05-28 01:54:04 +08:00
|
|
|
after_create :create_user_profile
|
2018-07-18 18:57:43 +08:00
|
|
|
after_create :set_random_avatar
|
2014-06-17 08:46:30 +08:00
|
|
|
after_create :ensure_in_trust_level_group
|
2015-08-22 02:39:21 +08:00
|
|
|
after_create :set_default_categories_preferences
|
2014-08-14 04:17:16 +08:00
|
|
|
|
2019-05-28 00:12:26 +08:00
|
|
|
after_update :trigger_user_updated_event, if: :saved_change_to_uploaded_avatar_id?
|
2019-06-17 13:10:47 +08:00
|
|
|
after_update :trigger_user_automatic_group_refresh, if: :saved_change_to_staged?
|
2019-05-28 00:12:26 +08:00
|
|
|
|
2019-04-23 18:22:47 +08:00
|
|
|
before_save :update_usernames
|
2014-08-14 04:17:16 +08:00
|
|
|
before_save :ensure_password_is_hashed
|
2018-09-17 13:08:39 +08:00
|
|
|
before_save :match_title_to_primary_group_changes
|
2018-09-21 10:06:08 +08:00
|
|
|
before_save :check_if_title_is_badged_granted
|
2014-08-14 04:17:16 +08:00
|
|
|
|
2017-02-01 06:21:37 +08:00
|
|
|
after_save :expire_tokens_if_password_changed
|
2014-08-14 04:17:16 +08:00
|
|
|
after_save :clear_global_notice_if_needed
|
2014-05-22 15:37:02 +08:00
|
|
|
after_save :refresh_avatar
|
2014-07-23 09:42:24 +08:00
|
|
|
after_save :badge_grant
|
2015-06-06 01:50:06 +08:00
|
|
|
after_save :expire_old_email_tokens
|
2016-12-22 10:13:14 +08:00
|
|
|
after_save :index_search
|
2018-12-15 05:52:37 +08:00
|
|
|
after_save :check_site_contact_username
|
2019-06-17 13:10:47 +08:00
|
|
|
|
2017-03-16 16:02:34 +08:00
|
|
|
after_commit :trigger_user_created_event, on: :create
|
2018-07-23 15:49:49 +08:00
|
|
|
after_commit :trigger_user_destroyed_event, on: :destroy
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-09-04 05:19:29 +08:00
|
|
|
before_destroy do
|
|
|
|
# These tables don't have primary keys, so destroying them with activerecord is tricky:
|
2017-08-31 12:06:56 +08:00
|
|
|
PostTiming.where(user_id: self.id).delete_all
|
|
|
|
TopicViewItem.where(user_id: self.id).delete_all
|
2019-01-17 09:40:30 +08:00
|
|
|
UserAction.where('user_id = :user_id OR target_user_id = :user_id OR acting_user_id = :user_id', user_id: self.id).delete_all
|
|
|
|
|
|
|
|
# we need to bypass the default scope here, which appears not bypassed for :delete_all
|
|
|
|
# however :destroy it is bypassed
|
|
|
|
PostAction.with_deleted.where(user_id: self.id).delete_all
|
2019-04-26 16:11:39 +08:00
|
|
|
|
|
|
|
# This is a perf optimisation to ensure we hit the index
|
|
|
|
# without this we need to scan a much larger number of rows
|
|
|
|
DirectoryItem.where(user_id: self.id)
|
|
|
|
.where('period_type in (?)', DirectoryItem.period_types.values)
|
|
|
|
.delete_all
|
2013-09-04 05:19:29 +08:00
|
|
|
end
|
|
|
|
|
2016-09-08 02:05:46 +08:00
|
|
|
# Skip validating email, for example from a particular auth provider plugin
|
|
|
|
attr_accessor :skip_email_validation
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Whether we need to be sending a system message after creation
|
|
|
|
attr_accessor :send_welcome_message
|
|
|
|
|
|
|
|
# This is just used to pass some information into the serializer
|
|
|
|
attr_accessor :notification_channel_position
|
|
|
|
|
2014-08-14 04:17:16 +08:00
|
|
|
# set to true to optimize creation and save for imports
|
|
|
|
attr_accessor :import_mode
|
|
|
|
|
2018-03-19 11:31:14 +08:00
|
|
|
scope :with_email, ->(email) do
|
2018-03-19 12:34:21 +08:00
|
|
|
joins(:user_emails).where("lower(user_emails.email) IN (?)", email)
|
2018-03-19 11:31:14 +08:00
|
|
|
end
|
2017-04-27 02:47:36 +08:00
|
|
|
|
2017-03-11 14:25:09 +08:00
|
|
|
scope :human_users, -> { where('users.id > 0') }
|
|
|
|
|
2015-05-11 07:10:10 +08:00
|
|
|
# excluding fake users like the system user or anonymous users
|
2017-03-11 14:25:09 +08:00
|
|
|
scope :real, -> { human_users.where('NOT EXISTS(
|
2015-05-11 07:10:10 +08:00
|
|
|
SELECT 1
|
2019-05-29 12:26:06 +08:00
|
|
|
FROM anonymous_users a
|
|
|
|
WHERE a.user_id = users.id
|
|
|
|
)') }
|
2013-03-29 14:29:58 +08:00
|
|
|
|
2014-09-04 05:50:19 +08:00
|
|
|
# TODO-PERF: There is no indexes on any of these
|
|
|
|
# and NotifyMailingListSubscribers does a select-all-and-loop
|
2017-11-11 01:18:08 +08:00
|
|
|
# may want to create an index on (active, silence, suspended_till)?
|
2017-11-14 02:41:36 +08:00
|
|
|
scope :silenced, -> { where("silenced_till IS NOT NULL AND silenced_till > ?", Time.zone.now) }
|
|
|
|
scope :not_silenced, -> { where("silenced_till IS NULL OR silenced_till <= ?", Time.zone.now) }
|
2014-09-04 05:50:19 +08:00
|
|
|
scope :suspended, -> { where('suspended_till IS NOT NULL AND suspended_till > ?', Time.zone.now) }
|
|
|
|
scope :not_suspended, -> { where('suspended_till IS NULL OR suspended_till <= ?', Time.zone.now) }
|
|
|
|
scope :activated, -> { where(active: true) }
|
|
|
|
|
2018-03-22 13:42:46 +08:00
|
|
|
scope :filter_by_username, ->(filter) do
|
2018-03-26 14:30:37 +08:00
|
|
|
if filter.is_a?(Array)
|
|
|
|
where('username_lower ~* ?', "(#{filter.join('|')})")
|
|
|
|
else
|
|
|
|
where('username_lower ILIKE ?', "%#{filter}%")
|
|
|
|
end
|
2018-03-22 13:42:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
scope :filter_by_username_or_email, ->(filter) do
|
|
|
|
if filter =~ /.+@.+/
|
|
|
|
# probably an email so try the bypass
|
|
|
|
if user_id = UserEmail.where("lower(email) = ?", filter.downcase).pluck(:user_id).first
|
|
|
|
return where('users.id = ?', user_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-26 14:30:37 +08:00
|
|
|
users = joins(:primary_email)
|
|
|
|
|
|
|
|
if filter.is_a?(Array)
|
|
|
|
users.where(
|
|
|
|
'username_lower ~* :filter OR lower(user_emails.email) SIMILAR TO :filter',
|
|
|
|
filter: "(#{filter.join('|')})"
|
|
|
|
)
|
|
|
|
else
|
|
|
|
users.where(
|
2018-03-22 13:42:46 +08:00
|
|
|
'username_lower ILIKE :filter OR lower(user_emails.email) ILIKE :filter',
|
|
|
|
filter: "%#{filter}%"
|
|
|
|
)
|
2018-03-26 14:30:37 +08:00
|
|
|
end
|
2018-03-22 13:42:46 +08:00
|
|
|
end
|
|
|
|
|
2013-02-14 14:32:58 +08:00
|
|
|
module NewTopicDuration
|
2013-02-26 00:42:20 +08:00
|
|
|
ALWAYS = -1
|
2013-02-14 14:32:58 +08:00
|
|
|
LAST_VISIT = -2
|
|
|
|
end
|
2014-03-08 01:58:53 +08:00
|
|
|
|
2019-08-10 18:02:12 +08:00
|
|
|
MAX_SELF_DELETE_POST_COUNT ||= 1
|
|
|
|
MAX_STAFF_DELETE_POST_COUNT ||= 5
|
|
|
|
|
2014-09-12 03:22:11 +08:00
|
|
|
def self.max_password_length
|
|
|
|
200
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.username_length
|
2014-07-17 00:25:24 +08:00
|
|
|
SiteSetting.min_username_length.to_i..SiteSetting.max_username_length.to_i
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2019-04-23 18:22:47 +08:00
|
|
|
def self.normalize_username(username)
|
|
|
|
username.unicode_normalize.downcase if username.present?
|
|
|
|
end
|
|
|
|
|
2018-08-01 11:08:45 +08:00
|
|
|
def self.username_available?(username, email = nil, allow_reserved_username: false)
|
2019-04-23 18:22:47 +08:00
|
|
|
lower = normalize_username(username)
|
2018-08-01 11:08:45 +08:00
|
|
|
return false if !allow_reserved_username && reserved_username?(lower)
|
2019-02-20 05:31:03 +08:00
|
|
|
return true if !username_exists?(lower)
|
2018-05-23 03:25:52 +08:00
|
|
|
|
2017-12-12 18:26:00 +08:00
|
|
|
# staged users can use the same username since they will take over the account
|
|
|
|
email.present? && User.joins(:user_emails).exists?(staged: true, username_lower: lower, user_emails: { primary: true, email: email })
|
2017-04-13 10:44:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.reserved_username?(username)
|
2019-04-23 18:22:47 +08:00
|
|
|
username = normalize_username(username)
|
2016-08-31 21:49:45 +08:00
|
|
|
|
2019-04-23 18:22:47 +08:00
|
|
|
SiteSetting.reserved_usernames.unicode_normalize.split("|").any? do |reserved|
|
|
|
|
username.match?(/^#{Regexp.escape(reserved).gsub('\*', '.*')}$/)
|
2017-04-13 10:44:26 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2018-09-04 18:45:36 +08:00
|
|
|
def self.plugin_editable_user_custom_fields
|
|
|
|
@plugin_editable_user_custom_fields ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.register_plugin_editable_user_custom_field(custom_field_name, plugin)
|
|
|
|
plugin_editable_user_custom_fields[custom_field_name] = plugin
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.editable_user_custom_fields
|
|
|
|
fields = []
|
|
|
|
|
|
|
|
plugin_editable_user_custom_fields.each do |k, v|
|
|
|
|
fields << k if v.enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
fields.uniq
|
|
|
|
end
|
|
|
|
|
2016-03-12 04:52:18 +08:00
|
|
|
def self.plugin_staff_user_custom_fields
|
|
|
|
@plugin_staff_user_custom_fields ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.register_plugin_staff_custom_field(custom_field_name, plugin)
|
|
|
|
plugin_staff_user_custom_fields[custom_field_name] = plugin
|
|
|
|
end
|
|
|
|
|
2018-10-17 17:33:27 +08:00
|
|
|
def self.plugin_public_user_custom_fields
|
|
|
|
@plugin_public_user_custom_fields ||= {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.register_plugin_public_custom_field(custom_field_name, plugin)
|
|
|
|
plugin_public_user_custom_fields[custom_field_name] = plugin
|
|
|
|
end
|
|
|
|
|
2016-03-12 04:52:18 +08:00
|
|
|
def self.whitelisted_user_custom_fields(guardian)
|
|
|
|
fields = []
|
|
|
|
|
2018-10-17 17:33:27 +08:00
|
|
|
plugin_public_user_custom_fields.each do |k, v|
|
|
|
|
fields << k if v.enabled?
|
|
|
|
end
|
|
|
|
|
2016-03-12 04:52:18 +08:00
|
|
|
if SiteSetting.public_user_custom_fields.present?
|
|
|
|
fields += SiteSetting.public_user_custom_fields.split('|')
|
|
|
|
end
|
|
|
|
|
|
|
|
if guardian.is_staff?
|
|
|
|
if SiteSetting.staff_user_custom_fields.present?
|
|
|
|
fields += SiteSetting.staff_user_custom_fields.split('|')
|
|
|
|
end
|
|
|
|
plugin_staff_user_custom_fields.each do |k, v|
|
|
|
|
fields << k if v.enabled?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
fields.uniq
|
|
|
|
end
|
|
|
|
|
2019-02-09 02:34:54 +08:00
|
|
|
def human?
|
|
|
|
self.id > 0
|
|
|
|
end
|
|
|
|
|
2019-03-12 07:58:14 +08:00
|
|
|
def bot?
|
|
|
|
!self.human?
|
|
|
|
end
|
|
|
|
|
2015-02-06 11:38:51 +08:00
|
|
|
def effective_locale
|
|
|
|
if SiteSetting.allow_user_locale && self.locale.present?
|
|
|
|
self.locale
|
|
|
|
else
|
|
|
|
SiteSetting.default_locale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-01 00:51:13 +08:00
|
|
|
EMAIL = %r{([^@]+)@([^\.]+)}
|
2018-01-19 22:29:15 +08:00
|
|
|
FROM_STAGED = "from_staged".freeze
|
2013-04-01 00:51:13 +08:00
|
|
|
|
2013-04-13 06:46:55 +08:00
|
|
|
def self.new_from_params(params)
|
|
|
|
user = User.new
|
|
|
|
user.name = params[:name]
|
|
|
|
user.email = params[:email]
|
|
|
|
user.password = params[:password]
|
|
|
|
user.username = params[:username]
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2018-05-13 23:00:02 +08:00
|
|
|
def unstage
|
2018-05-14 18:03:15 +08:00
|
|
|
if self.staged
|
|
|
|
self.staged = false
|
|
|
|
self.custom_fields[FROM_STAGED] = true
|
|
|
|
self.notifications.destroy_all
|
|
|
|
DiscourseEvent.trigger(:user_unstaged, self)
|
|
|
|
end
|
2018-05-13 23:00:02 +08:00
|
|
|
end
|
|
|
|
|
2018-01-19 22:29:15 +08:00
|
|
|
def self.unstage(params)
|
|
|
|
if user = User.where(staged: true).with_email(params[:email].strip.downcase).first
|
2019-05-07 09:27:05 +08:00
|
|
|
params.each { |k, v| user.public_send("#{k}=", v) }
|
2018-01-19 22:29:15 +08:00
|
|
|
user.active = false
|
2018-05-13 23:00:02 +08:00
|
|
|
user.unstage
|
2018-01-19 22:29:15 +08:00
|
|
|
end
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
2018-05-17 14:51:48 +08:00
|
|
|
def self.suggest_name(string)
|
|
|
|
return "" if string.blank?
|
2018-05-17 16:34:16 +08:00
|
|
|
(string[/\A[^@]+/].presence || string[/[^@]+\z/]).tr(".", " ").titleize
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
def self.find_by_username_or_email(username_or_email)
|
2013-10-28 13:29:07 +08:00
|
|
|
if username_or_email.include?('@')
|
|
|
|
find_by_email(username_or_email)
|
2013-06-19 08:31:19 +08:00
|
|
|
else
|
2013-10-28 13:29:07 +08:00
|
|
|
find_by_username(username_or_email)
|
2013-06-19 08:31:19 +08:00
|
|
|
end
|
2013-04-29 14:33:24 +08:00
|
|
|
end
|
|
|
|
|
2013-10-24 15:59:58 +08:00
|
|
|
def self.find_by_email(email)
|
2017-04-27 02:47:36 +08:00
|
|
|
self.with_email(Email.downcase(email)).first
|
2013-10-24 15:59:58 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_by_username(username)
|
2019-04-23 18:22:47 +08:00
|
|
|
find_by(username_lower: normalize_username(username))
|
2013-10-24 15:59:58 +08:00
|
|
|
end
|
|
|
|
|
2018-08-25 06:41:03 +08:00
|
|
|
def group_granted_trust_level
|
|
|
|
GroupUser
|
|
|
|
.where(user_id: id)
|
|
|
|
.includes(:group)
|
|
|
|
.maximum("groups.grant_trust_level")
|
|
|
|
end
|
2018-12-18 15:41:42 +08:00
|
|
|
|
|
|
|
def visible_groups
|
|
|
|
groups.visible_groups(self)
|
|
|
|
end
|
2018-08-25 06:41:03 +08:00
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
def enqueue_welcome_message(message_type)
|
|
|
|
return unless SiteSetting.send_welcome_message?
|
|
|
|
Jobs.enqueue(:send_system_message, user_id: id, message_type: message_type)
|
|
|
|
end
|
|
|
|
|
2018-06-23 00:51:07 +08:00
|
|
|
def enqueue_member_welcome_message
|
|
|
|
return unless SiteSetting.send_tl1_welcome_message?
|
|
|
|
Jobs.enqueue(:send_system_message, user_id: id, message_type: "welcome_tl1_user")
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def change_username(new_username, actor = nil)
|
2015-03-07 05:44:54 +08:00
|
|
|
UsernameChanger.change(self, new_username, actor)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-09-13 05:46:43 +08:00
|
|
|
def created_topic_count
|
2014-07-29 01:17:37 +08:00
|
|
|
stat = user_stat || create_user_stat
|
|
|
|
stat.topic_count
|
2013-09-13 05:46:43 +08:00
|
|
|
end
|
2013-02-27 00:27:59 +08:00
|
|
|
|
2014-07-29 01:17:37 +08:00
|
|
|
alias_method :topic_count, :created_topic_count
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# tricky, we need our bus to be subscribed from the right spot
|
|
|
|
def sync_notification_channel_position
|
|
|
|
@unread_notifications_by_type = nil
|
2015-05-04 10:21:00 +08:00
|
|
|
self.notification_channel_position = MessageBus.last_id("/notification/#{id}")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def invited_by
|
|
|
|
used_invite = invites.where("redeemed_at is not null").includes(:invited_by).first
|
2013-02-28 21:08:56 +08:00
|
|
|
used_invite.try(:invited_by)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-08-09 10:56:08 +08:00
|
|
|
def should_validate_email_address?
|
2017-04-27 02:47:36 +08:00
|
|
|
!skip_email_validation && !staged?
|
2016-09-08 02:05:46 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Approve this user
|
2019-01-04 01:03:01 +08:00
|
|
|
def approve(approved_by, send_mail = true)
|
|
|
|
Discourse.deprecate("User#approve is deprecated. Please use the Reviewable API instead.", output_in_test: true, since: "2.3.0beta5", drop_from: "2.4")
|
|
|
|
|
|
|
|
# Backwards compatibility - in case plugins or something is using the old API which accepted
|
|
|
|
# either a Number or object. Probably should remove at some point
|
|
|
|
approved_by = User.find_by(id: approved_by) if approved_by.is_a?(Numeric)
|
|
|
|
|
|
|
|
if reviewable_user = ReviewableUser.find_by(target: self)
|
2019-04-17 23:26:43 +08:00
|
|
|
result = reviewable_user.perform(approved_by, :approve_user, send_email: send_mail)
|
2019-01-04 01:03:01 +08:00
|
|
|
if result.success?
|
|
|
|
Reviewable.set_approved_fields!(self, approved_by)
|
|
|
|
return true
|
|
|
|
end
|
2016-09-13 16:03:17 +08:00
|
|
|
end
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
false
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.email_hash(email)
|
2013-02-06 10:44:49 +08:00
|
|
|
Digest::MD5.hexdigest(email.strip.downcase)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def email_hash
|
2013-02-28 21:08:56 +08:00
|
|
|
User.email_hash(email)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
2015-04-17 14:01:20 +08:00
|
|
|
@unread_notifications = nil
|
2014-10-13 18:26:30 +08:00
|
|
|
@unread_total_notifications = nil
|
2013-05-16 14:37:47 +08:00
|
|
|
@unread_pms = nil
|
2017-12-16 10:16:22 +08:00
|
|
|
@user_fields = nil
|
2013-02-06 03:16:51 +08:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2016-12-13 03:20:25 +08:00
|
|
|
def unread_notifications_of_type(notification_type)
|
|
|
|
# perf critical, much more efficient than AR
|
2018-05-26 08:09:48 +08:00
|
|
|
sql = <<~SQL
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM notifications n
|
|
|
|
LEFT JOIN topics t ON t.id = n.topic_id
|
|
|
|
WHERE t.deleted_at IS NULL
|
|
|
|
AND n.notification_type = :type
|
|
|
|
AND n.user_id = :user_id
|
|
|
|
AND NOT read
|
|
|
|
SQL
|
2016-12-13 03:20:25 +08:00
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
# to avoid coalesce we do to_i
|
|
|
|
DB.query_single(sql, user_id: id, type: notification_type)[0].to_i
|
2016-12-13 03:20:25 +08:00
|
|
|
end
|
2015-04-17 14:01:20 +08:00
|
|
|
|
2016-12-13 03:20:25 +08:00
|
|
|
def unread_private_messages
|
|
|
|
@unread_pms ||= unread_notifications_of_type(Notification.types[:private_message])
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2018-10-24 08:53:28 +08:00
|
|
|
# PERF: This safeguard is in place to avoid situations where
|
|
|
|
# a user with enormous amounts of unread data can issue extremely
|
|
|
|
# expensive queries
|
|
|
|
MAX_UNREAD_NOTIFICATIONS = 99
|
|
|
|
|
2018-10-24 09:10:27 +08:00
|
|
|
def self.max_unread_notifications
|
|
|
|
@max_unread_notifications ||= MAX_UNREAD_NOTIFICATIONS
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.max_unread_notifications=(val)
|
|
|
|
@max_unread_notifications = val
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def unread_notifications
|
2018-05-26 08:09:48 +08:00
|
|
|
@unread_notifications ||= begin
|
|
|
|
# perf critical, much more efficient than AR
|
|
|
|
sql = <<~SQL
|
2018-10-24 09:10:27 +08:00
|
|
|
SELECT COUNT(*) FROM (
|
|
|
|
SELECT 1 FROM
|
|
|
|
notifications n
|
|
|
|
LEFT JOIN topics t ON t.id = n.topic_id
|
|
|
|
WHERE t.deleted_at IS NULL AND
|
|
|
|
n.notification_type <> :pm AND
|
|
|
|
n.user_id = :user_id AND
|
|
|
|
n.id > :seen_notification_id AND
|
|
|
|
NOT read
|
|
|
|
LIMIT :limit
|
|
|
|
) AS X
|
2018-05-26 08:09:48 +08:00
|
|
|
SQL
|
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.query_single(sql,
|
2018-05-26 08:09:48 +08:00
|
|
|
user_id: id,
|
|
|
|
seen_notification_id: seen_notification_id,
|
2018-12-04 17:48:16 +08:00
|
|
|
pm: Notification.types[:private_message],
|
2018-10-24 09:10:27 +08:00
|
|
|
limit: User.max_unread_notifications
|
2018-06-19 14:13:14 +08:00
|
|
|
)[0].to_i
|
2018-05-26 08:09:48 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-06 10:44:49 +08:00
|
|
|
|
2014-10-13 18:26:30 +08:00
|
|
|
def total_unread_notifications
|
|
|
|
@unread_total_notifications ||= notifications.where("read = false").count
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def saw_notification_id(notification_id)
|
2016-09-16 10:02:19 +08:00
|
|
|
if seen_notification_id.to_i < notification_id.to_i
|
|
|
|
update_columns(seen_notification_id: notification_id.to_i)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2016-11-16 16:20:38 +08:00
|
|
|
TRACK_FIRST_NOTIFICATION_READ_DURATION = 1.week.to_i
|
|
|
|
|
2016-11-08 16:12:40 +08:00
|
|
|
def read_first_notification?
|
2017-02-17 23:28:38 +08:00
|
|
|
if (trust_level > TrustLevel[1] ||
|
2018-01-20 00:56:24 +08:00
|
|
|
(first_seen_at.present? && first_seen_at < TRACK_FIRST_NOTIFICATION_READ_DURATION.seconds.ago))
|
2016-11-16 16:20:38 +08:00
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2016-11-17 09:44:00 +08:00
|
|
|
self.seen_notification_id == 0 ? false : true
|
2016-11-08 16:12:40 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def publish_notifications_state
|
2018-05-26 08:09:48 +08:00
|
|
|
# publish last notification json with the message so we can apply an update
|
2018-06-28 23:04:40 +08:00
|
|
|
notification = notifications.visible.order('notifications.created_at desc').first
|
2015-09-04 11:20:33 +08:00
|
|
|
json = NotificationSerializer.new(notification).as_json if notification
|
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
sql = (<<~SQL).freeze
|
2016-02-15 16:29:35 +08:00
|
|
|
SELECT * FROM (
|
|
|
|
SELECT n.id, n.read FROM notifications n
|
|
|
|
LEFT JOIN topics t ON n.topic_id = t.id
|
|
|
|
WHERE
|
|
|
|
t.deleted_at IS NULL AND
|
|
|
|
n.notification_type = :type AND
|
|
|
|
n.user_id = :user_id AND
|
|
|
|
NOT read
|
|
|
|
ORDER BY n.id DESC
|
|
|
|
LIMIT 20
|
|
|
|
) AS x
|
|
|
|
UNION ALL
|
|
|
|
SELECT * FROM (
|
|
|
|
SELECT n.id, n.read FROM notifications n
|
|
|
|
LEFT JOIN topics t ON n.topic_id = t.id
|
|
|
|
WHERE
|
|
|
|
t.deleted_at IS NULL AND
|
|
|
|
(n.notification_type <> :type OR read) AND
|
|
|
|
n.user_id = :user_id
|
|
|
|
ORDER BY n.id DESC
|
|
|
|
LIMIT 20
|
|
|
|
) AS y
|
2018-06-19 14:13:14 +08:00
|
|
|
SQL
|
2016-02-15 16:29:35 +08:00
|
|
|
|
2018-06-19 14:13:14 +08:00
|
|
|
recent = DB.query(sql,
|
2017-09-15 16:18:30 +08:00
|
|
|
user_id: id,
|
|
|
|
type: Notification.types[:private_message]
|
2018-06-19 14:13:14 +08:00
|
|
|
).map! do |r|
|
|
|
|
[r.id, r.read]
|
2016-02-15 16:29:35 +08:00
|
|
|
end
|
|
|
|
|
2018-05-26 08:09:48 +08:00
|
|
|
payload = {
|
|
|
|
unread_notifications: unread_notifications,
|
|
|
|
unread_private_messages: unread_private_messages,
|
|
|
|
read_first_notification: read_first_notification?,
|
|
|
|
last_notification: json,
|
|
|
|
recent: recent,
|
|
|
|
seen_notification_id: seen_notification_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageBus.publish("/notification/#{id}", payload, user_ids: [id])
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# A selection of people to autocomplete on @mention
|
|
|
|
def self.mentionable_usernames
|
2013-02-06 10:44:49 +08:00
|
|
|
User.select(:username).order('last_posted_at desc').limit(20)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def password=(password)
|
2013-02-06 10:44:49 +08:00
|
|
|
# special case for passwordless accounts
|
2015-06-06 01:09:02 +08:00
|
|
|
unless password.blank?
|
|
|
|
@raw_password = password
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-12-20 04:12:03 +08:00
|
|
|
def password
|
|
|
|
'' # so that validator doesn't complain that a password attribute doesn't exist
|
|
|
|
end
|
|
|
|
|
2013-02-13 04:42:04 +08:00
|
|
|
# Indicate that this is NOT a passwordless account for the purposes of validation
|
2013-02-28 21:08:56 +08:00
|
|
|
def password_required!
|
2013-02-13 04:42:04 +08:00
|
|
|
@password_required = true
|
|
|
|
end
|
|
|
|
|
2013-12-20 04:12:03 +08:00
|
|
|
def password_required?
|
|
|
|
!!@password_required
|
|
|
|
end
|
|
|
|
|
2017-12-01 12:19:24 +08:00
|
|
|
def password_validation_required?
|
|
|
|
password_required? || @raw_password.present?
|
|
|
|
end
|
|
|
|
|
2014-01-22 01:42:20 +08:00
|
|
|
def has_password?
|
|
|
|
password_hash.present?
|
|
|
|
end
|
|
|
|
|
2013-12-20 04:12:03 +08:00
|
|
|
def password_validator
|
|
|
|
PasswordValidator.new(attributes: :password).validate_each(self, :password, @raw_password)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def confirm_password?(password)
|
2013-02-28 21:08:56 +08:00
|
|
|
return false unless password_hash && salt
|
|
|
|
self.password_hash == hash_password(password, salt)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-10-12 01:33:23 +08:00
|
|
|
|
2016-06-21 04:38:15 +08:00
|
|
|
def new_user_posting_on_first_day?
|
2015-03-26 13:48:36 +08:00
|
|
|
!staff? &&
|
|
|
|
trust_level < TrustLevel[2] &&
|
2018-06-21 08:25:03 +08:00
|
|
|
(trust_level == TrustLevel[0] || self.first_post_created_at.nil? || self.first_post_created_at >= 24.hours.ago)
|
2015-03-26 13:48:36 +08:00
|
|
|
end
|
|
|
|
|
2013-10-12 01:33:23 +08:00
|
|
|
def new_user?
|
2015-03-26 13:04:32 +08:00
|
|
|
(created_at >= 24.hours.ago || trust_level == TrustLevel[0]) &&
|
|
|
|
trust_level < TrustLevel[2] &&
|
|
|
|
!staff?
|
2013-10-12 01:33:23 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-02-12 13:41:04 +08:00
|
|
|
def seen_before?
|
|
|
|
last_seen_at.present?
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def create_visit_record!(date, opts = {})
|
2015-07-08 00:31:07 +08:00
|
|
|
user_stat.update_column(:days_visited, user_stat.days_visited + 1)
|
|
|
|
user_visits.create!(visited_at: date, posts_read: opts[:posts_read] || 0, mobile: opts[:mobile] || false)
|
|
|
|
end
|
|
|
|
|
2014-01-25 04:19:20 +08:00
|
|
|
def visit_record_for(date)
|
2014-05-06 21:41:59 +08:00
|
|
|
user_visits.find_by(visited_at: date)
|
2013-02-12 13:41:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_visit_record!(date)
|
2014-01-25 04:19:20 +08:00
|
|
|
create_visit_record!(date) unless visit_record_for(date)
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def update_posts_read!(num_posts, opts = {})
|
2015-07-08 00:31:07 +08:00
|
|
|
now = opts[:at] || Time.zone.now
|
|
|
|
_retry = opts[:retry] || false
|
|
|
|
|
2014-01-25 04:19:20 +08:00
|
|
|
if user_visit = visit_record_for(now.to_date)
|
|
|
|
user_visit.posts_read += num_posts
|
2015-07-08 00:31:07 +08:00
|
|
|
user_visit.mobile = true if opts[:mobile]
|
2014-01-25 04:19:20 +08:00
|
|
|
user_visit.save
|
|
|
|
user_visit
|
|
|
|
else
|
2015-06-01 09:55:07 +08:00
|
|
|
begin
|
2015-07-08 00:31:07 +08:00
|
|
|
create_visit_record!(now.to_date, posts_read: num_posts, mobile: opts.fetch(:mobile, false))
|
2015-06-01 09:55:07 +08:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
if !_retry
|
2017-07-28 09:20:09 +08:00
|
|
|
update_posts_read!(num_posts, opts.merge(retry: true))
|
2015-06-01 09:55:07 +08:00
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
2013-02-12 13:41:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-24 18:42:04 +08:00
|
|
|
def update_ip_address!(new_ip_address)
|
2013-03-09 09:24:10 +08:00
|
|
|
unless ip_address == new_ip_address || new_ip_address.blank?
|
2013-02-24 19:56:08 +08:00
|
|
|
update_column(:ip_address, new_ip_address)
|
2013-02-24 18:42:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def update_last_seen!(now = Time.zone.now)
|
2013-10-24 05:24:50 +08:00
|
|
|
now_date = now.to_date
|
2013-02-06 03:16:51 +08:00
|
|
|
# Only update last seen once every minute
|
2013-10-24 05:24:50 +08:00
|
|
|
redis_key = "user:#{id}:#{now_date}"
|
|
|
|
return unless $redis.setnx(redis_key, "1")
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-10-24 05:24:50 +08:00
|
|
|
$redis.expire(redis_key, SiteSetting.active_user_rate_limit_secs)
|
|
|
|
update_previous_visit(now)
|
|
|
|
# using update_column to avoid the AR transaction
|
|
|
|
update_column(:last_seen_at, now)
|
2016-05-21 21:17:54 +08:00
|
|
|
update_column(:first_seen_at, now) unless self.first_seen_at
|
2017-04-01 06:30:59 +08:00
|
|
|
|
|
|
|
DiscourseEvent.trigger(:user_seen, self)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-08-14 04:08:29 +08:00
|
|
|
def self.gravatar_template(email)
|
2018-07-18 18:57:43 +08:00
|
|
|
"//www.gravatar.com/avatar/#{self.email_hash(email)}.png?s={size}&r=pg&d=identicon"
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-09 04:58:37 +08:00
|
|
|
# Don't pass this up to the client - it's meant for server side use
|
2013-08-14 04:08:29 +08:00
|
|
|
# This is used in
|
|
|
|
# - self oneboxes in open graph data
|
|
|
|
# - emails
|
2013-03-09 04:58:37 +08:00
|
|
|
def small_avatar_url
|
2014-05-22 15:37:02 +08:00
|
|
|
avatar_template_url.gsub("{size}", "45")
|
2013-03-09 04:58:37 +08:00
|
|
|
end
|
|
|
|
|
2014-05-22 15:37:02 +08:00
|
|
|
def avatar_template_url
|
2015-06-12 18:02:36 +08:00
|
|
|
UrlHelper.schemaless UrlHelper.absolute avatar_template
|
2013-09-11 03:18:22 +08:00
|
|
|
end
|
|
|
|
|
2018-07-18 18:57:43 +08:00
|
|
|
def self.username_hash(username)
|
|
|
|
username.each_char.reduce(0) do |result, char|
|
|
|
|
[((result << 5) - result) + char.ord].pack('L').unpack('l').first
|
|
|
|
end.abs
|
|
|
|
end
|
|
|
|
|
2015-06-27 01:37:50 +08:00
|
|
|
def self.default_template(username)
|
|
|
|
if SiteSetting.default_avatars.present?
|
2018-07-18 18:57:43 +08:00
|
|
|
urls = SiteSetting.default_avatars.split("\n")
|
|
|
|
return urls[username_hash(username) % urls.size] if urls.present?
|
2015-06-27 01:37:50 +08:00
|
|
|
end
|
2018-07-18 18:57:43 +08:00
|
|
|
|
|
|
|
system_avatar_template(username)
|
2015-06-27 01:37:50 +08:00
|
|
|
end
|
|
|
|
|
2015-09-11 08:12:40 +08:00
|
|
|
def self.avatar_template(username, uploaded_avatar_id)
|
2014-05-22 15:37:02 +08:00
|
|
|
username ||= ""
|
2015-09-11 21:04:29 +08:00
|
|
|
return default_template(username) if !uploaded_avatar_id
|
2015-05-30 00:51:17 +08:00
|
|
|
hostname = RailsMultisite::ConnectionManagement.current_hostname
|
|
|
|
UserAvatar.local_avatar_template(hostname, username.downcase, uploaded_avatar_id)
|
2014-05-22 15:37:02 +08:00
|
|
|
end
|
|
|
|
|
2015-09-11 16:14:34 +08:00
|
|
|
def self.system_avatar_template(username)
|
2019-04-25 05:18:52 +08:00
|
|
|
normalized_username = normalize_username(username)
|
|
|
|
|
2015-09-11 16:14:34 +08:00
|
|
|
# TODO it may be worth caching this in a distributed cache, should be benched
|
|
|
|
if SiteSetting.external_system_avatars_enabled
|
|
|
|
url = SiteSetting.external_system_avatars_url.dup
|
2019-05-03 06:17:27 +08:00
|
|
|
url = +"#{Discourse::base_uri}#{url}" unless url =~ /^https?:\/\//
|
2019-04-25 05:18:52 +08:00
|
|
|
url.gsub! "{color}", letter_avatar_color(normalized_username)
|
2019-04-29 21:08:14 +08:00
|
|
|
url.gsub! "{username}", CGI.escape(username)
|
|
|
|
url.gsub! "{first_letter}", CGI.escape(normalized_username.grapheme_clusters.first)
|
2015-10-02 15:27:54 +08:00
|
|
|
url.gsub! "{hostname}", Discourse.current_hostname
|
2015-09-11 16:14:34 +08:00
|
|
|
url
|
2015-09-11 08:12:40 +08:00
|
|
|
else
|
2019-04-25 05:18:52 +08:00
|
|
|
"#{Discourse.base_uri}/letter_avatar/#{normalized_username}/{size}/#{LetterAvatar.version}.png"
|
2015-09-11 08:12:40 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.letter_avatar_color(username)
|
2015-09-11 21:04:29 +08:00
|
|
|
username ||= ""
|
2019-03-18 23:24:21 +08:00
|
|
|
if SiteSetting.restrict_letter_avatar_colors.present?
|
|
|
|
hex_length = 6
|
|
|
|
colors = SiteSetting.restrict_letter_avatar_colors
|
|
|
|
length = colors.count("|") + 1
|
|
|
|
num = color_index(username, length)
|
|
|
|
index = (num * hex_length) + num
|
|
|
|
colors[index, hex_length]
|
|
|
|
else
|
|
|
|
color = LetterAvatar::COLORS[color_index(username, LetterAvatar::COLORS.length)]
|
|
|
|
color.map { |c| c.to_s(16).rjust(2, '0') }.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.color_index(username, length)
|
|
|
|
Digest::MD5.hexdigest(username)[0...15].to_i(16) % length
|
2014-05-30 12:17:35 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def avatar_template
|
2015-09-11 08:12:40 +08:00
|
|
|
self.class.avatar_template(username, uploaded_avatar_id)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# The following count methods are somewhat slow - definitely don't use them in a loop.
|
2013-03-06 15:52:24 +08:00
|
|
|
# They might need to be denormalized
|
2013-02-06 03:16:51 +08:00
|
|
|
def like_count
|
2013-02-28 21:08:56 +08:00
|
|
|
UserAction.where(user_id: id, action_type: UserAction::WAS_LIKED).count
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-08-23 03:23:10 +08:00
|
|
|
def like_given_count
|
|
|
|
UserAction.where(user_id: id, action_type: UserAction::LIKE).count
|
2014-08-23 02:37:00 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def post_count
|
2014-07-29 01:17:37 +08:00
|
|
|
stat = user_stat || create_user_stat
|
|
|
|
stat.post_count
|
2014-02-21 01:29:40 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def flags_given_count
|
2017-10-18 01:31:45 +08:00
|
|
|
PostAction.where(user_id: id, post_action_type_id: PostActionType.flag_types_without_custom.values).count
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-09-08 23:11:56 +08:00
|
|
|
def warnings_received_count
|
2017-04-15 12:11:02 +08:00
|
|
|
user_warnings.count
|
2014-09-08 23:11:56 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def flags_received_count
|
2017-10-18 01:31:45 +08:00
|
|
|
posts.includes(:post_actions).where('post_actions.post_action_type_id' => PostActionType.flag_types_without_custom.values).count
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def private_topics_count
|
|
|
|
topics_allowed.where(archetype: Archetype.private_message).count
|
|
|
|
end
|
|
|
|
|
2013-12-20 02:45:55 +08:00
|
|
|
def posted_too_much_in_topic?(topic_id)
|
2016-04-19 04:08:42 +08:00
|
|
|
# Does not apply to staff and non-new members...
|
|
|
|
return false if staff? || (trust_level != TrustLevel[0])
|
|
|
|
# ... your own topics or in private messages
|
|
|
|
topic = Topic.where(id: topic_id).first
|
|
|
|
return false if topic.try(:private_message?) || (topic.try(:user_id) == self.id)
|
2014-01-03 01:57:40 +08:00
|
|
|
|
2014-04-30 00:59:14 +08:00
|
|
|
last_action_in_topic = UserAction.last_action_in_topic(id, topic_id)
|
|
|
|
since_reply = Post.where(user_id: id, topic_id: topic_id)
|
|
|
|
since_reply = since_reply.where('id > ?', last_action_in_topic) if last_action_in_topic
|
|
|
|
|
|
|
|
(since_reply.count >= SiteSetting.newuser_max_replies_per_topic)
|
2013-12-20 02:45:55 +08:00
|
|
|
end
|
|
|
|
|
2018-12-14 18:04:18 +08:00
|
|
|
def delete_posts_in_batches(guardian, batch_size = 20)
|
2013-02-07 15:11:56 +08:00
|
|
|
raise Discourse::InvalidAccess unless guardian.can_delete_all_posts? self
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
Reviewable.where(created_by_id: id).delete_all
|
2015-04-25 04:04:44 +08:00
|
|
|
|
2018-12-14 18:04:18 +08:00
|
|
|
posts.order("post_number desc").limit(batch_size).each do |p|
|
2013-06-06 04:00:45 +08:00
|
|
|
PostDestroyer.new(guardian.user, p).destroy
|
2013-02-07 15:11:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
def suspended?
|
2017-11-29 02:44:24 +08:00
|
|
|
!!(suspended_till && suspended_till > Time.zone.now)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-11-14 02:41:36 +08:00
|
|
|
def silenced?
|
2017-11-29 02:44:24 +08:00
|
|
|
!!(silenced_till && silenced_till > Time.zone.now)
|
2017-11-14 02:41:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def silenced_record
|
|
|
|
UserHistory.for(self, :silence_user).order('id DESC').first
|
|
|
|
end
|
|
|
|
|
|
|
|
def silence_reason
|
|
|
|
silenced_record.try(:details) if silenced?
|
|
|
|
end
|
|
|
|
|
|
|
|
def silenced_at
|
|
|
|
silenced_record.try(:created_at) if silenced?
|
|
|
|
end
|
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
def suspend_record
|
|
|
|
UserHistory.for(self, :suspend_user).order('id DESC').first
|
2013-11-01 22:47:03 +08:00
|
|
|
end
|
|
|
|
|
2017-12-08 02:20:42 +08:00
|
|
|
def full_suspend_reason
|
|
|
|
return suspend_record.try(:details) if suspended?
|
|
|
|
end
|
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
def suspend_reason
|
2017-12-08 02:20:42 +08:00
|
|
|
if details = full_suspend_reason
|
|
|
|
return details.split("\n")[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
2013-11-01 22:47:03 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Use this helper to determine if the user has a particular trust level.
|
|
|
|
# Takes into account admin, etc.
|
2013-02-06 10:44:49 +08:00
|
|
|
def has_trust_level?(level)
|
2016-05-30 11:38:04 +08:00
|
|
|
unless TrustLevel.valid?(level)
|
|
|
|
raise InvalidTrustLevel.new("Invalid trust level #{level}")
|
|
|
|
end
|
|
|
|
|
2015-12-08 00:01:08 +08:00
|
|
|
admin? || moderator? || staged? || TrustLevel.compare(trust_level, level)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-03-20 07:51:39 +08:00
|
|
|
# a touch faster than automatic
|
2013-04-01 00:51:13 +08:00
|
|
|
def admin?
|
2013-03-20 07:51:39 +08:00
|
|
|
admin
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def guardian
|
|
|
|
Guardian.new(self)
|
|
|
|
end
|
|
|
|
|
2018-04-03 00:44:04 +08:00
|
|
|
def username_format_validator
|
|
|
|
UsernameValidator.perform_validation(self, 'username')
|
2013-02-08 07:23:41 +08:00
|
|
|
end
|
|
|
|
|
2013-02-12 00:18:26 +08:00
|
|
|
def email_confirmed?
|
2018-05-15 07:48:30 +08:00
|
|
|
email_tokens.where(email: email, confirmed: true).present? ||
|
|
|
|
email_tokens.empty? ||
|
|
|
|
single_sign_on_record&.external_email == email
|
2013-02-12 00:18:26 +08:00
|
|
|
end
|
|
|
|
|
2013-05-08 09:58:34 +08:00
|
|
|
def activate
|
2017-09-12 23:36:17 +08:00
|
|
|
if email_token = self.email_tokens.active.where(email: self.email).first
|
2019-04-04 04:03:32 +08:00
|
|
|
user = EmailToken.confirm(email_token.token, skip_reviewable: true)
|
2018-02-14 22:52:05 +08:00
|
|
|
self.update!(active: true) if user.nil?
|
2013-05-08 09:58:34 +08:00
|
|
|
else
|
2017-09-13 15:33:59 +08:00
|
|
|
self.update!(active: true)
|
2013-05-08 09:58:34 +08:00
|
|
|
end
|
2019-04-04 00:04:05 +08:00
|
|
|
create_reviewable
|
2013-05-08 09:58:34 +08:00
|
|
|
end
|
|
|
|
|
2019-04-04 00:04:05 +08:00
|
|
|
def deactivate(performed_by)
|
2017-09-13 15:33:59 +08:00
|
|
|
self.update!(active: false)
|
2019-04-04 00:04:05 +08:00
|
|
|
|
|
|
|
if reviewable = ReviewableUser.pending.find_by(target: self)
|
2019-04-17 23:26:43 +08:00
|
|
|
reviewable.perform(performed_by, :reject_user_delete)
|
2019-04-04 00:04:05 +08:00
|
|
|
end
|
2013-05-08 09:58:34 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def change_trust_level!(level, opts = nil)
|
2014-06-17 08:46:30 +08:00
|
|
|
Promotion.new(self).change_trust_level!(level, opts)
|
|
|
|
end
|
|
|
|
|
2013-02-22 02:20:00 +08:00
|
|
|
def readable_name
|
2018-05-15 07:48:30 +08:00
|
|
|
name.present? && name != username ? "#{name} (#{username})" : username
|
2013-02-22 02:20:00 +08:00
|
|
|
end
|
|
|
|
|
2014-04-16 18:22:21 +08:00
|
|
|
def badge_count
|
2014-07-16 14:21:46 +08:00
|
|
|
user_badges.select('distinct badge_id').count
|
2014-04-16 18:22:21 +08:00
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def featured_user_badges(limit = 3)
|
2016-11-15 04:53:24 +08:00
|
|
|
tl_badge_ids = Badge.trust_level_badge_ids
|
|
|
|
|
|
|
|
query = user_badges
|
2017-07-28 09:20:09 +08:00
|
|
|
.group(:badge_id)
|
|
|
|
.select(UserBadge.attribute_names.map { |x| "MAX(user_badges.#{x}) AS #{x}" },
|
2016-11-15 04:53:24 +08:00
|
|
|
'COUNT(*) AS "count"',
|
|
|
|
'MAX(badges.badge_type_id) AS badges_badge_type_id',
|
|
|
|
'MAX(badges.grant_count) AS badges_grant_count')
|
2017-07-28 09:20:09 +08:00
|
|
|
.joins(:badge)
|
|
|
|
.order('badges_badge_type_id ASC, badges_grant_count ASC, badge_id DESC')
|
|
|
|
.includes(:user, :granted_by, { badge: :badge_type }, post: :topic)
|
2016-11-15 04:53:24 +08:00
|
|
|
|
|
|
|
tl_badge = query.where("user_badges.badge_id IN (:tl_badge_ids)",
|
|
|
|
tl_badge_ids: tl_badge_ids)
|
2017-07-28 09:20:09 +08:00
|
|
|
.limit(1)
|
2016-11-15 04:53:24 +08:00
|
|
|
|
|
|
|
other_badges = query.where("user_badges.badge_id NOT IN (:tl_badge_ids)",
|
|
|
|
tl_badge_ids: tl_badge_ids)
|
2017-07-28 09:20:09 +08:00
|
|
|
.limit(limit)
|
2016-11-15 04:53:24 +08:00
|
|
|
|
|
|
|
(tl_badge + other_badges).take(limit)
|
2014-04-16 18:11:11 +08:00
|
|
|
end
|
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
def self.count_by_signup_date(start_date = nil, end_date = nil, group_id = nil)
|
|
|
|
result = self
|
|
|
|
|
|
|
|
if start_date && end_date
|
2018-05-11 11:30:21 +08:00
|
|
|
result = result.group("date(users.created_at)")
|
|
|
|
result = result.where("users.created_at >= ? AND users.created_at <= ?", start_date, end_date)
|
2018-06-05 15:29:17 +08:00
|
|
|
result = result.order("date(users.created_at)")
|
2018-05-03 21:41:41 +08:00
|
|
|
end
|
2016-02-03 10:29:51 +08:00
|
|
|
|
|
|
|
if group_id
|
|
|
|
result = result.joins("INNER JOIN group_users ON group_users.user_id = users.id")
|
|
|
|
result = result.where("group_users.group_id = ?", group_id)
|
|
|
|
end
|
2018-04-26 20:49:41 +08:00
|
|
|
|
|
|
|
result.count
|
|
|
|
end
|
|
|
|
|
2018-05-03 21:41:41 +08:00
|
|
|
def self.count_by_first_post(start_date = nil, end_date = nil)
|
|
|
|
result = joins('INNER JOIN user_stats AS us ON us.user_id = users.id')
|
|
|
|
|
|
|
|
if start_date && end_date
|
2018-05-11 11:30:21 +08:00
|
|
|
result = result.group("date(us.first_post_created_at)")
|
|
|
|
result = result.where("us.first_post_created_at > ? AND us.first_post_created_at < ?", start_date, end_date)
|
|
|
|
result = result.order("date(us.first_post_created_at)")
|
2018-05-03 21:41:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
result.count
|
2013-03-08 00:07:59 +08:00
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
def secure_category_ids
|
2019-04-30 14:58:18 +08:00
|
|
|
cats = self.admin? ? Category.unscoped.where(read_restricted: true) : secure_categories.references(:categories)
|
2013-09-10 12:29:02 +08:00
|
|
|
cats.pluck('categories.id').sort
|
2013-04-29 14:33:24 +08:00
|
|
|
end
|
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
def topic_create_allowed_category_ids
|
|
|
|
Category.topic_create_allowed(self.id).select(:id)
|
|
|
|
end
|
|
|
|
|
2013-05-11 04:58:23 +08:00
|
|
|
# Flag all posts from a user as spam
|
|
|
|
def flag_linked_posts_as_spam
|
2019-01-04 01:03:01 +08:00
|
|
|
results = []
|
|
|
|
|
2016-04-26 05:03:17 +08:00
|
|
|
disagreed_flag_post_ids = PostAction.where(post_action_type_id: PostActionType.types[:spam])
|
2017-07-28 09:20:09 +08:00
|
|
|
.where.not(disagreed_at: nil)
|
|
|
|
.pluck(:post_id)
|
2015-10-17 03:16:44 +08:00
|
|
|
|
2016-04-26 05:03:17 +08:00
|
|
|
topic_links.includes(:post)
|
2017-07-28 09:20:09 +08:00
|
|
|
.where.not(post_id: disagreed_flag_post_ids)
|
|
|
|
.each do |tl|
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
message = I18n.t('flag_reason.spam_hosts', domain: tl.domain, base_path: Discourse.base_path)
|
|
|
|
results << PostActionCreator.create(Discourse.system_user, tl.post, :spam, message: message)
|
2013-05-11 04:58:23 +08:00
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
|
|
|
|
results
|
2013-05-11 04:58:23 +08:00
|
|
|
end
|
2013-05-13 16:04:03 +08:00
|
|
|
|
2013-08-14 04:08:29 +08:00
|
|
|
def has_uploaded_avatar
|
|
|
|
uploaded_avatar.present?
|
|
|
|
end
|
2013-05-24 18:58:26 +08:00
|
|
|
|
2013-10-23 03:53:08 +08:00
|
|
|
def generate_api_key(created_by)
|
|
|
|
if api_key.present?
|
|
|
|
api_key.regenerate!(created_by)
|
|
|
|
api_key
|
|
|
|
else
|
2019-05-24 12:06:58 +08:00
|
|
|
ApiKey.create!(user: self, key: SecureRandom.hex(32), created_by: created_by)
|
2013-10-23 03:53:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke_api_key
|
|
|
|
ApiKey.where(user_id: self.id).delete_all
|
|
|
|
end
|
|
|
|
|
2013-11-15 23:27:43 +08:00
|
|
|
def find_email
|
2017-04-15 17:18:05 +08:00
|
|
|
last_sent_email_address.present? && EmailValidator.email_regex =~ last_sent_email_address ? last_sent_email_address : email
|
2013-11-15 23:27:43 +08:00
|
|
|
end
|
|
|
|
|
2014-09-25 08:19:26 +08:00
|
|
|
def tl3_requirements
|
2014-09-05 13:20:39 +08:00
|
|
|
@lq ||= TrustLevel3Requirements.new(self)
|
2014-01-23 06:09:56 +08:00
|
|
|
end
|
|
|
|
|
2014-09-25 08:19:26 +08:00
|
|
|
def on_tl3_grace_period?
|
2018-10-12 03:11:40 +08:00
|
|
|
return true if SiteSetting.tl3_promotion_min_duration.to_i.days.ago.year < 2013
|
|
|
|
|
2014-09-14 04:55:26 +08:00
|
|
|
UserHistory.for(self, :auto_trust_level_change)
|
|
|
|
.where('created_at >= ?', SiteSetting.tl3_promotion_min_duration.to_i.days.ago)
|
|
|
|
.where(previous_value: TrustLevel[2].to_s)
|
|
|
|
.where(new_value: TrustLevel[3].to_s)
|
|
|
|
.exists?
|
|
|
|
end
|
|
|
|
|
2014-05-22 15:37:02 +08:00
|
|
|
def refresh_avatar
|
2014-08-14 04:17:16 +08:00
|
|
|
return if @import_mode
|
|
|
|
|
2014-05-28 14:54:21 +08:00
|
|
|
avatar = user_avatar || create_user_avatar
|
2014-05-22 15:37:02 +08:00
|
|
|
|
2014-05-28 14:54:21 +08:00
|
|
|
if SiteSetting.automatically_download_gravatars? && !avatar.last_gravatar_download_attempt
|
2016-04-18 18:44:09 +08:00
|
|
|
Jobs.cancel_scheduled_job(:update_gravatar, user_id: self.id, avatar_id: avatar.id)
|
|
|
|
Jobs.enqueue_in(1.second, :update_gravatar, user_id: self.id, avatar_id: avatar.id)
|
2014-07-03 15:29:44 +08:00
|
|
|
end
|
2015-03-30 18:31:10 +08:00
|
|
|
|
2015-04-24 17:14:10 +08:00
|
|
|
# mark all the user's quoted posts as "needing a rebake"
|
2017-08-31 12:06:56 +08:00
|
|
|
Post.rebake_all_quoted_posts(self.id) if self.will_save_change_to_uploaded_avatar_id?
|
2014-07-03 15:29:44 +08:00
|
|
|
end
|
|
|
|
|
2014-07-29 01:17:37 +08:00
|
|
|
def first_post_created_at
|
|
|
|
user_stat.try(:first_post_created_at)
|
|
|
|
end
|
|
|
|
|
2014-09-25 13:50:54 +08:00
|
|
|
def associated_accounts
|
|
|
|
result = []
|
|
|
|
|
2018-07-23 23:51:57 +08:00
|
|
|
Discourse.authenticators.each do |authenticator|
|
|
|
|
account_description = authenticator.description_for_user(self)
|
|
|
|
unless account_description.empty?
|
|
|
|
result << {
|
|
|
|
name: authenticator.name,
|
|
|
|
description: account_description,
|
|
|
|
}
|
|
|
|
end
|
2014-09-25 13:50:54 +08:00
|
|
|
end
|
|
|
|
|
2018-07-23 23:51:57 +08:00
|
|
|
result
|
2014-09-25 13:50:54 +08:00
|
|
|
end
|
|
|
|
|
2018-09-07 06:02:47 +08:00
|
|
|
USER_FIELD_PREFIX ||= "user_field_"
|
|
|
|
|
2014-09-27 02:48:34 +08:00
|
|
|
def user_fields
|
|
|
|
return @user_fields if @user_fields
|
|
|
|
user_field_ids = UserField.pluck(:id)
|
|
|
|
if user_field_ids.present?
|
|
|
|
@user_fields = {}
|
|
|
|
user_field_ids.each do |fid|
|
2018-09-07 06:02:47 +08:00
|
|
|
@user_fields[fid.to_s] = custom_fields["#{USER_FIELD_PREFIX}#{fid}"]
|
2014-09-27 02:48:34 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
@user_fields
|
|
|
|
end
|
|
|
|
|
2015-02-20 01:11:07 +08:00
|
|
|
def number_of_deleted_posts
|
|
|
|
Post.with_deleted
|
2017-07-28 09:20:09 +08:00
|
|
|
.where(user_id: self.id)
|
|
|
|
.where.not(deleted_at: nil)
|
|
|
|
.count
|
2015-02-20 01:11:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def number_of_flagged_posts
|
|
|
|
Post.with_deleted
|
2017-07-28 09:20:09 +08:00
|
|
|
.where(user_id: self.id)
|
|
|
|
.where(id: PostAction.where(post_action_type_id: PostActionType.notify_flag_type_ids)
|
2015-02-20 01:11:07 +08:00
|
|
|
.where(disagreed_at: nil)
|
|
|
|
.select(:post_id))
|
2017-07-28 09:20:09 +08:00
|
|
|
.count
|
2015-02-20 01:11:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def number_of_flags_given
|
|
|
|
PostAction.where(user_id: self.id)
|
2017-07-28 09:20:09 +08:00
|
|
|
.where(disagreed_at: nil)
|
|
|
|
.where(post_action_type_id: PostActionType.notify_flag_type_ids)
|
|
|
|
.count
|
2015-02-20 01:11:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def number_of_suspensions
|
|
|
|
UserHistory.for(self, :suspend_user).count
|
|
|
|
end
|
|
|
|
|
2015-03-07 05:44:54 +08:00
|
|
|
def create_user_profile
|
2019-05-24 12:06:58 +08:00
|
|
|
UserProfile.create!(user_id: id)
|
2015-03-07 05:44:54 +08:00
|
|
|
end
|
|
|
|
|
2018-07-18 18:57:43 +08:00
|
|
|
def set_random_avatar
|
|
|
|
if SiteSetting.selectable_avatars_enabled? && SiteSetting.selectable_avatars.present?
|
|
|
|
urls = SiteSetting.selectable_avatars.split("\n")
|
|
|
|
if urls.present?
|
|
|
|
if upload = Upload.find_by(url: urls.sample)
|
|
|
|
update_column(:uploaded_avatar_id, upload.id)
|
2019-05-24 12:06:58 +08:00
|
|
|
UserAvatar.create!(user_id: id, custom_upload_id: upload.id)
|
2018-07-18 18:57:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-08 10:29:43 +08:00
|
|
|
def anonymous?
|
|
|
|
SiteSetting.allow_anonymous_posting &&
|
|
|
|
trust_level >= 1 &&
|
2019-05-29 12:26:06 +08:00
|
|
|
!!anonymous_user_master
|
2015-04-08 10:29:43 +08:00
|
|
|
end
|
|
|
|
|
2016-04-27 01:08:19 +08:00
|
|
|
def is_singular_admin?
|
2017-03-14 14:33:06 +08:00
|
|
|
User.where(admin: true).where.not(id: id).human_users.blank?
|
2016-04-27 01:08:19 +08:00
|
|
|
end
|
|
|
|
|
2016-07-04 17:20:30 +08:00
|
|
|
def logged_out
|
|
|
|
MessageBus.publish "/logout", self.id, user_ids: [self.id]
|
|
|
|
DiscourseEvent.trigger(:user_logged_out, self)
|
|
|
|
end
|
|
|
|
|
2017-06-01 16:19:42 +08:00
|
|
|
def logged_in
|
|
|
|
DiscourseEvent.trigger(:user_logged_in, self)
|
|
|
|
|
|
|
|
if !self.seen_before?
|
|
|
|
DiscourseEvent.trigger(:user_first_logged_in, self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-15 01:20:18 +08:00
|
|
|
def set_automatic_groups
|
2018-05-15 07:48:30 +08:00
|
|
|
return if !active || staged || !email_confirmed?
|
2017-06-15 01:20:18 +08:00
|
|
|
|
|
|
|
Group.where(automatic: false)
|
2017-07-28 09:20:09 +08:00
|
|
|
.where("LENGTH(COALESCE(automatic_membership_email_domains, '')) > 0")
|
|
|
|
.each do |group|
|
2017-06-15 01:20:18 +08:00
|
|
|
|
|
|
|
domains = group.automatic_membership_email_domains.gsub('.', '\.')
|
|
|
|
|
|
|
|
if email =~ Regexp.new("@(#{domains})$", true) && !group.users.include?(self)
|
|
|
|
group.add(self)
|
|
|
|
GroupActionLogger.new(Discourse.system_user, group).log_add_user_to_group(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-27 02:47:36 +08:00
|
|
|
def email
|
|
|
|
primary_email.email
|
|
|
|
end
|
|
|
|
|
2018-03-02 16:41:02 +08:00
|
|
|
def email=(new_email)
|
2017-04-27 02:47:36 +08:00
|
|
|
if primary_email
|
2018-03-02 16:41:02 +08:00
|
|
|
new_record? ? primary_email.email = new_email : primary_email.update(email: new_email)
|
2017-04-27 02:47:36 +08:00
|
|
|
else
|
2018-03-09 15:59:46 +08:00
|
|
|
self.primary_email = UserEmail.new(email: new_email, user: self, primary: true)
|
2017-04-27 02:47:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-03 19:51:22 +08:00
|
|
|
def emails
|
|
|
|
self.user_emails.order("user_emails.primary DESC NULLS LAST").pluck(:email)
|
|
|
|
end
|
|
|
|
|
|
|
|
def secondary_emails
|
|
|
|
self.user_emails.secondary.pluck(:email)
|
|
|
|
end
|
|
|
|
|
2017-11-15 05:39:07 +08:00
|
|
|
def recent_time_read
|
|
|
|
self.created_at && self.created_at < 60.days.ago ?
|
|
|
|
self.user_visits.where('visited_at >= ?', 60.days.ago).sum(:time_read) :
|
|
|
|
self.user_stat&.time_read
|
|
|
|
end
|
|
|
|
|
2018-01-19 22:29:15 +08:00
|
|
|
def from_staged?
|
|
|
|
custom_fields[User::FROM_STAGED]
|
|
|
|
end
|
|
|
|
|
2018-06-19 08:05:04 +08:00
|
|
|
def mature_staged?
|
2018-06-20 00:41:10 +08:00
|
|
|
from_staged? && self.created_at && self.created_at < 1.day.ago
|
2018-06-19 08:05:04 +08:00
|
|
|
end
|
|
|
|
|
2018-09-21 10:06:08 +08:00
|
|
|
def next_best_title
|
|
|
|
group_titles_query = groups.where("groups.title <> ''")
|
|
|
|
group_titles_query = group_titles_query.order("groups.id = #{primary_group_id} DESC") if primary_group_id
|
|
|
|
group_titles_query = group_titles_query.order("groups.primary_group DESC").limit(1)
|
|
|
|
|
|
|
|
if next_best_group_title = group_titles_query.pluck(:title).first
|
|
|
|
return next_best_group_title
|
|
|
|
end
|
|
|
|
|
|
|
|
next_best_badge_title = badges.where(allow_title: true).limit(1).pluck(:name).first
|
|
|
|
next_best_badge_title ? Badge.display_name(next_best_badge_title) : nil
|
|
|
|
end
|
|
|
|
|
2019-04-04 04:10:36 +08:00
|
|
|
def create_reviewable
|
|
|
|
return unless SiteSetting.must_approve_users? || SiteSetting.invite_only?
|
|
|
|
return if approved?
|
|
|
|
|
|
|
|
Jobs.enqueue(:create_user_reviewable, user_id: self.id)
|
|
|
|
end
|
|
|
|
|
2019-08-10 18:02:12 +08:00
|
|
|
def has_more_posts_than?(max_post_count)
|
|
|
|
return true if user_stat && (user_stat.topic_count + user_stat.post_count) > max_post_count
|
|
|
|
|
|
|
|
DB.query_single(<<~SQL, user_id: self.id).first > max_post_count
|
|
|
|
SELECT COUNT(1)
|
|
|
|
FROM (
|
|
|
|
SELECT 1
|
|
|
|
FROM posts p
|
|
|
|
JOIN topics t ON (p.topic_id = t.id)
|
|
|
|
WHERE p.user_id = :user_id AND
|
|
|
|
p.deleted_at IS NULL AND
|
|
|
|
t.deleted_at IS NULL AND
|
|
|
|
(
|
|
|
|
t.archetype <> 'private_message' OR
|
|
|
|
EXISTS(
|
|
|
|
SELECT 1
|
|
|
|
FROM topic_allowed_users a
|
|
|
|
WHERE a.topic_id = t.id AND a.user_id > 0 AND a.user_id <> :user_id
|
|
|
|
) OR
|
|
|
|
EXISTS(
|
|
|
|
SELECT 1
|
|
|
|
FROM topic_allowed_groups g
|
|
|
|
WHERE g.topic_id = p.topic_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
LIMIT #{max_post_count + 1}
|
|
|
|
) x
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
protected
|
|
|
|
|
2014-07-23 09:42:24 +08:00
|
|
|
def badge_grant
|
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::UserChange, user: self)
|
|
|
|
end
|
|
|
|
|
2015-06-06 01:50:06 +08:00
|
|
|
def expire_old_email_tokens
|
2017-08-31 12:06:56 +08:00
|
|
|
if saved_change_to_password_hash? && !saved_change_to_id?
|
2015-06-06 01:50:06 +08:00
|
|
|
email_tokens.where('not expired').update_all(expired: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-22 10:13:14 +08:00
|
|
|
def index_search
|
|
|
|
SearchIndexer.index(self)
|
|
|
|
end
|
|
|
|
|
2014-03-24 15:03:39 +08:00
|
|
|
def clear_global_notice_if_needed
|
2017-03-14 14:33:06 +08:00
|
|
|
return if id < 0
|
2017-02-13 23:53:45 +08:00
|
|
|
|
2014-03-24 15:03:39 +08:00
|
|
|
if admin && SiteSetting.has_login_hint
|
|
|
|
SiteSetting.has_login_hint = false
|
|
|
|
SiteSetting.global_notice = ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-17 08:46:30 +08:00
|
|
|
def ensure_in_trust_level_group
|
|
|
|
Group.user_trust_level_change!(id, trust_level)
|
|
|
|
end
|
|
|
|
|
2013-09-12 02:50:26 +08:00
|
|
|
def create_user_stat
|
2014-03-04 03:31:29 +08:00
|
|
|
stat = UserStat.new(new_since: Time.now)
|
2013-10-04 11:28:49 +08:00
|
|
|
stat.user_id = id
|
2013-09-12 02:50:26 +08:00
|
|
|
stat.save!
|
|
|
|
end
|
|
|
|
|
2016-02-17 12:46:19 +08:00
|
|
|
def create_user_option
|
2019-05-24 12:06:58 +08:00
|
|
|
UserOption.create!(user_id: id)
|
2016-02-17 12:46:19 +08:00
|
|
|
end
|
|
|
|
|
2013-06-06 22:40:10 +08:00
|
|
|
def create_email_token
|
2019-05-24 12:06:58 +08:00
|
|
|
email_tokens.create!(email: email)
|
2013-06-06 22:40:10 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-06 22:40:10 +08:00
|
|
|
def ensure_password_is_hashed
|
|
|
|
if @raw_password
|
|
|
|
self.salt = SecureRandom.hex(16)
|
|
|
|
self.password_hash = hash_password(@raw_password, salt)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-06-06 22:40:10 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-02-01 06:21:37 +08:00
|
|
|
def expire_tokens_if_password_changed
|
|
|
|
# NOTE: setting raw password is the only valid way of changing a password
|
|
|
|
# the password field in the DB is actually hashed, nobody should be amending direct
|
|
|
|
if @raw_password
|
|
|
|
# Association in model may be out-of-sync
|
|
|
|
UserAuthToken.where(user_id: id).destroy_all
|
|
|
|
# We should not carry this around after save
|
|
|
|
@raw_password = nil
|
2017-12-01 12:19:24 +08:00
|
|
|
@password_required = false
|
2017-02-01 06:21:37 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-06 22:40:10 +08:00
|
|
|
def hash_password(password, salt)
|
2016-05-30 11:38:04 +08:00
|
|
|
raise StandardError.new("password is too long") if password.size > User.max_password_length
|
2013-07-23 09:36:01 +08:00
|
|
|
Pbkdf2.hash_password(password, salt, Rails.configuration.pbkdf2_iterations, Rails.configuration.pbkdf2_algorithm)
|
2013-06-06 22:40:10 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-06 22:40:10 +08:00
|
|
|
def add_trust_level
|
2013-12-21 15:19:22 +08:00
|
|
|
# there is a possibility we did not load trust level column, skip it
|
2013-06-06 22:40:10 +08:00
|
|
|
return unless has_attribute? :trust_level
|
|
|
|
self.trust_level ||= SiteSetting.default_trust_level
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-04-23 18:22:47 +08:00
|
|
|
def update_usernames
|
|
|
|
self.username.unicode_normalize!
|
2013-06-06 22:40:10 +08:00
|
|
|
self.username_lower = username.downcase
|
|
|
|
end
|
2013-02-06 10:44:49 +08:00
|
|
|
|
2018-04-03 00:44:04 +08:00
|
|
|
USERNAME_EXISTS_SQL = <<~SQL
|
2019-04-23 18:22:47 +08:00
|
|
|
(SELECT users.id AS id, true as is_user FROM users
|
|
|
|
WHERE users.username_lower = :username)
|
2018-04-03 00:44:04 +08:00
|
|
|
|
2019-04-23 18:22:47 +08:00
|
|
|
UNION ALL
|
2018-04-03 00:44:04 +08:00
|
|
|
|
2019-04-23 18:22:47 +08:00
|
|
|
(SELECT groups.id, false as is_user FROM groups
|
|
|
|
WHERE lower(groups.name) = :username)
|
2018-04-03 00:44:04 +08:00
|
|
|
SQL
|
|
|
|
|
2019-04-23 18:22:47 +08:00
|
|
|
def self.username_exists?(username)
|
|
|
|
username = normalize_username(username)
|
|
|
|
DB.exec(User::USERNAME_EXISTS_SQL, username: username) > 0
|
2019-02-20 05:31:03 +08:00
|
|
|
end
|
|
|
|
|
2018-04-03 00:44:04 +08:00
|
|
|
def username_validator
|
|
|
|
username_format_validator || begin
|
2019-05-14 04:43:19 +08:00
|
|
|
if will_save_change_to_username?
|
|
|
|
existing = DB.query(
|
|
|
|
USERNAME_EXISTS_SQL,
|
|
|
|
username: self.class.normalize_username(username)
|
|
|
|
)
|
|
|
|
|
|
|
|
user_id = existing.select { |u| u.is_user }.first&.id
|
|
|
|
same_user = user_id && user_id == self.id
|
2018-06-19 14:13:14 +08:00
|
|
|
|
2019-05-14 04:43:19 +08:00
|
|
|
if existing.present? && !same_user
|
|
|
|
errors.add(:username, I18n.t(:'user.username.unique'))
|
|
|
|
end
|
2018-04-03 00:44:04 +08:00
|
|
|
|
2019-05-14 04:43:19 +08:00
|
|
|
if confirm_password?(username) || confirm_password?(username.downcase)
|
|
|
|
errors.add(:username, :same_as_password)
|
|
|
|
end
|
2018-04-03 00:44:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-14 04:43:19 +08:00
|
|
|
def name_validator
|
|
|
|
if name.present? &&
|
|
|
|
(confirm_password?(name) || confirm_password?(name&.downcase))
|
|
|
|
|
|
|
|
errors.add(:name, :same_as_password)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-22 02:39:21 +08:00
|
|
|
def set_default_categories_preferences
|
2016-06-14 22:45:47 +08:00
|
|
|
return if self.staged?
|
|
|
|
|
2015-08-22 02:39:21 +08:00
|
|
|
values = []
|
|
|
|
|
2016-11-10 02:37:54 +08:00
|
|
|
%w{watching watching_first_post tracking muted}.each do |s|
|
2019-07-12 01:41:51 +08:00
|
|
|
category_ids = SiteSetting.get("default_categories_#{s}").split("|").map(&:to_i)
|
2015-08-22 02:39:21 +08:00
|
|
|
category_ids.each do |category_id|
|
2019-07-12 01:41:51 +08:00
|
|
|
next if category_id == 0
|
2015-08-22 02:39:21 +08:00
|
|
|
values << "(#{self.id}, #{category_id}, #{CategoryUser.notification_levels[s.to_sym]})"
|
2013-08-24 05:35:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-22 02:39:21 +08:00
|
|
|
if values.present?
|
2018-06-19 14:13:14 +08:00
|
|
|
DB.exec("INSERT INTO category_users (user_id, category_id, notification_level) VALUES #{values.join(",")}")
|
2014-01-03 04:27:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-03 13:36:25 +08:00
|
|
|
def self.purge_unactivated
|
2017-08-26 03:20:06 +08:00
|
|
|
return [] if SiteSetting.purge_unactivated_users_grace_period_days <= 0
|
|
|
|
|
2018-05-17 00:24:11 +08:00
|
|
|
destroyer = UserDestroyer.new(Discourse.system_user)
|
|
|
|
|
|
|
|
User
|
|
|
|
.where(active: false)
|
2017-07-28 09:20:09 +08:00
|
|
|
.where("created_at < ?", SiteSetting.purge_unactivated_users_grace_period_days.days.ago)
|
2018-05-17 00:24:11 +08:00
|
|
|
.where("NOT admin AND NOT moderator")
|
2018-06-29 11:33:54 +08:00
|
|
|
.where("NOT EXISTS
|
2018-07-13 10:28:27 +08:00
|
|
|
(SELECT 1 FROM topic_allowed_users tu JOIN topics t ON t.id = tu.topic_id AND t.user_id > 0 WHERE tu.user_id = users.id LIMIT 1)
|
2018-06-29 11:33:54 +08:00
|
|
|
")
|
2017-07-28 09:20:09 +08:00
|
|
|
.limit(200)
|
2018-05-17 00:24:11 +08:00
|
|
|
.find_each do |user|
|
2014-08-20 01:46:40 +08:00
|
|
|
begin
|
2018-05-17 00:24:11 +08:00
|
|
|
destroyer.destroy(user, context: I18n.t(:purge_reason))
|
2015-05-04 23:37:49 +08:00
|
|
|
rescue Discourse::InvalidAccess, UserDestroyer::PostsExistError
|
2018-05-17 00:24:11 +08:00
|
|
|
# keep going
|
2014-08-20 01:46:40 +08:00
|
|
|
end
|
2014-08-14 02:13:41 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-17 13:08:39 +08:00
|
|
|
def match_title_to_primary_group_changes
|
|
|
|
return unless primary_group_id_changed?
|
|
|
|
|
|
|
|
if title == Group.where(id: primary_group_id_was).pluck(:title).first
|
|
|
|
self.title = primary_group&.title
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-07 18:40:35 +08:00
|
|
|
private
|
|
|
|
|
2019-06-17 13:10:47 +08:00
|
|
|
def trigger_user_automatic_group_refresh
|
|
|
|
if !staged
|
|
|
|
Group.user_trust_level_change!(id, trust_level)
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-05-28 00:12:26 +08:00
|
|
|
def trigger_user_updated_event
|
|
|
|
DiscourseEvent.trigger(:user_updated, self)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-09-21 10:06:08 +08:00
|
|
|
def check_if_title_is_badged_granted
|
|
|
|
if title_changed? && !new_record? && user_profile
|
|
|
|
badge_granted_title = title.present? && badges.where(allow_title: true, name: title).exists?
|
|
|
|
user_profile.update_column(:badge_granted_title, badge_granted_title)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-24 05:24:50 +08:00
|
|
|
def previous_visit_at_update_required?(timestamp)
|
2014-01-17 11:38:08 +08:00
|
|
|
seen_before? && (last_seen_at < (timestamp - SiteSetting.previous_visit_timeout_hours.hours))
|
2013-10-24 05:24:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_previous_visit(timestamp)
|
|
|
|
update_visit_record!(timestamp.to_date)
|
|
|
|
if previous_visit_at_update_required?(timestamp)
|
|
|
|
update_column(:previous_visit_at, last_seen_at)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-16 15:36:27 +08:00
|
|
|
def trigger_user_created_event
|
2017-03-16 16:02:34 +08:00
|
|
|
DiscourseEvent.trigger(:user_created, self)
|
2017-03-16 15:36:27 +08:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-07-23 15:49:49 +08:00
|
|
|
def trigger_user_destroyed_event
|
|
|
|
DiscourseEvent.trigger(:user_destroyed, self)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-10-25 13:02:18 +08:00
|
|
|
def set_skip_validate_email
|
2017-08-09 10:56:08 +08:00
|
|
|
if self.primary_email
|
2017-10-25 13:02:18 +08:00
|
|
|
self.primary_email.skip_validate_email = !should_validate_email_address?
|
2017-08-09 10:56:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-12-15 05:52:37 +08:00
|
|
|
def check_site_contact_username
|
|
|
|
if (saved_change_to_admin? || saved_change_to_moderator?) &&
|
|
|
|
self.username == SiteSetting.site_contact_username && !staff?
|
|
|
|
SiteSetting.set_and_log(:site_contact_username, SiteSetting.defaults[:site_contact_username])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-31 12:46:22 +08:00
|
|
|
def self.ensure_consistency!
|
|
|
|
DB.exec <<~SQL
|
|
|
|
UPDATE users
|
|
|
|
SET uploaded_avatar_id = NULL
|
|
|
|
WHERE uploaded_avatar_id IN (
|
|
|
|
SELECT u1.uploaded_avatar_id FROM users u1
|
|
|
|
LEFT JOIN uploads up
|
|
|
|
ON u1.uploaded_avatar_id = up.id
|
|
|
|
WHERE u1.uploaded_avatar_id IS NOT NULL AND
|
|
|
|
up.id IS NULL
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-05-24 10:48:32 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: users
|
|
|
|
#
|
2017-11-24 04:55:44 +08:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# username :string(60) not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2019-01-12 03:29:56 +08:00
|
|
|
# name :string
|
2017-11-24 04:55:44 +08:00
|
|
|
# seen_notification_id :integer default(0), not null
|
|
|
|
# last_posted_at :datetime
|
|
|
|
# password_hash :string(64)
|
|
|
|
# salt :string(32)
|
|
|
|
# active :boolean default(FALSE), not null
|
|
|
|
# username_lower :string(60) not null
|
|
|
|
# last_seen_at :datetime
|
|
|
|
# admin :boolean default(FALSE), not null
|
|
|
|
# last_emailed_at :datetime
|
|
|
|
# trust_level :integer not null
|
|
|
|
# approved :boolean default(FALSE), not null
|
|
|
|
# approved_by_id :integer
|
|
|
|
# approved_at :datetime
|
|
|
|
# previous_visit_at :datetime
|
|
|
|
# suspended_at :datetime
|
|
|
|
# suspended_till :datetime
|
|
|
|
# date_of_birth :date
|
|
|
|
# views :integer default(0), not null
|
|
|
|
# flag_level :integer default(0), not null
|
|
|
|
# ip_address :inet
|
|
|
|
# moderator :boolean default(FALSE)
|
2019-01-12 03:29:56 +08:00
|
|
|
# title :string
|
2017-11-24 04:55:44 +08:00
|
|
|
# uploaded_avatar_id :integer
|
2019-01-12 01:19:23 +08:00
|
|
|
# locale :string(10)
|
2019-01-12 03:29:56 +08:00
|
|
|
# primary_group_id :integer
|
2017-11-24 04:55:44 +08:00
|
|
|
# registration_ip_address :inet
|
|
|
|
# staged :boolean default(FALSE), not null
|
|
|
|
# first_seen_at :datetime
|
|
|
|
# silenced_till :datetime
|
2019-01-04 01:03:01 +08:00
|
|
|
# group_locked_trust_level :integer
|
2017-11-24 04:55:44 +08:00
|
|
|
# manual_locked_trust_level :integer
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2018-07-16 14:18:07 +08:00
|
|
|
# idx_users_admin (id) WHERE admin
|
|
|
|
# idx_users_moderator (id) WHERE moderator
|
2016-11-24 10:13:03 +08:00
|
|
|
# index_users_on_last_posted_at (last_posted_at)
|
|
|
|
# index_users_on_last_seen_at (last_seen_at)
|
|
|
|
# index_users_on_uploaded_avatar_id (uploaded_avatar_id)
|
|
|
|
# index_users_on_username (username) UNIQUE
|
|
|
|
# index_users_on_username_lower (username_lower) UNIQUE
|
2013-05-24 10:48:32 +08:00
|
|
|
#
|