Add user management

This commit is contained in:
Kumi 2019-01-13 13:55:53 +01:00
parent ddff965bda
commit a81d1211b1
5 changed files with 108 additions and 4 deletions

View file

@ -0,0 +1,33 @@
{% extends "base.html" %}
{% block content %}
<form action="#" method="post">
{% csrf_token %}
<div class="form-group">
<label for="username">Username</label>
<input type="text" disabled class="form-control" id="username" value="{{ auser.username }}"></input>
</div>
<div class="form-group">
<label for="firstname">First Name</label>
<input type="text" class="form-control" id="firstname" name="firstname" value="{{ auser.first_name }}"></input>
</div>
<div class="form-group">
<label for="lastname">Last Name</label>
<input type="text" class="form-control" id="lastname" name="lastname" value="{{ auser.last_name }}"></input>
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" required class="form-control" id="email" name="email" value="{{ auser.email }}"></input>
</div>
<div class="form-group form-check">
<input class="form-check-input" type="checkbox" value="True" {% if auser.is_staff %} checked {% endif %} id="staff" name="staff">
<label class="form-check-label" for="staff">Grant special privileges to user</label>
</div>
<input hidden value="sent" name="form"/>
<button type="submit" class="btn btn-success">Apply Changes</button>
<a class="btn btn-danger" href="/" role="button">Cancel</a>
</form>
{% endblock %}

View file

@ -5,7 +5,7 @@
<p><b>Organization:</b> {{ organization }}</p> <p><b>Organization:</b> {{ organization }}</p>
<p><b>User:</b> {{ user.first_name }} {{ user.last_name }} ({{ user.username }})</p> <p><b>User:</b> {{ user.first_name }} {{ user.last_name }} ({{ user.username }})</p>
<div align="center"><b>Manage:</b> <a href="#" onclick="showdevices();">Devices</a> &dash; <a href="#" onclick="showwifi();">WiFi</a></div> <div align="center"><b>Manage:</b> <a href="#" onclick="showdevices();">Devices</a> &dash; <a href="#" onclick="showwifi();">WiFi</a>{% if user.is_staff %} &dash; <a href="#" onclick="showusers();">Users</a>{% endif %}</div>
<div name="devicespart" id="devicespart"> <div name="devicespart" id="devicespart">
<h2>Devices</h2> <h2>Devices</h2>
@ -61,6 +61,33 @@
</div> </div>
</div> </div>
{% if users %}
<div name="userpart" id="userpart">
<h2>Users</h2>
<div class="table-responsive">
<table id="users" name="users" class="table">
<thead>
<tr>
<th>Username</th>
<th>Real name</th>
<th>Options</th>
</tr>
</thead>
{% for auser in users %}
<tr>
<td>{% if auser.is_staff %}<b>{% endif %}{{ auser.username }}{% if auser.is_staff %}</b>{% endif %}</td>
<td>{% if auser.is_staff %}<b>{% endif %}{{ auser.first_name }} {{ auser.last_name }}{% if auser.is_staff %}</b>{% endif %}</td>
<td><a href="/user/{{ auser.id }}/edit"><i class="fas fa-edit" title="Edit User"></i></a></td>
</tr>
{% endfor %}
</table>
</div>
</div>
{% endif %}
<script src="/js/devices.js"></script> <script src="/js/devices.js"></script>
<script> <script>

View file

@ -17,5 +17,6 @@ urlpatterns = [
path('makewifi/', views.makewifi, name='makewifi'), path('makewifi/', views.makewifi, name='makewifi'),
path('wifi/<int:wifi_id>/edit/', views.editwifi, name='editwifi'), path('wifi/<int:wifi_id>/edit/', views.editwifi, name='editwifi'),
path('wifi/<int:wifi_id>/delete/', views.deletewifi, name='deletewifi'), path('wifi/<int:wifi_id>/delete/', views.deletewifi, name='deletewifi'),
path('user/<int:user_id>/edit/', views.edituser, name='edituser'),
path('update', views.update, name='update') path('update', views.update, name='update')
] ]

View file

@ -1,11 +1,12 @@
from django.shortcuts import render, get_object_or_404, redirect from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse from django.http import HttpResponse
from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm, AdminPasswordChangeForm
from django.db.models import Q from django.db.models import Q
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.utils import timezone from django.utils import timezone
from django.core.files import File from django.core.files import File
from django.db.models.fields.files import FieldFile from django.db.models.fields.files import FieldFile
from django.contrib.auth.models import User
from .models import Device, Organization, Network, Model, Wifi from .models import Device, Organization, Network, Model, Wifi
@ -256,6 +257,7 @@ def devices(request):
user = request.user user = request.user
devices = set() devices = set()
wifis = set() wifis = set()
users = set()
orga = ", ".join([x.__str__() for x in Organization.objects.filter(users=user)]) orga = ", ".join([x.__str__() for x in Organization.objects.filter(users=user)])
for organization in Organization.objects.filter(users=user): for organization in Organization.objects.filter(users=user):
@ -263,14 +265,18 @@ def devices(request):
devices.add(device) devices.add(device)
for wifi in Wifi.objects.filter(organization=organization): for wifi in Wifi.objects.filter(organization=organization):
wifis.add(wifi) wifis.add(wifi)
if user.is_staff:
for orgauser in User.objects.filter(organization=organization):
users.add(orgauser)
return render(request, "manager/index.html", return render(request, "manager/index.html",
{ {
"title": "Device Administration", "title": "Device Administration",
"user": user, "user": user,
"organization": orga, "organization": orga,
"devices": sorted(devices, key=lambda x: x.__str__()), "devices": sorted(devices, key=lambda x: x.serial),
"wifis": wifis "wifis": sorted(wifis, key=lambda x: x.serial),
"users": sorted(users, key=lambda x: x.username)
} }
) )
else: else:
@ -322,6 +328,35 @@ def editdevice(request, device_id):
else: else:
return redirect("/") return redirect("/")
def edituser(request, user_id):
if request.user.is_staff or request.user.id == user_id:
user = None
orgas = Organization.objects.filter(users=request.user)
for organization in orgas:
user = user or User.objects.filter(id=user_id, organization=organization)
if not user:
return redirect("/")
if request.POST.get("form", ""):
user[0].first_name = request.POST.get("firstname", "")
user[0].last_name = request.POST.get("lastname", "")
user[0].is_staff = True if request.POST.get("staff", "0") == "True" else False
user[0].email = request.POST.get("email", "")
user[0].save()
return redirect("/")
return render(request, "manager/edituser.html",
{
"title": "Edit User",
"auser": user[0]
}
)
else:
return redirect("/")
def editwifi(request, wifi_id): def editwifi(request, wifi_id):
if not request.user.is_authenticated: if not request.user.is_authenticated:
return redirect("/") return redirect("/")

View file

@ -68,9 +68,17 @@ function downloadnotice() {
function showdevices() { function showdevices() {
$("#devicespart").show(); $("#devicespart").show();
$("#wifipart").hide(); $("#wifipart").hide();
$("#userpart").hide();
}; };
function showwifi() { function showwifi() {
$("#devicespart").hide(); $("#devicespart").hide();
$("#wifipart").show(); $("#wifipart").show();
$("#userpart").hide();
};
function showusers() {
$("#devicespart").hide();
$("#wifipart").hide();
$("#userpart").show();
}; };