TG-124 : Correction du sytème de login + register
This commit is contained in:
parent
f4d9ebaca0
commit
711d0886d3
@ -1,11 +1,11 @@
|
|||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
|
|
||||||
from flask import session
|
from flask import session, request
|
||||||
from flask_restful import Resource
|
from flask_restful import Resource
|
||||||
from flask_restful.reqparse import RequestParser
|
|
||||||
|
|
||||||
from app.core import app
|
from app.core import app
|
||||||
from app.model import USER
|
from app.model import USER, getUser
|
||||||
|
from app.utils import checkParams
|
||||||
|
|
||||||
|
|
||||||
class LoginAPI(Resource):
|
class LoginAPI(Resource):
|
||||||
@ -14,10 +14,9 @@ class LoginAPI(Resource):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def post(self):
|
def post(self):
|
||||||
parser = RequestParser()
|
args = request.get_json(cache=False, force=True)
|
||||||
parser.add_argument('email', required=True, help="Email cannot be blank!")
|
if not checkParams(['email', 'password'], args):
|
||||||
parser.add_argument('password', required=True, help="Password cannot be blank!")
|
return {"ERROR": "One or more parameters are missing !"}, 400
|
||||||
args = parser.parse_args()
|
|
||||||
email = args['email']
|
email = args['email']
|
||||||
psw = args['password']
|
psw = args['password']
|
||||||
password = sha256(psw.encode('utf-8')).hexdigest()
|
password = sha256(psw.encode('utf-8')).hexdigest()
|
||||||
@ -27,17 +26,19 @@ class LoginAPI(Resource):
|
|||||||
|
|
||||||
query = USER.select(USER.c.email == email)
|
query = USER.select(USER.c.email == email)
|
||||||
rows = query.execute()
|
rows = query.execute()
|
||||||
user = rows.first()
|
res = rows.first()
|
||||||
|
|
||||||
if app.config['TESTING']:
|
if app.config['TESTING']:
|
||||||
if user is not None and psw == email:
|
if res is not None and psw == email:
|
||||||
|
user = getUser(uid=res.id)
|
||||||
session['user'] = user
|
session['user'] = user
|
||||||
return {'AUTH_RESULT': 'OK'}, 200
|
return {'AUTH_RESULT': 'OK'}, 200
|
||||||
else:
|
else:
|
||||||
session['user'] = None
|
session['user'] = None
|
||||||
return {'AUTH_RESULT': 'AUTHENTICATION_FAILED'}, 401
|
return {'AUTH_RESULT': 'AUTHENTICATION_FAILED'}, 401
|
||||||
else:
|
else:
|
||||||
if user is not None and password == user.psw:
|
if res is not None and password != "" and password == res.psw:
|
||||||
|
user = getUser(uid=res.id)
|
||||||
session['user'] = user
|
session['user'] = user
|
||||||
return {'AUTH_RESULT': 'OK'}, 200
|
return {'AUTH_RESULT': 'OK'}, 200
|
||||||
else:
|
else:
|
||||||
|
@ -34,15 +34,19 @@ class UserAPI(Resource):
|
|||||||
|
|
||||||
def put(self, uid):
|
def put(self, uid):
|
||||||
args = request.get_json(cache=False, force=True)
|
args = request.get_json(cache=False, force=True)
|
||||||
if not checkParams(['role', 'email', 'phone', 'name', 'password'], args):
|
if not checkParams(['role', 'email', 'phone', 'name', 'password', 'firstname'], args):
|
||||||
return {"ERROR": "One or more parameters are missing !"}, 400
|
return {"ERROR": "One or more parameters are missing !"}, 400
|
||||||
|
|
||||||
role = args['role']
|
role = args['role']
|
||||||
email = args['email']
|
email = args['email']
|
||||||
phone = args['phone']
|
phone = args['phone']
|
||||||
|
firstname = args['firstname']
|
||||||
name = args['name']
|
name = args['name']
|
||||||
psw = args['password']
|
psw = args['password']
|
||||||
|
|
||||||
|
name = firstname.title() + " " + name.upper()
|
||||||
|
# TODO : Lors de l'ajout des fiches d'absence ca sera ça le critère de recherche + le groupe
|
||||||
|
|
||||||
if psw is None or len(psw) < 8:
|
if psw is None or len(psw) < 8:
|
||||||
return {"ERROR": "Password can't be empty or less than 8 characters !"}, 400
|
return {"ERROR": "Password can't be empty or less than 8 characters !"}, 400
|
||||||
|
|
||||||
@ -64,5 +68,5 @@ class UserAPI(Resource):
|
|||||||
return {'USER': getUser(uid=uid)}, 200
|
return {'USER': getUser(uid=uid)}, 200
|
||||||
elif email != "":
|
elif email != "":
|
||||||
return {'USER': getUser(email=email)}, 200
|
return {'USER': getUser(email=email)}, 200
|
||||||
elif hash != "":
|
elif hashcode != "":
|
||||||
return {'USER': getUser(hashcode=hashcode)}, 200
|
return {'USER': getUser(hashcode=hashcode)}, 200
|
||||||
|
@ -27,7 +27,7 @@ def getParam(key):
|
|||||||
def getUser(uid=0, email="", hashcode=""):
|
def getUser(uid=0, email="", hashcode=""):
|
||||||
res = None
|
res = None
|
||||||
|
|
||||||
if uid == 0 and email == "":
|
if uid == 0 and email == "" and hashcode == "":
|
||||||
raise Exception("getUser must be called with one argument !")
|
raise Exception("getUser must be called with one argument !")
|
||||||
else:
|
else:
|
||||||
if uid != 0:
|
if uid != 0:
|
||||||
|
Reference in New Issue
Block a user