How to customize / override topic guardian through a plugin

I am getting a plugin developed that will add a boolean value based on some custom logic to the user_custom_fields table for each user who is not allowed to create a new topic.

user.custom_fields['can_create_topic'] = false;

What I want to do?
I want to hide the “+ New Topic” button for some users based on custom logic.

Is it possible to customize can_create_topic? guardian so that it performs an additional check on a user_custom_field to determine if a user is allowed to create a new topic or not?

1 Like

Yes, of course you can modify Ruby classes with plugins. It’s software, easily modified, not intergalactic space travel using solar sails or worm-holes, so of course it’s possible to modify Discourse behavior with a plugin:

I don’t know your use case, but the easy and safe way to do that would be to add or remove those users from a group with the permission to create a topic.


@pfaffman That is a good idea too. But I think if I can override the can_create_topic? method in the TopicGuardian that would be a cleaner solution. Is there any plugin I can refer to that has a similar change?

What I don’t know is how can I make my TopicGuardian subclass from the plugin to replace TopicGuardian in the Guardian class because it is being mixed in rather than dependency injected.

Okay. I think I found my answer. I just have to implement the “can_create_topic?” method in module ::TopicGuardian and it will automatically change TopicGuardian’s behavior without requiring to inject or registering it somewhere. Also, I can use super method inside my override method so that I don’t have to always manually keep it in sync with the original method in case that gets changed.

Got help from this topic: Overriding user_guardian.rb in a plugin (no fork necessary!)

1 Like