From d1f513e10656527e235c8bffb8cdf110027eb011 Mon Sep 17 00:00:00 2001 From: Anton Kamalov Date: Tue, 22 Oct 2024 01:29:32 +0300 Subject: [PATCH] =?UTF-8?q?=D0=90=D0=B2=D1=82=D0=BE=D1=80=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit + интеграция с другими запросами --- App/Auth/auth_route.py | 28 ++++++++++++++++++++++++++++ App/Auth/config.json | 7 +++++++ App/Auth/sql/auth.sql | 4 ++++ App/Auth/templates/auth.html | 16 ++++++++++++++++ App/Queries/req.py | 2 ++ App/app.py | 14 +++++++------- App/checker.py | 11 +++++++++++ 7 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 App/Auth/auth_route.py create mode 100644 App/Auth/config.json create mode 100644 App/Auth/sql/auth.sql create mode 100644 App/Auth/templates/auth.html create mode 100644 App/checker.py diff --git a/App/Auth/auth_route.py b/App/Auth/auth_route.py new file mode 100644 index 0000000..4298f15 --- /dev/null +++ b/App/Auth/auth_route.py @@ -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 'Неправильный логин или пароль' \ No newline at end of file diff --git a/App/Auth/config.json b/App/Auth/config.json new file mode 100644 index 0000000..75428e2 --- /dev/null +++ b/App/Auth/config.json @@ -0,0 +1,7 @@ +{ + "host": "localhost", + "port": 3306, + "user": "manager", + "password": "ilikepizza", + "db": "sklad" +} \ No newline at end of file diff --git a/App/Auth/sql/auth.sql b/App/Auth/sql/auth.sql new file mode 100644 index 0000000..c6b0820 --- /dev/null +++ b/App/Auth/sql/auth.sql @@ -0,0 +1,4 @@ +SELECT user_ID, role FROM user_table +WHERE 1=1 +AND login = '$login' +AND password = '$password'; \ No newline at end of file diff --git a/App/Auth/templates/auth.html b/App/Auth/templates/auth.html new file mode 100644 index 0000000..70ad0fe --- /dev/null +++ b/App/Auth/templates/auth.html @@ -0,0 +1,16 @@ + + + + + Authorization + + +
+ +
+ +
+ +
+ + diff --git a/App/Queries/req.py b/App/Queries/req.py index 55ba9c8..c1b7c3c 100644 --- a/App/Queries/req.py +++ b/App/Queries/req.py @@ -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', {})) diff --git a/App/app.py b/App/app.py index 6e33fbe..049d661 100644 --- a/App/app.py +++ b/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' diff --git a/App/checker.py b/App/checker.py new file mode 100644 index 0000000..a5fad32 --- /dev/null +++ b/App/checker.py @@ -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 \ No newline at end of file