API

Auth

alliance_platform.core.auth.resolve_perm_name(entity: Model | type[Model], action: str, is_global: bool) str[source]
alliance_platform.core.auth.resolve_perm_name(entity: AppConfig, action: str, is_global: bool) str

Resolve a permission name for an app or model, and an action.

This calls the function set in RESOLVE_PERM_NAME, which defaults to default_resolve_perm_name().

This method can be used when writing generic code that needs to generate a default permission name according to a convention. For example, on generic CRUD views.

Usage for model permissions:

>>> resolve_perm_name(MyModel, "list", True)
"myapp.mymodel_list"
>>> record = MyModel.objects.get(pk=5)
>>> resolve_perm_name(record, "update", False)
"myapp.mymodel_update"

For permissions that are not specific to a model, pass an AppConfig instead of a model:

>>> from django.apps import apps
>>> resolve_perm_name(apps.get_app_config("MyApp"), "dashboard", True)
"myapp.dashboard"

Note

The default implementation makes no use of the is_global parameter, but custom implementations may so it is required.

Parameters:
  • entity – This will either by an AppConfig, or a Model class or instance. All permissions are scoped to an app; if a model is passed the config will be extracted from the model.

  • action – The action to perform. For example, common ones for a model include "create", "update", "detail", "list", "delete", but it can be anything.

  • is_global – Whether the permission is global (True) or per-object (False).

alliance_platform.core.auth.default_resolve_perm_name(app_config, model, action, is_global)[source]

Default implementation of resolve_perm_name().

Note

Don’t call this directly; use resolve_perm_name() which will call the function set in RESOLVE_PERM_NAME (which defaults to default_resolve_perm_name).

Returns strings in the following format:

With a model: {app_config_label}.{model_name}_{action}, e.g. "myapp.mymodel_list" Without a model: {app_config_label}.{action}, e.g. "myapp.management"

We follow a different convention to django (model_action rather than action_model) so that permission names sort lexicographically.

The default implementation makes no use of the is_global parameter.

Parameters:
  • app_config (AppConfig) – The app config the permission is for. All permissions are scoped to an app. If model, you can pass model._meta.app_config here..

  • model (Model | type[Model] | None) – The model to use in the permission name. Can be None if permission is not specific to a model.

  • action (str) – The action to perform. For example, common ones include "create", "update", "detail", "list", "delete", but it can be anything.

  • is_global (bool) – Whether the permission is global (True) or per-object (False). The default implementation does not use this parameter, but it is required for compatability with RESOLVE_PERM_NAME.

Return type:

str