Skip to main content

A simple way to manage object permissions.

Project description

Permission Manager

example workflow codecov

A simple way to manage object permissions.

Full documentation on read the docs.

Install

pip install permission-manager

Example

Use BasePermissionManager

import dataclasses
from permission_manager import BasePermissionManager, PermissionResult


@dataclasses.dataclass
class User:
    name: str


@dataclasses.dataclass
class Person:
    first_name: str
    last_name: str
    status: str


class PersonPermissionManager(BasePermissionManager):
    instance: Person

    def has_create_permission(self):
        return True
    
    def has_delete_permission(self):
        return self.user.name == 'admin'

    def has_access_permission(self):
        if self.instance.status == 'excommunicado':
            return PermissionResult('Due status')
        return True


manager = PersonPermissionManager()
manager.has_permission('create')
# same as 
# manager.has_create_permission()
# > True

manager = PersonPermissionManager(
    instance=Person(
        first_name='John',
        last_name='Wick',
        status='excommunicado',
    ),
    user=User(name='Ms. Perkins'),
)
manager.has_permission('delete')
# > False
manager.has_permission('access')
# > PermissionResult(value=False, message=['Due status'])

manager.resolve(actions=('access', 'create', 'delete'))
# > {'access': False, 'create': True, 'delete': False}
manager.resolve(actions=('access', 'create', 'delete'), with_messages=True)
# > {'access': {'allow': False, 'messages': ['Due status']},
#    'create': {'allow': True, 'messages': None},
#    'delete': {'allow': False, 'messages': None}}

Also, it's include PermissionManager, which add additional functionality to check parent permissions

import dataclasses
from permission_manager import PermissionManager


class PostPermissionManager(PermissionManager):
    instance: 'Post'

    def has_update_permission(self):
        return self.instance.status == 'draft'


@dataclasses.dataclass
class Post:
    title: str
    status: str = 'draft'

    permission_manager = PostPermissionManager

    
class ImagePermissionManager(PermissionManager):
    parent_attr = 'post'
    instance: 'Image'
    
    def has_update_permission(self):
        return self.parent_permission_manager.has_permission('update')


@dataclasses.dataclass
class Image:
    post: Post
    file: str

post = Post(title='Test')
manager = ImagePermissionManager(
    instance=Image(
        post=post,
        file='/path/to/file',
    ),
)
manager.has_permission('update')
# > True
post.status = 'published'
manager.has_permission('update')
# > False

Async

import asyncio

from permission_manager import AsyncBasePermissionManager

class AsyncManager(AsyncBasePermissionManager):
    async def has_create_permission(self) -> bool:
        # Some IO bound operations here
        return True

async def main():
    manager = AsyncManager()
    result = await manager.has_permission('create')
    print(result)

asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

permission_manager-0.6.0.tar.gz (46.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

permission_manager-0.6.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file permission_manager-0.6.0.tar.gz.

File metadata

  • Download URL: permission_manager-0.6.0.tar.gz
  • Upload date:
  • Size: 46.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for permission_manager-0.6.0.tar.gz
Algorithm Hash digest
SHA256 fa53c9524033afe903f373c339ecf6d31099800abbca527029f1ed25bf47bb8d
MD5 659ee834e53664180c7186a70d7002be
BLAKE2b-256 1ce5b3608a09db418510e5148909bff97b40503076768ccc20fa7366d1fc0559

See more details on using hashes here.

Provenance

The following attestation bundles were made for permission_manager-0.6.0.tar.gz:

Publisher: release.yml on kindlycat/permission-manager

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file permission_manager-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for permission_manager-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6514f6aaa235da75a6f00ae4b2c3af6993c25bfdef1824827fe060ab7c5091b0
MD5 9fa41654fee852af4970dca70b39beb4
BLAKE2b-256 ce01c87fe50fb36a28665d0ae72f6ef366c9c1ca236df6f2a19a178ffeb32184

See more details on using hashes here.

Provenance

The following attestation bundles were made for permission_manager-0.6.0-py3-none-any.whl:

Publisher: release.yml on kindlycat/permission-manager

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page