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? ||
|
|
|
|
(
|
|
|
|
SiteSetting.allow_moderators_to_create_categories &&
|
|
|
|
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? ||
|
|
|
|
(
|
|
|
|
SiteSetting.allow_moderators_to_create_categories &&
|
|
|
|
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-02-13 06:24:25 +08:00
|
|
|
category.topic_count == 0 &&
|
|
|
|
!category.uncategorized? &&
|
|
|
|
!category.has_children?
|
2014-01-10 07:25:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
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-07 11:11:52 +08:00
|
|
|
end
|