mirror of
https://github.com/Llloooggg/Jarvis.git
synced 2026-03-06 03:56:23 +03:00
Переписана система сессий и логина
This commit is contained in:
53
__init__.py
53
__init__.py
@@ -1,16 +1,17 @@
|
|||||||
from flask import Flask, render_template, request
|
from flask import render_template, request, redirect
|
||||||
import db_routing
|
import db_routing
|
||||||
from flask_httpauth import HTTPBasicAuth
|
from flask_login import LoginManager, current_user, login_user, login_required
|
||||||
|
from db_routing import app, db
|
||||||
import os
|
import os
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
|
login_manager = LoginManager(app)
|
||||||
|
|
||||||
if not os.path.exists('./data.db'):
|
|
||||||
db_routing.db.create_all()
|
|
||||||
|
|
||||||
app = Flask('Jarvis', static_folder='static', template_folder='templates')
|
@login_manager.user_loader
|
||||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
|
def load_user(user):
|
||||||
auth = HTTPBasicAuth()
|
user_id = user.UserID
|
||||||
|
return user_id
|
||||||
|
|
||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
@app.route('/', methods=['GET'])
|
||||||
@@ -21,26 +22,25 @@ def index():
|
|||||||
@app.route('/registration', methods=['GET', 'POST'])
|
@app.route('/registration', methods=['GET', 'POST'])
|
||||||
def register():
|
def register():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
userLogin = request.form['UserLogin']
|
userLogin = request.form['RegUserLogin']
|
||||||
userPassw = request.form['UserPassw']
|
userPassw = request.form['RegUserPassw']
|
||||||
if db_routing.add_user(userLogin, passw_hash(userPassw)):
|
if db_routing.add_user(userLogin, passw_hash(userPassw)):
|
||||||
return render_template('content.html')
|
return redirect('/content')
|
||||||
return render_template('registration.html')
|
return render_template('registration.html')
|
||||||
|
|
||||||
|
|
||||||
@auth.verify_password
|
@app.route('/login', methods=['POST'])
|
||||||
def verify_password(user_login, user_passw):
|
def login():
|
||||||
User = db_routing.find_user(user_login)
|
userLogin = request.form['LogUserLogin']
|
||||||
if User:
|
userPassw = request.form['LogUserPassw']
|
||||||
userSalt = User.UserPassw[:32]
|
user = verify_password(userLogin, userPassw)
|
||||||
if passw_hash(user_passw, userSalt) == User.UserPassw:
|
if user:
|
||||||
return True
|
login_user(user)
|
||||||
else:
|
return redirect('/content')
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/content', methods=['GET'])
|
@app.route('/content', methods=['GET'])
|
||||||
@auth.login_required
|
@login_required
|
||||||
def content():
|
def content():
|
||||||
return render_template('content.html')
|
return render_template('content.html')
|
||||||
|
|
||||||
@@ -58,5 +58,18 @@ def passw_hash(user_passw, salt=os.urandom(32)):
|
|||||||
return storage
|
return storage
|
||||||
|
|
||||||
|
|
||||||
|
def verify_password(user_login, user_passw):
|
||||||
|
User = db_routing.find_user(user_login)
|
||||||
|
if User:
|
||||||
|
userSalt = User.UserPassw[:32]
|
||||||
|
if passw_hash(user_passw, userSalt) == User.UserPassw:
|
||||||
|
return User
|
||||||
|
else:
|
||||||
|
print('Неверный пароль')
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
if not os.path.exists('./data.db'):
|
||||||
|
db.create_all()
|
||||||
app.run()
|
app.run()
|
||||||
|
|||||||
@@ -1,23 +1,46 @@
|
|||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from __init__ import app
|
from flask import Flask
|
||||||
|
|
||||||
|
|
||||||
|
app = Flask('Jarvis', static_folder='static', template_folder='templates')
|
||||||
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
|
||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
|
|
||||||
class User(db.Model):
|
class User(db.Model):
|
||||||
__tablename__ = 'users'
|
__tablename__ = 'Users'
|
||||||
UserID = db.Column(db.Integer, primary_key=True)
|
UserID = db.Column(db.Integer, primary_key=True)
|
||||||
UserName = db.Column(db.String(80), unique=True, nullable=False)
|
UserName = db.Column(db.String(80), unique=True, nullable=False)
|
||||||
UserPassw = db.Column(db.String(120), nullable=False)
|
UserPassw = db.Column(db.String(120), nullable=False)
|
||||||
|
|
||||||
|
|
||||||
|
class Trigger(db.Model):
|
||||||
|
__tablename__ = 'Triggers'
|
||||||
|
TriggerID = db.Column(db.Integer, primary_key=True)
|
||||||
|
TriggerName = db.Column(db.String(80), unique=True, nullable=False)
|
||||||
|
TriggerArgs = db.Column(db.String(200))
|
||||||
|
|
||||||
|
|
||||||
|
class Action(db.Model):
|
||||||
|
__tablename__ = 'Actions'
|
||||||
|
ActionID = db.Column(db.Integer, primary_key=True)
|
||||||
|
ActionName = db.Column(db.String(80), unique=True, nullable=False)
|
||||||
|
ActionArgs = db.Column(db.String(200))
|
||||||
|
|
||||||
|
|
||||||
|
class Scenario(db.Model):
|
||||||
|
__tablename__ = 'Scenarios'
|
||||||
|
ScenarioID = db.Column(db.Integer, primary_key=True)
|
||||||
|
ScenarioTrigger = db.Column(db.Integer, nullable=False)
|
||||||
|
ScenarioAction = db.Column(db.Integer, nullable=False)
|
||||||
|
|
||||||
|
|
||||||
def add_user(user_name, passw_hash):
|
def add_user(user_name, passw_hash):
|
||||||
if not find_user(user_name):
|
if not find_user(user_name):
|
||||||
new_user = User(UserName=user_name, UserPassw=passw_hash)
|
new_user = User(UserName=user_name, UserPassw=passw_hash)
|
||||||
db.session.add(new_user)
|
db.session.add(new_user)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return True
|
return new_user
|
||||||
else:
|
else:
|
||||||
print('Логин занят')
|
print('Логин занят')
|
||||||
return False
|
return False
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ Flask==1.1.1
|
|||||||
Flask-HTTPAuth==3.3.0
|
Flask-HTTPAuth==3.3.0
|
||||||
Flask-SQLAlchemy==2.4.1
|
Flask-SQLAlchemy==2.4.1
|
||||||
itsdangerous==1.1.0
|
itsdangerous==1.1.0
|
||||||
Jinja2==2.11.1
|
Jinja2==3.0.0a1
|
||||||
MarkupSafe==1.1.1
|
MarkupSafe==1.1.1
|
||||||
SQLAlchemy==1.3.15
|
SQLAlchemy==1.3.15
|
||||||
Werkzeug==1.0.0
|
Werkzeug==1.0.0
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
<title>jarvis</title>
|
<title>jarvis</title>
|
||||||
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename = './css/main.css') }}">
|
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename = './css/main.css') }}">
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
<header class="header">
|
<header class="header">
|
||||||
<div class="container clearfix">
|
<div class="container clearfix">
|
||||||
<div class="header_left clearfix">
|
<div class="header_left clearfix">
|
||||||
<a href="/" class="logo-decor">
|
<a class="logo-decor">
|
||||||
<img class="logo_icon" src="{{ url_for('static', filename='./img/815a836e51c6f20226cb07df7f32895c.png') }}" alt="">
|
<img class="logo_icon" src="{{ url_for('static', filename='./img/815a836e51c6f20226cb07df7f32895c.png') }}" alt="">
|
||||||
<p class="logo-text">J.a.r.v.i.s </p>
|
<p class="logo-text">J.a.r.v.i.s </p>
|
||||||
</a>
|
</a>
|
||||||
@@ -32,8 +32,8 @@
|
|||||||
<label class="form__cell-header">
|
<label class="form__cell-header">
|
||||||
<input type="password" class="form__input-text-mod" placeholder="Пароль">
|
<input type="password" class="form__input-text-mod" placeholder="Пароль">
|
||||||
</label>
|
</label>
|
||||||
<a href="" class="input">
|
<a href="/login" class="input">
|
||||||
<input type="button" href="/content.html" name="scriptbutton" class="header_btn" value="Войти" >
|
<input type="button" href="/login" name="scriptbutton" class="header_btn" value="Войти" >
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user