2014-01-10 07:25:14 +08:00
|
|
|
require_dependency 'guardian/category_guardian'
|
|
|
|
require_dependency 'guardian/ensure_magic'
|
|
|
|
require_dependency 'guardian/post_guardian'
|
|
|
|
require_dependency 'guardian/topic_guardian'
|
2014-02-14 00:42:35 +08:00
|
|
|
require_dependency 'guardian/user_guardian'
|
|
|
|
|
2013-02-07 03:46:45 +08:00
|
|
|
# The guardian is responsible for confirming access to various site resources and operations
|
2013-02-06 03:16:51 +08:00
|
|
|
class Guardian
|
2014-01-10 07:25:14 +08:00
|
|
|
include EnsureMagic
|
|
|
|
include CategoryGuardian
|
2014-05-12 22:30:10 +08:00
|
|
|
include PostGuardian
|
2014-01-10 07:25:14 +08:00
|
|
|
include TopicGuardian
|
2014-02-14 00:42:35 +08:00
|
|
|
include UserGuardian
|
2014-02-05 03:05:50 +08:00
|
|
|
|
2013-05-20 14:04:53 +08:00
|
|
|
class AnonymousUser
|
|
|
|
def blank?; true; end
|
|
|
|
def admin?; false; end
|
|
|
|
def staff?; false; end
|
2014-02-07 11:24:19 +08:00
|
|
|
def moderator?; false; end
|
2013-05-20 14:04:53 +08:00
|
|
|
def approved?; false; end
|
|
|
|
def secure_category_ids; []; end
|
2013-07-14 09:24:16 +08:00
|
|
|
def topic_create_allowed_category_ids; []; end
|
2013-05-20 14:04:53 +08:00
|
|
|
def has_trust_level?(level); false; end
|
2013-09-05 08:27:34 +08:00
|
|
|
def email; nil; end
|
2013-05-20 14:04:53 +08:00
|
|
|
end
|
2014-05-12 22:30:10 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def initialize(user=nil)
|
2013-05-20 14:04:53 +08:00
|
|
|
@user = user.presence || AnonymousUser.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def user
|
|
|
|
@user.presence
|
|
|
|
end
|
|
|
|
alias :current_user :user
|
|
|
|
|
|
|
|
def anonymous?
|
|
|
|
!authenticated?
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-20 14:04:53 +08:00
|
|
|
def authenticated?
|
|
|
|
@user.present?
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_admin?
|
2013-05-20 14:04:53 +08:00
|
|
|
@user.admin?
|
2013-04-29 14:33:24 +08:00
|
|
|
end
|
|
|
|
|
2013-05-02 15:22:27 +08:00
|
|
|
def is_staff?
|
2013-05-20 14:04:53 +08:00
|
|
|
@user.staff?
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-02-07 11:24:19 +08:00
|
|
|
def is_moderator?
|
|
|
|
@user.moderator?
|
|
|
|
end
|
|
|
|
|
2013-09-05 08:27:34 +08:00
|
|
|
def is_developer?
|
|
|
|
@user &&
|
|
|
|
is_admin? &&
|
2013-09-06 12:07:23 +08:00
|
|
|
(Rails.env.development? ||
|
|
|
|
(
|
|
|
|
Rails.configuration.respond_to?(:developer_emails) &&
|
|
|
|
Rails.configuration.developer_emails.include?(@user.email)
|
|
|
|
)
|
|
|
|
)
|
2013-09-05 08:27:34 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Can the user see the object?
|
|
|
|
def can_see?(obj)
|
2013-05-20 14:04:53 +08:00
|
|
|
if obj
|
|
|
|
see_method = method_name_for :see, obj
|
|
|
|
return (see_method ? send(see_method, obj) : true)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-03-18 02:50:28 +08:00
|
|
|
def can_create?(klass, parent=nil)
|
|
|
|
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}?"
|
|
|
|
|
|
|
|
return send(create_method, parent) if respond_to?(create_method)
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Can the user edit the obj
|
|
|
|
def can_edit?(obj)
|
2013-08-16 20:24:29 +08:00
|
|
|
can_do?(:edit, obj)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Can we delete the object
|
|
|
|
def can_delete?(obj)
|
2013-08-16 20:24:29 +08:00
|
|
|
can_do?(:delete, obj)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_moderate?(obj)
|
2014-09-05 13:20:39 +08:00
|
|
|
obj && authenticated? && (is_staff? || (obj.is_a?(Topic) && @user.has_trust_level?(TrustLevel[4])))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
alias :can_move_posts? :can_moderate?
|
2013-02-06 03:16:51 +08:00
|
|
|
alias :can_see_flags? :can_moderate?
|
2013-05-08 09:58:34 +08:00
|
|
|
alias :can_send_activation_email? :can_moderate?
|
2014-09-03 04:12:27 +08:00
|
|
|
|
|
|
|
def can_grant_badges?(_user)
|
|
|
|
SiteSetting.enable_badges && is_staff?
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-04-23 04:43:46 +08:00
|
|
|
def can_see_group?(group)
|
2014-04-23 23:14:34 +08:00
|
|
|
group.present? && (is_admin? || group.visible?)
|
2014-04-23 04:43:46 +08:00
|
|
|
end
|
|
|
|
|
2014-01-17 00:59:26 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
# Can we impersonate this user?
|
|
|
|
def can_impersonate?(target)
|
2013-05-20 14:04:53 +08:00
|
|
|
target &&
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
# You must be an admin to impersonate
|
2013-05-20 14:04:53 +08:00
|
|
|
is_admin? &&
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-09-05 08:27:34 +08:00
|
|
|
# You may not impersonate other admins unless you are a dev
|
|
|
|
(!target.admin? || is_developer?)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-05-20 14:04:53 +08: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-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Can we approve it?
|
|
|
|
def can_approve?(target)
|
2013-05-20 14:04:53 +08:00
|
|
|
is_staff? && target && not(target.approved?)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2014-04-29 01:46:28 +08:00
|
|
|
|
|
|
|
def can_activate?(target)
|
|
|
|
is_staff? && target && not(target.active?)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
def can_suspend?(user)
|
2013-05-25 00:13:31 +08:00
|
|
|
user && is_staff? && user.regular?
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-11-08 02:53:32 +08:00
|
|
|
alias :can_deactivate? :can_suspend?
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def can_revoke_admin?(admin)
|
2013-05-20 14:04:53 +08:00
|
|
|
can_administer_user?(admin) && admin.admin?
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_grant_admin?(user)
|
2013-05-20 14:04:53 +08:00
|
|
|
can_administer_user?(user) && not(user.admin?)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-13 06:58:08 +08:00
|
|
|
def can_revoke_moderation?(moderator)
|
2013-05-20 14:04:53 +08:00
|
|
|
can_administer?(moderator) && moderator.moderator?
|
2013-02-13 06:58:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_grant_moderation?(user)
|
2013-05-20 14:04:53 +08:00
|
|
|
can_administer?(user) && not(user.moderator?)
|
2013-02-13 06:58:08 +08:00
|
|
|
end
|
|
|
|
|
2013-06-26 06:39:20 +08:00
|
|
|
def can_grant_title?(user)
|
|
|
|
user && is_staff?
|
|
|
|
end
|
|
|
|
|
2014-02-11 05:59:36 +08:00
|
|
|
def can_change_primary_group?(user)
|
|
|
|
user && is_staff?
|
|
|
|
end
|
|
|
|
|
2013-07-03 16:27:40 +08:00
|
|
|
def can_change_trust_level?(user)
|
2013-07-23 07:13:48 +08:00
|
|
|
user && is_staff?
|
2013-07-03 16:27:40 +08:00
|
|
|
end
|
|
|
|
|
2013-04-04 00:23:28 +08:00
|
|
|
# Support sites that have to approve users
|
|
|
|
def can_access_forum?
|
|
|
|
return true unless SiteSetting.must_approve_users?
|
2013-05-02 15:22:27 +08:00
|
|
|
return false unless @user
|
2013-04-04 00:23:28 +08:00
|
|
|
|
2013-05-02 15:22:27 +08:00
|
|
|
# Staff can't lock themselves out of a site
|
|
|
|
return true if is_staff?
|
2013-04-04 00:23:28 +08:00
|
|
|
|
2013-05-02 15:22:27 +08:00
|
|
|
@user.approved?
|
2013-04-04 00:23:28 +08:00
|
|
|
end
|
|
|
|
|
2014-03-22 02:13:04 +08:00
|
|
|
def can_see_invite_details?(user)
|
2013-05-20 14:04:53 +08:00
|
|
|
is_me?(user)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-05-09 16:22:15 +08:00
|
|
|
def can_invite_to_forum?(groups=nil)
|
2013-06-21 14:35:13 +08:00
|
|
|
authenticated? &&
|
2014-04-22 07:17:37 +08:00
|
|
|
!SiteSetting.enable_sso &&
|
2014-06-19 04:46:04 +08:00
|
|
|
SiteSetting.enable_local_logins &&
|
2013-06-21 14:35:13 +08:00
|
|
|
(
|
2014-09-05 13:20:39 +08:00
|
|
|
(!SiteSetting.must_approve_users? && @user.has_trust_level?(TrustLevel[2])) ||
|
2013-06-21 14:35:13 +08:00
|
|
|
is_staff?
|
2014-05-09 16:22:15 +08:00
|
|
|
) &&
|
|
|
|
(groups.blank? || is_admin?)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-05-09 09:45:18 +08:00
|
|
|
def can_invite_to?(object, group_ids=nil)
|
2014-07-05 01:34:19 +08:00
|
|
|
can_invite = can_see?(object) && can_invite_to_forum? && ( group_ids.blank? || is_admin? )
|
2014-07-05 14:56:26 +08:00
|
|
|
#TODO how should invite to PM work?
|
|
|
|
can_invite = can_invite && ( !object.category.read_restricted || is_admin? ) if object.is_a?(Topic) && object.category
|
2014-07-05 01:34:19 +08:00
|
|
|
can_invite
|
2013-11-07 01:56:26 +08:00
|
|
|
end
|
|
|
|
|
2014-05-28 04:14:37 +08:00
|
|
|
def can_bulk_invite_to_forum?(user)
|
|
|
|
user.admin?
|
|
|
|
end
|
|
|
|
|
2014-07-14 23:56:26 +08:00
|
|
|
def can_create_disposable_invite?(user)
|
|
|
|
user.admin?
|
|
|
|
end
|
|
|
|
|
2014-07-30 01:57:08 +08:00
|
|
|
def can_send_multiple_invites?(user)
|
|
|
|
user.staff?
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def can_see_private_messages?(user_id)
|
2014-02-07 11:24:19 +08:00
|
|
|
is_admin? || (authenticated? && @user.id == user_id)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-02 13:15:17 +08:00
|
|
|
def can_send_private_message?(target)
|
2014-03-27 03:20:41 +08:00
|
|
|
(target.is_a?(Group) || target.is_a?(User)) &&
|
2014-02-13 12:08:46 +08:00
|
|
|
# User is authenticated
|
2013-05-20 14:04:53 +08:00
|
|
|
authenticated? &&
|
2013-02-26 00:42:20 +08:00
|
|
|
# Can't send message to yourself
|
2013-05-20 14:04:53 +08:00
|
|
|
is_not_me?(target) &&
|
2013-02-06 03:16:51 +08:00
|
|
|
# Have to be a basic level at least
|
2014-09-05 13:20:39 +08:00
|
|
|
@user.has_trust_level?(TrustLevel[1]) &&
|
2014-02-13 12:08:46 +08:00
|
|
|
# PMs are enabled
|
2014-04-26 02:52:57 +08:00
|
|
|
(SiteSetting.enable_private_messages ||
|
|
|
|
@user.username == SiteSetting.site_contact_username ||
|
2014-05-07 03:01:19 +08:00
|
|
|
@user == Discourse.system_user) &&
|
|
|
|
# Can't send PMs to suspended users
|
|
|
|
(is_staff? || target.is_a?(Group) || !target.suspended?)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-05-20 14:04:53 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def is_my_own?(obj)
|
2013-10-04 11:28:49 +08:00
|
|
|
|
|
|
|
unless anonymous?
|
2013-10-04 15:00:23 +08:00
|
|
|
return obj.user_id == @user.id if obj.respond_to?(:user_id) && obj.user_id && @user.id
|
2013-10-04 11:28:49 +08:00
|
|
|
return obj.user == @user if obj.respond_to?(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2013-05-20 14:04:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_me?(other)
|
2014-03-27 03:20:41 +08:00
|
|
|
other && authenticated? && other.is_a?(User) && @user == other
|
2013-04-29 14:33:24 +08:00
|
|
|
end
|
2013-05-20 14:04:53 +08:00
|
|
|
|
|
|
|
def is_not_me?(other)
|
|
|
|
@user.blank? || !is_me?(other)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_administer?(obj)
|
|
|
|
is_admin? && obj.present?
|
|
|
|
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}?"
|
|
|
|
return method_name if respond_to?(method_name)
|
|
|
|
end
|
|
|
|
|
2013-08-16 20:24:29 +08:00
|
|
|
def can_do?(action, obj)
|
|
|
|
if obj && authenticated?
|
|
|
|
action_method = method_name_for action, obj
|
|
|
|
return (action_method ? send(action_method, obj) : true)
|
2014-01-17 00:59:26 +08:00
|
|
|
else
|
|
|
|
false
|
2013-08-16 20:24:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|