diff --git a/manager/logger.py b/manager/logger.py index 98bc67a..aa3733b 100644 --- a/manager/logger.py +++ b/manager/logger.py @@ -1,12 +1,17 @@ from django.dispatch import receiver from .signals import siglog -from .models import Device, Organization, Network, Model, Wifi, DeviceLog +from .models import Device, Wifi, DeviceLog, UserLog, WifiLog +from django.contrib.auth.models import User @receiver(siglog) def logger(sender, **kwargs): if sender == Device: logger = DeviceLog + elif sender == User: + logger = UserLog + elif sender == Wifi: + logger = WifiLog else: return @@ -18,19 +23,3 @@ def logger(sender, **kwargs): newvalue=kwargs["newvalue"] ) -# device = models.CharField("Device ID", max_length=64) -# user = models.CharField("User Name", max_length=128, null=True) -# date = models.DateTimeField("Action Date", auto_now_add=True) -# action = models.IntegerField("Action", choices=ACTION_CHOICES) -# oldvalue = models.CharField("Old Value", max_length=256, blank=True, null=True) -# newvalue = models.CharField("New Value", max_length=256, blank=True, null=True) - -# ADD = 0 -# REMOVE = 1 -# NAME = 2 -# NET = 3 -# REBOOT = 4 -# UPDATE = 5 -# STATUS = 6 -# INTERNALIP = 7 -# WIFI = 8 diff --git a/manager/migrations/0036_auto_20190119_0829.py b/manager/migrations/0036_auto_20190119_0829.py new file mode 100644 index 0000000..c8cca83 --- /dev/null +++ b/manager/migrations/0036_auto_20190119_0829.py @@ -0,0 +1,35 @@ +# Generated by Django 2.1.5 on 2019-01-19 08:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('manager', '0035_auto_20190118_1219'), + ] + + operations = [ + migrations.CreateModel( + name='UserLog', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('objid', models.CharField(max_length=64, verbose_name='Target User Name')), + ('user', models.CharField(max_length=128, null=True, verbose_name='Source User Name')), + ('date', models.DateTimeField(auto_now_add=True, verbose_name='Action Date')), + ('action', models.IntegerField(choices=[(0, 'ADD'), (1, 'REMOVE'), (2, 'NAME'), (3, 'MAIL'), (4, 'STAFF'), (5, 'SUPERUSER'), (6, 'ORGA')], verbose_name='Action')), + ('oldvalue', models.CharField(blank=True, max_length=256, null=True, verbose_name='Old Value')), + ('newvalue', models.CharField(blank=True, max_length=256, null=True, verbose_name='New Value')), + ], + ), + migrations.AlterField( + model_name='device', + name='wifi', + field=models.ManyToManyField(blank=True, to='manager.Wifi'), + ), + migrations.AlterField( + model_name='devicelog', + name='action', + field=models.IntegerField(choices=[(0, 'ADD'), (1, 'REMOVE'), (2, 'NAME'), (3, 'NET'), (4, 'REBOOT'), (5, 'UPDATE'), (6, 'STATUS'), (7, 'INTERNALIP'), (8, 'WIFI'), (9, 'ORGA')], verbose_name='Action'), + ), + ] diff --git a/manager/migrations/0037_auto_20190119_0912.py b/manager/migrations/0037_auto_20190119_0912.py new file mode 100644 index 0000000..6b8aafd --- /dev/null +++ b/manager/migrations/0037_auto_20190119_0912.py @@ -0,0 +1,30 @@ +# Generated by Django 2.1.5 on 2019-01-19 09:12 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('manager', '0036_auto_20190119_0829'), + ] + + operations = [ + migrations.CreateModel( + name='WifiLog', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('objid', models.CharField(max_length=64, verbose_name='Wifi Name')), + ('user', models.CharField(max_length=128, null=True, verbose_name='User Name')), + ('date', models.DateTimeField(auto_now_add=True, verbose_name='Action Date')), + ('action', models.IntegerField(choices=[(0, 'ADD'), (1, 'REMOVE'), (2, 'NAME'), (3, 'SSID'), (4, 'KEY'), (5, 'ORGA')], verbose_name='Action')), + ('oldvalue', models.CharField(blank=True, max_length=256, null=True, verbose_name='Old Value')), + ('newvalue', models.CharField(blank=True, max_length=256, null=True, verbose_name='New Value')), + ], + ), + migrations.AlterField( + model_name='device', + name='wifi', + field=models.ManyToManyField(blank=True, to='manager.Wifi'), + ), + ] diff --git a/manager/models.py b/manager/models.py index 40ecd24..fa32393 100644 --- a/manager/models.py +++ b/manager/models.py @@ -75,6 +75,7 @@ class DeviceLog(models.Model): STATUS = 6 INTERNALIP = 7 WIFI = 8 + ORGA = 9 ACTION_CHOICES = ( (ADD, "ADD"), @@ -85,7 +86,8 @@ class DeviceLog(models.Model): (UPDATE, "UPDATE"), (STATUS, "STATUS"), (INTERNALIP, "INTERNALIP"), - (WIFI, "WIFI") + (WIFI, "WIFI"), + (ORGA, "ORGA") ) objid = models.CharField("Device ID", max_length=64) @@ -94,3 +96,54 @@ class DeviceLog(models.Model): action = models.IntegerField("Action", choices=ACTION_CHOICES) oldvalue = models.CharField("Old Value", max_length=256, blank=True, null=True) newvalue = models.CharField("New Value", max_length=256, blank=True, null=True) + +class UserLog(models.Model): + ADD = 0 + REMOVE = 1 + NAME = 2 + MAIL = 3 + STAFF = 4 + SUPERUSER = 5 + ORGA = 6 + + ACTION_CHOICES = ( + (ADD, "ADD"), + (REMOVE, "REMOVE"), + (NAME, "NAME"), + (MAIL, "MAIL"), + (STAFF, "STAFF"), + (SUPERUSER, "SUPERUSER"), + (ORGA, "ORGA") + ) + + objid = models.CharField("Target User Name", max_length=64) + user = models.CharField("Source User Name", max_length=128, null=True) + date = models.DateTimeField("Action Date", auto_now_add=True) + action = models.IntegerField("Action", choices=ACTION_CHOICES) + oldvalue = models.CharField("Old Value", max_length=256, blank=True, null=True) + newvalue = models.CharField("New Value", max_length=256, blank=True, null=True) + +class WifiLog(models.Model): + ADD = 0 + REMOVE = 1 + NAME = 2 + SSID = 3 + KEY = 4 + ORGA = 5 + + ACTION_CHOICES = ( + (ADD, "ADD"), + (REMOVE, "REMOVE"), + (NAME, "NAME"), + (SSID, "SSID"), + (KEY, "KEY"), + (ORGA, "ORGA") + ) + + objid = models.CharField("Wifi Name", max_length=64) + user = models.CharField("User Name", max_length=128, null=True) + date = models.DateTimeField("Action Date", auto_now_add=True) + action = models.IntegerField("Action", choices=ACTION_CHOICES) + oldvalue = models.CharField("Old Value", max_length=256, blank=True, null=True) + newvalue = models.CharField("New Value", max_length=256, blank=True, null=True) + diff --git a/manager/signals.py b/manager/signals.py index 45ced88..29985e0 100644 --- a/manager/signals.py +++ b/manager/signals.py @@ -1,13 +1,16 @@ from django.dispatch import Signal -from .models import Device, Organization, Network, Model, Wifi, DeviceLog +from .models import Device, Wifi, DeviceLog, UserLog, WifiLog +from django.contrib.auth.models import User siglog = Signal(providing_args=["id", "user", "action", "oldvalue", "newvalue"]) +# Device Signals + def sigDevice(device_id, user, action, oldvalue=None, newvalue=None): siglog.send(sender=Device, id=device_id, user=user, action=action, oldvalue=oldvalue, newvalue=newvalue) def sigDeleteDevice(device_id, user): - sigDevice(device_id, user, DeviceLog.DELETE) + sigDevice(device_id, user, DeviceLog.REMOVE) def sigAddDevice(device_id, user): sigDevice(device_id, user, DeviceLog.ADD) @@ -31,3 +34,55 @@ def sigRebootDevice(device_id, user, newstate): def sigUpdateDevice(device_id, user, newstate): sigDevice(device_id, user, DeviceLog.UPDATE, str(not newstate), str(newstate)) + +def sigOrgaDevice(device_id, user, oldorga, neworga): + sigDevice(device_id, user, DeviceLog.ORGA, oldorga, neworga) + +# User Signals + +def sigUser(target, source, action, oldvalue=None, newvalue=None): + siglog.send(sender=User, id=target, user=source, action=action, oldvalue=oldvalue, newvalue=newvalue) + +def sigAddUser(newuser, source): + sigUser(newuser, source, UserLog.ADD) + +def sigDeleteUser(target, source): + sigUser(target, source, UserLog.REMOVE) + +def sigRenameUser(target, source, oldname, newname): + sigUser(target, source, UserLog.NAME, oldname, newname) + +def sigMailUser(target, source, oldmail, newmail): + sigUser(target, source, UserLog.MAIL, oldmail, newmail) + +def sigStaffUser(target, source, newstatus): + sigUser(target, source, UserLog.STAFF, str(not newstatus), str(newstatus)) + +def sigSuperUser(target, source, newstatus): + sigUser(target, source, UserLog.SUPERUSER, str(not newstatus), str(newstatus)) + +def sigOrgaUser(target, source, oldorga, neworga): + sigUser(target, source, UserLog.ORGA, oldorga, neworga) + +# WiFi Signals + +def sigWifi(wifi, user, action, oldvalue=None, newvalue=None): + siglog.send(sender=Wifi, id=wifi, user=user, action=action, oldvalue=oldvalue, newvalue=newvalue) + +def sigAddWifi(wifi, user): + sigWifi(wifi, user, WifiLog.ADD) + +def sigDeleteWifi(wifi, user): + sigWifi(wifi, user, WifiLog.REMOVE) + +def sigRenameWifi(wifi, user, oldname, newname): + sigWifi(wifi, user, WifiLog.NAME, oldname, newname) + +def sigSSIDWifi(wifi, user, oldssid, newssid): + sigWifi(wifi, user, WifiLog.SSID, oldssid, newssid) + +def sigKeyWifi(wifi, user, oldkey, newkey): + sigWifi(wifi, user, WifiLog.KEY, oldkey, newkey) + +def sigOrgaWifi(wifi, user, oldorga, neworga): + sigWifi(wifi, user, WifiLog.ORGA, oldorga, neworga) diff --git a/manager/views.py b/manager/views.py index 8e37990..72e2dd7 100644 --- a/manager/views.py +++ b/manager/views.py @@ -298,7 +298,6 @@ def editdevice(request, device_id): for wifi in Wifi.objects.filter(organization=device[0].organization): wifis.add(wifi) - if request.POST.get("subnet", ""): subnet = Network.objects.filter(intip=request.POST.get("subnet", device[0].network.intip if device[0].network else "No VPN")) @@ -356,16 +355,30 @@ def edituser(request, user_id): return redirect("/") if request.POST.get("form", ""): - user[0].first_name = request.POST.get("firstname", "") - user[0].last_name = request.POST.get("lastname", "") + newfirst = request.POST.get("firstname", "") + newlast = request.POST.get("lastname", "") + if newlast != user[0].last_name or newfirst != user[0].first_name: + sigRenameUser(user[0].username, request.user.username, "%s %s" % (user[0].first_name, user[0].last_name), "%s %s" % (newfirst, newlast)) + user[0].first_name = request.POST.get("firstname", "") + user[0].last_name = request.POST.get("lastname", "") if request.user.is_staff or request.user.is_superuser: - user[0].is_staff = True if request.POST.get("staff", "0") == "True" else False + newstaff = request.POST.get("staff", "0") == "True" + if newstaff != user[0].is_staff: + sigStaffUser(user[0].username, request.user.username, newstaff) + user[0].is_staff = newstaff if request.user.is_superuser: - user[0].is_superuser = True if request.POST.get("superuser", "0") == "True" else False + newsuper = request.POST.get("superuser", "0") == "True" + if newsuper != user[0].is_superuser: + sigSuperUser(user[0].username, request.user.username, newsuper) + user[0].is_superuser = newsuper + + newmail = request.POST.get("email", "") + if newmail != user[0].email: + sigMailUser(user[0].username, request.user.username, user[0].email, newmail) + user[0].email = newmail - user[0].email = request.POST.get("email", "") user[0].save() return redirect("/") @@ -391,9 +404,21 @@ def editwifi(request, wifi_id): return redirect("/") if request.POST.get("serial", ""): - wifi[0].serial = request.POST.get("serial", "") - wifi[0].ssid = request.POST.get("ssid", "") - wifi[0].key = request.POST.get("key", "") + newserial = request.POST.get("serial", "") + if newserial != wifi[0].serial: + sigRenameWifi(wifi[0].serial, request.user.username, wifi[0].serial, newserial) + wifi[0].serial = newserial + + newssid = request.POST.get("ssid", "") + if newssid != wifi[0].ssid: + sigSSIDWifi(wifi[0].serial, request.user.username, wifi[0].ssid, newssid) + wifi[0].ssid = newssid + + newkey = request.POST.get("key", "") + if newkey != wifi[0].key: + sigKeyWifi(wifi[0].serial, request.user.username, wifi[0].key, newkey) + wifi[0].key = newkey + wifi[0].save() return redirect("/") diff --git a/templates/manager/editwifi.html b/templates/manager/editwifi.html index 7fec696..0b798aa 100644 --- a/templates/manager/editwifi.html +++ b/templates/manager/editwifi.html @@ -6,15 +6,15 @@ {% csrf_token %}