Skip to main content

Build Status

Miracle

Miracle is an ACL for Python that was designed to be well-structuted, simple yet exhaustive. It uses permissions defined on resources, and roles are granted with the access to them.

To be a universal tool, it does not include any special cases, does not force you to persist and does not insist on any formats or conventions.

Maximum flexibility and total control. Enjoy! :)

Highlights:

  • Inspired by miracle for NodeJS ;

  • Simple core

  • No restrictions on authorization entities

  • Unit-tested

Table of Contents

  • Define The Structure

    • Acl

    • Create

      • add_role(role)

      • add_resource(resource)

      • add_permission(resource, permission)

      • add(structure)

    • Remove

      • remove_role(role)

      • remove_resource(resource)

      • remove_permission(resource, permission)

    • Get

      • get_roles()

      • get_resources()

      • get_permissions(resource)

      • get()

    • Export and Import

  • Authorize

    • Grant Permissions

      • grant(role, resource, permission)

      • revoke(role, resource, permission)

    • Check Permissions

      • check(role, resource, permission)

      • check_any(roles, resource, permission)

      • check_all(roles, resource, permission)

    • Show Grants

      • which(role)

      • which_any(roles)

      • which_all(roles)

      • show()

Define The Structure

Acl

To start using miracle, instantiate the Acl object:

from acl import Acl
acl = Acl()

The Acl object keeps track of your resources and permissions defined on them, handles grants over roles and provides utilities to manage them. When configured, you can check the access against the defined state.

Create

Methods from this section allow you to build the structure: list of roles, resources and permissions.

It’s not required that you have the structure defined before you start granting the access: the grant() method implicitly creates all resources and permissions that were not previously defined.

Start with defining the resources and permissions on them, then you can grant a role with the access to some permissions on a resource.

For roles, resources & permissions, any hashable objects will do.

add_role(role)

Define a role.

  • role: the role to define.

The role will have no permissions granted, but will appear in get_roles().

acl.add_role('admin')
acl.get_roles()  # -> {'admin'}

add_resource(resource)

Define a resource.

  • resources: the resource to define.

The resource will have no permissions defined but will appear in get_resources().

acl.add_resource('blog')
acl.get_resources()  # -> {'blog'}

add_permission(resource, permission)

Define a permission on a resource.

  • resource: the resource to define the permission on. Is created if was not previously defined.

  • permission: the permission to define.

The defined permission is not granted to anyone, but will appear in get_permissions(resource).

acl.add_permission('blog', 'post')
acl.get_permissions('blog')  # -> {'post'}

add(structure)

Define the whole resource/permission structure with a single dict.

  • structure: a dict that maps resources to an iterable of permissions.

acl.add({
    'blog': ['post'],
    'page': {'create', 'read', 'update', 'delete'},
})

Remove

remove_role(role)

Remove the role and its grants.

  • role: the role to remove.

acl.remove_role('admin')

remove_resource(resource)

Remove the resource along with its grants and permissions.

  • resource: the resource to remove.

acl.remove_resource('blog')

remove_permission(resource, permission)

Remove the permission from a resource.

  • resource: the resource to remove the permission from.

  • permission: the permission to remove.

The resource is not implicitly removed: it remains with an empty set of permissions.

acl.remove_permission('blog', 'post')

Get

get_roles()

Get the set of defined roles.

acl.get_roles()  # -> {'admin', 'anonymous', 'registered'}

get_resources()

Get the set of defined resources, including those with empty permissions set.

acl.get_resources()  # -> {'blog', 'page', 'article'}

get_permissions(resource)

Get the set of permissions for a resource.

  • resource: the resource to get the permissions for.

acl.get_permissions('page')  # -> {'create', 'read', 'update', 'delete'}

get()

Get the structure: hash of all resources mapped to their permissions.

Returns a dict: { resource: set(permission,...), ... }.

acl.get()  # -> { blog: {'post'}, page: {'create', ...} }

Export and Import

The Acl class is picklable:

acl = miracle.Acl()
save = acl.__getstate__()

#...

acl = miracle.Acl()
acl.__setstate__(save)

Authorize

Grant Permissions

grant(role, resource, permission)

Grant a permission over resource to the specified role.

  • role: The role to grant the access to

  • resource: The resource to grant the access over

  • permission: The permission to grant with

Roles, resources and permissions are implicitly created if missing.

acl.grant('admin', 'blog', 'delete')
acl.grant('anonymous', 'page', 'view')

revoke(role, resource, permission)

Revoke a permission over a resource from the specified role.

acl.revoke('anonymous', 'page', 'view')
acl.revoke('user', 'account', 'delete')

Check Permissions

check(role, resource, permission)

Test whether the given role has access to the resource with the specified permission.

  • role: The role to check

  • resource: The protected resource

  • permission: The required permission

Returns a boolean.

acl.check('admin', 'blog') # True
acl.check('anonymous', 'page', 'delete') # -> False

check_any(roles, resource, permission)

Test whether any of the given roles have access to the resource with the specified permission.

  • roles: An iterable of roles.

When no roles are provided, returns False.

check_all(roles, resource, permission)

Test whether all of the given roles have access to the resource with the specified permission.

  • roles: An iterable of roles.

When no roles are provided, returns False.

Show Grants

which(role)

Collect grants that the provided role has:

acl.which('admin')  # -> { blog: {'post'} }

which_any(roles)

Collect grants that any of the provided roles have (union).

acl.which(['anonymous', 'registered'])  # -> { page: ['view'] }

which_all(roles)

Collect grants that all of the provided roles have (intersection):

acl.which(['anonymous', 'registered'])  # -> { page: ['view'] }

show()

Get all current grants.

Returns a dict { role: { resource: set(permission) } }.

acl.show()  # -> { admin: { blog: ['post'] } }

Metadata

Release files for miracle-acl 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for miracle-acl 0.0.1
File Size Uploaded
miracle-acl-0.0.1.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for miracle-acl 0.0.1
File Interpreter ABI Platform
miracle_acl-0.0.1-py2.7.egg Legacy Egg format - - Details

Total release size: 17.0 kB

Release files / miracle-acl-0.0.1.tar.gz

Download URL miracle-acl-0.0.1.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
142c200e82aa63ca510a9c6e445524363fbe4bc84a31e3d11accd8fe37e6ea74
BLAKE2b-256 checksum
How to use checksums
5fa2dfc92c62ee4f3926ff41e46cdb95aa4ea97a6c851dd282deff065e65c3e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / miracle_acl-0.0.1-py2.7.egg

Download URL miracle_acl-0.0.1-py2.7.egg
Size 8.9 kB
Tags Egg
SHA-256 checksum
How to use checksums
64fdafe3f8632567fe03b7f981bfb0244199ac485cf5abcffd29b4aff3eb1ebc
BLAKE2b-256 checksum
How to use checksums
97ad450a209630f1e9e141b1dec0eb8cf17307d821be0e07fbeb0c9f7b674bf9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.0.3

2 release files

0.0.2

2 release files

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page