2014-01-09 18:25:14 -05:00
|
|
|
require_dependency 'guardian/category_guardian'
|
|
|
|
require_dependency 'guardian/ensure_magic'
|
|
|
|
require_dependency 'guardian/post_guardian'
|
|
|
|
require_dependency 'guardian/topic_guardian'
|
2013-02-06 14:46:45 -05:00
|
|
|
# The guardian is responsible for confirming access to various site resources and operations
|
2013-02-05 14:16:51 -05:00
|
|
|
class Guardian
|
2014-01-09 18:25:14 -05:00
|
|
|
include EnsureMagic
|
|
|
|
include CategoryGuardian
|
|
|
|
include PostGuardain
|
|
|
|
include TopicGuardian
|
2014-02-04 14:05:50 -05:00
|
|
|
|
2013-05-20 02:04:53 -04:00
|
|
|
class AnonymousUser
|
|
|
|
def blank?; true; end
|
|
|
|
def admin?; false; end
|
|
|
|
def staff?; false; end
|
2014-02-06 22:24:19 -05:00
|
|
|
def moderator?; false; end
|
2013-05-20 02:04:53 -04:00
|
|
|
def approved?; false; end
|
|
|
|
def secure_category_ids; []; end
|
2013-07-13 21:24:16 -04:00
|
|
|
def topic_create_allowed_category_ids; []; end
|
2013-05-20 02:04:53 -04:00
|
|
|
def has_trust_level?(level); false; end
|
2013-09-04 20:27:34 -04:00
|
|
|
def email; nil; end
|
2013-05-20 02:04:53 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
def initialize(user=nil)
|
2013-05-20 02:04:53 -04:00
|
|
|
@user = user.presence || AnonymousUser.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def user
|
|
|
|
@user.presence
|
|
|
|
end
|
|
|
|
alias :current_user :user
|
|
|
|
|
|
|
|
def anonymous?
|
|
|
|
!authenticated?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-20 02:04:53 -04:00
|
|
|
def authenticated?
|
|
|
|
@user.present?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_admin?
|
2013-05-20 02:04:53 -04:00
|
|
|
@user.admin?
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
|
|
|
|
2013-05-02 03:22:27 -04:00
|
|
|
def is_staff?
|
2013-05-20 02:04:53 -04:00
|
|
|
@user.staff?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-02-06 22:24:19 -05:00
|
|
|
def is_moderator?
|
|
|
|
@user.moderator?
|
|
|
|
end
|
|
|
|
|
2013-09-04 20:27:34 -04:00
|
|
|
def is_developer?
|
|
|
|
@user &&
|
|
|
|
is_admin? &&
|
2013-09-06 00:07:23 -04:00
|
|
|
(Rails.env.development? ||
|
|
|
|
(
|
|
|
|
Rails.configuration.respond_to?(:developer_emails) &&
|
|
|
|
Rails.configuration.developer_emails.include?(@user.email)
|
|
|
|
)
|
|
|
|
)
|
2013-09-04 20:27:34 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Can the user see the object?
|
|
|
|
def can_see?(obj)
|
2013-05-20 02:04:53 -04:00
|
|
|
if obj
|
|
|
|
see_method = method_name_for :see, obj
|
|
|
|
return (see_method ? send(see_method, obj) : true)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Can the user edit the obj
|
|
|
|
def can_edit?(obj)
|
2013-08-16 08:24:29 -04:00
|
|
|
can_do?(:edit, obj)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Can we delete the object
|
|
|
|
def can_delete?(obj)
|
2013-08-16 08:24:29 -04:00
|
|
|
can_do?(:delete, obj)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_moderate?(obj)
|
2013-05-20 02:04:53 -04:00
|
|
|
obj && is_staff?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
alias :can_move_posts? :can_moderate?
|
2013-02-05 14:16:51 -05:00
|
|
|
alias :can_see_flags? :can_moderate?
|
2013-05-07 21:58:34 -04:00
|
|
|
alias :can_send_activation_email? :can_moderate?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-01-16 11:59:26 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# Can we impersonate this user?
|
|
|
|
def can_impersonate?(target)
|
2013-05-20 02:04:53 -04:00
|
|
|
target &&
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# You must be an admin to impersonate
|
2013-05-20 02:04:53 -04:00
|
|
|
is_admin? &&
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-09-04 20:27:34 -04:00
|
|
|
# You may not impersonate other admins unless you are a dev
|
|
|
|
(!target.admin? || is_developer?)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-20 02:04:53 -04: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 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Can we approve it?
|
|
|
|
def can_approve?(target)
|
2013-05-20 02:04:53 -04:00
|
|
|
is_staff? && target && not(target.approved?)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-07 21:58:34 -04:00
|
|
|
alias :can_activate? :can_approve?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-11-07 13:53:32 -05:00
|
|
|
def can_suspend?(user)
|
2013-05-24 12:13:31 -04:00
|
|
|
user && is_staff? && user.regular?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-11-07 13:53:32 -05:00
|
|
|
alias :can_deactivate? :can_suspend?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def can_revoke_admin?(admin)
|
2013-05-20 02:04:53 -04:00
|
|
|
can_administer_user?(admin) && admin.admin?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_grant_admin?(user)
|
2013-05-20 02:04:53 -04:00
|
|
|
can_administer_user?(user) && not(user.admin?)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-12 17:58:08 -05:00
|
|
|
def can_revoke_moderation?(moderator)
|
2013-05-20 02:04:53 -04:00
|
|
|
can_administer?(moderator) && moderator.moderator?
|
2013-02-12 17:58:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_grant_moderation?(user)
|
2013-05-20 02:04:53 -04:00
|
|
|
can_administer?(user) && not(user.moderator?)
|
2013-02-12 17:58:08 -05:00
|
|
|
end
|
|
|
|
|
2013-06-25 18:39:20 -04:00
|
|
|
def can_grant_title?(user)
|
|
|
|
user && is_staff?
|
|
|
|
end
|
|
|
|
|
2014-02-10 16:59:36 -05:00
|
|
|
def can_change_primary_group?(user)
|
|
|
|
user && is_staff?
|
|
|
|
end
|
|
|
|
|
2013-07-03 04:27:40 -04:00
|
|
|
def can_change_trust_level?(user)
|
2013-07-22 19:13:48 -04:00
|
|
|
user && is_staff?
|
2013-07-03 04:27:40 -04:00
|
|
|
end
|
|
|
|
|
2013-05-31 11:41:40 -04:00
|
|
|
def can_block_user?(user)
|
|
|
|
user && is_staff? && not(user.staff?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_unblock_user?(user)
|
|
|
|
user && is_staff?
|
|
|
|
end
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
def can_delete_user?(user)
|
2013-07-26 15:40:08 -04:00
|
|
|
user && is_staff? && !user.admin? && user.created_at > SiteSetting.delete_user_max_age.to_i.days.ago
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2013-04-03 12:23:28 -04:00
|
|
|
# Support sites that have to approve users
|
|
|
|
def can_access_forum?
|
|
|
|
return true unless SiteSetting.must_approve_users?
|
2013-05-02 03:22:27 -04:00
|
|
|
return false unless @user
|
2013-04-03 12:23:28 -04:00
|
|
|
|
2013-05-02 03:22:27 -04:00
|
|
|
# Staff can't lock themselves out of a site
|
|
|
|
return true if is_staff?
|
2013-04-03 12:23:28 -04:00
|
|
|
|
2013-05-02 03:22:27 -04:00
|
|
|
@user.approved?
|
2013-04-03 12:23:28 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def can_see_pending_invites_from?(user)
|
2013-05-20 02:04:53 -04:00
|
|
|
is_me?(user)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-06 12:56:26 -05:00
|
|
|
def can_invite_to_forum?
|
2013-06-21 02:35:13 -04:00
|
|
|
authenticated? &&
|
|
|
|
(
|
|
|
|
(!SiteSetting.must_approve_users? && @user.has_trust_level?(:regular)) ||
|
|
|
|
is_staff?
|
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-11-06 12:56:26 -05:00
|
|
|
def can_invite_to?(object)
|
|
|
|
can_see?(object) && can_invite_to_forum?
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def can_see_private_messages?(user_id)
|
2014-02-06 22:24:19 -05:00
|
|
|
is_admin? || (authenticated? && @user.id == user_id)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_edit_user?(user)
|
2013-05-20 02:04:53 -04:00
|
|
|
is_me?(user) || is_staff?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-08-12 14:54:52 -04:00
|
|
|
def can_edit_username?(user)
|
2013-09-07 22:42:41 -04:00
|
|
|
return true if is_staff?
|
|
|
|
return false if SiteSetting.username_change_period <= 0
|
|
|
|
is_me?(user) && (user.post_count == 0 || user.created_at > SiteSetting.username_change_period.days.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_edit_email?(user)
|
|
|
|
return true if is_staff?
|
|
|
|
return false unless SiteSetting.email_editable?
|
|
|
|
can_edit?(user)
|
2013-08-12 14:54:52 -04:00
|
|
|
end
|
|
|
|
|
2013-05-02 01:15:17 -04:00
|
|
|
def can_send_private_message?(target)
|
2013-05-20 02:04:53 -04:00
|
|
|
(User === target || Group === target) &&
|
|
|
|
authenticated? &&
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
# Can't send message to yourself
|
2013-05-20 02:04:53 -04:00
|
|
|
is_not_me?(target) &&
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# Have to be a basic level at least
|
2013-05-20 02:04:53 -04:00
|
|
|
@user.has_trust_level?(:basic) &&
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
SiteSetting.enable_private_messages
|
|
|
|
end
|
|
|
|
|
2013-05-20 02:04:53 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def is_my_own?(obj)
|
2013-10-03 23:28:49 -04:00
|
|
|
|
|
|
|
unless anonymous?
|
2013-10-04 03:00:23 -04:00
|
|
|
return obj.user_id == @user.id if obj.respond_to?(:user_id) && obj.user_id && @user.id
|
2013-10-03 23:28:49 -04:00
|
|
|
return obj.user == @user if obj.respond_to?(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
2013-05-20 02:04:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_me?(other)
|
|
|
|
other && authenticated? && User === other && @user == other
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
2013-05-20 02:04:53 -04: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 08:24:29 -04: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-16 11:59:26 -05:00
|
|
|
else
|
|
|
|
false
|
2013-08-16 08:24:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|