diff --git a/zezere/default.conf b/zezere/default.conf index 78fc39f..42bf231 100644 --- a/zezere/default.conf +++ b/zezere/default.conf @@ -4,7 +4,6 @@ debug = no allowed_hosts = localhost, localhost.localdomain secure_cookie = yes # auth_method = local, oidc -# ov_base_url = [oidc.rp] # client_id = diff --git a/zezere/migrations/0013_alter_device_mac_address.py b/zezere/migrations/0013_alter_device_mac_address.py new file mode 100644 index 0000000..fd55ac6 --- /dev/null +++ b/zezere/migrations/0013_alter_device_mac_address.py @@ -0,0 +1,20 @@ +# Generated by Django 4.0.4 on 2022-04-20 06:28 + +import django.core.validators +from django.db import migrations, models +import zezere.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('zezere', '0012_auto_20200323_1130'), + ] + + operations = [ + migrations.AlterField( + model_name='device', + name='mac_address', + field=models.CharField(max_length=20, unique=True, validators=[django.core.validators.RegexValidator('^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$'), zezere.models.validator_disallow_blacklisted_mac], verbose_name='Device MAC Address'), + ), + ] diff --git a/zezere/migrations/0014_config.py b/zezere/migrations/0014_config.py new file mode 100644 index 0000000..ac2a343 --- /dev/null +++ b/zezere/migrations/0014_config.py @@ -0,0 +1,25 @@ +# Generated by Django 4.0.4 on 2022-08-14 06:22 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('zezere', '0013_alter_device_mac_address'), + ] + + operations = [ + migrations.CreateModel( + name='Config', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('auth_token', models.CharField(default='', max_length=255)), + ('ov_base_url', models.URLField(default='', max_length=255)), + ('owner', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/zezere/models.py b/zezere/models.py index 731113d..1026126 100644 --- a/zezere/models.py +++ b/zezere/models.py @@ -208,3 +208,14 @@ class UnownedDeviceSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Device fields = ["mac_address"] + + +class Config(models.Model): + owner = models.ForeignKey( + User, on_delete=models.PROTECT, default=None, blank=True, null=True + ) + auth_token = models.CharField(max_length=255, default="") + ov_base_url = models.URLField(max_length=255, default="") + + def __str__(self): + return "configuration for %s" % self.owner diff --git a/zezere/settings.py b/zezere/settings.py index b42b882..202197b 100644 --- a/zezere/settings.py +++ b/zezere/settings.py @@ -170,5 +170,3 @@ messages.WARNING: "alert-warning", messages.ERROR: "alert-danger", } - -OV_BASE_URL = get("global", "ov_base_url", "OV_BASE_URL") diff --git a/zezere/templates/portal/configure.html b/zezere/templates/portal/configure.html new file mode 100644 index 0000000..6dabc41 --- /dev/null +++ b/zezere/templates/portal/configure.html @@ -0,0 +1,25 @@ +{% extends "./master.html" %} + +{% load rules %} + +{% block title %}Add Voucher{% endblock %} + +{% block content %} + +{% if messages %} + {% for message in messages %} + {{ message }} + {% endfor %} +{% endif %} + +

Your Configuration:

+
+ {% csrf_token %} + Enter the Authentication token:
+

+ Enter your OV Management Server URL:
+

+
+
+ +{% endblock %} diff --git a/zezere/templates/portal/master.html b/zezere/templates/portal/master.html index 43825c4..c5dd33f 100644 --- a/zezere/templates/portal/master.html +++ b/zezere/templates/portal/master.html @@ -59,6 +59,9 @@
  • Add Ownership Voucher
  • +
  • + Configure +
  • {% else %}
  • Log In diff --git a/zezere/urls.py b/zezere/urls.py index eb9b4c4..c87bdc8 100644 --- a/zezere/urls.py +++ b/zezere/urls.py @@ -43,6 +43,7 @@ path("portal/ov/", views_portal.ov, name="portal_ov"), path("portal/ov/add/", views_portal.add_ov, name="portal_ov_add"), # path("portal/ov/delete/", views_portal.remove_ov, name="portal_ov_remove"), + path("portal/configure/", views_portal.configure, name="portal_configure"), # API path("api/", include(router.urls), name="apis"), path( diff --git a/zezere/views_portal.py b/zezere/views_portal.py index 693a130..2643368 100644 --- a/zezere/views_portal.py +++ b/zezere/views_portal.py @@ -11,8 +11,7 @@ from rules.contrib.views import permission_required from ipware import get_client_ip -from zezere.models import Device, RunRequest, device_getter, SSHKey -from zezere.settings import OV_BASE_URL +from zezere.models import Device, RunRequest, device_getter, SSHKey, Config @login_required @@ -126,6 +125,9 @@ def ov(request): @login_required @require_POST def add_ov(request): + OV_BASE_URL = Config.objects.get(owner=request.user).ov_base_url + AUTH_TOKEN = Config.objects.get(owner=request.user).auth_token + payload = None no_of_vouchers = request.POST["no_of_vouchers"] @@ -146,7 +148,7 @@ def add_ov(request): headers = { "X-Number-Of-Vouchers": no_of_vouchers, "Content-Type": content_type, - "Authorization": "Bearer", + "Authorization": f"Bearer {AUTH_TOKEN}", } try: @@ -162,3 +164,32 @@ def add_ov(request): messages.error(request, "Error adding ownership voucher: {}".format(error_code)) return redirect("portal_ov") + + +@login_required +def configure(request): + if request.method == "GET": + config = Config.objects.filter(owner=request.user).first() + auth_token = config.auth_token if config else "" + ov_base_url = config.ov_base_url if config else "" + return render( + request, + "portal/configure.html", + {"auth_token": auth_token, "ov_base_url": ov_base_url}, + ) + elif request.method == "POST": + auth_token = request.POST.get("auth_token") + ov_base_url = request.POST.get("ov_base_url") + owner = request.user + if not auth_token or not ov_base_url: + messages.error(request, "Please fill in all fields") + return redirect("portal_configure") + + config = Config.objects.filter(owner=owner).first() + if config is None: + config = Config(owner=owner) + config.auth_token = auth_token + config.ov_base_url = ov_base_url + config.save() + messages.success(request, "Configuration saved") + return redirect("portal_configure")