-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
89 lines (73 loc) · 2.26 KB
/
manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import json
import logging
from logging.handlers import RotatingFileHandler
from flask import jsonify, request, g
from Exception import AuthDeniedException
from Exception.jwt.JwtExpiredException import JWTExpiredException
from app import creatApp
from common.redis_client import get_redis_client
from exts import db
from flask_cors import CORS
from flask_migrate import Migrate
from model import User
from utils.jwtUtils import get_payload
def setup_logging(app):
handler = RotatingFileHandler('app.log', maxBytes=10000, backupCount=1)
handler.setLevel(logging.INFO)
app.logger.addHandler(handler)
app = creatApp()
setup_logging(app)
app.logger.info('App has started')
except_jwt = ['login', 'register', 'logout']
@app.before_request
def jwt_authentication():
if request.method == 'OPTIONS':
return
g.data = request.get_json(silent=True)
g.get_data = request.args
for i in except_jwt:
if i in request.path:
g.user_info = None
return
token = request.headers.get('Authorization')
client = get_redis_client()
if not client.get(token):
raise JWTExpiredException()
g.user_info = get_payload(token)
@app.before_request
def authorization():
if request.method == 'OPTIONS':
return
return # 先关闭
if not g.user_info: # 如果没有jwt解析的信息或者id为超级管理员,直接下一步
return
if g.user_info['id'] == 1:
return
try:
user = User.query.filter_by(id=g.user_info['id']).first()
role = user.roles[0]
perms = role.perms
for i in perms:
ps = json.loads(i.url)
for o in ps:
if request.path == o:
return
raise AuthDeniedException()
except Exception as e:
app.logger.error(e)
raise AuthDeniedException()
CORS(app, supports_credentials=True)
migrate = Migrate(app, db)
@app.route('/')
# @login_required
def hello_world(): # put application's code here
routes = []
for rule in app.url_map.iter_rules():
routes.append({
"endpoint": rule.endpoint,
"methods": list(rule.methods),
"url": rule.rule
})
return jsonify(routes)
if __name__ == '__main__':
app.run(debug=True)