Авторизация
+ интеграция с другими запросами
This commit is contained in:
28
App/Auth/auth_route.py
Normal file
28
App/Auth/auth_route.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from flask import request, Blueprint, render_template, session, redirect, url_for
|
||||
from os import path
|
||||
from db.sql_provider import SQLProvider
|
||||
from db.select import select_list
|
||||
import json
|
||||
|
||||
sql_provider = SQLProvider(path.join(path.dirname(__file__), 'sql'))
|
||||
auth_bp = Blueprint('auth_bp', __name__, template_folder='templates')
|
||||
|
||||
with open(path.join(path.dirname(__file__), 'config.json')) as f:
|
||||
config = json.load(f)
|
||||
|
||||
@auth_bp.route('/', methods=['GET', 'POST'])
|
||||
def auth():
|
||||
if request.method == 'GET':
|
||||
return render_template('auth.html')
|
||||
else:
|
||||
data = dict(request.form)
|
||||
print(data)
|
||||
result = select_list(config, sql_provider.get('auth.sql', data))
|
||||
if result[1]:
|
||||
session['user_id'] = result[1][0]['user_ID']
|
||||
session['role'] = result[1][0]['role']
|
||||
session['db_config'] = config
|
||||
session.permanent = True
|
||||
return redirect(url_for('index'))
|
||||
else:
|
||||
return 'Неправильный логин или пароль'
|
||||
7
App/Auth/config.json
Normal file
7
App/Auth/config.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"user": "manager",
|
||||
"password": "ilikepizza",
|
||||
"db": "sklad"
|
||||
}
|
||||
4
App/Auth/sql/auth.sql
Normal file
4
App/Auth/sql/auth.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
SELECT user_ID, role FROM user_table
|
||||
WHERE 1=1
|
||||
AND login = '$login'
|
||||
AND password = '$password';
|
||||
16
App/Auth/templates/auth.html
Normal file
16
App/Auth/templates/auth.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Authorization</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="" method="post">
|
||||
<label for="login">Login: </label>
|
||||
<input type="text" name="login"><br>
|
||||
<label for="password">Password: </label>
|
||||
<input type="password" name="password"><br>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@@ -2,11 +2,13 @@ from flask import request, Blueprint, render_template, session, redirect, url_fo
|
||||
from os import path
|
||||
from db.sql_provider import SQLProvider
|
||||
from db.select import select_list
|
||||
from checker import check_auth
|
||||
|
||||
sql_provider = SQLProvider(path.join(path.dirname(__file__), 'sql'))
|
||||
requests_bp = Blueprint('requests_bp', __name__, template_folder='templates')
|
||||
|
||||
@requests_bp.route('/', methods=['GET', 'POST'])
|
||||
@check_auth
|
||||
def sklad_zapros():
|
||||
if request.method == 'GET':
|
||||
result = select_list(session['db_config'], sql_provider.get('zagotovki.sql', {}))
|
||||
|
||||
14
App/app.py
14
App/app.py
@@ -1,22 +1,22 @@
|
||||
from flask import Flask, redirect, render_template, url_for, session, request
|
||||
from os import path
|
||||
from Queries.req import requests_bp
|
||||
import json
|
||||
|
||||
from Auth.auth_route import auth_bp
|
||||
from checker import check_auth
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = 'super secret key'
|
||||
with open(path.join(path.dirname(__file__), 'db/config.json')) as f:
|
||||
app.config['db_config'] = json.load(f)
|
||||
app.register_blueprint(requests_bp, url_prefix='/requests')
|
||||
|
||||
app.register_blueprint(requests_bp, url_prefix='/requests')
|
||||
app.register_blueprint(auth_bp, url_prefix='/auth')
|
||||
|
||||
@app.route('/')
|
||||
@check_auth
|
||||
def index():
|
||||
session['db_config'] = app.config['db_config'] # Временное решение до момента с авторизацией
|
||||
return render_template('index.html')
|
||||
return "YATTA"
|
||||
|
||||
@app.route('/logout')
|
||||
@check_auth
|
||||
def logout():
|
||||
session.clear()
|
||||
return 'OK'
|
||||
|
||||
11
App/checker.py
Normal file
11
App/checker.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from flask import render_template, redirect, url_for, session, request
|
||||
from functools import wraps
|
||||
|
||||
|
||||
def check_auth(func):
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
if 'user_id' not in session:
|
||||
return redirect(url_for('auth_bp.auth'))
|
||||
return func(*args, **kwargs)
|
||||
return wrapper
|
||||
Reference in New Issue
Block a user