Add rudimentary logging functions

This commit is contained in:
Kumi 2019-01-19 10:51:17 +01:00
parent 0c8b87bb00
commit 4391326072
7 changed files with 219 additions and 32 deletions

View file

@ -1,12 +1,17 @@
from django.dispatch import receiver from django.dispatch import receiver
from .signals import siglog 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) @receiver(siglog)
def logger(sender, **kwargs): def logger(sender, **kwargs):
if sender == Device: if sender == Device:
logger = DeviceLog logger = DeviceLog
elif sender == User:
logger = UserLog
elif sender == Wifi:
logger = WifiLog
else: else:
return return
@ -18,19 +23,3 @@ def logger(sender, **kwargs):
newvalue=kwargs["newvalue"] 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

View file

@ -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'),
),
]

View file

@ -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'),
),
]

View file

@ -75,6 +75,7 @@ class DeviceLog(models.Model):
STATUS = 6 STATUS = 6
INTERNALIP = 7 INTERNALIP = 7
WIFI = 8 WIFI = 8
ORGA = 9
ACTION_CHOICES = ( ACTION_CHOICES = (
(ADD, "ADD"), (ADD, "ADD"),
@ -85,7 +86,8 @@ class DeviceLog(models.Model):
(UPDATE, "UPDATE"), (UPDATE, "UPDATE"),
(STATUS, "STATUS"), (STATUS, "STATUS"),
(INTERNALIP, "INTERNALIP"), (INTERNALIP, "INTERNALIP"),
(WIFI, "WIFI") (WIFI, "WIFI"),
(ORGA, "ORGA")
) )
objid = models.CharField("Device ID", max_length=64) objid = models.CharField("Device ID", max_length=64)
@ -94,3 +96,54 @@ class DeviceLog(models.Model):
action = models.IntegerField("Action", choices=ACTION_CHOICES) action = models.IntegerField("Action", choices=ACTION_CHOICES)
oldvalue = models.CharField("Old Value", max_length=256, blank=True, null=True) oldvalue = models.CharField("Old Value", max_length=256, blank=True, null=True)
newvalue = models.CharField("New 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)

View file

@ -1,13 +1,16 @@
from django.dispatch import Signal 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"]) siglog = Signal(providing_args=["id", "user", "action", "oldvalue", "newvalue"])
# Device Signals
def sigDevice(device_id, user, action, oldvalue=None, newvalue=None): 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) siglog.send(sender=Device, id=device_id, user=user, action=action, oldvalue=oldvalue, newvalue=newvalue)
def sigDeleteDevice(device_id, user): def sigDeleteDevice(device_id, user):
sigDevice(device_id, user, DeviceLog.DELETE) sigDevice(device_id, user, DeviceLog.REMOVE)
def sigAddDevice(device_id, user): def sigAddDevice(device_id, user):
sigDevice(device_id, user, DeviceLog.ADD) sigDevice(device_id, user, DeviceLog.ADD)
@ -31,3 +34,55 @@ def sigRebootDevice(device_id, user, newstate):
def sigUpdateDevice(device_id, user, newstate): def sigUpdateDevice(device_id, user, newstate):
sigDevice(device_id, user, DeviceLog.UPDATE, str(not newstate), str(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)

View file

@ -298,7 +298,6 @@ def editdevice(request, device_id):
for wifi in Wifi.objects.filter(organization=device[0].organization): for wifi in Wifi.objects.filter(organization=device[0].organization):
wifis.add(wifi) wifis.add(wifi)
if request.POST.get("subnet", ""): 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")) 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("/") return redirect("/")
if request.POST.get("form", ""): if request.POST.get("form", ""):
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].first_name = request.POST.get("firstname", "")
user[0].last_name = request.POST.get("lastname", "") user[0].last_name = request.POST.get("lastname", "")
if request.user.is_staff or request.user.is_superuser: 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: 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() user[0].save()
return redirect("/") return redirect("/")
@ -391,9 +404,21 @@ def editwifi(request, wifi_id):
return redirect("/") return redirect("/")
if request.POST.get("serial", ""): if request.POST.get("serial", ""):
wifi[0].serial = request.POST.get("serial", "") newserial = request.POST.get("serial", "")
wifi[0].ssid = request.POST.get("ssid", "") if newserial != wifi[0].serial:
wifi[0].key = request.POST.get("key", "") 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() wifi[0].save()
return redirect("/") return redirect("/")

View file

@ -6,15 +6,15 @@
{% csrf_token %} {% csrf_token %}
<div class="form-group"> <div class="form-group">
<label for="serial">Common Name (Serial)</label> <label for="serial">Common Name (Serial)</label>
<input type="text" class="form-control" id="serial" value="{{ wifi.serial }}"></input> <input type="text" class="form-control" name="serial" id="serial" value="{{ wifi.serial }}"></input>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="ssid">WiFi SSID</label> <label for="ssid">WiFi SSID</label>
<input type="text" class="form-control" id="ssid" value="{{ wifi.ssid }}"></input> <input type="text" class="form-control" name="ssid" id="ssid" value="{{ wifi.ssid }}"></input>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="secret">WiFi WPA2 Key</label> <label for="secret">WiFi WPA2 Key</label>
<input type="text" class="form-control" id="key" value="{{ wifi.key }}"></input> <input type="text" class="form-control" id="key" name="key" value="{{ wifi.key }}"></input>
</div> </div>
<button type="submit" class="btn btn-success">Apply Changes</button> <button type="submit" class="btn btn-success">Apply Changes</button>
<a class="btn btn-danger" href="/" role="button">Cancel</a> <a class="btn btn-danger" href="/" role="button">Cancel</a>