Invenio-Records-Permissions
Invenio-Records-Permissions is a Python library that provides a flexible access control system for Invenio records, allowing developers to define and enforce permission policies for various record-related actions. It is a core module within the InvenioRDM ecosystem. The library is actively maintained, with recent updates in early 2026, often released in conjunction with other Invenio modules and InvenioRDM releases.
Warnings
- gotcha Understanding the difference between a 'permission factory' and a 'search filter' is crucial. A permission factory processes a single record to determine access, while a search filter operates on the current user to filter search results efficiently across multiple records.
- gotcha Invenio-Records-Permissions, by design, does not automatically set permissions for files attached to records. It is the developer's responsibility to implement specific permission logic for file access based on the associated record's permissions.
- breaking Major versions of the overarching InvenioRDM platform (e.g., v1.x to v2.x) can introduce significant changes to record serialization (e.g., for versioning) or core components, which might indirectly impact how permission policies are structured or how record data is accessed for permission checks.
- gotcha The concepts of 'Needs' and 'Permissions' from `invenio-access` can initially be abstract. A 'Need' represents a specific requirement (e.g., 'user ID 1', 'admin role'), while a 'Permission' is a collection of Needs.
Install
-
pip install invenio-records-permissions
Imports
- Generator
from invenio_records_permissions.generators import Generator
- SystemProcess
from invenio_records_permissions.generators import SystemProcess
- Permission
from invenio_access import Permission
Quickstart
from invenio_access import Permission
from flask_principal import UserNeed
from invenio_records_permissions.generators import AnyUser
# Example of a simple permission factory
def owner_permission_factory(record=None):
"""Grants permission if the current user is the record owner."""
if record and "owner" in record:
# In a real application, 'record["owner"]' would be the user ID
# and UserNeed would compare against the authenticated user's ID.
return Permission(UserNeed(record["owner"]))
return Permission()
# Example of a basic permission policy
class MyRecordPermissionPolicy:
can_read = [AnyUser()]
can_create = [AnyUser()] # For simplicity, usually restricted
can_update = [owner_permission_factory]
can_delete = [owner_permission_factory]
# How you might use a generator directly (e.g., in a Policy's can_read list)
# This is typically integrated into an Invenio application context.
print(f"Can any user read? {AnyUser().needs(record=None)}")