A simple way to manage object permissions.
Project description
Permission Manager
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()
# > {'access': False, 'create': True, 'delete': False}
manager.resolve(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
Built Distribution
File details
Details for the file permission_manager-0.4.1.tar.gz
.
File metadata
- Download URL: permission_manager-0.4.1.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4bf9ba123aff79b4bcc595c53da2e9207068bc1a7521a4d2570ef2d14ae6a46b |
|
MD5 | 0d8c590801947d83a67636225346253e |
|
BLAKE2b-256 | 7497651990748fe89acd8dc263bd9e7c2ca095d5fad35df5c33c83335ac4292e |
File details
Details for the file permission_manager-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: permission_manager-0.4.1-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 65050326a005980e69d3a83f7747cad0231d69160789d3ac01ec57fabe804599 |
|
MD5 | 37ff89a2ad42196e34143cb9c59f15a4 |
|
BLAKE2b-256 | 763c1bf2aa472d0e48d7f0232afc45ca4580836f3628647d8501a56f57c0c70c |