Skip to content

Commit 3f651f1

Browse files
committed
start with the skeleton
1 parent 242e806 commit 3f651f1

8 files changed

Lines changed: 149 additions & 0 deletions

File tree

.coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[run]
2+
include = casbin_pymongo_adapter/*

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,7 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
# my
132+
.idea
133+
.vscode

.travis.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
language: python
2+
python:
3+
- '3.4'
4+
- '3.5'
5+
- '3.6'
6+
- '3.7'
7+
install:
8+
- pip install -r requirements.txt
9+
- pip install coveralls
10+
script:
11+
- coverage run -m unittest discover -s tests -t tests
12+
after_success:
13+
- coveralls
14+
deploy:
15+
provider: pypi
16+
user: techlee
17+
password:
18+
secure: pTL4bF82LZ+Usn7rhm/7tPDz4QiF+RWuVyu+7aNtgqy4JZ5nCloIxohSxODCr58vYabAMecxwCsvCVm7DSWeJiYzFiyWMiEIRxmuc6LHSNnG8pYIHuj9qBcOdyshQbN5ALpuvnpFX2sUQ20hy3ge6cdFT4xLZAm4nzmNnR6e20MNOT675agdNfgDsjnNEBV7lCGfvpYtAJgaB95f8P24Q/WX6JevcrhCLLoLOnZdjt8RxvhV95QF03E3dI9c7d1NwFEZ/IbK2WfXjZ8rY2vYutscSlvag2r+N/ZVlOwTQvwWeZHXkDu6bRsV2mKC1Sq2J6q6mwtFm81YLB5A0RgMIJu+4Ec36Xl1HxRCBgDxDh4WfZkx/yZJsAMqTKVnFsImDt76MD6sDyemG/cPhli3qkAYazgdL2rv9l8KQWVwIhUTN4zOigzDpoEGIvk3DjrAoCoyP/9U5oagBnHSdL/e5jHFoJQ9CS8RRUYvsez6QWE7V06qRLfTSpxmxzvFrA0RENw6RcpabVmbFac/HdMyv9aFL8Z/bWHf2vkyAYUDBDiyFKKYcGWxTsS3L+fTDvyycfy8NJ3jSgloJiXH2FFsEJ03ZxgCIONNMw8gZ58gglJC0jJHZbzjIAl7oNcm+6S0T56y3H6Hb2zQk0eRD6qiY6RQZowc+HH9Is0ejNhW9zc=
19+
distributions: sdist bdist_wheel
20+
skip_existing: true
21+
on:
22+
tags: true
23+
python: '3.6'

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
PyMongo Adapter for PyCasbin
2+
====
3+
4+
[![Build Status](https://www.travis-ci.org/pycasbin/pymongo-adapter.svg?branch=master)](https://www.travis-ci.org/pycasbin/pymongo-adapter)
5+
[![Coverage Status](https://coveralls.io/repos/github/pycasbin/pymongo-adapter/badge.svg)](https://coveralls.io/github/pycasbin/pymongo-adapter)
6+
[![Version](https://img.shields.io/pypi/v/casbin_pymongo_adapter.svg)](https://pypi.org/project/casbin_pymongo_adapter/)
7+
[![PyPI - Wheel](https://img.shields.io/pypi/wheel/casbin_pymongo_adapter.svg)](https://pypi.org/project/casbin_pymongo_adapter/)
8+
[![Pyversions](https://img.shields.io/pypi/pyversions/casbin_pymongo_adapter.svg)](https://pypi.org/project/casbin_pymongo_adapter/)
9+
[![Download](https://img.shields.io/pypi/dm/casbin_pymongo_adapter.svg)](https://pypi.org/project/casbin_pymongo_adapter/)
10+
[![License](https://img.shields.io/pypi/l/casbin_pymongo_adapter.svg)](https://pypi.org/project/casbin_pymongo_adapter/)
11+
12+
PyMongo Adapter is the [PyMongo](https://pypi.org/project/pymongo/) adapter for [PyCasbin](https://github.com/casbin/pycasbin). With this library, Casbin can load policy from MongoDB or save policy to it.
13+
14+
## Installation
15+
16+
```
17+
pip install casbin_pymongo_adapter
18+
```
19+
20+
## Simple Example
21+
22+
```python
23+
import casbin_pymongo_adapter
24+
import casbin
25+
26+
adapter = casbin_pymongo_adapter.Adapter('mongodb://localhost:27017/', "dbname")
27+
28+
e = casbin.Enforcer('path/to/model.conf', adapter, True)
29+
30+
sub = "alice" # the user that wants to access a resource.
31+
obj = "data1" # the resource that is going to be accessed.
32+
act = "read" # the operation that the user performs on the resource.
33+
34+
if e.enforce(sub, obj, act):
35+
# permit alice to read data1casbin_sqlalchemy_adapter
36+
pass
37+
else:
38+
# deny the request, show an error
39+
pass
40+
```
41+
42+
43+
### Getting Help
44+
45+
- [PyCasbin](https://github.com/casbin/pycasbin)
46+
47+
### License
48+
49+
This project is licensed under the [Apache 2.0 license](LICENSE).

casbin_pymongo_adapter/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .adapter import Adapter

casbin_pymongo_adapter/adapter.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from casbin import persist
2+
from pymongo import MongoClient
3+
4+
5+
class Adapter(persist.Adapter):
6+
"""the pymongo adapter for Casbin."""
7+
8+
def __init__(self, uri, dbname, collection="casbin_rule"):
9+
client = MongoClient(uri)
10+
db = client[dbname]
11+
self._collection = db[collection]
12+
pass
13+
14+
def load_policy(self, model):
15+
"""loads all policy rules from the storage."""
16+
for lines in self._collection.find():
17+
pass
18+
19+
def save_policy(self, model):
20+
"""saves all policy rules to the storage."""
21+
pass
22+
23+
def add_policy(self, sec, ptype, rule):
24+
"""adds a policy rule to the storage."""
25+
pass
26+
27+
def remove_policy(self, sec, ptype, rule):
28+
"""removes a policy rule from the storage."""
29+
pass
30+
31+
def remove_filtered_policy(self, sec, ptype, field_index, *field_values):
32+
"""removes policy rules that match the filter from the storage.
33+
This is part of the Auto-Save feature.
34+
"""
35+
pass

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
casbin>=0.8.4
2+
pymongo>=3.10.1

setup.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import setuptools
2+
3+
desc_file = "README.md"
4+
5+
with open(desc_file, "r") as fh:
6+
long_description = fh.read()
7+
8+
setuptools.setup(
9+
name="casbin_pymongo_adapter",
10+
version="0.1.0",
11+
author="TechLee",
12+
author_email="techlee@qq.com",
13+
description="PyMongo Adapter for PyCasbin",
14+
long_description=long_description,
15+
long_description_content_type="text/markdown",
16+
url="https://github.com/pycasbin/pymongo-adapter",
17+
keywords=["casbin", "pymongo", "casbin-adapter", "rbac", "access control", "abac", "acl", "permission"],
18+
packages=setuptools.find_packages(),
19+
install_requires=['casbin>=0.8.4', 'pymongo>=3.10.1'],
20+
python_requires=">=3.3",
21+
license="Apache 2.0",
22+
classifiers=[
23+
"Programming Language :: Python :: 3.3",
24+
"Programming Language :: Python :: 3.4",
25+
"Programming Language :: Python :: 3.5",
26+
"Programming Language :: Python :: 3.6",
27+
"Programming Language :: Python :: 3.7",
28+
"Programming Language :: Python :: 3.8",
29+
"License :: OSI Approved :: Apache Software License",
30+
"Operating System :: OS Independent",
31+
],
32+
data_files=[desc_file],
33+
)

0 commit comments

Comments
 (0)