fix: add readme, small fix (#2)
diff --git a/README.md b/README.md index eb6dd49..827f9c0 100644 --- a/README.md +++ b/README.md
@@ -1 +1,103 @@ -# django-authorization-example \ No newline at end of file +# Django-Authorization Example + +[Django-authorization, or dauthz](https://github.com/pycasbin/django-authorization) is an authorization library for Django framework. + + + + + +## How To Run the Example + +``` +pip install -r requirements.txt +python manage.py makemigrations +python manage.py migrate +python manage.py runserver +``` + +## Configure of Example: by step + +### 1.add the django-orm-adapter and dauthz to INSTALLED_APPS + +```python +INSTALLED_APPS = [ + ... + # STEP1: setup adapter(django-orm-adapter here) + 'casbin_adapter.apps.CasbinAdapterConfig', + # STEP2: setup django-authorization + 'dauthz.apps.DauthzConfig', + # STEP3: setup the app of your app + 'user_management.apps.UserManagementConfig', + ... +] +``` + +### 2.add the middlewares you need to MIDDLEWARES + +```python +MIDDLEWARE = [ + ... + 'django.contrib.auth.middleware.AuthenticationMiddleware', + # STEP2: setup django-authorization + # be aware: should after AuthenticationMiddleware + "dauthz.middlewares.request_middleware.RequestMiddleware", + ... +] +``` + +### 3.add more config of adapter and dauthz + +```python +# STEP1: setup adapter(django-orm-adapter here) +CASBIN_MODEL = os.path.join(BASE_DIR, 'dauthz_model.conf') +# STEP2: setup django-authorization +DAUTHZ = { + # DEFAULT Dauthz enforcer + "DEFAULT": { + # Casbin model setting. + "MODEL": { + # Available Settings: "file", "text" + "CONFIG_TYPE": "file", + "CONFIG_FILE_PATH": CASBIN_MODEL, + "CONFIG_TEXT": "", + }, + # Casbin adapter. + "ADAPTER": { + "NAME": "casbin_adapter.adapter.Adapter", + }, + "LOG": { + # Changes whether Dauthz will log messages to the Logger. + "ENABLED": False, + }, + }, +} +``` + +### 4. Add RBAC model to database(in /user_management/apps.py) + +```python +p_rules = [ + ["anonymous", "/", "(GET)|(POST)"], + ["anonymous", "/login", "(GET)|(POST)"], + ["anonymous", "/register", "(GET)|(POST)"], + ["normal_user", "/logout", "(GET)|(POST)"], + ["admin", "/all_users_profile", "(GET)|(POST)"], + ] +g_rules = [ + ["normal_user", "anonymous"], + ["admin", "normal_user"] +] +enforcer.add_policies(p_rules) +enforcer.add_grouping_policies(g_rules) +enforcer.save_policy() +``` + +#### Model of Example : + + + +### 5. Completed. + +## License + +This project is licensed under the [Apache 2.0 license](https://github.com/php-casbin/laravel-authz/blob/master/LICENSE). \ No newline at end of file
diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1872bec --- /dev/null +++ b/requirements.txt
@@ -0,0 +1,3 @@ +Django +casbin-django-orm-adapter +django_authorization
diff --git a/user_management/apps.py b/user_management/apps.py index f0d5d5a..39734f4 100644 --- a/user_management/apps.py +++ b/user_management/apps.py
@@ -1,23 +1,30 @@ +import os + from django.apps import AppConfig +def load_policy(): + from dauthz.core import enforcer + p_rules = [ + ["anonymous", "/", "(GET)|(POST)"], + ["anonymous", "/login", "(GET)|(POST)"], + ["anonymous", "/register", "(GET)|(POST)"], + ["normal_user", "/logout", "(GET)|(POST)"], + ["admin", "/all_users_profile", "(GET)|(POST)"], + ] + g_rules = [ + ["normal_user", "anonymous"], + ["admin", "normal_user"] + ] + enforcer.add_policies(p_rules) + enforcer.add_grouping_policies(g_rules) + enforcer.save_policy() + + class UserManagementConfig(AppConfig): default_auto_filed = 'django.db.models.BigAutoField' name = 'user_management' def ready(self): - from dauthz.core import enforcer - p_rules = [ - ["anonymous", "/", "(GET)|(POST)"], - ["anonymous", "/login", "(GET)|(POST)"], - ["anonymous", "/register", "(GET)|(POST)"], - ["normal_user", "/logout", "(GET)|(POST)"], - ["admin", "/all_users_profile", "(GET)|(POST)"], - ] - g_rules = [ - ["normal_user", "anonymous"], - ["admin", "normal_user"] - ] - enforcer.add_policies(p_rules) - enforcer.add_grouping_policies(g_rules) - enforcer.save_policy() + if os.environ.get('RUN_MAIN'): + load_policy()