# Permission Manager
![example workflow](https://github.com/kindlycat/permission-manager/actions/workflows/tests.yml/badge.svg)
[![codecov](https://codecov.io/gh/kindlycat/permission-manager/graph/badge.svg?token=XET0GPM9VW)](https://codecov.io/gh/kindlycat/permission-manager)
A simple way to manage object permissions.
Full documentation on [read the docs](https://permission-manager.readthedocs.io/en/latest/).
## Install
```bash
pip install permission-manager
```
## Example
Use `BasePermissionManager`
```python
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
```python
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
```
Raw data
{
"_id": null,
"home_page": "https://github.com/kindlycat/permission-manager",
"name": "permission-manager",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": "permissions",
"author": "Grigory Mishchenko",
"author_email": "grishkokot@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/e3/db/88504bf2de8c235b136b33e854a34fce2e95b2164cb6765a1e957fa9ca35/permission_manager-0.5.1.tar.gz",
"platform": null,
"description": "# Permission Manager\n\n![example workflow](https://github.com/kindlycat/permission-manager/actions/workflows/tests.yml/badge.svg)\n[![codecov](https://codecov.io/gh/kindlycat/permission-manager/graph/badge.svg?token=XET0GPM9VW)](https://codecov.io/gh/kindlycat/permission-manager)\n\nA simple way to manage object permissions.\n\nFull documentation on [read the docs](https://permission-manager.readthedocs.io/en/latest/).\n\n## Install\n\n```bash\npip install permission-manager\n```\n\n## Example\n\nUse `BasePermissionManager`\n\n```python\nimport dataclasses\nfrom permission_manager import BasePermissionManager, PermissionResult\n\n\n@dataclasses.dataclass\nclass User:\n name: str\n\n\n@dataclasses.dataclass\nclass Person:\n first_name: str\n last_name: str\n status: str\n\n\nclass PersonPermissionManager(BasePermissionManager):\n instance: Person\n\n def has_create_permission(self):\n return True\n \n def has_delete_permission(self):\n return self.user.name == 'admin'\n\n def has_access_permission(self):\n if self.instance.status == 'excommunicado':\n return PermissionResult('Due status')\n return True\n\n\nmanager = PersonPermissionManager()\nmanager.has_permission('create')\n# same as \n# manager.has_create_permission()\n# > True\n\nmanager = PersonPermissionManager(\n instance=Person(\n first_name='John',\n last_name='Wick',\n status='excommunicado',\n ),\n user=User(name='Ms. Perkins'),\n)\nmanager.has_permission('delete')\n# > False\nmanager.has_permission('access')\n# > PermissionResult(value=False, message=['Due status'])\n\nmanager.resolve(actions=('access', 'create', 'delete'))\n# > {'access': False, 'create': True, 'delete': False}\nmanager.resolve(actions=('access', 'create', 'delete'), with_messages=True)\n# > {'access': {'allow': False, 'messages': ['Due status']},\n# 'create': {'allow': True, 'messages': None},\n# 'delete': {'allow': False, 'messages': None}}\n```\n\nAlso, it's include `PermissionManager`, which add additional functionality to check parent permissions\n\n```python\nimport dataclasses\nfrom permission_manager import PermissionManager\n\n\nclass PostPermissionManager(PermissionManager):\n instance: 'Post'\n\n def has_update_permission(self):\n return self.instance.status == 'draft'\n\n\n@dataclasses.dataclass\nclass Post:\n title: str\n status: str = 'draft'\n\n permission_manager = PostPermissionManager\n\n \nclass ImagePermissionManager(PermissionManager):\n parent_attr = 'post'\n instance: 'Image'\n \n def has_update_permission(self):\n return self.parent_permission_manager.has_permission('update')\n\n\n@dataclasses.dataclass\nclass Image:\n post: Post\n file: str\n\npost = Post(title='Test')\nmanager = ImagePermissionManager(\n instance=Image(\n post=post,\n file='/path/to/file',\n ),\n)\nmanager.has_permission('update')\n# > True\npost.status = 'published'\nmanager.has_permission('update')\n# > False\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A simple way to manage object permissions.",
"version": "0.5.1",
"project_urls": {
"Homepage": "https://github.com/kindlycat/permission-manager",
"Repository": "https://github.com/kindlycat/permission-manager"
},
"split_keywords": [
"permissions"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "bb39255a3ef8edebdf6e17cc30eb50d62c766ac82c57f6008f2afccdba978681",
"md5": "30354afe8fe2de10fe09d83e2b7f738a",
"sha256": "2f8e7cff06ebc43af99928a24c7f2bf5c0b6d6ba0eacfb70a5d009e216134d8a"
},
"downloads": -1,
"filename": "permission_manager-0.5.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "30354afe8fe2de10fe09d83e2b7f738a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 8371,
"upload_time": "2024-08-04T19:15:00",
"upload_time_iso_8601": "2024-08-04T19:15:00.191076Z",
"url": "https://files.pythonhosted.org/packages/bb/39/255a3ef8edebdf6e17cc30eb50d62c766ac82c57f6008f2afccdba978681/permission_manager-0.5.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e3db88504bf2de8c235b136b33e854a34fce2e95b2164cb6765a1e957fa9ca35",
"md5": "b0ac86dbb21d6a5addbc995a9e5c8264",
"sha256": "10e694192986cb987ee04fa6846b79865c79e0288b78939f1ae6ede504ccdd3a"
},
"downloads": -1,
"filename": "permission_manager-0.5.1.tar.gz",
"has_sig": false,
"md5_digest": "b0ac86dbb21d6a5addbc995a9e5c8264",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 7597,
"upload_time": "2024-08-04T19:15:01",
"upload_time_iso_8601": "2024-08-04T19:15:01.890297Z",
"url": "https://files.pythonhosted.org/packages/e3/db/88504bf2de8c235b136b33e854a34fce2e95b2164cb6765a1e957fa9ca35/permission_manager-0.5.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-04 19:15:01",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kindlycat",
"github_project": "permission-manager",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "permission-manager"
}