权限

备注

本文档介绍 Wagtail 权限系统的内部实现。你应该先阅读Wagtail 如何使用 Django 的权限系统。

请注意,除了 register_permission_policy() 函数之外,本文档描述的 API 都被视为内部实现,因此不受我们的 弃用政策约束。

在最基础的层面上,Wagtail 的权限系统是用一组叫“权限策略”的类实现的,它们为某个给定的 Django 模型定义权限规则。每当执行权限检查时,都会查询一个全局注册表,以确定针对给定 Django 模型应使用哪个权限策略实例。因此,任何由 Wagtail 管理的模型都应向全局注册表注册相应的权限策略实例。

其他支撑代码(此处未记录)被页面和 Snippet 用来满足发布、锁定和工作流等功能所需的更细粒度权限检查。因此,以下内容不应被视为 Wagtail 权限检查方式的完整文档。

权限策略

class wagtail.permission_policies.BasePermissionPolicy(model)

A 'permission policy' is an object that handles all decisions about the actions users are allowed to perform on a given model. The mechanism by which it does this is arbitrary, and may or may not involve the django.contrib.auth Permission model; it could be as simple as "allow all users to do everything".

In this way, admin apps can change their permission-handling logic just by swapping to a different policy object, rather than having that logic spread across numerous view functions.

BasePermissionPolicy is an abstract class that all permission policies inherit from. The only method that subclasses need to implement is users_with_any_permission(); all other methods can be derived from that (but in practice, subclasses will probably want to override additional methods, either for efficiency or to implement more fine-grained permission logic).

参数:

model -- The model class that this permission policy applies to. This may be a model class or a string in the form of app_label.model_name.

get_all_permissions_for_user(user)

Return a set of all permissions that the given user has on this model.

They may be instances of Permission, or custom permission objects defined by the policy, which are not necessarily model instances.

get_cached_permissions_for_user(user)

Return a list of all permissions that the given user has on this model, using the cache if available and populating the cache if not.

This can be useful for the other methods to perform efficient queries against the set of permissions that the user has.

user_has_permission(user, action)

Return whether the given user has permission to perform the given action on some or all instances of this model.

user_has_any_permission(user, actions)

Return whether the given user has permission to perform any of the given actions on some or all instances of this model.

users_with_any_permission(actions)

Return a queryset of users who have permission to perform any of the given actions on some or all instances of this model.

users_with_permission(action)

Return a queryset of users who have permission to perform the given action on some or all instances of this model.

user_has_permission_for_instance(user, action, instance)

Return whether the given user has permission to perform the given action on the given model instance.

user_has_any_permission_for_instance(user, actions, instance)

Return whether the given user has permission to perform any of the given actions on the given model instance.

instances_user_has_any_permission_for(user, actions)

Return a queryset of all instances of this model for which the given user has permission to perform any of the given actions.

instances_user_has_permission_for(user, action)

Return a queryset of all instances of this model for which the given user has permission to perform the given action.

users_with_any_permission_for_instance(actions, instance)

Return a queryset of all users who have permission to perform any of the given actions on the given model instance.

users_with_permission_for_instance(action, instance)

Return a queryset of all users who have permission to perform the given action on the given model instance.

class wagtail.permission_policies.BaseDjangoAuthPermissionPolicy(model, auth_model=None)

Extends BasePermissionPolicy with helper methods useful for policies that need to perform lookups against the Permission model.

参数:
  • model -- The model class that this permission policy applies to. This may be a model class or a string in the form of app_label.model_name.

  • auth_model -- The model class (or a string in the form of app_label.model_name) to use for permission record lookups. Usually this will match model (which specifies the type of instances that instances_user_has_permission_for() will return), but this may differ when swappable models are in use - for example, an interface for editing user records might use a custom User model but will typically still refer to the permission records for auth.User.

class wagtail.permission_policies.ModelPermissionPolicy(model, auth_model=None)

A permission policy that enforces permissions at the model level, by consulting the standard Permission model directly.

class wagtail.permission_policies.OwnershipPermissionPolicy(model, auth_model=None, owner_field_name='owner')

A permission policy for objects that support a concept of 'ownership', where the owner is typically the user who created the object.

