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
|
||||
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 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')
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
|
||||
auth = HTTPBasicAuth()
|
||||
@login_manager.user_loader
|
||||
def load_user(user):
|
||||
user_id = user.UserID
|
||||
return user_id
|
||||
|
||||
|
||||
@app.route('/', methods=['GET'])
|
||||
@@ -21,26 +22,25 @@ def index():
|
||||
@app.route('/registration', methods=['GET', 'POST'])
|
||||
def register():
|
||||
if request.method == 'POST':
|
||||
userLogin = request.form['UserLogin']
|
||||
userPassw = request.form['UserPassw']
|
||||
userLogin = request.form['RegUserLogin']
|
||||
userPassw = request.form['RegUserPassw']
|
||||
if db_routing.add_user(userLogin, passw_hash(userPassw)):
|
||||
return render_template('content.html')
|
||||
return redirect('/content')
|
||||
return render_template('registration.html')
|
||||
|
||||
|
||||
@auth.verify_password
|
||||
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 True
|
||||
else:
|
||||
return False
|
||||
@app.route('/login', methods=['POST'])
|
||||
def login():
|
||||
userLogin = request.form['LogUserLogin']
|
||||
userPassw = request.form['LogUserPassw']
|
||||
user = verify_password(userLogin, userPassw)
|
||||
if user:
|
||||
login_user(user)
|
||||
return redirect('/content')
|
||||
|
||||
|
||||
@app.route('/content', methods=['GET'])
|
||||
@auth.login_required
|
||||
@login_required
|
||||
def content():
|
||||
return render_template('content.html')
|
||||
|
||||
@@ -58,5 +58,18 @@ def passw_hash(user_passw, salt=os.urandom(32)):
|
||||
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 not os.path.exists('./data.db'):
|
||||
db.create_all()
|
||||
app.run()
|
||||
|
||||
@@ -1,23 +1,46 @@
|
||||
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)
|
||||
|
||||
|
||||
class User(db.Model):
|
||||
__tablename__ = 'users'
|
||||
__tablename__ = 'Users'
|
||||
UserID = db.Column(db.Integer, primary_key=True)
|
||||
UserName = db.Column(db.String(80), unique=True, 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):
|
||||
if not find_user(user_name):
|
||||
new_user = User(UserName=user_name, UserPassw=passw_hash)
|
||||
db.session.add(new_user)
|
||||
db.session.commit()
|
||||
return True
|
||||
return new_user
|
||||
else:
|
||||
print('Логин занят')
|
||||
return False
|
||||
|
||||
@@ -3,7 +3,7 @@ Flask==1.1.1
|
||||
Flask-HTTPAuth==3.3.0
|
||||
Flask-SQLAlchemy==2.4.1
|
||||
itsdangerous==1.1.0
|
||||
Jinja2==2.11.1
|
||||
Jinja2==3.0.0a1
|
||||
MarkupSafe==1.1.1
|
||||
SQLAlchemy==1.3.15
|
||||
Werkzeug==1.0.0
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<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>
|
||||
|
||||
<body>
|
||||
@@ -14,7 +14,7 @@
|
||||
<header class="header">
|
||||
<div class="container 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="">
|
||||
<p class="logo-text">J.a.r.v.i.s </p>
|
||||
</a>
|
||||
@@ -32,8 +32,8 @@
|
||||
<label class="form__cell-header">
|
||||
<input type="password" class="form__input-text-mod" placeholder="Пароль">
|
||||
</label>
|
||||
<a href="" class="input">
|
||||
<input type="button" href="/content.html" name="scriptbutton" class="header_btn" value="Войти" >
|
||||
<a href="/login" class="input">
|
||||
<input type="button" href="/login" name="scriptbutton" class="header_btn" value="Войти" >
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user