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

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

Uploaded Source

Built Distribution

permission_manager-0.5.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: permission_manager-0.5.1.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for permission_manager-0.5.1.tar.gz
Algorithm Hash digest
SHA256 10e694192986cb987ee04fa6846b79865c79e0288b78939f1ae6ede504ccdd3a
MD5 b0ac86dbb21d6a5addbc995a9e5c8264
BLAKE2b-256 e3db88504bf2de8c235b136b33e854a34fce2e95b2164cb6765a1e957fa9ca35

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for permission_manager-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2f8e7cff06ebc43af99928a24c7f2bf5c0b6d6ba0eacfb70a5d009e216134d8a
MD5 30354afe8fe2de10fe09d83e2b7f738a
BLAKE2b-256 bb39255a3ef8edebdf6e17cc30eb50d62c766ac82c57f6008f2afccdba978681

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