2013-04-18 14:27:04 -04:00
|
|
|
require_dependency 'email'
|
2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'email_token'
|
|
|
|
require_dependency 'trust_level'
|
2013-03-06 07:12:16 -05:00
|
|
|
require_dependency 'pbkdf2'
|
2013-03-08 15:04:37 -05:00
|
|
|
require_dependency 'summarize'
|
2013-05-10 16:58:23 -04:00
|
|
|
require_dependency 'discourse'
|
2013-06-05 16:00:45 -04:00
|
|
|
require_dependency 'post_destroyer'
|
2013-06-06 10:40:10 -04:00
|
|
|
require_dependency 'user_name_suggester'
|
2013-06-23 00:32:46 -04:00
|
|
|
require_dependency 'pretty_text'
|
2013-11-22 13:18:45 -05:00
|
|
|
require_dependency 'url_helper'
|
2014-05-30 00:17:35 -04:00
|
|
|
require_dependency 'letter_avatar'
|
2014-06-16 20:46:30 -04:00
|
|
|
require_dependency 'promotion'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
class User < ActiveRecord::Base
|
2013-06-06 18:07:59 -04:00
|
|
|
include Roleable
|
2013-11-22 13:18:45 -05:00
|
|
|
include UrlHelper
|
2014-04-28 04:31:51 -04:00
|
|
|
include HasCustomFields
|
2013-06-06 18:07:59 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :posts
|
2013-09-03 17:19:29 -04:00
|
|
|
has_many :notifications, dependent: :destroy
|
|
|
|
has_many :topic_users, dependent: :destroy
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :topics
|
2013-04-11 16:04:20 -04:00
|
|
|
has_many :user_open_ids, dependent: :destroy
|
2013-09-03 17:19:29 -04:00
|
|
|
has_many :user_actions, dependent: :destroy
|
|
|
|
has_many :post_actions, dependent: :destroy
|
2014-07-14 03:40:01 -04:00
|
|
|
has_many :user_badges, -> {where('user_badges.badge_id IN (SELECT id FROM badges where enabled)')}, dependent: :destroy
|
2014-07-09 01:31:49 -04:00
|
|
|
has_many :badges, through: :user_badges
|
2013-09-03 17:19:29 -04:00
|
|
|
has_many :email_logs, dependent: :destroy
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :post_timings
|
2013-09-03 17:19:29 -04:00
|
|
|
has_many :topic_allowed_users, dependent: :destroy
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :topics_allowed, through: :topic_allowed_users, source: :topic
|
2013-09-03 17:19:29 -04:00
|
|
|
has_many :email_tokens, dependent: :destroy
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :views
|
2013-09-03 17:19:29 -04:00
|
|
|
has_many :user_visits, dependent: :destroy
|
|
|
|
has_many :invites, dependent: :destroy
|
|
|
|
has_many :topic_links, dependent: :destroy
|
2013-11-22 12:29:07 -05:00
|
|
|
has_many :uploads
|
2014-09-08 11:11:56 -04:00
|
|
|
has_many :warnings
|
2013-05-10 16:58:23 -04:00
|
|
|
|
2014-05-22 03:37:02 -04:00
|
|
|
has_one :user_avatar, dependent: :destroy
|
2013-06-24 10:03:51 -04:00
|
|
|
has_one :facebook_user_info, dependent: :destroy
|
2013-04-11 16:04:20 -04:00
|
|
|
has_one :twitter_user_info, dependent: :destroy
|
|
|
|
has_one :github_user_info, dependent: :destroy
|
2014-09-25 01:50:54 -04:00
|
|
|
has_one :google_user_info, dependent: :destroy
|
2013-08-18 00:43:59 -04:00
|
|
|
has_one :oauth2_user_info, dependent: :destroy
|
2013-09-11 14:50:26 -04:00
|
|
|
has_one :user_stat, dependent: :destroy
|
2014-06-10 01:19:08 -04:00
|
|
|
has_one :user_profile, dependent: :destroy, inverse_of: :user
|
2014-02-24 22:30:49 -05:00
|
|
|
has_one :single_sign_on_record, dependent: :destroy
|
2013-02-05 14:16:51 -05:00
|
|
|
belongs_to :approved_by, class_name: 'User'
|
2014-04-23 22:42:04 -04:00
|
|
|
belongs_to :primary_group, class_name: 'Group'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-09-03 17:19:29 -04:00
|
|
|
has_many :group_users, dependent: :destroy
|
2013-04-29 02:33:24 -04:00
|
|
|
has_many :groups, through: :group_users
|
|
|
|
has_many :secure_categories, through: :groups, source: :categories
|
|
|
|
|
2013-09-03 17:19:29 -04:00
|
|
|
has_one :user_search_data, dependent: :destroy
|
2013-10-22 15:53:08 -04:00
|
|
|
has_one :api_key, dependent: :destroy
|
2013-05-22 15:33:33 -04:00
|
|
|
|
2014-05-22 03:37:02 -04:00
|
|
|
belongs_to :uploaded_avatar, class_name: 'Upload'
|
2013-08-13 16:08:29 -04:00
|
|
|
|
2013-11-15 10:27:43 -05:00
|
|
|
delegate :last_sent_email_address, :to => :email_logs
|
|
|
|
|
2014-07-14 10:16:24 -04:00
|
|
|
before_validation :downcase_email
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
validates_presence_of :username
|
|
|
|
validate :username_validator
|
2013-07-25 13:01:27 -04:00
|
|
|
validates :email, presence: true, uniqueness: true
|
|
|
|
validates :email, email: true, if: :email_changed?
|
2013-02-05 14:16:51 -05:00
|
|
|
validate :password_validator
|
2013-10-21 14:49:51 -04:00
|
|
|
validates :ip_address, allowed_ip_address: {on: :create, message: :signup_not_allowed}
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
after_initialize :add_trust_level
|
2013-08-23 17:35:01 -04:00
|
|
|
after_initialize :set_default_email_digest
|
2014-01-02 15:27:26 -05:00
|
|
|
after_initialize :set_default_external_links_in_new_tab
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-05 21:44:49 -05:00
|
|
|
after_create :create_email_token
|
2013-09-11 14:50:26 -04:00
|
|
|
after_create :create_user_stat
|
2014-05-27 13:54:04 -04:00
|
|
|
after_create :create_user_profile
|
2014-06-16 20:46:30 -04:00
|
|
|
after_create :ensure_in_trust_level_group
|
2014-08-13 16:17:16 -04:00
|
|
|
|
|
|
|
before_save :update_username_lower
|
|
|
|
before_save :ensure_password_is_hashed
|
|
|
|
|
|
|
|
after_save :update_tracked_topics
|
|
|
|
after_save :clear_global_notice_if_needed
|
2014-05-22 03:37:02 -04:00
|
|
|
after_save :refresh_avatar
|
2014-07-22 21:42:24 -04:00
|
|
|
after_save :badge_grant
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-09-03 17:19:29 -04:00
|
|
|
before_destroy do
|
|
|
|
# These tables don't have primary keys, so destroying them with activerecord is tricky:
|
|
|
|
PostTiming.delete_all(user_id: self.id)
|
2014-08-04 05:07:55 -04:00
|
|
|
TopicViewItem.delete_all(user_id: self.id)
|
2013-09-03 17:19:29 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05: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-13 16:17:16 -04:00
|
|
|
# set to true to optimize creation and save for imports
|
|
|
|
attr_accessor :import_mode
|
|
|
|
|
2014-09-03 17:50:19 -04:00
|
|
|
# excluding fake users like the system user
|
2013-09-06 00:07:23 -04:00
|
|
|
scope :real, -> { where('id > 0') }
|
2013-03-29 02:29:58 -04:00
|
|
|
|
2014-09-03 17:50:19 -04:00
|
|
|
# TODO-PERF: There is no indexes on any of these
|
|
|
|
# and NotifyMailingListSubscribers does a select-all-and-loop
|
|
|
|
# may want to create an index on (active, blocked, suspended_till, mailing_list_mode)?
|
|
|
|
scope :blocked, -> { where(blocked: true) }
|
|
|
|
scope :not_blocked, -> { where(blocked: false) }
|
|
|
|
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) }
|
|
|
|
|
2013-02-14 01:32:58 -05:00
|
|
|
module NewTopicDuration
|
2013-02-25 11:42:20 -05:00
|
|
|
ALWAYS = -1
|
2013-02-14 01:32:58 -05:00
|
|
|
LAST_VISIT = -2
|
|
|
|
end
|
2014-03-07 12:58:53 -05:00
|
|
|
|
2014-09-11 15:22:11 -04:00
|
|
|
def self.max_password_length
|
|
|
|
200
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.username_length
|
2014-07-16 12:25:24 -04:00
|
|
|
SiteSetting.min_username_length.to_i..SiteSetting.max_username_length.to_i
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-02-06 17:34:48 -05:00
|
|
|
def custom_groups
|
2014-04-22 16:43:46 -04:00
|
|
|
groups.where(automatic: false, visible: true)
|
2014-02-06 17:34:48 -05:00
|
|
|
end
|
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def self.username_available?(username)
|
|
|
|
lower = username.downcase
|
|
|
|
User.where(username_lower: lower).blank?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-31 12:51:13 -04:00
|
|
|
EMAIL = %r{([^@]+)@([^\.]+)}
|
|
|
|
|
2013-04-12 18:46:55 -04: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
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.suggest_name(email)
|
|
|
|
return "" unless email
|
|
|
|
name = email.split(/[@\+]/)[0]
|
2013-02-28 08:08:56 -05:00
|
|
|
name = name.gsub(".", " ")
|
|
|
|
name.titleize
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
# Find a user by temporary key, nil if not found or key is invalid
|
|
|
|
def self.find_by_temporary_key(key)
|
|
|
|
user_id = $redis.get("temporary_key:#{key}")
|
|
|
|
if user_id.present?
|
2014-05-06 09:41:59 -04:00
|
|
|
find_by(id: user_id.to_i)
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_by_username_or_email(username_or_email)
|
2013-10-28 01:29:07 -04:00
|
|
|
if username_or_email.include?('@')
|
|
|
|
find_by_email(username_or_email)
|
2013-06-18 20:31:19 -04:00
|
|
|
else
|
2013-10-28 01:29:07 -04:00
|
|
|
find_by_username(username_or_email)
|
2013-06-18 20:31:19 -04:00
|
|
|
end
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
|
|
|
|
2013-10-24 03:59:58 -04:00
|
|
|
def self.find_by_email(email)
|
2014-05-06 09:41:59 -04:00
|
|
|
find_by(email: Email.downcase(email))
|
2013-10-24 03:59:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_by_username(username)
|
2014-05-06 09:41:59 -04:00
|
|
|
find_by(username_lower: username.downcase)
|
2013-10-24 03:59:58 -04:00
|
|
|
end
|
|
|
|
|
2013-12-19 13:45:55 -05:00
|
|
|
|
2013-04-29 02:33:24 -04: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
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def change_username(new_username)
|
2013-06-28 16:21:46 -04:00
|
|
|
self.username = new_username
|
2013-02-28 08:08:56 -05:00
|
|
|
save
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Use a temporary key to find this user, store it in redis with an expiry
|
2013-02-05 21:44:49 -05:00
|
|
|
def temporary_key
|
2013-02-05 14:16:51 -05:00
|
|
|
key = SecureRandom.hex(32)
|
2014-07-15 17:19:45 -04:00
|
|
|
$redis.setex "temporary_key:#{key}", 2.months, id.to_s
|
2013-02-05 14:16:51 -05:00
|
|
|
key
|
|
|
|
end
|
|
|
|
|
2013-09-12 17:46:43 -04:00
|
|
|
def created_topic_count
|
2014-07-28 13:17:37 -04:00
|
|
|
stat = user_stat || create_user_stat
|
|
|
|
stat.topic_count
|
2013-09-12 17:46:43 -04:00
|
|
|
end
|
2013-02-26 11:27:59 -05:00
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
alias_method :topic_count, :created_topic_count
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# tricky, we need our bus to be subscribed from the right spot
|
|
|
|
def sync_notification_channel_position
|
|
|
|
@unread_notifications_by_type = nil
|
2013-02-24 17:31:38 -05:00
|
|
|
self.notification_channel_position = MessageBus.last_id("/notification/#{id}")
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def invited_by
|
|
|
|
used_invite = invites.where("redeemed_at is not null").includes(:invited_by).first
|
2013-02-28 08:08:56 -05:00
|
|
|
used_invite.try(:invited_by)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Approve this user
|
2013-06-26 13:24:30 -04:00
|
|
|
def approve(approved_by, send_mail=true)
|
2013-02-05 14:16:51 -05:00
|
|
|
self.approved = true
|
2013-07-10 21:21:39 -04:00
|
|
|
|
2014-03-26 15:20:41 -04:00
|
|
|
if approved_by.is_a?(Fixnum)
|
2013-07-10 21:21:39 -04:00
|
|
|
self.approved_by_id = approved_by
|
|
|
|
else
|
|
|
|
self.approved_by = approved_by
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
self.approved_at = Time.now
|
2013-06-05 23:16:31 -04:00
|
|
|
|
2013-06-26 13:24:30 -04:00
|
|
|
send_approval_email if save and send_mail
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.email_hash(email)
|
2013-02-05 21:44:49 -05:00
|
|
|
Digest::MD5.hexdigest(email.strip.downcase)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def email_hash
|
2013-02-28 08:08:56 -05:00
|
|
|
User.email_hash(email)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def unread_notifications_by_type
|
|
|
|
@unread_notifications_by_type ||= notifications.where("id > ? and read = false", seen_notification_id).group(:notification_type).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@unread_notifications_by_type = nil
|
2013-05-16 02:37:47 -04:00
|
|
|
@unread_pms = nil
|
2013-02-05 14:16:51 -05:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread_private_messages
|
2013-05-16 02:37:47 -04:00
|
|
|
@unread_pms ||= notifications.where("read = false AND notification_type = ?", Notification.types[:private_message]).count
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def unread_notifications
|
2013-03-01 07:07:44 -05:00
|
|
|
unread_notifications_by_type.except(Notification.types[:private_message]).values.sum
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-05 21:44:49 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def saw_notification_id(notification_id)
|
2014-10-02 02:03:56 -04:00
|
|
|
User.where("id = ? and seen_notification_id < ?", id, notification_id)
|
2013-08-09 12:12:56 -04:00
|
|
|
.update_all ["seen_notification_id = ?", notification_id]
|
2014-10-02 02:03:56 -04:00
|
|
|
|
|
|
|
# mark all badge notifications read
|
|
|
|
Notification.where('user_id = ? AND NOT read AND notification_type = ?', id, Notification.types[:granted_badge])
|
|
|
|
.update_all ["read = ?", true]
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def publish_notifications_state
|
2013-02-28 08:08:56 -05:00
|
|
|
MessageBus.publish("/notification/#{id}",
|
2013-06-06 10:40:10 -04:00
|
|
|
{unread_notifications: unread_notifications,
|
|
|
|
unread_private_messages: unread_private_messages},
|
|
|
|
user_ids: [id] # only publish the notification to this user
|
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# A selection of people to autocomplete on @mention
|
|
|
|
def self.mentionable_usernames
|
2013-02-05 21:44:49 -05:00
|
|
|
User.select(:username).order('last_posted_at desc').limit(20)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def password=(password)
|
2013-02-05 21:44:49 -05:00
|
|
|
# special case for passwordless accounts
|
2013-02-28 08:08:56 -05:00
|
|
|
@raw_password = password unless password.blank?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-12-19 15:12:03 -05:00
|
|
|
def password
|
|
|
|
'' # so that validator doesn't complain that a password attribute doesn't exist
|
|
|
|
end
|
|
|
|
|
2013-02-12 15:42:04 -05:00
|
|
|
# Indicate that this is NOT a passwordless account for the purposes of validation
|
2013-02-28 08:08:56 -05:00
|
|
|
def password_required!
|
2013-02-12 15:42:04 -05:00
|
|
|
@password_required = true
|
|
|
|
end
|
|
|
|
|
2013-12-19 15:12:03 -05:00
|
|
|
def password_required?
|
|
|
|
!!@password_required
|
|
|
|
end
|
|
|
|
|
2014-01-21 12:42:20 -05:00
|
|
|
def has_password?
|
|
|
|
password_hash.present?
|
|
|
|
end
|
|
|
|
|
2013-12-19 15:12:03 -05:00
|
|
|
def password_validator
|
|
|
|
PasswordValidator.new(attributes: :password).validate_each(self, :password, @raw_password)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def confirm_password?(password)
|
2013-02-28 08:08:56 -05:00
|
|
|
return false unless password_hash && salt
|
|
|
|
self.password_hash == hash_password(password, salt)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-10-11 13:33:23 -04:00
|
|
|
|
|
|
|
def new_user?
|
2014-09-05 01:20:39 -04:00
|
|
|
created_at >= 24.hours.ago || trust_level == TrustLevel[0]
|
2013-10-11 13:33:23 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-12 00:41:04 -05:00
|
|
|
def seen_before?
|
|
|
|
last_seen_at.present?
|
|
|
|
end
|
|
|
|
|
2014-01-24 15:19:20 -05:00
|
|
|
def visit_record_for(date)
|
2014-05-06 09:41:59 -04:00
|
|
|
user_visits.find_by(visited_at: date)
|
2013-02-12 00:41:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_visit_record!(date)
|
2014-01-24 15:19:20 -05:00
|
|
|
create_visit_record!(date) unless visit_record_for(date)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_posts_read!(num_posts, now=Time.zone.now)
|
|
|
|
if user_visit = visit_record_for(now.to_date)
|
|
|
|
user_visit.posts_read += num_posts
|
|
|
|
user_visit.save
|
|
|
|
user_visit
|
|
|
|
else
|
|
|
|
create_visit_record!(now.to_date, num_posts)
|
2013-02-12 00:41:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-24 05:42:04 -05:00
|
|
|
def update_ip_address!(new_ip_address)
|
2013-03-08 20:24:10 -05:00
|
|
|
unless ip_address == new_ip_address || new_ip_address.blank?
|
2013-02-24 06:56:08 -05:00
|
|
|
update_column(:ip_address, new_ip_address)
|
2013-02-24 05:42:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-23 17:24:50 -04:00
|
|
|
def update_last_seen!(now=Time.zone.now)
|
|
|
|
now_date = now.to_date
|
2013-02-05 14:16:51 -05:00
|
|
|
# Only update last seen once every minute
|
2013-10-23 17:24:50 -04:00
|
|
|
redis_key = "user:#{id}:#{now_date}"
|
|
|
|
return unless $redis.setnx(redis_key, "1")
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-10-23 17:24:50 -04: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)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-08-13 16:08:29 -04:00
|
|
|
def self.gravatar_template(email)
|
2013-02-05 14:16:51 -05:00
|
|
|
email_hash = self.email_hash(email)
|
2014-04-25 05:40:38 -04:00
|
|
|
"//www.gravatar.com/avatar/#{email_hash}.png?s={size}&r=pg&d=identicon"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-08 15:58:37 -05:00
|
|
|
# Don't pass this up to the client - it's meant for server side use
|
2013-08-13 16:08:29 -04:00
|
|
|
# This is used in
|
|
|
|
# - self oneboxes in open graph data
|
|
|
|
# - emails
|
2013-03-08 15:58:37 -05:00
|
|
|
def small_avatar_url
|
2014-05-22 03:37:02 -04:00
|
|
|
avatar_template_url.gsub("{size}", "45")
|
2013-03-08 15:58:37 -05:00
|
|
|
end
|
|
|
|
|
2014-05-22 03:37:02 -04:00
|
|
|
def avatar_template_url
|
2014-01-07 11:45:06 -05:00
|
|
|
schemaless absolute avatar_template
|
2013-09-10 15:18:22 -04:00
|
|
|
end
|
|
|
|
|
2014-05-22 03:37:02 -04:00
|
|
|
def self.avatar_template(username,uploaded_avatar_id)
|
2014-05-30 00:17:35 -04:00
|
|
|
return letter_avatar_template(username) if !uploaded_avatar_id
|
|
|
|
id = uploaded_avatar_id
|
2014-05-22 03:37:02 -04:00
|
|
|
username ||= ""
|
2014-05-27 11:08:45 -04:00
|
|
|
"/user_avatar/#{RailsMultisite::ConnectionManagement.current_hostname}/#{username.downcase}/{size}/#{id}.png"
|
2014-05-22 03:37:02 -04:00
|
|
|
end
|
|
|
|
|
2014-05-30 00:17:35 -04:00
|
|
|
def self.letter_avatar_template(username)
|
|
|
|
"/letter_avatar/#{username.downcase}/{size}/#{LetterAvatar::VERSION}.png"
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def avatar_template
|
2014-05-22 03:37:02 -04:00
|
|
|
self.class.avatar_template(username,uploaded_avatar_id)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# The following count methods are somewhat slow - definitely don't use them in a loop.
|
2013-03-06 02:52:24 -05:00
|
|
|
# They might need to be denormalized
|
2013-02-05 14:16:51 -05:00
|
|
|
def like_count
|
2013-02-28 08:08:56 -05:00
|
|
|
UserAction.where(user_id: id, action_type: UserAction::WAS_LIKED).count
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-08-22 15:23:10 -04:00
|
|
|
def like_given_count
|
|
|
|
UserAction.where(user_id: id, action_type: UserAction::LIKE).count
|
2014-08-22 14:37:00 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def post_count
|
2014-07-28 13:17:37 -04:00
|
|
|
stat = user_stat || create_user_stat
|
|
|
|
stat.post_count
|
2014-02-20 12:29:40 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def flags_given_count
|
2013-03-01 07:07:44 -05:00
|
|
|
PostAction.where(user_id: id, post_action_type_id: PostActionType.flag_types.values).count
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-09-08 11:11:56 -04:00
|
|
|
def warnings_received_count
|
|
|
|
warnings.count
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def flags_received_count
|
2013-03-01 07:07:44 -05:00
|
|
|
posts.includes(:post_actions).where('post_actions.post_action_type_id' => PostActionType.flag_types.values).count
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def private_topics_count
|
|
|
|
topics_allowed.where(archetype: Archetype.private_message).count
|
|
|
|
end
|
|
|
|
|
2013-12-19 13:45:55 -05:00
|
|
|
def posted_too_much_in_topic?(topic_id)
|
2014-01-02 12:57:40 -05:00
|
|
|
|
2014-04-29 12:59:14 -04:00
|
|
|
# Does not apply to staff, non-new members or your own topics
|
|
|
|
return false if staff? ||
|
2014-09-05 01:20:39 -04:00
|
|
|
(trust_level != TrustLevel[0]) ||
|
2014-04-29 12:59:14 -04:00
|
|
|
Topic.where(id: topic_id, user_id: id).exists?
|
2014-01-02 12:57:40 -05:00
|
|
|
|
2014-04-29 12:59:14 -04: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-19 13:45:55 -05:00
|
|
|
end
|
|
|
|
|
2013-02-07 02:11:56 -05:00
|
|
|
def delete_all_posts!(guardian)
|
|
|
|
raise Discourse::InvalidAccess unless guardian.can_delete_all_posts? self
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-07 02:11:56 -05:00
|
|
|
posts.order("post_number desc").each do |p|
|
2013-06-05 16:00:45 -04:00
|
|
|
PostDestroyer.new(guardian.user, p).destroy
|
2013-02-07 02:11:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-07 13:53:32 -05:00
|
|
|
def suspended?
|
|
|
|
suspended_till && suspended_till > DateTime.now
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-07 13:53:32 -05:00
|
|
|
def suspend_record
|
|
|
|
UserHistory.for(self, :suspend_user).order('id DESC').first
|
2013-11-01 10:47:03 -04:00
|
|
|
end
|
|
|
|
|
2013-11-07 13:53:32 -05:00
|
|
|
def suspend_reason
|
|
|
|
suspend_record.try(:details) if suspended?
|
2013-11-01 10:47:03 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Use this helper to determine if the user has a particular trust level.
|
|
|
|
# Takes into account admin, etc.
|
2013-02-05 21:44:49 -05:00
|
|
|
def has_trust_level?(level)
|
2014-09-05 01:20:39 -04:00
|
|
|
raise "Invalid trust level #{level}" unless TrustLevel.valid?(level)
|
2013-03-20 00:05:19 -04:00
|
|
|
admin? || moderator? || TrustLevel.compare(trust_level, level)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-19 19:51:39 -04:00
|
|
|
# a touch faster than automatic
|
2013-03-31 12:51:13 -04:00
|
|
|
def admin?
|
2013-03-19 19:51:39 -04:00
|
|
|
admin
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def guardian
|
|
|
|
Guardian.new(self)
|
|
|
|
end
|
|
|
|
|
2013-02-07 18:23:41 -05:00
|
|
|
def username_format_validator
|
2013-07-07 07:05:18 -04:00
|
|
|
UsernameValidator.perform_validation(self, 'username')
|
2013-02-07 18:23:41 -05:00
|
|
|
end
|
|
|
|
|
2013-02-11 11:18:26 -05:00
|
|
|
def email_confirmed?
|
2013-02-28 08:08:56 -05:00
|
|
|
email_tokens.where(email: email, confirmed: true).present? || email_tokens.empty?
|
2013-02-11 11:18:26 -05:00
|
|
|
end
|
|
|
|
|
2013-05-07 21:58:34 -04:00
|
|
|
def activate
|
|
|
|
email_token = self.email_tokens.active.first
|
|
|
|
if email_token
|
|
|
|
EmailToken.confirm(email_token.token)
|
|
|
|
else
|
|
|
|
self.active = true
|
|
|
|
save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def deactivate
|
|
|
|
self.active = false
|
|
|
|
save
|
|
|
|
end
|
|
|
|
|
2014-06-16 20:46:30 -04:00
|
|
|
def change_trust_level!(level, opts=nil)
|
|
|
|
Promotion.new(self).change_trust_level!(level, opts)
|
|
|
|
end
|
|
|
|
|
2013-02-14 01:32:58 -05:00
|
|
|
def treat_as_new_topic_start_date
|
2013-02-25 11:42:20 -05:00
|
|
|
duration = new_topic_duration_minutes || SiteSetting.new_topic_duration_minutes
|
2014-03-03 16:11:59 -05:00
|
|
|
[case duration
|
2013-06-06 10:40:10 -04:00
|
|
|
when User::NewTopicDuration::ALWAYS
|
2014-03-03 16:11:59 -05:00
|
|
|
created_at
|
2013-06-06 10:40:10 -04:00
|
|
|
when User::NewTopicDuration::LAST_VISIT
|
2014-03-03 14:31:29 -05:00
|
|
|
previous_visit_at || user_stat.new_since
|
2013-06-06 10:40:10 -04:00
|
|
|
else
|
|
|
|
duration.minutes.ago
|
2014-03-03 16:11:59 -05:00
|
|
|
end, user_stat.new_since].max
|
2013-02-14 01:32:58 -05:00
|
|
|
end
|
2013-02-07 18:23:41 -05:00
|
|
|
|
2013-02-21 13:20:00 -05:00
|
|
|
def readable_name
|
2013-03-06 15:17:07 -05:00
|
|
|
return "#{name} (#{username})" if name.present? && name != username
|
|
|
|
username
|
2013-02-21 13:20:00 -05:00
|
|
|
end
|
|
|
|
|
2014-04-16 06:22:21 -04:00
|
|
|
def badge_count
|
2014-07-16 02:21:46 -04:00
|
|
|
user_badges.select('distinct badge_id').count
|
2014-04-16 06:22:21 -04:00
|
|
|
end
|
|
|
|
|
2014-04-16 06:11:11 -04:00
|
|
|
def featured_user_badges
|
2014-07-03 03:29:44 -04:00
|
|
|
user_badges
|
|
|
|
.joins(:badge)
|
2014-10-02 01:55:33 -04:00
|
|
|
.order("CASE WHEN badges.id = (SELECT MAX(ub2.badge_id) FROM user_badges ub2
|
|
|
|
WHERE ub2.badge_id IN (#{Badge.trust_level_badge_ids.join(",")}) AND
|
|
|
|
ub2.user_id = #{self.id}) THEN 1 ELSE 0 END DESC")
|
2014-07-03 03:29:44 -04:00
|
|
|
.order('badges.badge_type_id ASC, badges.grant_count ASC')
|
|
|
|
.includes(:user, :granted_by, badge: :badge_type)
|
|
|
|
.where("user_badges.id in (select min(u2.id)
|
|
|
|
from user_badges u2 where u2.user_id = ? group by u2.badge_id)", id)
|
|
|
|
.limit(3)
|
2014-04-16 06:11:11 -04:00
|
|
|
end
|
|
|
|
|
2013-04-03 13:25:52 -04:00
|
|
|
def self.count_by_signup_date(sinceDaysAgo=30)
|
|
|
|
where('created_at > ?', sinceDaysAgo.days.ago).group('date(created_at)').order('date(created_at)').count
|
2013-03-07 11:07:59 -05:00
|
|
|
end
|
|
|
|
|
2013-04-05 01:48:38 -04:00
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
def secure_category_ids
|
2014-02-06 22:11:52 -05:00
|
|
|
cats = self.admin? ? Category.where(read_restricted: true) : secure_categories.references(:categories)
|
2013-09-10 00:29:02 -04:00
|
|
|
cats.pluck('categories.id').sort
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
|
|
|
|
2013-07-13 21:24:16 -04:00
|
|
|
def topic_create_allowed_category_ids
|
|
|
|
Category.topic_create_allowed(self.id).select(:id)
|
|
|
|
end
|
|
|
|
|
2013-09-12 17:46:43 -04:00
|
|
|
|
2013-05-10 16:58:23 -04:00
|
|
|
# Flag all posts from a user as spam
|
|
|
|
def flag_linked_posts_as_spam
|
|
|
|
admin = Discourse.system_user
|
|
|
|
topic_links.includes(:post).each do |tl|
|
|
|
|
begin
|
2013-10-17 15:08:11 -04:00
|
|
|
PostAction.act(admin, tl.post, PostActionType.types[:spam], message: I18n.t('flag_reason.spam_hosts'))
|
2013-05-10 16:58:23 -04:00
|
|
|
rescue PostAction::AlreadyActed
|
|
|
|
# If the user has already acted, just ignore it
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-05-13 04:04:03 -04:00
|
|
|
|
2013-08-13 16:08:29 -04:00
|
|
|
def has_uploaded_avatar
|
|
|
|
uploaded_avatar.present?
|
|
|
|
end
|
2013-05-24 06:58:26 -04:00
|
|
|
|
2013-10-16 05:28:18 -04:00
|
|
|
def added_a_day_ago?
|
|
|
|
created_at > 1.day.ago
|
|
|
|
end
|
|
|
|
|
2013-10-22 15:53:08 -04:00
|
|
|
def generate_api_key(created_by)
|
|
|
|
if api_key.present?
|
|
|
|
api_key.regenerate!(created_by)
|
|
|
|
api_key
|
|
|
|
else
|
|
|
|
ApiKey.create(user: self, key: SecureRandom.hex(32), created_by: created_by)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def revoke_api_key
|
|
|
|
ApiKey.where(user_id: self.id).delete_all
|
|
|
|
end
|
|
|
|
|
2013-11-15 10:27:43 -05:00
|
|
|
def find_email
|
|
|
|
last_sent_email_address || email
|
|
|
|
end
|
|
|
|
|
2014-09-24 20:19:26 -04:00
|
|
|
def tl3_requirements
|
2014-09-05 01:20:39 -04:00
|
|
|
@lq ||= TrustLevel3Requirements.new(self)
|
2014-01-22 17:09:56 -05:00
|
|
|
end
|
|
|
|
|
2014-09-24 20:19:26 -04:00
|
|
|
def on_tl3_grace_period?
|
2014-09-13 16:55:26 -04: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-03-07 12:58:53 -05:00
|
|
|
def should_be_redirected_to_top
|
|
|
|
redirected_to_top_reason.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirected_to_top_reason
|
2014-03-31 15:53:38 -04:00
|
|
|
# redirect is enabled
|
|
|
|
return unless SiteSetting.redirect_users_to_top_page
|
2014-03-07 12:58:53 -05:00
|
|
|
# top must be in the top_menu
|
|
|
|
return unless SiteSetting.top_menu =~ /top/i
|
|
|
|
# there should be enough topics
|
|
|
|
return unless SiteSetting.has_enough_topics_to_redirect_to_top
|
2014-05-05 13:00:40 -04:00
|
|
|
|
|
|
|
if !seen_before? || (trust_level == 0 && !redirected_to_top_yet?)
|
|
|
|
update_last_redirected_to_top!
|
|
|
|
return I18n.t('redirected_to_top_reasons.new_user')
|
|
|
|
elsif last_seen_at < 1.month.ago
|
|
|
|
update_last_redirected_to_top!
|
|
|
|
return I18n.t('redirected_to_top_reasons.not_seen_in_a_month')
|
|
|
|
end
|
|
|
|
|
|
|
|
# no reason
|
2014-03-07 12:58:53 -05:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2014-05-05 13:00:40 -04:00
|
|
|
def redirected_to_top_yet?
|
|
|
|
last_redirected_to_top_at.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_last_redirected_to_top!
|
|
|
|
key = "user:#{id}:update_last_redirected_to_top"
|
|
|
|
delay = SiteSetting.active_user_rate_limit_secs
|
|
|
|
|
|
|
|
# only update last_redirected_to_top_at once every minute
|
|
|
|
return unless $redis.setnx(key, "1")
|
|
|
|
$redis.expire(key, delay)
|
|
|
|
|
|
|
|
# delay the update
|
|
|
|
Jobs.enqueue_in(delay / 2, :update_top_redirection, user_id: self.id, redirected_at: Time.zone.now)
|
|
|
|
end
|
|
|
|
|
2014-05-22 03:37:02 -04:00
|
|
|
def refresh_avatar
|
2014-08-13 16:17:16 -04:00
|
|
|
return if @import_mode
|
|
|
|
|
2014-05-28 02:54:21 -04:00
|
|
|
avatar = user_avatar || create_user_avatar
|
|
|
|
gravatar_downloaded = false
|
2014-05-22 03:37:02 -04:00
|
|
|
|
2014-05-28 02:54:21 -04:00
|
|
|
if SiteSetting.automatically_download_gravatars? && !avatar.last_gravatar_download_attempt
|
|
|
|
avatar.update_gravatar!
|
|
|
|
gravatar_downloaded = avatar.gravatar_upload_id
|
2014-05-22 03:37:02 -04:00
|
|
|
end
|
|
|
|
|
2014-06-26 13:50:16 -04:00
|
|
|
if !self.uploaded_avatar_id && gravatar_downloaded
|
2014-05-30 00:17:35 -04:00
|
|
|
self.update_column(:uploaded_avatar_id, avatar.gravatar_upload_id)
|
2014-07-03 03:29:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-28 13:17:37 -04:00
|
|
|
def first_post_created_at
|
|
|
|
user_stat.try(:first_post_created_at)
|
|
|
|
end
|
|
|
|
|
2014-09-25 01:50:54 -04:00
|
|
|
def associated_accounts
|
|
|
|
result = []
|
|
|
|
|
2014-09-29 16:31:05 -04:00
|
|
|
result << "Twitter(#{twitter_user_info.screen_name})" if twitter_user_info
|
|
|
|
result << "Facebook(#{facebook_user_info.username})" if facebook_user_info
|
|
|
|
result << "Google(#{google_user_info.email})" if google_user_info
|
|
|
|
result << "Github(#{github_user_info.screen_name})" if github_user_info
|
2014-09-25 01:50:54 -04:00
|
|
|
|
|
|
|
user_open_ids.each do |oid|
|
|
|
|
result << "OpenID #{oid.url[0..20]}...(#{oid.email})"
|
|
|
|
end
|
|
|
|
|
|
|
|
result.empty? ? I18n.t("user.no_accounts_associated") : result.join(", ")
|
|
|
|
end
|
|
|
|
|
2014-09-26 14:48:34 -04: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|
|
|
|
|
@user_fields[fid.to_s] = custom_fields["user_field_#{fid}"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@user_fields
|
|
|
|
end
|
|
|
|
|
2014-10-07 19:26:18 -04:00
|
|
|
def title=(val)
|
|
|
|
write_attribute(:title, val)
|
|
|
|
if !new_record? && user_profile
|
|
|
|
user_profile.update_column(:badge_granted_title, false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
protected
|
|
|
|
|
2014-07-22 21:42:24 -04:00
|
|
|
def badge_grant
|
|
|
|
BadgeGranter.queue_badge_grant(Badge::Trigger::UserChange, user: self)
|
|
|
|
end
|
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def update_tracked_topics
|
|
|
|
return unless auto_track_topics_after_msecs_changed?
|
2013-10-23 17:24:50 -04:00
|
|
|
TrackedTopicsUpdater.new(id, auto_track_topics_after_msecs).call
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-03-24 03:03:39 -04:00
|
|
|
def clear_global_notice_if_needed
|
|
|
|
if admin && SiteSetting.has_login_hint
|
|
|
|
SiteSetting.has_login_hint = false
|
|
|
|
SiteSetting.global_notice = ""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-27 13:54:04 -04:00
|
|
|
def create_user_profile
|
|
|
|
UserProfile.create(user_id: id)
|
|
|
|
end
|
|
|
|
|
2014-06-16 20:46:30 -04:00
|
|
|
def ensure_in_trust_level_group
|
|
|
|
Group.user_trust_level_change!(id, trust_level)
|
|
|
|
end
|
|
|
|
|
2013-09-11 14:50:26 -04:00
|
|
|
def create_user_stat
|
2014-03-03 14:31:29 -05:00
|
|
|
stat = UserStat.new(new_since: Time.now)
|
2013-10-03 23:28:49 -04:00
|
|
|
stat.user_id = id
|
2013-09-11 14:50:26 -04:00
|
|
|
stat.save!
|
|
|
|
end
|
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def create_email_token
|
|
|
|
email_tokens.create(email: email)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-01-24 15:19:20 -05:00
|
|
|
def create_visit_record!(date, posts_read=0)
|
|
|
|
user_stat.update_column(:days_visited, user_stat.days_visited + 1)
|
|
|
|
user_visits.create!(visited_at: date, posts_read: posts_read)
|
|
|
|
end
|
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def ensure_password_is_hashed
|
|
|
|
if @raw_password
|
|
|
|
self.salt = SecureRandom.hex(16)
|
|
|
|
self.password_hash = hash_password(@raw_password, salt)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def hash_password(password, salt)
|
2014-09-11 15:22:11 -04:00
|
|
|
raise "password is too long" if password.size > User.max_password_length
|
2013-07-22 21:36:01 -04:00
|
|
|
Pbkdf2.hash_password(password, salt, Rails.configuration.pbkdf2_iterations, Rails.configuration.pbkdf2_algorithm)
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def add_trust_level
|
2013-12-21 02:19:22 -05:00
|
|
|
# there is a possibility we did not load trust level column, skip it
|
2013-06-06 10:40:10 -04:00
|
|
|
return unless has_attribute? :trust_level
|
|
|
|
self.trust_level ||= SiteSetting.default_trust_level
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def update_username_lower
|
|
|
|
self.username_lower = username.downcase
|
|
|
|
end
|
2013-02-05 21:44:49 -05:00
|
|
|
|
2014-07-14 10:16:24 -04:00
|
|
|
def downcase_email
|
|
|
|
self.email = self.email.downcase if self.email
|
|
|
|
end
|
|
|
|
|
2013-06-06 10:40:10 -04:00
|
|
|
def username_validator
|
|
|
|
username_format_validator || begin
|
|
|
|
lower = username.downcase
|
2014-05-06 09:41:59 -04:00
|
|
|
existing = User.find_by(username_lower: lower)
|
2013-06-28 16:21:46 -04:00
|
|
|
if username_changed? && existing && existing.id != self.id
|
2013-06-06 10:40:10 -04:00
|
|
|
errors.add(:username, I18n.t(:'user.username.unique'))
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-07 18:23:41 -05:00
|
|
|
end
|
2013-06-06 10:40:10 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-08-13 16:08:29 -04:00
|
|
|
def send_approval_email
|
|
|
|
Jobs.enqueue(:user_email,
|
|
|
|
type: :signup_after_approval,
|
|
|
|
user_id: id,
|
|
|
|
email_token: email_tokens.first.token
|
|
|
|
)
|
|
|
|
end
|
2013-07-07 06:40:35 -04:00
|
|
|
|
2013-08-23 17:35:01 -04:00
|
|
|
def set_default_email_digest
|
|
|
|
if has_attribute?(:email_digests) && self.email_digests.nil?
|
|
|
|
if SiteSetting.default_digest_email_frequency.blank?
|
|
|
|
self.email_digests = false
|
|
|
|
else
|
|
|
|
self.email_digests = true
|
|
|
|
self.digest_after_days ||= SiteSetting.default_digest_email_frequency.to_i if has_attribute?(:digest_after_days)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-02 15:27:26 -05:00
|
|
|
def set_default_external_links_in_new_tab
|
|
|
|
if has_attribute?(:external_links_in_new_tab) && self.external_links_in_new_tab.nil?
|
|
|
|
self.external_links_in_new_tab = !SiteSetting.default_external_links_in_new_tab.blank?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-13 14:13:41 -04:00
|
|
|
# Delete inactive accounts that are over a week old
|
|
|
|
def self.purge_inactive
|
|
|
|
|
|
|
|
# You might be wondering why this query matches on post_count = 0. The reason
|
|
|
|
# is a long time ago we had a bug where users could post before being activated
|
|
|
|
# and some sites still have those records which can't be purged.
|
|
|
|
to_destroy = User.where(active: false)
|
|
|
|
.joins('INNER JOIN user_stats AS us ON us.user_id = users.id')
|
2014-08-13 15:11:19 -04:00
|
|
|
.where("created_at < ?", SiteSetting.purge_inactive_users_grace_period_days.days.ago)
|
2014-08-13 14:13:41 -04:00
|
|
|
.where('us.post_count = 0')
|
2014-08-19 13:46:40 -04:00
|
|
|
.where('NOT admin AND NOT moderator')
|
2014-08-13 17:56:21 -04:00
|
|
|
.limit(100)
|
2014-08-13 14:13:41 -04:00
|
|
|
|
|
|
|
destroyer = UserDestroyer.new(Discourse.system_user)
|
|
|
|
to_destroy.each do |u|
|
2014-08-19 13:46:40 -04:00
|
|
|
begin
|
2014-08-27 16:04:46 -04:00
|
|
|
destroyer.destroy(u, context: I18n.t(:purge_reason))
|
2014-08-19 13:46:40 -04:00
|
|
|
rescue Discourse::InvalidAccess
|
|
|
|
# if for some reason the user can't be deleted, continue on to the next one
|
|
|
|
end
|
2014-08-13 14:13:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-07 06:40:35 -04:00
|
|
|
private
|
|
|
|
|
2013-10-23 17:24:50 -04:00
|
|
|
def previous_visit_at_update_required?(timestamp)
|
2014-01-16 22:38:08 -05:00
|
|
|
seen_before? && (last_seen_at < (timestamp - SiteSetting.previous_visit_timeout_hours.hours))
|
2013-10-23 17:24:50 -04: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
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: users
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
2014-04-15 01:53:48 -04:00
|
|
|
# username :string(60) not null
|
2014-08-27 01:19:25 -04:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-05-23 22:48:32 -04:00
|
|
|
# name :string(255)
|
|
|
|
# seen_notification_id :integer default(0), not null
|
|
|
|
# last_posted_at :datetime
|
|
|
|
# email :string(256) not null
|
|
|
|
# password_hash :string(64)
|
|
|
|
# salt :string(32)
|
2014-08-22 13:01:44 -04:00
|
|
|
# active :boolean default(FALSE), not null
|
2014-04-15 01:53:48 -04:00
|
|
|
# username_lower :string(60) not null
|
2013-05-23 22:48:32 -04:00
|
|
|
# auth_token :string(32)
|
|
|
|
# last_seen_at :datetime
|
|
|
|
# admin :boolean default(FALSE), not null
|
|
|
|
# last_emailed_at :datetime
|
2013-08-27 20:42:58 -04:00
|
|
|
# email_digests :boolean not null
|
2013-05-23 22:48:32 -04:00
|
|
|
# trust_level :integer not null
|
|
|
|
# email_private_messages :boolean default(TRUE)
|
|
|
|
# email_direct :boolean default(TRUE), not null
|
|
|
|
# approved :boolean default(FALSE), not null
|
|
|
|
# approved_by_id :integer
|
|
|
|
# approved_at :datetime
|
2013-08-27 20:42:58 -04:00
|
|
|
# digest_after_days :integer
|
2013-05-23 22:48:32 -04:00
|
|
|
# previous_visit_at :datetime
|
2013-11-07 13:53:32 -05:00
|
|
|
# suspended_at :datetime
|
|
|
|
# suspended_till :datetime
|
2013-05-23 22:48:32 -04:00
|
|
|
# date_of_birth :date
|
|
|
|
# auto_track_topics_after_msecs :integer
|
|
|
|
# views :integer default(0), not null
|
|
|
|
# flag_level :integer default(0), not null
|
2013-12-05 01:40:35 -05:00
|
|
|
# ip_address :inet
|
2013-05-23 22:48:32 -04:00
|
|
|
# new_topic_duration_minutes :integer
|
2014-02-06 19:07:36 -05:00
|
|
|
# external_links_in_new_tab :boolean not null
|
2013-05-23 22:48:32 -04:00
|
|
|
# enable_quoting :boolean default(TRUE), not null
|
|
|
|
# moderator :boolean default(FALSE)
|
2013-06-16 20:48:58 -04:00
|
|
|
# blocked :boolean default(FALSE)
|
2013-06-20 03:42:15 -04:00
|
|
|
# dynamic_favicon :boolean default(FALSE), not null
|
2013-07-13 21:24:16 -04:00
|
|
|
# title :string(255)
|
2013-08-13 16:08:29 -04:00
|
|
|
# uploaded_avatar_id :integer
|
2013-10-03 23:28:49 -04:00
|
|
|
# email_always :boolean default(FALSE), not null
|
2014-02-06 19:07:36 -05:00
|
|
|
# mailing_list_mode :boolean default(FALSE), not null
|
2014-08-10 19:21:06 -04:00
|
|
|
# primary_group_id :integer
|
2014-08-27 01:19:25 -04:00
|
|
|
# locale :string(10)
|
2014-05-08 02:45:49 -04:00
|
|
|
# registration_ip_address :inet
|
|
|
|
# last_redirected_to_top_at :datetime
|
2014-05-27 21:49:50 -04:00
|
|
|
# disable_jump_reply :boolean default(FALSE), not null
|
2014-07-27 00:04:46 -04:00
|
|
|
# edit_history_public :boolean default(FALSE), not null
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_users_on_auth_token (auth_token)
|
|
|
|
# index_users_on_last_posted_at (last_posted_at)
|
2014-08-06 23:33:11 -04:00
|
|
|
# index_users_on_last_seen_at (last_seen_at)
|
2013-05-23 22:48:32 -04:00
|
|
|
# index_users_on_username (username) UNIQUE
|
|
|
|
# index_users_on_username_lower (username_lower) UNIQUE
|
|
|
|
#
|