From 5c3010d2334694b9d402a1c11d7207748e1246cf Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Tue, 17 Sep 2019 11:23:47 -0500 Subject: [PATCH] Added models_api perm + This will be used for the model API and for Townffrey --- GeoffreyApp/api/key.py | 17 +++++++++++++++++ GeoffreyApp/api/views.py | 19 ++----------------- GeoffreyApp/apps.py | 5 ++--- .../0003_apitoken_model_api_perm.py | 18 ++++++++++++++++++ GeoffreyApp/models.py | 5 +++++ 5 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 GeoffreyApp/api/key.py create mode 100644 GeoffreyApp/migrations/0003_apitoken_model_api_perm.py diff --git a/GeoffreyApp/api/key.py b/GeoffreyApp/api/key.py new file mode 100644 index 0000000..98dfe8a --- /dev/null +++ b/GeoffreyApp/api/key.py @@ -0,0 +1,17 @@ +from GeoffreyApp.models import APIToken + + +def check_request_for_key(request): + if "api" in request: + key = request["api"] + + return APIToken.objects.get(key=key) + + return None + + +def check_key(key, **kwargs): + if APIToken.objects.filter(key=key, **kwargs).exists(): + return True + else: + return False \ No newline at end of file diff --git a/GeoffreyApp/api/views.py b/GeoffreyApp/api/views.py index b231c65..f12796d 100644 --- a/GeoffreyApp/api/views.py +++ b/GeoffreyApp/api/views.py @@ -6,24 +6,9 @@ import sys from GeoffreyApp.api.commands import RequestTypes import GeoffreyApp.api.commands as commands +from GeoffreyApp.api.key import check_request_for_key, check_key from GeoffreyApp.errors import * -from GeoffreyApp.models import APIToken, PermissionLevel - - -def check_key(key, **kwargs): - if APIToken.objects.filter(key=key, **kwargs).exists(): - return True - else: - return False - - -def check_request_for_key(request): - if "api" in request: - key = request["api"] - - return APIToken.objects.get(key=key) - - return None +from GeoffreyApp.models import PermissionLevel def run_command(request, command_name, req_type, key): diff --git a/GeoffreyApp/apps.py b/GeoffreyApp/apps.py index 9d3d5e8..452b528 100644 --- a/GeoffreyApp/apps.py +++ b/GeoffreyApp/apps.py @@ -1,9 +1,8 @@ from django.apps import AppConfig -import os - -path = os.path.dirname(os.path.abspath(__file__)) + "/assets/bots/geoffrey.py" class GeoffreyAppConfig(AppConfig): name = 'GeoffreyApp' verbose_name = "Geoffrey: Minecraft Web Database" + + diff --git a/GeoffreyApp/migrations/0003_apitoken_model_api_perm.py b/GeoffreyApp/migrations/0003_apitoken_model_api_perm.py new file mode 100644 index 0000000..7aa14f5 --- /dev/null +++ b/GeoffreyApp/migrations/0003_apitoken_model_api_perm.py @@ -0,0 +1,18 @@ +# Generated by Django 2.1.2 on 2019-09-17 16:23 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('GeoffreyApp', '0002_auto_20190818_0116'), + ] + + operations = [ + migrations.AddField( + model_name='apitoken', + name='model_api_perm', + field=models.BooleanField(default=False), + ), + ] diff --git a/GeoffreyApp/models.py b/GeoffreyApp/models.py index facecd9..ff467e1 100644 --- a/GeoffreyApp/models.py +++ b/GeoffreyApp/models.py @@ -41,6 +41,11 @@ class APIToken(models.Model): Permission to use admin commands """ + model_api_perm = models.BooleanField(default=False) + """ + Permission to access the model api + """ + def has_command_permission(self, permission_level): if permission_level == PermissionLevel.ADMIN: return self.admin_commands_perm