This policy piggybacks off add and change permissions defined through the Permission model, as follows:

  • any user with add permission can create instances, and ALSO edit instances that they own

  • any user with change permission can edit instances regardless of ownership

  • ability to edit also implies ability to delete

Besides add, change and delete, no other actions are recognised or permitted (unless the user is an active superuser, in which case they can do everything).

class wagtail.permission_policies.collections.CollectionPermissionPolicy(model, auth_model=None)

A permission policy for objects that are assigned locations in the Collection tree. Permissions may be defined at any node of the hierarchy, through the GroupCollectionPermission model, and propagate downwards. These permissions are applied to objects according to the standard Permission model.

class wagtail.permission_policies.collections.CollectionOwnershipPermissionPolicy(model, auth_model=None, owner_field_name='owner')

A permission policy for objects that are assigned locations in the Collection tree. Permissions may be defined at any node of the hierarchy, through the GroupCollectionPermission model, and propagate downwards. These permissions are applied to objects according to the 'ownership' permission model (see OwnershipPermissionPolicy).

class wagtail.permission_policies.collections.CollectionManagementPermissionPolicy(model, auth_model=None)

A permission policy for managing collections themselves, rather than objects assigned to collections.

class wagtail.permission_policies.pages.PagePermissionPolicy(model=None)

A permission policy for page objects, which are arranged in a tree structure. Permissions may be defined at any node of the tree, through the GroupPagePermission model, and propagate downwards. These permissions are applied to objects according to the 'ownership' permission model (see OwnershipPermissionPolicy).

class wagtail.permission_policies.sites.SitePermissionPolicy(model, auth_model=None, site_field_name='site')

A permission policy for objects that are associated with site records, such as wagtail.contrib.settings.models.BaseSiteSetting subclasses (see 设置模型). Permissions may be assigned globally through standard Permission objects, or for individual sites through wagtail.models.GroupSitePermission records.

权限策略注册表

class wagtail.permissions.PolicyRegistry

A registry that maps model classes to their permission policy instances. This is used by Wagtail to determine which permission policy to use for a given model class.

Instead of using this class directly, use the global policy_registry instance and the register_permission_policy() function instead.

get_by_type(cls: type[Model], fallback=True) → BasePermissionPolicy

Get the permission policy for a given model class. If a matching policy was registered with exact_class=True, it will be returned. Otherwise, the policy registered with exact_class=False for the given class or its nearest ancestor class will be returned. If no policy can be found and fallback is True, a default fallback ModelPermissionPolicy will be used. Otherwise, return None.

get(obj: Model) → BasePermissionPolicy

Get the permission policy for a given model instance based on its class.

wagtail.permissions.policy_registry = <wagtail.permissions.PolicyRegistry object>

A global instance of PolicyRegistry used to register and look up permission policies for models managed by Wagtail.

获取某个给定 Django 模型类或实例的权限策略,可以按下面的方式做。

from wagtail.permissions import policy_registry
from .models import MyModel

# 用模型类
policy_registry.get_by_type(MyModel)
# 用模型实例
policy_registry.get(MyModel.objects.first())
wagtail.permissions.register_permission_policy(model: type[Model], policy: BasePermissionPolicy = None, exact_class=False)

Register a permission policy for a given model class.

If no policy is provided, a default ModelPermissionPolicy will be created. If exact_class is set to True, the policy will only be used for the exact model class, otherwise it will also be used for subclasses of the model class.

要为某个模型注册权限策略,请在 该模型所在应用的 wagtail_hooks.py 顶部调用这个函数。

# wagtail_hooks.py
from wagtail.permissions import register_permission_policy
from .models import MyModel


register_permission_policy(MyModel)
...  # 更多自定义

或者,你也可以在 AppConfig.ready() 方法中调用该函数。

# apps.py
class MyAppConfig(AppConfig):
    ...

    def ready(self):
        from wagtail.permissions import register_permission_policy
        from .models import MyModel

        register_permission_policy(MyModel)

备注

目前,register_permission_policy(MyModel) 是这个函数唯一官方支持的用法。

虽然可以为任何模型(包括 Wagtail 的内置模型)注册自定义的权限策略实例,但我们无法保证自定义的权限实现总会被 Wagtail 尊重。我们打算在未来支持这一点。更多细节参见我们的 issue 追踪器中关于支持自定义权限行为的条目。