flask-rbac-icdc


Nameflask-rbac-icdc JSON
Version 0.1.8 PyPI version JSON
download
home_pageNone
SummaryFlask RBAC library using YAML config file
upload_time2025-07-30 14:52:57
maintainerNone
docs_urlNone
authorNone
requires_pythonNone
licenseMIT License Copyright (c) 2025 ICDC Platform Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords flask rbac yaml icdc
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # RBAC library for Flask

## Overview

This library provides role-based access control (RBAC) for Flask applications using a YAML configuration file.

## Installation

```sh
pip install flask-rbac-icdc
```

## Usage
### Configuration
Create a YAML configuration file for RBAC rules. For example, `rbac_config.yaml`:
```yaml
roles:
  admin:
    products:
      permissions:
        - list
        - create
        - update
        - delete
      filters: {}
    accounts:
      permissions:
        - list
        - get
      filters:
        id: account_id
  member:
    products:
      permissions:
        - list
      filters:
        account_id: account_id
        owner: owner
```
### Define Account Class
Implement the `RbacAccount` abstract class:
```py
from flask_sqlalchemy import SQLAlchemy
from flask_rbac_icdc import RbacAccount

db = SQLAlchemy()

class Account(db.Model, RbacAccount):
    __tablename__ = "accounts"
    object_name = "accounts"
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(64), unique=True, nullable=False)
    # ...Other account properties here

    @classmethod
    def get_by_name(cls, account_name: str) -> Optional["Account"]:
        return cls.query.filter_by(name=account_name).first()

    def get_role(self, requested_role: str) -> str:
        operator = is_operator(self.name, requested_role)
        if requested_role == "operator" and not operator:
            raise PermissionException("You are not operator")
        if operator:
            return "operator"
        return requested_role
```

### Initialize RBAC
Initialize the RBAC instance in your Flask application:
```py
from flask_rbac_icdc import RBAC
from app.models.accounts import Account

rbac = RBAC(config_path='rbac_config.yaml', Accounts)
```

### Protect Endpoints
Use the `allow` decorator to protect your endpoints:
```py
@app.route('/create', methods=['POST'])
@rbac.allow('products.create')
def create_product(subject):
    # Your logic to create a product
    return 'Product created', 201

@app.route('/read', methods=['GET'])
@rbac.allow('products.list')
def list_products(subject):
    # Your logic to list products
    return 'Products data', 200
```

