def do_admin_login(): """ Check if the entered user and password are in the db. Only allowed the POST method :return: redirect to main function of the main module """ # enter in the page by the GET method, only render the login.html page if request.method == 'GET': return render_template('login.html') else: # enter in the page by a POST method (filled the register page) # next = request.form['next'] username = str(request.form['username']) password = str(request.form['password']) # password has been hashed with md5 in the db, to avoid collecting in clear password hashed_password = hashlib.md5(password.encode()) result = user_db.get_specific_user({ "username": username, "password": hashed_password.hexdigest() }) if result: session['logged_in'] = True session['user'] = username session['role'] = result['role'] session.permanent = True # uncomment/comment to activate/deactivate expiring session log_queue.put( ["INFO", 'User "{}" logged correctly'.format(username)]) else: flash('Username and/or Password are incorrect', 'warning') log_queue.put(["DEBUG", 'Username and/or Password are incorrect']) return redirect(url_for('home'))
def register(): """ Register a new user for the SO platform. Allowed GET and POST methods. :return: rendering of the corresponding html page """ # enter in the page by the GET method, only render the register.html page if request.method == 'GET': return render_template('register.html') # enter in the page by a POST method (filled the register page) else: username = request.form['username'] password = request.form['password'] password_confirmed = request.form['password_confirmed'] if password != password_confirmed: logger.debug("Confirmed password is not the same!") flash('Confirmed password is not the same!', 'danger') return render_template("register.html") # hashing the password with md5 user = user_db.get_specific_user({"username": username}) # check if the entered 'user' is already in the db if user: log_queue.put(["DEBUG", "Username already in database".format(username)]) flash('Username already registered', 'danger') return render_template("register.html") else: hash_object = hashlib.md5(password.encode()) # insert the new user (and hashed password) in the db user_db.insert_user({"username": username, "password": hash_object.hexdigest(), "role": "Admin"}) log_queue.put(["INFO", 'User "{}" successfully registered'.format(username)]) flash('User "{}" successfully registered'.format(username), 'success') return render_template("login.html")