Skip to main content

A simple way to manage object permissions.

Project description

Permission Manager

A simple way to manage object permissions.

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()
# > {'access': False, 'create': True, 'delete': False}
manager.resolve(with_messages=True)
# > {'access': {'allow': False, 'message': ['Due status']},
#    'create': {'allow': True, 'message': None},
#    'delete': {'allow': False, 'message': 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

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.1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

permission_manager-0.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: permission_manager-0.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for permission_manager-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7eeadd2303ad43fe30e7eaa6e51456bd3f03384192dace3367fe9c38b54d7e5c
MD5 f6990f17c808974422f5daabbcff857f
BLAKE2b-256 286e9b3a6fbb3d8ce91f43ce9c2141107e7cfbfa6bf77b87674305e408f2eae0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for permission_manager-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 35b219e73f5e10813ae0344319b3df61e4b34e46ccda51fd661cd272ab59800b
MD5 0ad44c9722a20dafda3434d80c4f2886
BLAKE2b-256 11f4d604c74e2a2991d4114b3e984dc4d19053baeb7a27f93204a50c6426fea0

See more details on using hashes here.

Supported by

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