{ "info": { "author": "TonySeek", "author_email": "tonyseek@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3", "Topic :: Security", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "Simple RBAC\n===========\n\nThis is a simple role based access control utility in Python.\n\nQuick Start\n-----------\n\n1. Install Simple RBAC\n~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n pip install simple-rbac\n\n2. Create a Access Control List\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n import rbac.acl\n\n acl = rbac.acl.Registry()\n\n3. Register Roles and Resources\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n acl.add_role(\"member\")\n acl.add_role(\"student\", [\"member\"])\n acl.add_role(\"teacher\", [\"member\"])\n acl.add_role(\"junior-student\", [\"student\"])\n\n acl.add_resource(\"course\")\n acl.add_resource(\"senior-course\", [\"course\"])\n\n4. Add Rules\n~~~~~~~~~~~~\n\n::\n\n acl.allow(\"member\", \"view\", \"course\")\n acl.allow(\"student\", \"learn\", \"course\")\n acl.allow(\"teacher\", \"teach\", \"course\")\n acl.deny(\"junior-student\", \"learn\", \"senior-course\")\n\n5. Use It to Check Permission\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n if acl.is_allowed(\"student\", \"view\", \"course\"):\n print(\"Students chould view courses.\")\n else:\n print(\"Students chould not view courses.\")\n\n if acl.is_allowed(\"junior-student\", \"learn\", \"senior-course\"):\n print(\"Junior students chould learn senior courses.\")\n else:\n print(\"Junior students chould not learn senior courses.\")\n\nCustom Role and Resource Class\n------------------------------\n\nIt\u2019s not necessary to use string as role object and resource object like\n\"Quick Start\". You could define role class and resource class of\nyourself, such as a database mapped model in SQLAlchemy.\n\nWhatever which role class and resource class you will use, it must\nimplement ``__hash__`` method and ``__eq__`` method to be `hashable`_.\n\nExample\n~~~~~~~\n\n::\n\n class Role(db.Model):\n \"\"\"The role.\"\"\"\n\n id = db.Column(db.Integer, primary_key=True)\n screen_name = db.Column(db.Unicode, nullable=False, unique=True)\n\n def __hash__(self):\n return hash(\"ROLE::%d\" % self.id)\n\n def __eq__(self, other):\n return self.id == other.id\n\n\n class Resource(db.Model):\n \"\"\"The resource.\"\"\"\n\n id = db.Column(db.Integer, primary_key=True)\n screen_name = db.Column(db.Unicode, nullable=False, unique=True)\n\n def __hash__(self):\n return hash(\"RESOURCE::%d\" % self.id)\n\n def __eq__(self, other):\n return self.id == other.id\n\nOf course, You could use the built-in hashable types too, such as tuple,\nnamedtuple, frozenset and more.\n\nUse the Identity Context Check Your Permission\n----------------------------------------------\n\nObviously, the work of checking permission is a cross-cutting concern.\nThe module named ``rbac.context``, our ``IdentityContext``, provide some\nways to make our work neater.\n\n1. Create the Context Manager\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n acl = Registry()\n context = IdentityContext(acl)\n\n2. Set a Loader\n~~~~~~~~~~~~~~~\n\nThe loader should load the roles of current user.\n\n::\n\n from myapp import get_current_user\n\n @context.set_roles_loader\n def second_load_roles():\n user = get_current_user()\n yield \"everyone\"\n for role in user.roles:\n yield str(role)\n\n3. Protect Your Action\n~~~~~~~~~~~~~~~~~~~~~~\n\nNow you could protect your action from unauthorized access. As you\nplease, you could choose many ways to check the permission, including\npython ``decorator``, python ``with statement`` or simple method\ncalling.\n\nDecorator\n^^^^^^^^^\n\n::\n\n @context.check_permission(\"view\", \"article\", message=\"can't view\")\n def article_page():\n return \"your-article\"\n\nWith Statement\n^^^^^^^^^^^^^^\n\n::\n\n def article_page():\n with context.check_permission(\"view\", \"article\", message=\"can't view\"):\n return \"your-article\"\n\nSimple Method Calling\n^^^^^^^^^^^^^^^^^^^^^\n\n::\n\n def article_page():\n context.check_permission(\"view\", \"article\", message=\"can't view\").check()\n return \"your-article\"\n\nException Handler and Non-Zero Checking\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nWhatever which way you choosen, a exception\n``rbac.context.PermissionDenied`` will be raised while a unauthorized\naccess happening. The keyword arguments sent to the\n``context.check_permission`` will be set into a attirbute named\n``kwargs`` of the exception. You could get those data in your exception\nhandler.\n\n::\n\n @context.check_permission(\"view\", \"article\", message=\"can not view\")\n def article_page():\n return \"your-article\"\n\n try:\n print article_page()\n except PermissionDenied as exception:\n print \"The access has been denied, you %s\" % exception.kwargs['message']\n\nIf you don\u2019t want to raise the exception but only check the access is\nallowed or not, you could use the checking like a boolean value.\n\n::\n\n if not context.check_permission(\"view\", \"article\"):\n print \"Oh! the access has been denied.\"\n\n is_allowed = bool(context.check_permission(\"view\", \"article\"))\n\n.. _hashable: http://docs.python.org/glossary.html#term-hashable", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.tonyseek.com/simple-rbac/", "keywords": "rbac permission acl access-control", "license": "MIT License", "maintainer": "", "maintainer_email": "", "name": "simple-rbac", "package_url": "https://pypi.org/project/simple-rbac/", "platform": "any", "project_url": "https://pypi.org/project/simple-rbac/", "project_urls": { "Download": "UNKNOWN", "Homepage": "http://github.tonyseek.com/simple-rbac/" }, "release_url": "https://pypi.org/project/simple-rbac/0.1.1/", "requires_dist": null, "requires_python": null, "summary": "A simple role based access control utility", "version": "0.1.1" }, "last_serial": 727609, "releases": { "0.1": [], "0.1.1": [ { "comment_text": "", "digests": { "md5": "b5073b173e727734f019cc5afa8b8aaf", "sha256": "d0489c11afc775a02f32a20b7e74c79f20ec217f725d739bdc2ff2a3f21699b8" }, "downloads": -1, "filename": "simple-rbac-0.1.1.win-amd64.exe", "has_sig": false, "md5_digest": "b5073b173e727734f019cc5afa8b8aaf", "packagetype": "bdist_wininst", "python_version": "any", "requires_python": null, "size": 236225, "upload_time": "2012-06-10T06:01:21", "url": "https://files.pythonhosted.org/packages/24/bd/be82023accf80e7a714a0b1107383c0eef2cd9ce33d128762178672403d1/simple-rbac-0.1.1.win-amd64.exe" }, { "comment_text": "", "digests": { "md5": "349c0e4a5941746aaf5ba4812ae773a1", "sha256": "e740e793fd4db759ad786ab6ec8b4e97fa0db43ca65f91e2570e2621044e3ad3" }, "downloads": -1, "filename": "simple-rbac-0.1.1.zip", "has_sig": false, "md5_digest": "349c0e4a5941746aaf5ba4812ae773a1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11890, "upload_time": "2012-06-10T06:01:18", "url": "https://files.pythonhosted.org/packages/19/49/0ebe7834f85e781c4e0a1edbc99b444707dbe7d6e1789cd1fa99291481bd/simple-rbac-0.1.1.zip" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b5073b173e727734f019cc5afa8b8aaf", "sha256": "d0489c11afc775a02f32a20b7e74c79f20ec217f725d739bdc2ff2a3f21699b8" }, "downloads": -1, "filename": "simple-rbac-0.1.1.win-amd64.exe", "has_sig": false, "md5_digest": "b5073b173e727734f019cc5afa8b8aaf", "packagetype": "bdist_wininst", "python_version": "any", "requires_python": null, "size": 236225, "upload_time": "2012-06-10T06:01:21", "url": "https://files.pythonhosted.org/packages/24/bd/be82023accf80e7a714a0b1107383c0eef2cd9ce33d128762178672403d1/simple-rbac-0.1.1.win-amd64.exe" }, { "comment_text": "", "digests": { "md5": "349c0e4a5941746aaf5ba4812ae773a1", "sha256": "e740e793fd4db759ad786ab6ec8b4e97fa0db43ca65f91e2570e2621044e3ad3" }, "downloads": -1, "filename": "simple-rbac-0.1.1.zip", "has_sig": false, "md5_digest": "349c0e4a5941746aaf5ba4812ae773a1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11890, "upload_time": "2012-06-10T06:01:18", "url": "https://files.pythonhosted.org/packages/19/49/0ebe7834f85e781c4e0a1edbc99b444707dbe7d6e1789cd1fa99291481bd/simple-rbac-0.1.1.zip" } ] }