Coder Social home page Coder Social logo

guardrail's Introduction

guardrail

Latest version Travis CI

guardrail is a Python library for managing object-level permissions that's designed to integrate with arbitrary databases and web frameworks. guardrail is inspired by django-guardian and currently supports the SQLAlchemy, Peewee, Pony, and Django ORMs.

Homepage: https://guardrail.readthedocs.org/

Install

pip install guardrail

guardrail supports Python >= 2.7 or >= 3.3 and pypy.

Examples

Define your models as usual, using the registry.agent and registry.target decorators to set up permissions relationships:

import peewee as pw

from guardrail.core import registry
from guardrail.ext.peewee import PeeweePermissionSchemaFactory

database = pw.SqliteDatabase(':memory:')
class Base(pw.Model):
    class Meta:
        database = database

@registry.agent
class User(Base):
    name = pw.CharField()

@registry.target
class Post(Base):
    name = pw.CharField()

@registry.target
class Comment(Base):
    name = pw.CharField()

factory = PeeweePermissionSchemaFactory((Base, ))
registry.make_schemas(factory)

database.connect()
database.create_tables([User, Post, Comment], safe=True)
database.create_tables(registry.permissions, safe=True)

Then use the permission manager to perform CRUD operations on permissions between any agent and target models:

from guardrail.ext.peewee import PeeweePermissionManager

manager = PeeweePermissionManager()

user = User.create(name='fred')
post = Post.create(name='news of the world')
comment = Comment.create(name='dragon attack')

manager.add_permission(user, post, 'edit')
manager.add_permission(user, comment, 'delete')

manager.has_permission(user, post, 'edit')          # True

manager.remove_permission(user, comment, 'delete')

manager.has_permission(user, comment, 'delete')     # False

Project Links

License

MIT licensed. See the LICENSE file for details.

guardrail's People

Contributors

jmcarp avatar yarreg avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.