discourse/lib/guardian/category_guardian.rb
Patrick ffb29dea77 Refactor guardian as dissused in this topic https://meta.discourse.org/t/so-you-want-to-help-out-with-discourse/3823/41?u=hunter
Creates a mixin for the ensure_* functions and creates seperate mixins for functions dealing with posts, categories, and topics.
2014-01-10 21:22:54 -06:00

34 lines
871 B
Ruby

#mixin for all guardian methods dealing with category permisions
module CategoryGuardian
# Creating Method
def can_create_category?(parent)
is_staff?
end
# Editing Method
def can_edit_category?(category)
is_staff?
end
def can_delete_category?(category)
is_staff? && category.topic_count == 0 && !category.uncatgorized?
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
end