2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-05 23:08:48 +00:00

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.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
'simple_history.middleware.HistoryRequestMiddleware', 'simple_history.middleware.HistoryRequestMiddleware',
'InvenTree.middleware.AuthRequiredMiddleware'
] ]
ROOT_URLCONF = 'InvenTree.urls' ROOT_URLCONF = 'InvenTree.urls'

View File

@ -129,3 +129,10 @@
.warning-msg { .warning-msg {
color: #e00; 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"> <nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid"> <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> <a class="navbar-brand" id='logo' href="{% url 'index' %}"><img src="{% static 'img/inventree.png' %}" width="40" height="40"/></a>
</div> </div>
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">

View File

@ -1,10 +1,49 @@
{% extends "base.html" %} {% load static %}
{% block content %} <!DOCTYPE html>
<h2>Login</h2> <html lang="en">
<form method="post"> <head>
{% csrf_token %}
{{ form.as_p }} <!-- Required meta tags -->
<button type="submit">Login</button> <meta charset="utf-8">
</form> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{% endblock %}
<!-- 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>