All Projects → cocoakekeyu → Cancan

cocoakekeyu / Cancan

Licence: mit
cancan is a tiny permission controller base on ruby cancan library.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Cancan

Bugsnag Python
Official bugsnag error monitoring and error reporting for django, flask, tornado and other python apps.
Stars: ✭ 69 (-71.72%)
Mutual labels:  django, flask
Dynaconf
Configuration Management for Python ⚙
Stars: ✭ 2,082 (+753.28%)
Mutual labels:  django, flask
Pyreportjasper
Python Reporting with JasperReports
Stars: ✭ 77 (-68.44%)
Mutual labels:  django, flask
Webargs
A friendly library for parsing HTTP request arguments, with built-in support for popular web frameworks, including Flask, Django, Bottle, Tornado, Pyramid, webapp2, Falcon, and aiohttp.
Stars: ✭ 1,145 (+369.26%)
Mutual labels:  django, flask
Pyrollbar
Error tracking and logging from Python to Rollbar
Stars: ✭ 169 (-30.74%)
Mutual labels:  django, flask
Ziggurat foundations
Framework agnostic set of sqlalchemy classes that make building applications that require permissions an easy task.
Stars: ✭ 67 (-72.54%)
Mutual labels:  flask, permission
Work At Olist
Apply for a job at Olist's Development Team: https://bit.ly/olist-webdev
Stars: ✭ 93 (-61.89%)
Mutual labels:  django, flask
Channelstream
Channelstream is a websocket communication server for web applications
Stars: ✭ 52 (-78.69%)
Mutual labels:  django, flask
Zappa
Serverless Python
Stars: ✭ 11,859 (+4760.25%)
Mutual labels:  django, flask
Bento
[DEPRECATED] Find Python web-app bugs delightfully fast, without changing your workflow. 🍱
Stars: ✭ 147 (-39.75%)
Mutual labels:  django, flask
Best Of Web Python
🏆 A ranked list of awesome python libraries for web development. Updated weekly.
Stars: ✭ 1,118 (+358.2%)
Mutual labels:  django, flask
Bolt Python
A framework to build Slack apps using Python
Stars: ✭ 190 (-22.13%)
Mutual labels:  django, flask
Lagom
📦 Autowiring dependency injection container for python 3
Stars: ✭ 61 (-75%)
Mutual labels:  django, flask
Python Testing Crawler
A crawler for automated functional testing of a web application
Stars: ✭ 68 (-72.13%)
Mutual labels:  django, flask
Awesome Python Primer
自学入门 Python 优质中文资源索引,包含 书籍 / 文档 / 视频,适用于 爬虫 / Web / 数据分析 / 机器学习 方向
Stars: ✭ 57 (-76.64%)
Mutual labels:  django, flask
Pybooks
python books
Stars: ✭ 87 (-64.34%)
Mutual labels:  django, flask
Heroku Buildpack Python
The official Heroku buildpack for Python apps.
Stars: ✭ 849 (+247.95%)
Mutual labels:  django, flask
Yesterday I Learned
Brainfarts are caused by the rupturing of the cerebral sphincter.
Stars: ✭ 50 (-79.51%)
Mutual labels:  django, flask
Python Resources 2019
A curated list of Python 3 resources, books, websites, tutorials, code challenges
Stars: ✭ 125 (-48.77%)
Mutual labels:  django, flask
Flango
A Django template for using Flask for the frontend, Django for the backend.
Stars: ✭ 188 (-22.95%)
Mutual labels:  django, flask

cancan

Introduction

cancan is a tiny permission controller base on ruby cancan library. Once defined user ability, can easily check user's permission.

Install

pip install cancan

Basic Usage

inherit from cancan.Ability

use add method to add user Ability

def add(self, action=None, subject=None, **conditions)`
    """
    Add ability are allowed using two arguments.

    The first one is the action you're setting the permission for,
    the second one is the class of object you're setting it on.
    the third one is the subject's attributes must be matches or a function
    to be test.

    self.add('update', Article)
    self.add('update', Article, user_id=1)
    self.add('update', Article, user_id=1, title='hello')
    self.add('update', Article, function=test_title)
    """
import cancan

class User(object):
    def __init__(self, id, name, role):
        self.id = id
        self.name = name
        self.role = role

class Article(object):
    def __init__(self, title, user_id):
        self.title = title
        self.user_id = user_id

class Ability(cancan.Ability):
    def __init__(self, user):
        if user.role == 'admin':
            self.add('manage', 'all')
        else:
            self.add('read', Article)
            self.add('create', Article)
            self.add('update', Article, user_id=user.id)
            self.add('create', 'bbb')


admin = User(1, 'neven', 'admin')
ability = Ability(admin)

# admin
ability.can('read', Article)    # True
ability.can('create', Article)  # True
ability.can('delete', Article)  # True
ability.can('aaa', Article)     # True
ability.can('create', 'bbb')    # True
ability.can('create', 'ccc')    # True

user = User(2, 'joe', 'user')
ability2 = Ability(user)

# user
ability2.can('read', Article)   # True
ability2.can('create', Article) # True
ability2.can('delete', Article) # False
ability2.can('aaa', Article)    # False
ability2.can('create', 'bbb')   # True
ability2.can('create', 'ccc')   # False

article = Article('hello', 2)
# admin
ability.can('update', article) # True

# user
ability2.can('update', article) # True
ability2.can('update', Article) # True(class dont check conditions)

Advanced

import cancan


def test_title_gt_100(article):
    return len(article.title) > 100

def anoter_test(article, id, len_title):
    return article.user_id < id and len(article.title) > len_article

class Ability(cancan.Ability):
    def __init__(self, user):
        self.alias_action('create', 'read', 'update', to='cru')

        if user.role == 'admin':
            self.add('manage', 'all')
            self.addnot('destroy', 'gem')
        elif user.role == 'editor':
            self.add('cru', Article)
            self.add(['read', 'create'], 'gem')
            self.add('update', Article, function=test_title_gt_100)
            self.add('delete', Article, function=another_test, func_args=(10,), func_kwargs={"len_title": 4})
        else:
            self.add('create', Article)
            self.add('update', Article, user_id=user.id)
            self.add('create', 'bbb')

editor = User(3, 'kali', 'editor')
ability3 = Ability(editor)

# editor
ability3.can('create', Article)  # True
ability3.can('update', Article)  # True
ability3.can('cru', Article)  # True
ability3.can('read', 'gem')  # True
ability3.can('create', 'gem')  # True

article = Article('world', 1)
ability3.can('update', article)  # False
ability3.can('delete', article)  # True

article = Article('world'*100, 1)
ability3.can('delete', article)  # True

Example

see example.py

Integrate Django

see django_example

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].