From 522c02699690b672b3e302099b1436550107765d Mon Sep 17 00:00:00 2001 From: Gerben Neven Date: Sun, 17 May 2020 11:27:40 +0200 Subject: [PATCH 1/7] build proof of concept with AppConfig.ready --- authorization/__init__.py | 0 authorization/admin.py | 3 + authorization/apps.py | 5 ++ authorization/migrations/__init__.py | 0 authorization/models.py | 3 + authorization/tests.py | 3 + authorization/views.py | 10 +++ casbin_adapter/adapter.py | 53 +++++++++++- casbin_adapter/apps.py | 19 +++++ django_test/__init__.py | 0 django_test/settings.py | 122 +++++++++++++++++++++++++++ django_test/urls.py | 22 +++++ django_test/wsgi.py | 16 ++++ manage.py | 21 +++++ 14 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 authorization/__init__.py create mode 100644 authorization/admin.py create mode 100644 authorization/apps.py create mode 100644 authorization/migrations/__init__.py create mode 100644 authorization/models.py create mode 100644 authorization/tests.py create mode 100644 authorization/views.py create mode 100644 django_test/__init__.py create mode 100644 django_test/settings.py create mode 100644 django_test/urls.py create mode 100644 django_test/wsgi.py create mode 100755 manage.py diff --git a/authorization/__init__.py b/authorization/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/authorization/admin.py b/authorization/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/authorization/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/authorization/apps.py b/authorization/apps.py new file mode 100644 index 0000000..5b8d112 --- /dev/null +++ b/authorization/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class AuthorizationConfig(AppConfig): + name = 'authorization' diff --git a/authorization/migrations/__init__.py b/authorization/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/authorization/models.py b/authorization/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/authorization/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/authorization/tests.py b/authorization/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/authorization/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/authorization/views.py b/authorization/views.py new file mode 100644 index 0000000..de639c5 --- /dev/null +++ b/authorization/views.py @@ -0,0 +1,10 @@ +from django.shortcuts import render + +from casbin_adapter.adapter import Enforcer + +print('views init') +e = Enforcer() + +print('views.py enforcer', e, id(e)) + +# Create your views here. diff --git a/casbin_adapter/adapter.py b/casbin_adapter/adapter.py index f46b142..3b9db98 100644 --- a/casbin_adapter/adapter.py +++ b/casbin_adapter/adapter.py @@ -1,7 +1,58 @@ -from casbin import persist +from casbin import persist, Enforcer as BaseEnforcer from .models import CasbinRule +_enforcer = None +_ready = False + +def _mark_ready(): + global _ready + _ready = True + +def register(enforcer): + _enforcer = enforcer + +class Enforcer(BaseEnforcer): + _loaded = False + _calls = list() + + def __init__(self, *args, **kwargs): + global _ready + if _ready == True: + self._init_enforcer() + else: + attr = super().__init__ + self._record_call(attr, *args, **kwargs) + + global _enforcer + _enforcer = self + + + def _init_enforcer(self): + self._loaded = True + for call in self._calls: + attr, args, kwargs = call + attr(*args, **kwargs) + + def _record_call(self, attr, *args, **kwargs): + self._calls.append([attr, args, kwargs]) + + def __getattribute__(self, name): + if _ready: + return super().__getattribute__(name) + + attr = super().__getattribute__(name) + if name in ['__init__', '_init_enforcer', '_record_call', '_calls', '_loaded']: + return attr + else: + if hasattr(attr, '__call__'): + def proxy(*args, **kwargs): + self._record_call(attr, *args, **kwargs) + return proxy + else: + return attr + + class Adapter(persist.Adapter): """the interface for Casbin adapters.""" diff --git a/casbin_adapter/apps.py b/casbin_adapter/apps.py index 1d19f74..4924595 100644 --- a/casbin_adapter/apps.py +++ b/casbin_adapter/apps.py @@ -1,5 +1,24 @@ from django.apps import AppConfig +from django.db import connection +from django.db.utils import OperationalError class CasbinAdapterConfig(AppConfig): name = 'casbin_adapter' + + def ready(self): + try: + with connection.cursor() as cursor: + cursor.execute( + "SELECT app, name applied FROM django_migrations WHERE app = 'casbin_adapter' AND name = '0001_initial';") + row = cursor.fetchone() + if row: + from .adapter import _mark_ready, _enforcer as enforcer + + _mark_ready() + if enforcer: + print('delayed enforcer init') + enforcer._init_enforcer() + except OperationalError: + pass + diff --git a/django_test/__init__.py b/django_test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django_test/settings.py b/django_test/settings.py new file mode 100644 index 0000000..e5dafdf --- /dev/null +++ b/django_test/settings.py @@ -0,0 +1,122 @@ +""" +Django settings for django_test project. + +Generated by 'django-admin startproject' using Django 2.2.12. + +For more information on this file, see +https://docs.djangoproject.com/en/2.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/2.2/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '@-ao7r3f5tgid-w6bz4c)peb6rn@h8n((*lldjxv)drdu8i(*9' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + "casbin_adapter.apps.CasbinAdapterConfig", + 'authorization', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'django_test.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'django_test.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/2.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/2.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/2.2/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/django_test/urls.py b/django_test/urls.py new file mode 100644 index 0000000..a09381c --- /dev/null +++ b/django_test/urls.py @@ -0,0 +1,22 @@ +"""django_test URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/2.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path +from authorization import views + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/django_test/wsgi.py b/django_test/wsgi.py new file mode 100644 index 0000000..2f9b1bd --- /dev/null +++ b/django_test/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for django_test project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_test.settings') + +application = get_wsgi_application() diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..661e1dc --- /dev/null +++ b/manage.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_test.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() From 461d41a02d2683b36257056d6545c8ce65891fcc Mon Sep 17 00:00:00 2001 From: Gerben Neven Date: Sun, 17 May 2020 15:25:34 +0200 Subject: [PATCH 2/7] configure enforcer through django settings --- authorization/views.py | 8 +----- casbin_adapter/adapter.py | 54 ++------------------------------------ casbin_adapter/apps.py | 14 +++++----- casbin_adapter/enforcer.py | 42 +++++++++++++++++++++++++++++ django_test/settings.py | 2 ++ tests/settings.py | 6 +++++ 6 files changed, 60 insertions(+), 66 deletions(-) create mode 100644 casbin_adapter/enforcer.py diff --git a/authorization/views.py b/authorization/views.py index de639c5..d2fd618 100644 --- a/authorization/views.py +++ b/authorization/views.py @@ -1,10 +1,4 @@ from django.shortcuts import render -from casbin_adapter.adapter import Enforcer - -print('views init') -e = Enforcer() - -print('views.py enforcer', e, id(e)) - +from casbin_adapter.enforcer import enforcer # Create your views here. diff --git a/casbin_adapter/adapter.py b/casbin_adapter/adapter.py index 3b9db98..7ffd23d 100644 --- a/casbin_adapter/adapter.py +++ b/casbin_adapter/adapter.py @@ -1,58 +1,8 @@ -from casbin import persist, Enforcer as BaseEnforcer +from django.conf import settings +from casbin import persist from .models import CasbinRule -_enforcer = None -_ready = False - -def _mark_ready(): - global _ready - _ready = True - -def register(enforcer): - _enforcer = enforcer - -class Enforcer(BaseEnforcer): - _loaded = False - _calls = list() - - def __init__(self, *args, **kwargs): - global _ready - if _ready == True: - self._init_enforcer() - else: - attr = super().__init__ - self._record_call(attr, *args, **kwargs) - - global _enforcer - _enforcer = self - - - def _init_enforcer(self): - self._loaded = True - for call in self._calls: - attr, args, kwargs = call - attr(*args, **kwargs) - - def _record_call(self, attr, *args, **kwargs): - self._calls.append([attr, args, kwargs]) - - def __getattribute__(self, name): - if _ready: - return super().__getattribute__(name) - - attr = super().__getattribute__(name) - if name in ['__init__', '_init_enforcer', '_record_call', '_calls', '_loaded']: - return attr - else: - if hasattr(attr, '__call__'): - def proxy(*args, **kwargs): - self._record_call(attr, *args, **kwargs) - return proxy - else: - return attr - - class Adapter(persist.Adapter): """the interface for Casbin adapters.""" diff --git a/casbin_adapter/apps.py b/casbin_adapter/apps.py index 4924595..9805b76 100644 --- a/casbin_adapter/apps.py +++ b/casbin_adapter/apps.py @@ -10,15 +10,15 @@ def ready(self): try: with connection.cursor() as cursor: cursor.execute( - "SELECT app, name applied FROM django_migrations WHERE app = 'casbin_adapter' AND name = '0001_initial';") + """ + SELECT app, name applied FROM django_migrations + WHERE app = 'casbin_adapter' AND name = '0001_initial'; + """ + ) row = cursor.fetchone() if row: - from .adapter import _mark_ready, _enforcer as enforcer - - _mark_ready() - if enforcer: - print('delayed enforcer init') - enforcer._init_enforcer() + from .enforcer import enforcer + enforcer._load() except OperationalError: pass diff --git a/casbin_adapter/enforcer.py b/casbin_adapter/enforcer.py new file mode 100644 index 0000000..3132fbc --- /dev/null +++ b/casbin_adapter/enforcer.py @@ -0,0 +1,42 @@ +from django.conf import settings +from casbin import Enforcer +from .adapter import Adapter + + +class ProxyEnforcer(Enforcer): + _initialized = False + + def __init__(self, *args, **kwargs): + if self._initialized: + super().__init__(*args, **kwargs) + + def _load(self): + if self._initialized == False: + self._initialized = True + model = getattr(settings, 'CASBIN_MODEL') + enable_log = getattr(settings, 'CASBIN_LOG_ENABLED', False) + adapter = Adapter() + + super().__init__(model, adapter, enable_log) + + watcher = getattr(settings, 'CASBIN_WATCHER', None) + if watcher: + self.set_watcher(watcher) + + role_manager = getattr(settings, 'CASBIN_ROLE_MANAGER', None) + if role_manager: + self.set_role_manager(role_manager) + + def __getattribute__(self, name): + safe_methods = ['__init__', '_load', '_initialized'] + if not super().__getattribute__('_initialized') and name not in safe_methods: + raise Exception(( + "Calling enforcer attributes before django registry is ready. " + "Prevent making any calls to the enforcer on import/startup" + )) + + return super().__getattribute__(name) + + +enforcer = ProxyEnforcer() + diff --git a/django_test/settings.py b/django_test/settings.py index e5dafdf..6596010 100644 --- a/django_test/settings.py +++ b/django_test/settings.py @@ -120,3 +120,5 @@ # https://docs.djangoproject.com/en/2.2/howto/static-files/ STATIC_URL = '/static/' + +CASBIN_MODEL = os.path.join(BASE_DIR, 'tests', 'rbac_model.conf') diff --git a/tests/settings.py b/tests/settings.py index 9c4e87f..1801f31 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -1,3 +1,7 @@ +import os + +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + SECRET_KEY = 'not-a-production-secret' INSTALLED_APPS = [ @@ -37,3 +41,5 @@ ] DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}} + +CASBIN_MODEL = os.path.join(BASE_DIR, 'tests', 'rbac_model.conf') From b3517c58bd9bc16103f72d1dd8dd873a1552b9da Mon Sep 17 00:00:00 2001 From: Gerben Neven Date: Sun, 17 May 2020 16:24:00 +0200 Subject: [PATCH 3/7] update readme to new setup --- README.md | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index b1e11ee..d5d077f 100644 --- a/README.md +++ b/README.md @@ -24,42 +24,47 @@ Based on [Officially Supported Databases](https://docs.djangoproject.com/en/3.0/ ## Installation +> ``` +> pip install casbin_django_orm_adapter +> ``` ``` -pip install casbin_django_orm_adapter +pip install git+https://github.com/pycasbin/django-orm-adapter ``` -Add `casbin_adapter` to your `INSTALLED_APPS` +Add `casbin_adapter.apps.CasbinAdapterConfig` to your `INSTALLED_APPS` ```python +# settings.py +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + INSTALLED_APPS = [ ... - 'casbin_adapter', + 'casbin_adapter.apps.CasbinAdapterConfig', ... ] + +CASBIN_MODEL = os.path.join(BASE_DIR, 'casbin.conf') ``` -To run schema migration, execute `python manage.py migrate casbin_adapter +To run schema migration, execute `python manage.py migrate casbin_adapter` ## Simple Example ```python -import casbin -from casbin_adapter.adapter import Adapter - -adapter = Adapter() - -e = casbin.Enforcer('path/to/model.conf', adapter, True) - -sub = "alice" # the user that wants to access a resource. -obj = "data1" # the resource that is going to be accessed. -act = "read" # the operation that the user performs on the resource. - -if e.enforce(sub, obj, act): - # permit alice to read data1casbin_django_orm_adapter - pass -else: - # deny the request, show an error - pass +# views.py +from casbin_adapter.enforcer import enforcer + +def hello(request): + sub = "alice" # the user that wants to access a resource. + obj = "data1" # the resource that is going to be accessed. + act = "read" # the operation that the user performs on the resource. + + if e.enforce(sub, obj, act): + # permit alice to read data1casbin_django_orm_adapter + pass + else: + # deny the request, show an error + pass ``` ### Getting Help From 2f9b85495042a2ed4741d356cf258a1a01db19d2 Mon Sep 17 00:00:00 2001 From: Gerben Neven Date: Sun, 17 May 2020 16:24:12 +0200 Subject: [PATCH 4/7] catch programming error pre migrations --- casbin_adapter/apps.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/casbin_adapter/apps.py b/casbin_adapter/apps.py index 9805b76..b882249 100644 --- a/casbin_adapter/apps.py +++ b/casbin_adapter/apps.py @@ -1,6 +1,6 @@ from django.apps import AppConfig from django.db import connection -from django.db.utils import OperationalError +from django.db.utils import OperationalError, ProgrammingError class CasbinAdapterConfig(AppConfig): @@ -19,6 +19,6 @@ def ready(self): if row: from .enforcer import enforcer enforcer._load() - except OperationalError: + except (OperationalError, ProgrammingError): pass From f2db1223d8a5fff31506aed69f66948e6dccc4f8 Mon Sep 17 00:00:00 2001 From: Gerben Neven Date: Sun, 17 May 2020 16:39:23 +0200 Subject: [PATCH 5/7] retry init --- casbin_adapter/apps.py | 16 ++-------------- casbin_adapter/enforcer.py | 30 ++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/casbin_adapter/apps.py b/casbin_adapter/apps.py index b882249..f93ad4d 100644 --- a/casbin_adapter/apps.py +++ b/casbin_adapter/apps.py @@ -7,18 +7,6 @@ class CasbinAdapterConfig(AppConfig): name = 'casbin_adapter' def ready(self): - try: - with connection.cursor() as cursor: - cursor.execute( - """ - SELECT app, name applied FROM django_migrations - WHERE app = 'casbin_adapter' AND name = '0001_initial'; - """ - ) - row = cursor.fetchone() - if row: - from .enforcer import enforcer - enforcer._load() - except (OperationalError, ProgrammingError): - pass + from .enforcer import initialize_enforcer + initialize_enforcer() diff --git a/casbin_adapter/enforcer.py b/casbin_adapter/enforcer.py index 3132fbc..65cfcbc 100644 --- a/casbin_adapter/enforcer.py +++ b/casbin_adapter/enforcer.py @@ -1,5 +1,9 @@ from django.conf import settings +from django.db import connection +from django.db.utils import OperationalError, ProgrammingError + from casbin import Enforcer + from .adapter import Adapter @@ -30,13 +34,31 @@ def _load(self): def __getattribute__(self, name): safe_methods = ['__init__', '_load', '_initialized'] if not super().__getattribute__('_initialized') and name not in safe_methods: - raise Exception(( - "Calling enforcer attributes before django registry is ready. " - "Prevent making any calls to the enforcer on import/startup" - )) + initialize_enforcer() + if not super().__getattribute__('_initialized'): + raise Exception(( + "Calling enforcer attributes before django registry is ready. " + "Prevent making any calls to the enforcer on import/startup" + )) return super().__getattribute__(name) enforcer = ProxyEnforcer() + +def initialize_enforcer(): + try: + with connection.cursor() as cursor: + cursor.execute( + """ + SELECT app, name applied FROM django_migrations + WHERE app = 'casbin_adapter' AND name = '0001_initial'; + """ + ) + row = cursor.fetchone() + if row: + enforcer._load() + except (OperationalError, ProgrammingError): + pass + From b3e6577d50886957e573fbd88995501361efbd56 Mon Sep 17 00:00:00 2001 From: Gerben Neven Date: Sun, 17 May 2020 17:05:08 +0200 Subject: [PATCH 6/7] remove test django project, update test settings --- authorization/__init__.py | 0 authorization/admin.py | 3 - authorization/apps.py | 5 -- authorization/migrations/__init__.py | 0 authorization/models.py | 3 - authorization/tests.py | 3 - authorization/views.py | 4 - django_test/__init__.py | 0 django_test/settings.py | 124 --------------------------- django_test/urls.py | 22 ----- django_test/wsgi.py | 16 ---- manage.py | 21 ----- tests/settings.py | 2 +- 13 files changed, 1 insertion(+), 202 deletions(-) delete mode 100644 authorization/__init__.py delete mode 100644 authorization/admin.py delete mode 100644 authorization/apps.py delete mode 100644 authorization/migrations/__init__.py delete mode 100644 authorization/models.py delete mode 100644 authorization/tests.py delete mode 100644 authorization/views.py delete mode 100644 django_test/__init__.py delete mode 100644 django_test/settings.py delete mode 100644 django_test/urls.py delete mode 100644 django_test/wsgi.py delete mode 100755 manage.py diff --git a/authorization/__init__.py b/authorization/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/authorization/admin.py b/authorization/admin.py deleted file mode 100644 index 8c38f3f..0000000 --- a/authorization/admin.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.contrib import admin - -# Register your models here. diff --git a/authorization/apps.py b/authorization/apps.py deleted file mode 100644 index 5b8d112..0000000 --- a/authorization/apps.py +++ /dev/null @@ -1,5 +0,0 @@ -from django.apps import AppConfig - - -class AuthorizationConfig(AppConfig): - name = 'authorization' diff --git a/authorization/migrations/__init__.py b/authorization/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/authorization/models.py b/authorization/models.py deleted file mode 100644 index 71a8362..0000000 --- a/authorization/models.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.db import models - -# Create your models here. diff --git a/authorization/tests.py b/authorization/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/authorization/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/authorization/views.py b/authorization/views.py deleted file mode 100644 index d2fd618..0000000 --- a/authorization/views.py +++ /dev/null @@ -1,4 +0,0 @@ -from django.shortcuts import render - -from casbin_adapter.enforcer import enforcer -# Create your views here. diff --git a/django_test/__init__.py b/django_test/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/django_test/settings.py b/django_test/settings.py deleted file mode 100644 index 6596010..0000000 --- a/django_test/settings.py +++ /dev/null @@ -1,124 +0,0 @@ -""" -Django settings for django_test project. - -Generated by 'django-admin startproject' using Django 2.2.12. - -For more information on this file, see -https://docs.djangoproject.com/en/2.2/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/2.2/ref/settings/ -""" - -import os - -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) -BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '@-ao7r3f5tgid-w6bz4c)peb6rn@h8n((*lldjxv)drdu8i(*9' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = [] - - -# Application definition - -INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', - "casbin_adapter.apps.CasbinAdapterConfig", - 'authorization', -] - -MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', -] - -ROOT_URLCONF = 'django_test.urls' - -TEMPLATES = [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', - ], - }, - }, -] - -WSGI_APPLICATION = 'django_test.wsgi.application' - - -# Database -# https://docs.djangoproject.com/en/2.2/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), - } -} - - -# Password validation -# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators - -AUTH_PASSWORD_VALIDATORS = [ - { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', - }, -] - - -# Internationalization -# https://docs.djangoproject.com/en/2.2/topics/i18n/ - -LANGUAGE_CODE = 'en-us' - -TIME_ZONE = 'UTC' - -USE_I18N = True - -USE_L10N = True - -USE_TZ = True - - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/2.2/howto/static-files/ - -STATIC_URL = '/static/' - -CASBIN_MODEL = os.path.join(BASE_DIR, 'tests', 'rbac_model.conf') diff --git a/django_test/urls.py b/django_test/urls.py deleted file mode 100644 index a09381c..0000000 --- a/django_test/urls.py +++ /dev/null @@ -1,22 +0,0 @@ -"""django_test URL Configuration - -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/2.2/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: path('', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.urls import include, path - 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) -""" -from django.contrib import admin -from django.urls import path -from authorization import views - -urlpatterns = [ - path('admin/', admin.site.urls), -] diff --git a/django_test/wsgi.py b/django_test/wsgi.py deleted file mode 100644 index 2f9b1bd..0000000 --- a/django_test/wsgi.py +++ /dev/null @@ -1,16 +0,0 @@ -""" -WSGI config for django_test project. - -It exposes the WSGI callable as a module-level variable named ``application``. - -For more information on this file, see -https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ -""" - -import os - -from django.core.wsgi import get_wsgi_application - -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_test.settings') - -application = get_wsgi_application() diff --git a/manage.py b/manage.py deleted file mode 100755 index 661e1dc..0000000 --- a/manage.py +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env python -"""Django's command-line utility for administrative tasks.""" -import os -import sys - - -def main(): - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_test.settings') - try: - from django.core.management import execute_from_command_line - except ImportError as exc: - raise ImportError( - "Couldn't import Django. Are you sure it's installed and " - "available on your PYTHONPATH environment variable? Did you " - "forget to activate a virtual environment?" - ) from exc - execute_from_command_line(sys.argv) - - -if __name__ == '__main__': - main() diff --git a/tests/settings.py b/tests/settings.py index 1801f31..9040917 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -11,7 +11,7 @@ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", - "casbin_adapter", + "casbin_adapter.apps.CasbinAdapterConfig", "tests", ] From c5fee8cbaae5fbc53125ec7b0bddb3fc920e146b Mon Sep 17 00:00:00 2001 From: Gerben Neven Date: Mon, 18 May 2020 11:44:11 +0200 Subject: [PATCH 7/7] tidy readme --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 217e659..3ac5ffb 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,6 @@ Based on [Officially Supported Databases](https://docs.djangoproject.com/en/3.0/ ## Installation -> ``` -> pip install casbin_django_orm_adapter -> ``` ``` pip install casbin-django-orm-adapter ```