Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add before and after guardian checks #26936

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

angusmcleod
Copy link
Contributor

Adds a system to allow plugins to more easily modify guardian can_ method outcomes.

Example

Currently the only way to modify guardian can_ method outcomes is via module pre-pension, for example:

module DiscourseActivityPubGuardianExtension
  def can_edit_post?(post)
    return false if post.activity_pub_remote?
    super
  end

  def can_change_post_owner?
    return false if activity_pub_enabled_topic?
    super
  end

  def activity_pub_enabled_topic?
    return false unless DiscourseActivityPub.enabled && request&.params&.[]("topic_id")
    topic = Topic.find_by(id: request.params["topic_id"].to_i)
    topic&.activity_pub_enabled
  end
end
...
Guardian.prepend DiscourseActivityPubGuardianExtension

See further

This change would mean the above could be done via the plugin api like so:

add_guardian_check(:before, :edit_post) do |guardian, result, post|
  !post.activity_pub_remote?
end

add_guardian_check(:before, :change_post_owner) do |guardian|
  return true unless DiscourseActivityPub.enabled && guardian.request&.params&.[]("topic_id")
  topic = Topic.find_by(id: guardian.request.params["topic_id"].to_i)
  !topic&.activity_pub_enabled
end

@discoursebot
Copy link

This pull request has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/guardian-before-and-after-checks-api/307241/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants