-1

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)


Niklas
  • 436
  • 1
  • 4
  • 16
  • When you run it in Flask, does it run from a different computer from when you run it just as a regular script? – Gabriel Luci Jan 12 '22 at 18:38
  • No, it is the same computer. – Niklas Jan 12 '22 at 18:41
  • Do you know which line is throwing the exception? – Gabriel Luci Jan 12 '22 at 18:57
  • Thats the output I get from Flask: https://pastebin.com/swLH5RyR – Niklas Jan 12 '22 at 19:07
  • I know you changed the OU before posting, but this looks suspicious: `"ou=OU1, ou=OU2, ou=OU3, dc=my, dc=domain"` - There should be no spaces. For example, if your domain is `example.com` and you want to put the new user in your `Users` OU, the DN should look like this: `OU=Users,DC=example,DC=com` – Gabriel Luci Jan 12 '22 at 19:39
  • And if you want the user in `OU3`, which is inside `OU2`, which is in `OU1`, then the order is reversed in the DN, like `OU=OU3,OU=OU2,OU=OU1,DC=example,DC=com` – Gabriel Luci Jan 12 '22 at 19:45
  • I changed the Code to your solution, but it doesn't work too. My Code above runs normally when I put this in a separate script and define the variables by myself. I think it has a problem with Flask. – Niklas Jan 13 '22 at 06:58

1 Answers1

0

I fixed the error by importing the libary called pythoncom and add the initialize command in my function, like this:

import pythoncom

def createUser(firstname, lastname):
    pythoncom.CoInitialize()

And yes, it is a problem between pyad and Flask

Niklas
  • 436
  • 1
  • 4
  • 16