feat(reset): successfully linked the app with the LDAP server
This commit is contained in:
31
app/reset.py
31
app/reset.py
@@ -1,15 +1,18 @@
|
||||
import functools
|
||||
from . import ldap_client
|
||||
|
||||
from flask import (
|
||||
Blueprint, flash, g, redirect, render_template, request, session, url_for
|
||||
Blueprint, render_template, flash,
|
||||
current_app
|
||||
)
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, PasswordField, BooleanField, \
|
||||
from wtforms import (
|
||||
StringField, PasswordField,
|
||||
SubmitField
|
||||
from wtforms.validators import ValidationError, DataRequired, \
|
||||
Email, EqualTo, Length, Regexp
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
import re
|
||||
)
|
||||
from wtforms.validators import (
|
||||
ValidationError, DataRequired,
|
||||
EqualTo, Length, Regexp
|
||||
)
|
||||
|
||||
bp = Blueprint('reset', __name__, url_prefix='/reset')
|
||||
|
||||
@@ -49,11 +52,21 @@ class ResetPasswordForm(FlaskForm):
|
||||
for char in self.username.data:
|
||||
if char in excluded_chars:
|
||||
raise ValidationError(
|
||||
f"Character {char} is not allowed in a login.")
|
||||
f"Character {char} is not allowed in an username.")
|
||||
|
||||
@bp.route('/', methods=('GET', 'POST'))
|
||||
def reset():
|
||||
form = ResetPasswordForm()
|
||||
if form.validate_on_submit():
|
||||
return f'''<h1> Welcome {form.username.data} </h1>'''
|
||||
client = ldap_client.Client(address=current_app.config["LDAP_ADDR"], port=current_app.config["LDAP_PORT"], base_dn=current_app.config["BASE_DN"], tls=current_app.config["LDAP_TLS"])
|
||||
bind_status = client.bind(form.username._value(), form.currentpassword._value())
|
||||
if bind_status[0] == False:
|
||||
flash(f"Connection failed, are you sure that your login and password are correct ? ({client.link.last_error})")
|
||||
elif client.change_pwd(bind_status[1], form.newpassword._value()) == False:
|
||||
flash(f"An error occured and your password was not changed, sorry. ({client.link.last_error})")
|
||||
client.unbind()
|
||||
else:
|
||||
flash('Your password has been changed !')
|
||||
client.unbind()
|
||||
|
||||
return render_template('reset.html', form=form)
|
||||
Reference in New Issue
Block a user