2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
#mixin for all guardian methods dealing with category permisions
|
|
|
|
module CategoryGuardian
|
2014-04-15 14:49:22 +08:00
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
# Creating Method
|
2014-04-15 14:49:22 +08:00
|
|
|
def can_create_category?(parent = nil)
|
|
|
|
is_admin? ||
|
|
|
|
(
|
2020-08-19 22:41:40 +08:00
|
|
|
SiteSetting.moderators_manage_categories_and_groups &&
|
2014-04-15 14:49:22 +08:00
|
|
|
is_moderator?
|
|
|
|
)
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Editing Method
|
|
|
|
def can_edit_category?(category)
|
2014-04-15 14:49:22 +08:00
|
|
|
is_admin? ||
|
|
|
|
(
|
2020-08-19 22:41:40 +08:00
|
|
|
SiteSetting.moderators_manage_categories_and_groups &&
|
2014-04-15 14:49:22 +08:00
|
|
|
is_moderator? &&
|
|
|
|
can_see_category?(category)
|
|
|
|
)
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_category?(category)
|
2014-04-15 14:49:22 +08:00
|
|
|
can_edit_category?(category) &&
|
2014-08-23 00:25:22 +08:00
|
|
|
category.topic_count <= 0 &&
|
2014-02-13 06:24:25 +08:00
|
|
|
!category.uncategorized? &&
|
|
|
|
!category.has_children?
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
2014-07-17 03:43:44 +08:00
|
|
|
def cannot_delete_category_reason(category)
|
|
|
|
return I18n.t('category.cannot_delete.uncategorized') if category.uncategorized?
|
|
|
|
return I18n.t('category.cannot_delete.has_subcategories') if category.has_children?
|
|
|
|
|
|
|
|
if category.topic_count != 0
|
|
|
|
oldest_topic = category.topics.where.not(id: category.topic_id).order('created_at ASC').limit(1).first
|
|
|
|
if oldest_topic
|
2018-04-15 21:14:28 +08:00
|
|
|
return I18n.t('category.cannot_delete.topic_exists', count: category.topic_count, topic_link: "<a href=\"#{oldest_topic.url}\">#{CGI.escapeHTML(oldest_topic.title)}</a>")
|
2014-07-17 03:43:44 +08:00
|
|
|
else
|
|
|
|
# This is a weird case, probably indicating a bug.
|
|
|
|
return I18n.t('category.cannot_delete.topic_exists_no_oldest', count: category.topic_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
def can_see_category?(category)
|
2016-07-02 18:21:14 +08:00
|
|
|
return false unless category
|
2016-06-27 20:36:57 +08:00
|
|
|
return true if is_admin?
|
|
|
|
return true if !category.read_restricted
|
|
|
|
return true if is_staged? && category.email_in.present? && category.email_in_allow_strangers
|
2016-02-24 18:30:17 +08:00
|
|
|
secure_category_ids.include?(category.id)
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
2020-07-23 21:50:00 +08:00
|
|
|
def can_edit_category_description?(category)
|
|
|
|
can_perform_action_available_to_group_moderators?(category.topic)
|
|
|
|
end
|
|
|
|
|
2014-01-10 07:25:14 +08:00
|
|
|
def secure_category_ids
|
|
|
|
@secure_category_ids ||= @user.secure_category_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
# all allowed category ids
|
|
|
|
def allowed_category_ids
|
2015-09-23 11:13:34 +08:00
|
|
|
@allowed_category_ids ||=
|
|
|
|
begin
|
|
|
|
unrestricted = Category.where(read_restricted: false).pluck(:id)
|
|
|
|
unrestricted.concat(secure_category_ids)
|
|
|
|
end
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def topic_create_allowed_category_ids
|
|
|
|
@topic_create_allowed_category_ids ||= @user.topic_create_allowed_category_ids
|
|
|
|
end
|
2016-12-05 20:31:43 +08:00
|
|
|
|
|
|
|
def topic_featured_link_allowed_category_ids
|
2016-12-16 06:46:43 +08:00
|
|
|
@topic_featured_link_allowed_category_ids = Category.where(topic_featured_link_allowed: true).pluck(:id)
|
2016-12-05 20:31:43 +08:00
|
|
|
end
|
2014-02-07 11:11:52 +08:00
|
|
|
end
|