2014-01-09 18:25:14 -05:00
|
|
|
#mixin for all guardian methods dealing with category permisions
|
|
|
|
module CategoryGuardian
|
2014-04-15 02:49:22 -04:00
|
|
|
|
2014-01-09 18:25:14 -05:00
|
|
|
# Creating Method
|
2014-04-15 02:49:22 -04:00
|
|
|
def can_create_category?(parent=nil)
|
|
|
|
is_admin? ||
|
|
|
|
(
|
|
|
|
SiteSetting.allow_moderators_to_create_categories &&
|
|
|
|
is_moderator?
|
|
|
|
)
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Editing Method
|
|
|
|
def can_edit_category?(category)
|
2014-04-15 02:49:22 -04:00
|
|
|
is_admin? ||
|
|
|
|
(
|
|
|
|
SiteSetting.allow_moderators_to_create_categories &&
|
|
|
|
is_moderator? &&
|
|
|
|
can_see_category?(category)
|
|
|
|
)
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_delete_category?(category)
|
2014-04-15 02:49:22 -04:00
|
|
|
can_edit_category?(category) &&
|
2014-02-12 17:24:25 -05:00
|
|
|
category.topic_count == 0 &&
|
|
|
|
!category.uncategorized? &&
|
|
|
|
!category.has_children?
|
2014-01-09 18:25:14 -05:00
|
|
|
end
|
|
|
|
|
2014-07-16 15:43:44 -04: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
|
|
|
|
return I18n.t('category.cannot_delete.topic_exists', {count: category.topic_count, topic_link: "<a href=\"#{oldest_topic.url}\">#{oldest_topic.title}</a>"})
|
|
|
|
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-09 18:25:14 -05:00
|
|
|
def can_see_category?(category)
|
|
|
|
not(category.read_restricted) || secure_category_ids.include?(category.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def secure_category_ids
|
|
|
|
@secure_category_ids ||= @user.secure_category_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
# all allowed category ids
|
|
|
|
def allowed_category_ids
|
|
|
|
unrestricted = Category.where(read_restricted: false).pluck(:id)
|
|
|
|
unrestricted.concat(secure_category_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_create_allowed_category_ids
|
|
|
|
@topic_create_allowed_category_ids ||= @user.topic_create_allowed_category_ids
|
|
|
|
end
|
2014-02-06 22:11:52 -05:00
|
|
|
end
|