Redirect if not authed

If user not logged in, redirect any requests to login page
This commit is contained in:
Oliver 2018-05-10 20:40:08 +10:00
parent 57713556d1
commit 7b478fed4e
5 changed files with 83 additions and 10 deletions

View File

@ -0,0 +1,26 @@
from django.shortcuts import HttpResponseRedirect
from django.urls import reverse_lazy
class AuthRequiredMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
assert hasattr(request, 'user')
response = self.get_response(request)
if not request.user.is_authenticated():
print(request.path_info)
if not request.path_info == reverse_lazy('login'):
return HttpResponseRedirect(reverse_lazy('login'))
# Code to be executed for each request/response after
# the view is called.
return response

View File

@ -71,6 +71,7 @@ MIDDLEWARE = [
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'simple_history.middleware.HistoryRequestMiddleware',
'InvenTree.middleware.AuthRequiredMiddleware'
]
ROOT_URLCONF = 'InvenTree.urls'

View File

@ -128,4 +128,11 @@
.warning-msg {
color: #e00;
}
.login {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

View File

@ -2,7 +2,7 @@
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<div class="navbar-header clearfix content-heading">
<a class="navbar-brand" id='logo' href="{% url 'index' %}"><img src="{% static 'img/inventree.png' %}" width="40" height="40"/></a>
</div>
<ul class="nav navbar-nav">

View File

@ -1,10 +1,49 @@
{% extends "base.html" %}
{% load static %}
{% block content %}
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
{% endblock %}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- CSS -->
<link rel="stylesheet" href="{% static 'css/bootstrap_3.3.7_css_bootstrap.min.css' %}">
<link rel="stylesheet" href="{% static 'css/select2.css' %}">
<link rel="stylesheet" href="{% static 'css/bootstrap-table.css' %}">
<link rel="stylesheet" href="{% static 'css/inventree.css' %}">
<title>
InvenTree
</title>
</head>
<body>
<div class='main body-wrapper'>
<div class='login'>
<div class="row">
<div class="col-md-2 col-md-offset-5">
<div class='clearfix content-heading'>
<img class="pull-left" src="{% static 'img/inventree.png' %}" width="60" height="60"/> <h3>InvenTree</h3>
</div>
<hr>
<div class='container-fluid'>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button class='pull-right btn btn-primary' type="submit">Login</button>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>