2019-05-03 08:17:27 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-05-20 21:43:47 -04:00
|
|
|
#mixin for all guardian methods dealing with category permissions
|
2014-01-09 17:25:14 -06:00
|
|
|
module CategoryGuardian
|
|
|
|
# Creating Method
|
2017-07-28 10:20:09 +09:00
|
|
|
def can_create_category?(parent = nil)
|
2023-01-09 12:10:19 +00:00
|
|
|
is_admin? || (SiteSetting.moderators_manage_categories_and_groups && is_moderator?)
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# Editing Method
|
|
|
|
def can_edit_category?(category)
|
2014-04-15 16:49:22 +10:00
|
|
|
is_admin? ||
|
2023-01-09 12:10:19 +00:00
|
|
|
(
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups && is_moderator? &&
|
|
|
|
can_see_category?(category)
|
|
|
|
)
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
2021-06-28 10:39:13 +08:00
|
|
|
def can_edit_serialized_category?(category_id:, read_restricted:)
|
|
|
|
is_admin? ||
|
2023-01-09 12:10:19 +00:00
|
|
|
(
|
|
|
|
SiteSetting.moderators_manage_categories_and_groups && is_moderator? &&
|
|
|
|
can_see_serialized_category?(category_id: category_id, read_restricted: read_restricted)
|
|
|
|
)
|
2021-06-28 10:39:13 +08:00
|
|
|
end
|
|
|
|
|
2014-01-09 17:25:14 -06:00
|
|
|
def can_delete_category?(category)
|
2023-01-09 12:10:19 +00:00
|
|
|
can_edit_category?(category) && category.topic_count <= 0 && !category.uncategorized? &&
|
|
|
|
!category.has_children?
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
2021-06-23 15:21:11 +08:00
|
|
|
def can_see_serialized_category?(category_id:, read_restricted: true)
|
|
|
|
# Guard to ensure only a boolean is passed in
|
|
|
|
read_restricted = true unless !!read_restricted == read_restricted
|
|
|
|
|
|
|
|
return true if !read_restricted
|
|
|
|
secure_category_ids.include?(category_id)
|
|
|
|
end
|
|
|
|
|
2014-01-09 17:25:14 -06:00
|
|
|
def can_see_category?(category)
|
2016-07-02 12:21:14 +02:00
|
|
|
return false unless category
|
2016-06-27 14:36:57 +02: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 11:30:17 +01:00
|
|
|
secure_category_ids.include?(category.id)
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
2022-12-19 11:35:28 +11:00
|
|
|
def can_post_in_category?(category)
|
|
|
|
return false unless category
|
|
|
|
return false if is_anonymous?
|
|
|
|
return true if is_admin?
|
|
|
|
Category.post_create_allowed(self).exists?(id: category.id)
|
|
|
|
end
|
|
|
|
|
2020-07-23 09:50:00 -04:00
|
|
|
def can_edit_category_description?(category)
|
|
|
|
can_perform_action_available_to_group_moderators?(category.topic)
|
|
|
|
end
|
|
|
|
|
2014-01-09 17:25:14 -06:00
|
|
|
def secure_category_ids
|
|
|
|
@secure_category_ids ||= @user.secure_category_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
# all allowed category ids
|
|
|
|
def allowed_category_ids
|
2015-09-23 13:13:34 +10:00
|
|
|
@allowed_category_ids ||=
|
|
|
|
begin
|
|
|
|
unrestricted = Category.where(read_restricted: false).pluck(:id)
|
|
|
|
unrestricted.concat(secure_category_ids)
|
|
|
|
end
|
2014-01-09 17:25:14 -06:00
|
|
|
end
|
|
|
|
|
2016-12-05 13:31:43 +01:00
|
|
|
def topic_featured_link_allowed_category_ids
|
2023-01-09 12:10:19 +00:00
|
|
|
@topic_featured_link_allowed_category_ids =
|
|
|
|
Category.where(topic_featured_link_allowed: true).pluck(:id)
|
2016-12-05 13:31:43 +01:00
|
|
|
end
|
2014-02-07 14:11:52 +11:00
|
|
|
end
|