2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-02-02 21:44:40 -06:00
|
|
|
require "guardian/bookmark_guardian"
|
2023-01-09 06:10:19 -06:00
|
|
|
require "guardian/category_guardian"
|
|
|
|
require "guardian/ensure_magic"
|
2023-02-02 21:44:40 -06:00
|
|
|
require "guardian/group_guardian"
|
2023-01-09 06:10:19 -06:00
|
|
|
require "guardian/post_guardian"
|
|
|
|
require "guardian/post_revision_guardian"
|
2023-02-02 21:44:40 -06:00
|
|
|
require "guardian/sidebar_guardian"
|
2023-01-09 06:10:19 -06:00
|
|
|
require "guardian/tag_guardian"
|
2023-02-02 21:44:40 -06:00
|
|
|
require "guardian/topic_guardian"
|
|
|
|
require "guardian/user_guardian"
|
2014-02-13 10:42:35 -06:00
|
|
|
|
2013-02-06 13:46:45 -06:00
|
|
|
# The guardian is responsible for confirming access to various site resources and operations
|
2013-02-05 13:16:51 -06:00
|
|
|
class Guardian
|
2023-02-02 21:44:40 -06:00
|
|
|
include BookmarkGuardian
|
2014-01-09 17:25:14 -06:00
|
|
|
include CategoryGuardian
|
2023-02-02 21:44:40 -06:00
|
|
|
include EnsureMagic
|
|
|
|
include GroupGuardian
|
2014-05-12 09:30:10 -05:00
|
|
|
include PostGuardian
|
2014-10-27 16:06:43 -05:00
|
|
|
include PostRevisionGuardian
|
2023-02-02 21:44:40 -06:00
|
|
|
include SidebarGuardian
|
2016-06-06 13:18:15 -05:00
|
|
|
include TagGuardian
|
2023-02-02 21:44:40 -06:00
|
|
|
include TopicGuardian
|
|
|
|
include UserGuardian
|
2014-02-04 13:05:50 -06:00
|
|
|
|
2013-05-20 01:04:53 -05:00
|
|
|
class AnonymousUser
|
2018-12-03 21:48:13 -06:00
|
|
|
def blank?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def admin?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def staff?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def moderator?
|
|
|
|
false
|
|
|
|
end
|
2019-04-17 03:05:02 -05:00
|
|
|
def anonymous?
|
|
|
|
true
|
|
|
|
end
|
2018-12-03 21:48:13 -06:00
|
|
|
def approved?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def staged?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
def silenced?
|
|
|
|
false
|
|
|
|
end
|
2022-03-21 19:23:14 -05:00
|
|
|
def is_system_user?
|
|
|
|
false
|
|
|
|
end
|
2022-10-04 19:50:20 -05:00
|
|
|
def bot?
|
|
|
|
false
|
|
|
|
end
|
2018-12-03 21:48:13 -06:00
|
|
|
def secure_category_ids
|
|
|
|
[]
|
|
|
|
end
|
2022-06-29 19:18:12 -05:00
|
|
|
def groups
|
|
|
|
[]
|
|
|
|
end
|
2018-12-03 21:48:13 -06:00
|
|
|
def has_trust_level?(level)
|
|
|
|
false
|
|
|
|
end
|
2021-11-22 12:18:53 -06:00
|
|
|
def has_trust_level_or_staff?(level)
|
|
|
|
false
|
|
|
|
end
|
2018-12-03 21:48:13 -06:00
|
|
|
def email
|
|
|
|
nil
|
|
|
|
end
|
2022-06-29 19:18:12 -05:00
|
|
|
def whisperer?
|
|
|
|
false
|
|
|
|
end
|
2022-09-25 22:58:40 -05:00
|
|
|
def in_any_groups?(group_ids)
|
|
|
|
false
|
|
|
|
end
|
2013-05-20 01:04:53 -05:00
|
|
|
end
|
2014-05-12 09:30:10 -05:00
|
|
|
|
2018-10-09 09:21:41 -05:00
|
|
|
attr_reader :request
|
2014-11-03 05:46:08 -06:00
|
|
|
|
2018-10-09 09:21:41 -05:00
|
|
|
def initialize(user = nil, request = nil)
|
2023-12-06 00:37:32 -06:00
|
|
|
@user = user.presence || AnonymousUser.new
|
2018-10-09 09:21:41 -05:00
|
|
|
@request = request
|
2013-05-20 01:04:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def user
|
2023-12-06 00:37:32 -06:00
|
|
|
@user.presence
|
2013-05-20 01:04:53 -05:00
|
|
|
end
|
2023-01-09 06:10:19 -06:00
|
|
|
alias current_user user
|
2013-05-20 01:04:53 -05:00
|
|
|
|
|
|
|
def anonymous?
|
|
|
|
!authenticated?
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-05-20 01:04:53 -05:00
|
|
|
def authenticated?
|
2023-12-06 00:37:32 -06:00
|
|
|
@user.present?
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_admin?
|
2013-05-20 01:04:53 -05:00
|
|
|
@user.admin?
|
2013-04-29 01:33:24 -05:00
|
|
|
end
|
|
|
|
|
2013-05-02 02:22:27 -05:00
|
|
|
def is_staff?
|
2013-05-20 01:04:53 -05:00
|
|
|
@user.staff?
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2014-02-06 21:24:19 -06:00
|
|
|
def is_moderator?
|
|
|
|
@user.moderator?
|
|
|
|
end
|
|
|
|
|
2022-06-29 19:18:12 -05:00
|
|
|
def is_whisperer?
|
|
|
|
@user.whisperer?
|
|
|
|
end
|
|
|
|
|
2020-07-28 16:15:04 -05:00
|
|
|
def is_category_group_moderator?(category)
|
2022-08-03 19:07:12 -05:00
|
|
|
return false if !category
|
2022-10-26 17:13:21 -05:00
|
|
|
return false if !category_group_moderation_allowed?
|
2022-08-03 19:07:12 -05:00
|
|
|
|
|
|
|
reviewable_by_group_id = category.reviewable_by_group_id
|
|
|
|
return false if reviewable_by_group_id.blank?
|
|
|
|
|
2022-10-26 17:13:21 -05:00
|
|
|
@category_group_moderator_groups ||= {}
|
2022-08-03 19:07:12 -05:00
|
|
|
|
2022-10-26 17:13:21 -05:00
|
|
|
if @category_group_moderator_groups.key?(reviewable_by_group_id)
|
|
|
|
@category_group_moderator_groups[reviewable_by_group_id]
|
2022-08-03 19:07:12 -05:00
|
|
|
else
|
2023-01-09 06:10:19 -06:00
|
|
|
@category_group_moderator_groups[
|
|
|
|
reviewable_by_group_id
|
|
|
|
] = category_group_moderator_scope.exists?("categories.id": category.id)
|
2020-07-28 16:15:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-10 11:18:08 -06:00
|
|
|
def is_silenced?
|
|
|
|
@user.silenced?
|
2016-06-20 02:41:17 -05:00
|
|
|
end
|
|
|
|
|
2013-09-04 19:27:34 -05:00
|
|
|
def is_developer?
|
2023-01-09 06:10:19 -06:00
|
|
|
@user && is_admin? &&
|
2013-09-05 23:07:23 -05:00
|
|
|
(
|
2023-01-09 06:10:19 -06:00
|
|
|
Rails.env.development? || Developer.user_ids.include?(@user.id) ||
|
|
|
|
(
|
|
|
|
Rails.configuration.respond_to?(:developer_emails) &&
|
|
|
|
Rails.configuration.developer_emails.include?(@user.email)
|
|
|
|
)
|
2018-01-16 22:50:18 -06:00
|
|
|
)
|
2013-09-04 19:27:34 -05:00
|
|
|
end
|
|
|
|
|
2016-06-26 12:25:45 -05:00
|
|
|
def is_staged?
|
|
|
|
@user.staged?
|
|
|
|
end
|
|
|
|
|
2019-04-17 03:05:02 -05:00
|
|
|
def is_anonymous?
|
|
|
|
@user.anonymous?
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# Can the user see the object?
|
|
|
|
def can_see?(obj)
|
2013-05-20 01:04:53 -05:00
|
|
|
if obj
|
|
|
|
see_method = method_name_for :see, obj
|
2023-02-24 01:57:01 -06:00
|
|
|
see_method && public_send(see_method, obj)
|
2013-05-20 01:04:53 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def can_create?(klass, parent = nil)
|
2014-03-17 13:50:28 -05:00
|
|
|
return false unless authenticated? && klass
|
|
|
|
|
|
|
|
# If no parent is provided, we look for a can_create_klass?
|
|
|
|
# custom method.
|
|
|
|
#
|
|
|
|
# If a parent is provided, we look for a method called
|
|
|
|
# can_create_klass_on_parent?
|
|
|
|
target = klass.name.underscore
|
|
|
|
if parent.present?
|
|
|
|
return false unless can_see?(parent)
|
|
|
|
target << "_on_#{parent.class.name.underscore}"
|
|
|
|
end
|
|
|
|
create_method = :"can_create_#{target}?"
|
|
|
|
|
2019-05-06 21:22:37 -05:00
|
|
|
return public_send(create_method, parent) if respond_to?(create_method)
|
2014-03-17 13:50:28 -05:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-04-25 10:46:54 -05:00
|
|
|
def can_enable_safe_mode?
|
|
|
|
SiteSetting.enable_safe_mode? || is_staff?
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# Can the user edit the obj
|
|
|
|
def can_edit?(obj)
|
2013-08-16 07:24:29 -05:00
|
|
|
can_do?(:edit, obj)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# Can we delete the object
|
|
|
|
def can_delete?(obj)
|
2013-08-16 07:24:29 -05:00
|
|
|
can_do?(:delete, obj)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2021-10-13 04:53:23 -05:00
|
|
|
def can_permanently_delete?(obj)
|
|
|
|
can_do?(:permanently_delete, obj)
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def can_moderate?(obj)
|
2023-01-09 06:10:19 -06:00
|
|
|
obj && authenticated? && !is_silenced? &&
|
|
|
|
(
|
|
|
|
is_staff? ||
|
|
|
|
(obj.is_a?(Topic) && @user.has_trust_level?(TrustLevel[4]) && can_see_topic?(obj))
|
|
|
|
)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2023-01-09 06:10:19 -06:00
|
|
|
alias can_see_flags? can_moderate?
|
2014-09-02 15:12:27 -05:00
|
|
|
|
2018-02-22 08:57:02 -06:00
|
|
|
def can_tag?(topic)
|
|
|
|
return false if topic.blank?
|
2018-02-13 14:46:25 -06:00
|
|
|
|
2018-02-22 08:57:02 -06:00
|
|
|
topic.private_message? ? can_tag_pms? : can_tag_topics?
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_see_tags?(topic)
|
|
|
|
SiteSetting.tagging_enabled && topic.present? && (!topic.private_message? || can_tag_pms?)
|
2018-02-13 14:46:25 -06:00
|
|
|
end
|
|
|
|
|
2017-07-04 03:29:05 -05:00
|
|
|
def can_send_activation_email?(user)
|
|
|
|
user && is_staff? && !SiteSetting.must_approve_users?
|
|
|
|
end
|
|
|
|
|
2014-09-02 15:12:27 -05:00
|
|
|
def can_grant_badges?(_user)
|
|
|
|
SiteSetting.enable_badges && is_staff?
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2019-04-12 08:55:27 -05:00
|
|
|
def can_delete_reviewable_queued_post?(reviewable)
|
2023-04-20 11:07:35 -05:00
|
|
|
return false if reviewable.blank?
|
|
|
|
return false if !authenticated?
|
2023-04-24 20:22:37 -05:00
|
|
|
return true if is_api? && is_admin?
|
2023-04-20 11:07:35 -05:00
|
|
|
|
2023-08-18 10:30:59 -05:00
|
|
|
reviewable.target_created_by_id == @user.id
|
2019-04-12 08:55:27 -05:00
|
|
|
end
|
|
|
|
|
2014-04-22 15:43:46 -05:00
|
|
|
def can_see_group?(group)
|
2020-01-15 04:21:58 -06:00
|
|
|
group.present? && can_see_groups?([group])
|
2014-04-22 15:43:46 -05:00
|
|
|
end
|
|
|
|
|
2019-08-14 08:30:04 -05:00
|
|
|
def can_see_group_members?(group)
|
|
|
|
return false if group.blank?
|
2020-01-15 04:21:58 -06:00
|
|
|
return true if is_admin? || group.members_visibility_level == Group.visibility_levels[:public]
|
2019-08-14 08:30:04 -05:00
|
|
|
return true if is_staff? && group.members_visibility_level == Group.visibility_levels[:staff]
|
2020-09-21 11:32:43 -05:00
|
|
|
return true if is_staff? && group.members_visibility_level == Group.visibility_levels[:members]
|
2023-01-09 06:10:19 -06:00
|
|
|
if authenticated? && group.members_visibility_level == Group.visibility_levels[:logged_on_users]
|
|
|
|
return true
|
|
|
|
end
|
2019-08-14 08:30:04 -05:00
|
|
|
return false if user.blank?
|
|
|
|
|
2020-01-15 04:21:58 -06:00
|
|
|
return false unless membership = GroupUser.find_by(group_id: group.id, user_id: user.id)
|
|
|
|
return true if membership.owner
|
2019-08-14 08:30:04 -05:00
|
|
|
|
2020-01-15 04:21:58 -06:00
|
|
|
return false if group.members_visibility_level == Group.visibility_levels[:owners]
|
|
|
|
return false if group.members_visibility_level == Group.visibility_levels[:staff]
|
2019-08-14 08:30:04 -05:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-05-29 17:40:43 -05:00
|
|
|
def can_see_groups?(groups)
|
|
|
|
return false if groups.blank?
|
2023-01-09 06:10:19 -06:00
|
|
|
if is_admin? || groups.all? { |g| g.visibility_level == Group.visibility_levels[:public] }
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
if is_staff? && groups.all? { |g| g.visibility_level == Group.visibility_levels[:staff] }
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
if is_staff? && groups.all? { |g| g.visibility_level == Group.visibility_levels[:members] }
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
if authenticated? &&
|
|
|
|
groups.all? { |g| g.visibility_level == Group.visibility_levels[:logged_on_users] }
|
|
|
|
return true
|
|
|
|
end
|
2019-05-29 17:40:43 -05:00
|
|
|
return false if user.blank?
|
|
|
|
|
|
|
|
memberships = GroupUser.where(group: groups, user_id: user.id).pluck(:owner)
|
2020-01-15 04:21:58 -06:00
|
|
|
return false if memberships.size < groups.size
|
|
|
|
return true if memberships.all? # owner of all groups
|
2019-05-29 17:40:43 -05:00
|
|
|
|
2020-01-15 04:21:58 -06:00
|
|
|
return false if groups.all? { |g| g.visibility_level == Group.visibility_levels[:owners] }
|
|
|
|
return false if groups.all? { |g| g.visibility_level == Group.visibility_levels[:staff] }
|
2019-05-29 17:40:43 -05:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-10-24 07:23:19 -05:00
|
|
|
def can_see_groups_members?(groups)
|
|
|
|
return false if groups.blank?
|
|
|
|
|
|
|
|
requested_group_ids = groups.map(&:id) # Can't use pluck, groups could be a regular array
|
2023-01-09 06:10:19 -06:00
|
|
|
matching_group_ids =
|
|
|
|
Group.where(id: requested_group_ids).members_visible_groups(user).pluck(:id)
|
2019-10-24 07:23:19 -05:00
|
|
|
|
2020-01-15 04:21:58 -06:00
|
|
|
matching_group_ids.sort == requested_group_ids.sort
|
2019-10-24 07:23:19 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# Can we impersonate this user?
|
|
|
|
def can_impersonate?(target)
|
2023-03-22 23:16:05 -05:00
|
|
|
GlobalSetting.allow_impersonation && target &&
|
2023-01-09 06:10:19 -06:00
|
|
|
# You must be an admin to impersonate
|
|
|
|
is_admin? &&
|
|
|
|
# You may not impersonate other admins unless you are a dev
|
|
|
|
(!target.admin? || is_developer?)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-05-20 01:04:53 -05:00
|
|
|
# Additionally, you may not impersonate yourself;
|
|
|
|
# but the two tests for different admin statuses
|
|
|
|
# make it impossible to be the same user.
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2017-02-21 06:45:30 -06:00
|
|
|
def can_view_action_logs?(target)
|
2017-04-20 11:15:52 -05:00
|
|
|
target.present? && is_staff?
|
2017-02-21 06:45:30 -06:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# Can we approve it?
|
|
|
|
def can_approve?(target)
|
2019-04-16 13:42:47 -05:00
|
|
|
is_staff? && target && target.active? && !target.approved?
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2014-04-28 12:46:28 -05:00
|
|
|
|
|
|
|
def can_activate?(target)
|
|
|
|
is_staff? && target && not(target.active?)
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2013-11-07 12:53:32 -06:00
|
|
|
def can_suspend?(user)
|
2013-05-24 11:13:31 -05:00
|
|
|
user && is_staff? && user.regular?
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2023-01-09 06:10:19 -06:00
|
|
|
alias can_deactivate? can_suspend?
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
def can_revoke_admin?(admin)
|
2013-05-20 01:04:53 -05:00
|
|
|
can_administer_user?(admin) && admin.admin?
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_grant_admin?(user)
|
2016-12-28 21:11:33 -06:00
|
|
|
can_administer_user?(user) && !user.admin?
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2013-02-12 16:58:08 -06:00
|
|
|
def can_revoke_moderation?(moderator)
|
2013-05-20 01:04:53 -05:00
|
|
|
can_administer?(moderator) && moderator.moderator?
|
2013-02-12 16:58:08 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_grant_moderation?(user)
|
2016-12-28 21:11:33 -06:00
|
|
|
can_administer?(user) && !user.moderator?
|
2013-02-12 16:58:08 -06:00
|
|
|
end
|
|
|
|
|
2018-04-06 13:29:27 -05:00
|
|
|
def can_grant_title?(user, title = nil)
|
|
|
|
return true if user && is_staff?
|
2018-04-26 15:50:50 -05:00
|
|
|
return false if title.nil?
|
2018-05-31 18:10:52 -05:00
|
|
|
return true if title.empty? # A title set to '(none)' in the UI is an empty string
|
2018-04-06 13:29:27 -05:00
|
|
|
return false if user != @user
|
2020-01-08 11:01:46 -06:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
if user
|
|
|
|
.badges
|
|
|
|
.where(allow_title: true)
|
|
|
|
.pluck(:name)
|
|
|
|
.any? { |name| Badge.display_name(name) == title }
|
|
|
|
return true
|
|
|
|
end
|
2020-01-08 11:01:46 -06:00
|
|
|
|
2018-04-06 13:29:27 -05:00
|
|
|
user.groups.where(title: title).exists?
|
2013-06-25 17:39:20 -05:00
|
|
|
end
|
|
|
|
|
2019-10-28 12:46:27 -05:00
|
|
|
def can_use_primary_group?(user, group_id = nil)
|
|
|
|
return false if !user || !group_id
|
|
|
|
group = Group.find_by(id: group_id.to_i)
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
user.group_ids.include?(group_id.to_i) && (group ? !group.automatic : false)
|
2019-10-28 12:46:27 -05:00
|
|
|
end
|
|
|
|
|
2021-07-21 06:41:04 -05:00
|
|
|
def can_use_flair_group?(user, group_id = nil)
|
|
|
|
return false if !user || !group_id || !user.group_ids.include?(group_id.to_i)
|
2023-02-12 22:39:45 -06:00
|
|
|
flair_icon, flair_upload_id = Group.where(id: group_id.to_i).pick(:flair_icon, :flair_upload_id)
|
2021-07-21 06:41:04 -05:00
|
|
|
flair_icon.present? || flair_upload_id.present?
|
|
|
|
end
|
|
|
|
|
2022-11-11 05:06:05 -06:00
|
|
|
def can_change_primary_group?(user, group)
|
|
|
|
user && can_edit_group?(group)
|
2014-02-10 15:59:36 -06:00
|
|
|
end
|
|
|
|
|
2013-07-03 03:27:40 -05:00
|
|
|
def can_change_trust_level?(user)
|
2013-07-22 18:13:48 -05:00
|
|
|
user && is_staff?
|
2013-07-03 03:27:40 -05:00
|
|
|
end
|
|
|
|
|
2013-04-03 11:23:28 -05:00
|
|
|
# Support sites that have to approve users
|
|
|
|
def can_access_forum?
|
|
|
|
return true unless SiteSetting.must_approve_users?
|
2020-04-05 18:56:47 -05:00
|
|
|
return false if anonymous?
|
2013-04-03 11:23:28 -05:00
|
|
|
|
2013-05-02 02:22:27 -05:00
|
|
|
# Staff can't lock themselves out of a site
|
|
|
|
return true if is_staff?
|
2013-04-03 11:23:28 -05:00
|
|
|
|
2013-05-02 02:22:27 -05:00
|
|
|
@user.approved?
|
2013-04-03 11:23:28 -05:00
|
|
|
end
|
|
|
|
|
2014-03-21 13:13:04 -05:00
|
|
|
def can_see_invite_details?(user)
|
2021-10-14 09:57:01 -05:00
|
|
|
is_staff? || is_me?(user)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2020-03-04 10:47:09 -06:00
|
|
|
def can_see_invite_emails?(user)
|
|
|
|
is_staff? || is_me?(user)
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def can_invite_to_forum?(groups = nil)
|
2023-01-09 06:10:19 -06:00
|
|
|
authenticated? && (is_staff? || SiteSetting.max_invites_per_day.to_i.positive?) &&
|
|
|
|
(is_staff? || @user.has_trust_level?(SiteSetting.min_trust_level_to_allow_invite.to_i)) &&
|
|
|
|
(is_admin? || groups.blank? || groups.all? { |g| can_edit_group?(g) })
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def can_invite_to?(object, groups = nil)
|
2021-10-11 04:19:31 -05:00
|
|
|
return false if !authenticated?
|
2021-09-29 09:40:16 -05:00
|
|
|
return false if !object.is_a?(Topic) || !can_see?(object)
|
2017-07-21 01:12:24 -05:00
|
|
|
return false if groups.present?
|
2015-03-02 13:25:25 -06:00
|
|
|
|
2021-09-29 09:40:16 -05:00
|
|
|
if object.is_a?(Topic)
|
2018-12-05 09:43:07 -06:00
|
|
|
if object.private_message?
|
|
|
|
return true if is_admin?
|
2022-09-25 22:58:40 -05:00
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
return false if !@user.in_any_groups?(SiteSetting.personal_message_enabled_groups_map)
|
2018-12-05 09:43:07 -06:00
|
|
|
return false if object.reached_recipients_limit? && !is_staff?
|
|
|
|
end
|
2018-03-07 14:04:17 -06:00
|
|
|
|
2018-12-05 09:43:07 -06:00
|
|
|
if (category = object.category) && category.read_restricted
|
2023-12-06 16:25:00 -06:00
|
|
|
return category.groups&.where(automatic: false)&.any? { |g| can_edit_group?(g) }
|
2015-03-02 13:25:25 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-29 09:40:16 -05:00
|
|
|
true
|
2013-11-06 11:56:26 -06:00
|
|
|
end
|
|
|
|
|
2017-02-02 11:38:25 -06:00
|
|
|
def can_invite_via_email?(object)
|
2021-10-11 04:19:31 -05:00
|
|
|
return false if !can_invite_to_forum?
|
2021-09-29 09:40:16 -05:00
|
|
|
return false if !can_invite_to?(object)
|
|
|
|
|
2021-03-18 19:20:10 -05:00
|
|
|
(SiteSetting.enable_local_logins || SiteSetting.enable_discourse_connect) &&
|
|
|
|
(!SiteSetting.must_approve_users? || is_staff?)
|
2017-02-02 11:38:25 -06:00
|
|
|
end
|
|
|
|
|
2014-05-27 15:14:37 -05:00
|
|
|
def can_bulk_invite_to_forum?(user)
|
2021-11-09 11:43:23 -06:00
|
|
|
user.admin?
|
2014-05-27 15:14:37 -05:00
|
|
|
end
|
|
|
|
|
2016-06-07 00:27:08 -05:00
|
|
|
def can_resend_all_invites?(user)
|
|
|
|
user.staff?
|
2014-07-29 12:57:08 -05:00
|
|
|
end
|
|
|
|
|
2021-03-03 03:45:29 -06:00
|
|
|
def can_destroy_all_invites?(user)
|
2017-06-29 09:32:07 -05:00
|
|
|
user.staff?
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
def can_see_private_messages?(user_id)
|
2014-02-06 21:24:19 -06:00
|
|
|
is_admin? || (authenticated? && @user.id == user_id)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2017-12-13 20:53:21 -06:00
|
|
|
def can_invite_group_to_private_message?(group, topic)
|
2023-01-09 06:10:19 -06:00
|
|
|
can_see_topic?(topic) && can_send_private_message?(group)
|
2017-12-13 20:53:21 -06:00
|
|
|
end
|
|
|
|
|
2022-11-06 17:11:18 -06:00
|
|
|
##
|
|
|
|
# This should be used as a general, but not definitive, check for whether
|
|
|
|
# the user can send private messages _generally_, which is mostly useful
|
|
|
|
# for changing the UI.
|
|
|
|
#
|
|
|
|
# Please otherwise use can_send_private_message?(target, notify_moderators)
|
|
|
|
# to check if a single target can be messaged.
|
|
|
|
def can_send_private_messages?(notify_moderators: false)
|
2022-03-21 19:23:14 -05:00
|
|
|
from_system = @user.is_system_user?
|
2022-10-04 19:50:20 -05:00
|
|
|
from_bot = @user.bot?
|
2017-10-06 02:56:58 -05:00
|
|
|
|
2014-02-12 22:08:46 -06:00
|
|
|
# User is authenticated
|
2013-05-20 01:04:53 -05:00
|
|
|
authenticated? &&
|
2022-11-06 17:11:18 -06:00
|
|
|
# User can send PMs, this can be covered by trust levels as well via AUTO_GROUPS
|
2023-01-09 06:10:19 -06:00
|
|
|
(
|
|
|
|
is_staff? || from_bot || from_system ||
|
|
|
|
(@user.in_any_groups?(SiteSetting.personal_message_enabled_groups_map)) ||
|
|
|
|
notify_moderators
|
|
|
|
)
|
2022-11-06 17:11:18 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# This should be used as a final check for when a user is sending a message
|
|
|
|
# to a target user or group.
|
|
|
|
def can_send_private_message?(target, notify_moderators: false)
|
|
|
|
target_is_user = target.is_a?(User)
|
|
|
|
target_is_group = target.is_a?(Group)
|
|
|
|
from_system = @user.is_system_user?
|
|
|
|
|
|
|
|
# Must be a valid target
|
2022-12-19 21:11:14 -06:00
|
|
|
return false if !(target_is_group || target_is_user)
|
|
|
|
|
|
|
|
# Users can send messages to certain groups with the `everyone` messageable_level
|
|
|
|
# even if they are not in personal_message_enabled_groups
|
|
|
|
group_is_messageable = target_is_group && Group.messageable(@user).where(id: target.id).exists?
|
|
|
|
|
2022-11-06 17:11:18 -06:00
|
|
|
# User is authenticated and can send PMs, this can be covered by trust levels as well via AUTO_GROUPS
|
2022-12-19 21:11:14 -06:00
|
|
|
(can_send_private_messages?(notify_moderators: notify_moderators) || group_is_messageable) &&
|
2023-01-09 06:10:19 -06:00
|
|
|
# User disabled private message
|
|
|
|
(is_staff? || target_is_group || target.user_option.allow_private_messages) &&
|
|
|
|
# Can't send PMs to suspended users
|
|
|
|
(is_staff? || target_is_group || !target.suspended?) &&
|
|
|
|
# Check group messageable level
|
|
|
|
(from_system || target_is_user || group_is_messageable || notify_moderators) &&
|
|
|
|
# Silenced users can only send PM to staff
|
|
|
|
(!is_silenced? || target.staff?)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2018-08-26 20:38:11 -05:00
|
|
|
def can_send_private_messages_to_email?
|
2017-08-28 11:07:30 -05:00
|
|
|
# Staged users must be enabled to create a temporary user.
|
2021-04-06 07:39:27 -05:00
|
|
|
return false if !SiteSetting.enable_staged_users
|
2017-08-28 11:07:30 -05:00
|
|
|
# User is authenticated
|
2021-04-06 07:39:27 -05:00
|
|
|
return false if !authenticated?
|
2017-08-28 11:07:30 -05:00
|
|
|
# User is trusted enough
|
2022-10-04 19:50:20 -05:00
|
|
|
@user.in_any_groups?(SiteSetting.personal_message_enabled_groups_map) &&
|
2022-09-25 22:58:40 -05:00
|
|
|
@user.has_trust_level_or_staff?(SiteSetting.min_trust_to_send_email_messages)
|
2017-08-28 11:07:30 -05:00
|
|
|
end
|
|
|
|
|
2017-05-19 14:59:37 -05:00
|
|
|
def can_export_entity?(entity)
|
2020-04-05 18:56:47 -05:00
|
|
|
return false if anonymous?
|
2018-09-20 20:07:13 -05:00
|
|
|
return true if is_admin?
|
2023-01-09 06:10:19 -06:00
|
|
|
return entity != "user_list" if is_moderator?
|
2017-05-19 14:59:37 -05:00
|
|
|
|
|
|
|
# Regular users can only export their archives
|
|
|
|
return false unless entity == "user_archive"
|
2023-01-09 06:10:19 -06:00
|
|
|
UserExport.where(
|
|
|
|
user_id: @user.id,
|
|
|
|
created_at: (Time.zone.now.beginning_of_day..Time.zone.now.end_of_day),
|
|
|
|
).count == 0
|
2014-12-22 10:17:04 -06:00
|
|
|
end
|
|
|
|
|
2020-01-02 07:04:08 -06:00
|
|
|
def can_mute_user?(target_user)
|
2023-01-09 06:10:19 -06:00
|
|
|
can_mute_users? && @user.id != target_user.id && !target_user.staff?
|
2019-03-27 04:41:50 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_mute_users?
|
|
|
|
return false if anonymous?
|
|
|
|
@user.staff? || @user.trust_level >= TrustLevel.levels[:basic]
|
|
|
|
end
|
|
|
|
|
2020-01-02 07:04:08 -06:00
|
|
|
def can_ignore_user?(target_user)
|
|
|
|
can_ignore_users? && @user.id != target_user.id && !target_user.staff?
|
2019-03-20 14:55:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_ignore_users?
|
|
|
|
return false if anonymous?
|
2020-11-20 12:05:20 -06:00
|
|
|
@user.staff? || @user.has_trust_level?(SiteSetting.min_trust_level_to_allow_ignore.to_i)
|
2019-03-20 05:18:46 -05:00
|
|
|
end
|
|
|
|
|
2020-06-02 22:19:42 -05:00
|
|
|
def allowed_theme_repo_import?(repo)
|
|
|
|
return false if !@user.admin?
|
|
|
|
|
2020-07-26 19:23:54 -05:00
|
|
|
allowed_repos = GlobalSetting.allowed_theme_repos
|
|
|
|
if !allowed_repos.blank?
|
|
|
|
urls = allowed_repos.split(",").map(&:strip)
|
2020-06-02 22:19:42 -05:00
|
|
|
return urls.include?(repo)
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2018-09-06 19:44:57 -05:00
|
|
|
def allow_themes?(theme_ids, include_preview: false)
|
2018-08-10 06:12:02 -05:00
|
|
|
return true if theme_ids.blank?
|
|
|
|
|
2021-10-29 10:46:52 -05:00
|
|
|
if allowed_theme_ids = Theme.allowed_remote_theme_ids
|
2023-01-09 06:10:19 -06:00
|
|
|
return false if (theme_ids - allowed_theme_ids).present?
|
2020-06-02 22:19:42 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
return true if include_preview && is_staff? && (theme_ids - Theme.theme_ids).blank?
|
2018-08-07 23:46:34 -05:00
|
|
|
|
|
|
|
parent = theme_ids.first
|
|
|
|
components = theme_ids[1..-1] || []
|
|
|
|
|
2023-01-09 06:10:19 -06:00
|
|
|
Theme.user_theme_ids.include?(parent) && (components - Theme.components_for(parent)).empty?
|
2017-04-14 12:35:12 -05:00
|
|
|
end
|
|
|
|
|
2020-04-08 11:52:36 -05:00
|
|
|
def can_publish_page?(topic)
|
2020-11-05 18:33:19 -06:00
|
|
|
return false if !SiteSetting.enable_page_publishing?
|
2022-09-28 18:24:33 -05:00
|
|
|
return false if SiteSetting.secure_uploads?
|
2020-04-08 11:52:36 -05:00
|
|
|
return false if topic.blank?
|
|
|
|
return false if topic.private_message?
|
|
|
|
return false unless can_see_topic?(topic)
|
|
|
|
is_staff?
|
|
|
|
end
|
|
|
|
|
2020-05-22 23:56:13 -05:00
|
|
|
def can_see_about_stats?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2021-05-19 01:15:24 -05:00
|
|
|
def can_see_site_contact_details?
|
|
|
|
!SiteSetting.login_required? || authenticated?
|
|
|
|
end
|
|
|
|
|
2018-10-11 18:40:48 -05:00
|
|
|
def auth_token
|
FEATURE: Apply rate limits per user instead of IP for trusted users (#14706)
Currently, Discourse rate limits all incoming requests by the IP address they
originate from regardless of the user making the request. This can be
frustrating if there are multiple users using Discourse simultaneously while
sharing the same IP address (e.g. employees in an office).
This commit implements a new feature to make Discourse apply rate limits by
user id rather than IP address for users at or higher than the configured trust
level (1 is the default).
For example, let's say a Discourse instance is configured to allow 200 requests
per minute per IP address, and we have 10 users at trust level 4 using
Discourse simultaneously from the same IP address. Before this feature, the 10
users could only make a total of 200 requests per minute before they got rate
limited. But with the new feature, each user is allowed to make 200 requests
per minute because the rate limits are applied on user id rather than the IP
address.
The minimum trust level for applying user-id-based rate limits can be
configured by the `skip_per_ip_rate_limit_trust_level` global setting. The
default is 1, but it can be changed by either adding the
`DISCOURSE_SKIP_PER_IP_RATE_LIMIT_TRUST_LEVEL` environment variable with the
desired value to your `app.yml`, or changing the setting's value in the
`discourse.conf` file.
Requests made with API keys are still rate limited by IP address and the
relevant global settings that control API keys rate limits.
Before this commit, Discourse's auth cookie (`_t`) was simply a 32 characters
string that Discourse used to lookup the current user from the database and the
cookie contained no additional information about the user. However, we had to
change the cookie content in this commit so we could identify the user from the
cookie without making a database query before the rate limits logic and avoid
introducing a bottleneck on busy sites.
Besides the 32 characters auth token, the cookie now includes the user id,
trust level and the cookie's generation date, and we encrypt/sign the cookie to
prevent tampering.
Internal ticket number: t54739.
2021-11-17 14:27:30 -06:00
|
|
|
return if !request
|
|
|
|
|
|
|
|
token = Auth::DefaultCurrentUserProvider.find_v0_auth_cookie(request).presence
|
|
|
|
|
|
|
|
if !token
|
|
|
|
cookie = Auth::DefaultCurrentUserProvider.find_v1_auth_cookie(request.env)
|
|
|
|
token = cookie[:token] if cookie
|
2018-10-11 18:51:41 -05:00
|
|
|
end
|
FEATURE: Apply rate limits per user instead of IP for trusted users (#14706)
Currently, Discourse rate limits all incoming requests by the IP address they
originate from regardless of the user making the request. This can be
frustrating if there are multiple users using Discourse simultaneously while
sharing the same IP address (e.g. employees in an office).
This commit implements a new feature to make Discourse apply rate limits by
user id rather than IP address for users at or higher than the configured trust
level (1 is the default).
For example, let's say a Discourse instance is configured to allow 200 requests
per minute per IP address, and we have 10 users at trust level 4 using
Discourse simultaneously from the same IP address. Before this feature, the 10
users could only make a total of 200 requests per minute before they got rate
limited. But with the new feature, each user is allowed to make 200 requests
per minute because the rate limits are applied on user id rather than the IP
address.
The minimum trust level for applying user-id-based rate limits can be
configured by the `skip_per_ip_rate_limit_trust_level` global setting. The
default is 1, but it can be changed by either adding the
`DISCOURSE_SKIP_PER_IP_RATE_LIMIT_TRUST_LEVEL` environment variable with the
desired value to your `app.yml`, or changing the setting's value in the
`discourse.conf` file.
Requests made with API keys are still rate limited by IP address and the
relevant global settings that control API keys rate limits.
Before this commit, Discourse's auth cookie (`_t`) was simply a 32 characters
string that Discourse used to lookup the current user from the database and the
cookie contained no additional information about the user. However, we had to
change the cookie content in this commit so we could identify the user from the
cookie without making a database query before the rate limits logic and avoid
introducing a bottleneck on busy sites.
Besides the 32 characters auth token, the cookie now includes the user id,
trust level and the cookie's generation date, and we encrypt/sign the cookie to
prevent tampering.
Internal ticket number: t54739.
2021-11-17 14:27:30 -06:00
|
|
|
|
|
|
|
UserAuthToken.hash_token(token) if token
|
2018-10-11 18:40:48 -05:00
|
|
|
end
|
|
|
|
|
2021-11-23 14:25:54 -06:00
|
|
|
def can_mention_here?
|
|
|
|
return false if SiteSetting.here_mention.blank?
|
|
|
|
return false if SiteSetting.max_here_mentioned < 1
|
|
|
|
return false if !authenticated?
|
|
|
|
return false if User.where(username_lower: SiteSetting.here_mention).exists?
|
|
|
|
|
2023-11-08 20:07:01 -06:00
|
|
|
@user.in_any_groups?(SiteSetting.here_mention_allowed_groups_map) ||
|
|
|
|
@user.has_trust_level_or_staff?(SiteSetting.min_trust_level_for_here_mention)
|
2021-11-23 14:25:54 -06:00
|
|
|
end
|
|
|
|
|
2021-08-26 11:16:00 -05:00
|
|
|
def is_me?(other)
|
|
|
|
other && authenticated? && other.is_a?(User) && @user == other
|
|
|
|
end
|
|
|
|
|
2013-05-20 01:04:53 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def is_my_own?(obj)
|
2023-07-21 08:21:07 -05:00
|
|
|
if anonymous?
|
|
|
|
return(
|
|
|
|
SiteSetting.allow_anonymous_likes? && obj.class == PostAction && obj.is_like? &&
|
|
|
|
obj.user_id == @user.id
|
|
|
|
)
|
2013-10-03 22:28:49 -05:00
|
|
|
end
|
2023-07-21 08:21:07 -05:00
|
|
|
return obj.user_id == @user.id if obj.respond_to?(:user_id) && obj.user_id && @user.id
|
|
|
|
return obj.user == @user if obj.respond_to?(:user)
|
2013-10-03 22:28:49 -05:00
|
|
|
|
|
|
|
false
|
2013-05-20 01:04:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_not_me?(other)
|
|
|
|
@user.blank? || !is_me?(other)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_administer?(obj)
|
2016-12-28 21:11:33 -06:00
|
|
|
is_admin? && obj.present? && obj.id&.positive?
|
2013-05-20 01:04:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_administer_user?(other_user)
|
|
|
|
can_administer?(other_user) && is_not_me?(other_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_name_for(action, obj)
|
|
|
|
method_name = :"can_#{action}_#{obj.class.name.underscore}?"
|
2023-08-09 19:03:38 -05:00
|
|
|
method_name if respond_to?(method_name)
|
2013-05-20 01:04:53 -05:00
|
|
|
end
|
|
|
|
|
2013-08-16 07:24:29 -05:00
|
|
|
def can_do?(action, obj)
|
|
|
|
if obj && authenticated?
|
|
|
|
action_method = method_name_for action, obj
|
2019-11-14 14:10:51 -06:00
|
|
|
(action_method ? public_send(action_method, obj) : true)
|
2014-01-16 10:59:26 -06:00
|
|
|
else
|
|
|
|
false
|
2013-08-16 07:24:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-24 20:22:37 -05:00
|
|
|
def is_api?
|
|
|
|
@user && request&.env&.dig(Auth::DefaultCurrentUserProvider::API_KEY_ENV)
|
|
|
|
end
|
|
|
|
|
2022-10-26 17:13:21 -05:00
|
|
|
protected
|
|
|
|
|
|
|
|
def category_group_moderation_allowed?
|
|
|
|
authenticated? && SiteSetting.enable_category_group_moderation
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_group_moderator_scope
|
2023-01-09 06:10:19 -06:00
|
|
|
Category.joins(
|
|
|
|
"INNER JOIN group_users ON group_users.group_id = categories.reviewable_by_group_id",
|
|
|
|
).where("group_users.user_id = ?", user.id)
|
2022-10-26 17:13:21 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|