I am trying to write an application with flask to automate HRMS process for user creation in active directory. I am using my office laptop which is joined to domain.
While I am starting the debug, the code is working perfectly and making connection to LDAP which I have defined. However, when doing postman request to create user, it is not creating user in mentioned AD but to the server my laptop is joined.
Below is the code for connection with LDAP:
from ldap3 import Server, Connection, ALLimport configparserimport os#from backend.Models.user_dao import UserDaoclass ActiveDirectory: def __init__(self): self.config = self.load_config() self.server_uri = self.config['LDAP']['SERVER'] self.base_dn = self.config['LDAP']['BASE_DN'] self.domain = self.config['LDAP']['DOMAIN'] self.bind_dn = self.config['LDAP']['USERNAME'] self.bind_password = self.config['LDAP']['PASSWORD'] def load_config(self): # Get the directory of the current script current_dir = os.path.dirname(__file__) # Construct the path to the config.ini file config_file = os.path.join(current_dir, 'config.ini') config = configparser.ConfigParser() config.read(config_file) return config def connect(self): try: server = Server(self.server_uri, get_info=ALL) # Create a Connection object conn = Connection(server, user=f"{self.bind_dn}@{self.domain}", password=self.bind_password, auto_bind=True) print(f"Successfully connected to Active Directory: {self.server_uri}", conn) return conn, self.server_uri except Exception as e: print(f"Failed to connect to Active Directory: {e}") return None, None
and below is the function to create user in users.py
file which is located in backend/routes/users.py
AD_conn, server_uri = active_directory.connect()if not AD_conn: return jsonify({'error': 'Failed to connect to Active Directory'}), 500print(f"Successfully connected to Active Directory at: {server_uri}")script_path = "backend/scripts/createuser.ps1"command = ["powershell.exe", "-File", script_path,"-domainController", domain_controller,"-username", username,"-email", email,"-department", department,"-emp_id", emp_id,"-display_name", display_name,"-first_name", first_name,"-last_name", last_name,"-line_manager", line_manager,"-phone_number", phone_number,"-designation", designation]with open(log_file, "a") as log: result = subprocess.run(command, stdout=log, stderr=subprocess.STDOUT, text=True) if result.returncode == 0: return jsonify({'message': 'User onboarded successfully and created in Active Directory'}), 200 else: error_message = result.stderr.strip() return jsonify({'error': 'Failed to create user in Active Directory. See log for details.'}), 500
Need assistance on this