## License
This project is licensed under the MIT License. See the [LICENSE](https://github.com/icdc-io/flask-rbac/blob/main/LICENSE) file for details.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "flask-rbac-icdc",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "flask, rbac, yaml, icdc",
    "author": null,
    "author_email": "ICDC <support@icdc.io>, Vitali Starastsenka <vstarastsenka@ibagroup.eu>",
    "download_url": "https://files.pythonhosted.org/packages/e0/0b/63d60f1b4d934d8ea2ddd8786cd473a95bd20e5d98f064cfe735cf3e5be1/flask_rbac_icdc-0.1.8.tar.gz",
    "platform": null,
    "description": "# RBAC library for Flask\r\n\r\n## Overview\r\n\r\nThis library provides role-based access control (RBAC) for Flask applications using a YAML configuration file.\r\n\r\n## Installation\r\n\r\n```sh\r\npip install flask-rbac-icdc\r\n```\r\n\r\n## Usage\r\n### Configuration\r\nCreate a YAML configuration file for RBAC rules. For example, `rbac_config.yaml`:\r\n```yaml\r\nroles:\r\n  admin:\r\n    products:\r\n      permissions:\r\n        - list\r\n        - create\r\n        - update\r\n        - delete\r\n      filters: {}\r\n    accounts:\r\n      permissions:\r\n        - list\r\n        - get\r\n      filters:\r\n        id: account_id\r\n  member:\r\n    products:\r\n      permissions:\r\n        - list\r\n      filters:\r\n        account_id: account_id\r\n        owner: owner\r\n```\r\n### Define Account Class\r\nImplement the `RbacAccount` abstract class:\r\n```py\r\nfrom flask_sqlalchemy import SQLAlchemy\r\nfrom flask_rbac_icdc import RbacAccount\r\n\r\ndb = SQLAlchemy()\r\n\r\nclass Account(db.Model, RbacAccount):\r\n    __tablename__ = \"accounts\"\r\n    object_name = \"accounts\"\r\n    id = Column(Integer, primary_key=True, autoincrement=True)\r\n    name = Column(String(64), unique=True, nullable=False)\r\n    # ...Other account properties here\r\n\r\n    @classmethod\r\n    def get_by_name(cls, account_name: str) -> Optional[\"Account\"]:\r\n        return cls.query.filter_by(name=account_name).first()\r\n\r\n    def get_role(self, requested_role: str) -> str:\r\n        operator = is_operator(self.name, requested_role)\r\n        if requested_role == \"operator\" and not operator:\r\n            raise PermissionException(\"You are not operator\")\r\n        if operator:\r\n            return \"operator\"\r\n        return requested_role\r\n```\r\n\r\n### Initialize RBAC\r\nInitialize the RBAC instance in your Flask application:\r\n```py\r\nfrom flask_rbac_icdc import RBAC\r\nfrom app.models.accounts import Account\r\n\r\nrbac = RBAC(config_path='rbac_config.yaml', Accounts)\r\n```\r\n\r\n### Protect Endpoints\r\nUse the `allow` decorator to protect your endpoints:\r\n```py\r\n@app.route('/create', methods=['POST'])\r\n@rbac.allow('products.create')\r\ndef create_product(subject):\r\n    # Your logic to create a product\r\n    return 'Product created', 201\r\n\r\n@app.route('/read', methods=['GET'])\r\n@rbac.allow('products.list')\r\ndef list_products(subject):\r\n    # Your logic to list products\r\n    return 'Products data', 200\r\n```\r\n\r\n## License\r\nThis project is licensed under the MIT License. See the [LICENSE](https://github.com/icdc-io/flask-rbac/blob/main/LICENSE) file for details.\r\n",
    "bugtrack_url": null,
    "license": "MIT License\r\n        \r\n        Copyright (c) 2025 ICDC Platform\r\n        \r\n        Permission is hereby granted, free of charge, to any person obtaining a copy\r\n        of this software and associated documentation files (the \"Software\"), to deal\r\n        in the Software without restriction, including without limitation the rights\r\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\n        copies of the Software, and to permit persons to whom the Software is\r\n        furnished to do so, subject to the following conditions:\r\n        \r\n        The above copyright notice and this permission notice shall be included in all\r\n        copies or substantial portions of the Software.\r\n        \r\n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r\n        SOFTWARE.\r\n        ",
    "summary": "Flask RBAC library using YAML config file",
    "version": "0.1.8",
    "project_urls": {
        "Documentation": "https://icdc-io.github.io/flask-rbac-docs",
        "Homepage": "https://github.com/icdc-io/flask-rbac",
        "Repository": "https://github.com/icdc-io/flask-rbac.git"
    },
    "split_keywords": [
        "flask",
        " rbac",
        " yaml",
        " icdc"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "1203b89c796ff053122e4c72a5cba6705a8a9e7b5a39e2f3a795894809916723",
                "md5": "69e8b6b84d2796b84e0a80ccb8df3486",
                "sha256": "aa0b7b4c532b624489687f9ee3ba8b7a3958098ea8c9a7052b0959327c3c4c43"
            },
            "downloads": -1,
            "filename": "flask_rbac_icdc-0.1.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "69e8b6b84d2796b84e0a80ccb8df3486",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 9153,
            "upload_time": "2025-07-30T14:52:56",
            "upload_time_iso_8601": "2025-07-30T14:52:56.822043Z",
            "url": "https://files.pythonhosted.org/packages/12/03/b89c796ff053122e4c72a5cba6705a8a9e7b5a39e2f3a795894809916723/flask_rbac_icdc-0.1.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "e00b63d60f1b4d934d8ea2ddd8786cd473a95bd20e5d98f064cfe735cf3e5be1",
                "md5": "57e4ea61f739745e51357e2e640820c3",
                "sha256": "64d2d1c5a6547171d653f206e23e17ea0372430c7f5027aa48d7e05014c7f569"
            },
            "downloads": -1,
            "filename": "flask_rbac_icdc-0.1.8.tar.gz",
            "has_sig": false,
            "md5_digest": "57e4ea61f739745e51357e2e640820c3",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 12477,
            "upload_time": "2025-07-30T14:52:57",
            "upload_time_iso_8601": "2025-07-30T14:52:57.667436Z",
            "url": "https://files.pythonhosted.org/packages/e0/0b/63d60f1b4d934d8ea2ddd8786cd473a95bd20e5d98f064cfe735cf3e5be1/flask_rbac_icdc-0.1.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-30 14:52:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "icdc-io",
    "github_project": "flask-rbac",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "flask-rbac-icdc"
}
        
Elapsed time: 0.86466s