This hook wraps the standard function that Misago uses to check if the user has a permission to start bew private threads. Raises Django's PermissionDenied with an error message if they don't.
This hook can be imported from misago.permissions.hooks:
from misago.permissions.hooks import check_start_private_threads_permission_hookdef custom_check_start_private_threads_permission_filter(
action: CheckStartPrivateThreadsPermissionHookAction,
permissions: 'UserPermissionsProxy',
) -> None:
...A function implemented by a plugin that can be registered in this hook.
Next function registered in this hook, either a custom function or Misago's standard one.
See the action section for details.
A proxy object with the current user's permissions.
def check_start_private_threads_permission_action(permissions: 'UserPermissionsProxy') -> None:
...Misago function used to check if the user has a permission to start new private threads. Raises Django's PermissionDenied with an error message if they don't.
A proxy object with the current user's permissions.
The code below implements a custom filter function that blocks user from starting new private threads if there's a custom flag set on their account.
from django.core.exceptions import PermissionDenied
from django.utils.translation import pgettext
from misago.permissions.hooks import check_start_private_threads_permission_hook
from misago.permissions.proxy import UserPermissionsProxy
@check_start_private_threads_permission_hook.append_filter
def check_user_is_banned_from_starting_private_threads(
action,
permissions: UserPermissionsProxy,
) -> None:
# Run standard permission checks
action(permissions)
if permissions.user.plugin_data.get("ban_start_private_threads"):
raise PermissionDenied(
pgettext(
"private threads permission error",
"Site admin has removed your option to start private threads."
)
)