I have a Flask Webserver running with a site where you can create a user in my Windows Active-Directory. If I run the script as a separate file and set the variables manually it works as it should. But when I try to use input from the website in my script in my Flask program it does not work. I get the following error code: pywintypes.com_error: (-2147352567, 'Exception error occurred.', (0, None, None, 0, -2147221020), None)
. I am wondering that outside of the Flask script, it works. When I replace all varibles with strings it doesn't work either. So it seems to me that my function has a problem with Flask.
My Python code is the following:
from flask import Flask, redirect, url_for, render_template, request
from pyad import *
app = Flask(__name__)
def replaceUmlauts(string):
string = string.replace('ü', 'u')
string = string.replace('ö', 'o')
string = string.replace('ä', 'a')
string = string.replace('Ü', 'U')
string = string.replace('Ö', 'O')
string = string.replace('Ä', 'A')
return string
def createUser(firstname,lastname):
initials = replaceUmlauts(firstname)[0:1] + replaceUmlauts(lastname)[0:1]
loginName = replaceUmlauts(lastname)[0:4].lower() + replaceUmlauts(firstname)[0:2].lower()
email = replaceUmlauts(firstname).lower() + "." + replaceUmlauts(lastname).lower() + "@my.domain"
pyad.set_defaults(ldap_server="my.domain", username="Administrator", password="mypassword")
ou = pyad.adcontainer.ADContainer.from_dn("ou=OU1, ou=OU2, ou=OU3, dc=my, dc=domain")
new_user = pyad.aduser.ADUser.create(loginName, ou, password="Secret123", optional_attributes={
"givenName": firstname,
"sn": lastname,
"displayName": firstname + " " + lastname,
"mail": email,
"initials": initials
})
return True
@app.route("/")
def home():
return render_template("index.html")
@app.route("/addUser", methods=["POST", "GET"])
def addUser():
if request.method == "POST":
firstname = request.form["firstname"]
lastname = request.form["lastname"]
department = request.form["category"]
passwort = request.form["password"]
if len(firstname) == 0:
return redirect(url_for("addUser", error="The first name must not be empty!"))
exit(1)
elif any(chr.isdigit() for chr in firstname):
return redirect(url_for("addUser", error="The first name must not contain numbers!"))
exit(1)
elif len(lastname) == 0:
return redirect(url_for("addUser", error="The last name must not be empty!"))
exit(1)
elif any(chr.isdigit() for chr in lastname):
return redirect(url_for("addUser", error="The last name must not contain numbers!"))
exit(1)
elif len(passwort) < 6:
return redirect(url_for("addUser", error="The password must not have less than 6 characters!"))
exit(1)
createUser(firstname,lastname)
else:
return render_template("addUser.html")
if __name__ == "__main__":
app.run(debug=True)