def update(): if request.method == 'POST': try: data = request.get_json() #converting to python dictionary print('Data Received: "{data}"'.format(data=data)) dn = "cn=" + data['fullname'] + "," + "cn=users," + ldap_base user_input = [i for (i, j) in data.items()] #key of all user input modifiable_attr = [ 'description', 'mobile', 'mCode', 'mail', 'description', 'mobile', 'mCode', 'mail' ] temp = [x for x in user_input if x in modifiable_att] if (len(temp) == len(user_input)): pass else: rValue = "Unmodifiable attributes passed in request" return Response(mimetype="application/json", response=rValue, status=400) #verifying correct email format if ('mail' in user_input): if (everify(data['mail']) == 0): rValue = "Incorrect email format!" return Response(mimetype="application/json", response=rValue, status=400) #verifying correct mobile number format if ('mobile' in user_input): if (pverify(data['mobile']) == 0): rValue = "Incorrect mobile number format!" return Response(mimetype="application/json", response=rValue, status=400) # entry ={"sn":data['lastname'],"description":data['description'],"telephoneNumber":data['mobile']} entry = { "sn": data['lastname'], "description": data['description'], "mobile": data['mobile'], "userPassword": data['password'] } parsed_entry = [(ldap.MOD_REPLACE, i, bytes(j, encoding='utf-8')) for i, j in entry.items()] con.modify_s(dn, parsed_entry) rValue = "Updated user : "******"\n" return Response(mimetype="application/json", response=rValue, status=200) except ldap.LDAPError as e: mssg = list(e.args)[0]['desc'] rValue = "Error while updating user: "******"application/json", response=rValue, status=400)
def update(): if request.method == 'POST': try: con.simple_bind_s(request.authorization["username"], request.authorization["password"]) data = request.get_json() #converting to python dictionary #search user to get dn filter = "(&(objectClass=*)(cn=" + data['fullname'] + "))" attr = None results = con.search_s(ldap_base, ldap.SCOPE_SUBTREE, filter, attr) if len(results) == 0: return Response(mimetype="application/json", response=json.dumps("User doesn't exists"), status=400) dn = results[0][0] user_input = [i for (i, j) in data.items()] #key of all user input #verifying correct email format if ('mail' in user_input): if (everify(data['mail']) == 0): rValue = "Incorrect email format!" return Response(mimetype="application/json", response=json.dumps(rValue), status=400) #verifying correct mobile number format if ('mobile' in user_input): if (pverify(data['mobile']) == 0): rValue = "Incorrect mobile number format!" return Response(mimetype="application/json", response=json.dumps(rValue), status=400) modifiable_attr = ['description', 'mobile', 'mCode', 'mail'] temp = [x for x in user_input if x in modifiable_attr] if (len(temp) == len(user_input) - 1): #minus 1 for fullname entry = {} if 'description' in user_input: entry['description'] = data['description'] if 'mobile' in user_input: entry['mobile'] = data['mobile'] if 'mCode' in user_input: entry['mail'] = data['mail'] if 'mail' in user_input: entry['mail'] = data['mail'] parsed_entry = [(ldap.MOD_REPLACE, i, bytes(j, encoding='utf-8')) for i, j in entry.items()] con.modify_s(dn, parsed_entry) rValue = "Updated user : "******"application/json", response=json.dumps(rValue), status=200) else: rValue = "Unmodifiable attributes passed in request" return Response(mimetype="application/json", response=json.dumps(rValue), status=400) except ldap.LDAPError as e: mssg = list(e.args)[0]['desc'] rValue = "Error while updating user: "******"application/json", response=json.dumps(rValue), status=400)
def create(): if request.method == 'POST': try: data = request.get_json() #converting to python dictionary print('Data Received: "{data}"'.format(data=data)) user_input = [i for (i, j) in data.items()] #key of all user input #verifying correct email format if ('mail' in user_input): #verify mail format only if it exists in body of user request if (everify(data['mail']) == 0): rValue = "Incorrect email format!" return Response(mimetype="application/json", response=rValue, status=400) #verifying correct mobile number format if ('mobile' in user_input): #verify mail format only if it exists in body of user request if (pverify(data['mobile']) == 0): rValue = "Incorrect mobile number format!" return Response(mimetype="application/json", response=rValue, status=400) #verifying mandatory inputs from user mandatory = [ "fullname", "lastname", "description", "mobile", "mCode", "mail", "password" ] temp = [x for x in mandatory if x in user_input] missing_attr = set(mandatory) - set(temp) if ( len(missing_attr) == 0 ): #i.e all mandatory fields are present in user input request body #adding user data to LDAP DIT dn = "cn=" + data['fullname'] + "," + "cn=users," + ldap_base entry = { "cn": data['fullname'], "sn": data['lastname'], "givenName": data['firstname'], "objectClass": "inetOrgPerson", "description": data['description'], "mobile": '+' + data['mCode'] + data['mobile'], "mail": data['mail'], "userPassword": data['password'], "ou": data['ou'], "uid": data['uid'] } parsed_entry = [(i, bytes(j, encoding='utf-8')) for i, j in entry.items()] con.add_s(dn, parsed_entry) rValue = "Created user : "******"\n" return Response(mimetype="application/json", response=rValue, status=200) else: #missing mandatory fields! Exit with 400 rValue = "Missing mandatory user attributes " + str( missing_attr) + "\n" return Response(mimetype="application/json", response=rValue, status=400) except ldap.LDAPError as e: mssg = list(e.args)[0]['desc'] rValue = "Error while adding user: "******"\n" return Response(mimetype="application/json", response=rValue, status=400)
def create(): if request.method == 'POST': try: con.simple_bind_s(request.authorization["username"], request.authorization["password"]) data = request.get_json() #converting to python dictionary #exit if Business Unit doesn't exist buFilter = "(&(objectClass=organizationalUnit)(ou=" + data[ 'businessUnit'] + "))" buAttr = None results = con.search_s(ldap_base, ldap.SCOPE_SUBTREE, buFilter, buAttr) if (len(results) == 0): #business unit doesn't exist return Response( mimetype="application/json", response=json.dumps("Business Unit doesn't exist "), status=400) user_input = [i for (i, j) in data.items()] #key of all user input #verifying correct email format if ('mail' in user_input): #verify mail format only if it exists in body of user request if (everify(data['mail']) == 0): rValue = "Incorrect email format!" return Response(mimetype="application/json", response=json.dumps(rValue), status=400) #verifying correct mobile number format if ('mobile' in user_input): #verify mail format only if it exists in body of user request if (pverify(data['mobile']) == 0): rValue = "Incorrect mobile number format!" return Response(mimetype="application/json", response=json.dumps(rValue), status=400) #verifying mandatory inputs from user mandatory = [ "fullname", "lastname", "mobile", "mCode", "mail", "password", "businessUnit" ] temp = [x for x in mandatory if x in user_input] missing_attr = set(mandatory) - set(temp) if ( len(missing_attr) == 0 ): #i.e all mandatory fields are present in user input request body #adding user data to LDAP DIT dn = "cn=" + data['fullname'] + ",ou=" + data[ 'businessUnit'] + ",cn=users," + ldap_base #entry ={"cn":data['fullname'],"sn":data['lastname'],"givenName":data['firstname'],"objectClass":"inetOrgPerson","description":data['description'],"mobile":'+'+data['mCode']+data['mobile'],"mail":data['mail'],"userPassword":data['password'],"uid":data['uid']} entry = { "cn": data['fullname'], "sn": data['lastname'], "givenName": data['firstname'], "displayName": data['displayName'], "o": data['organization'], "objectClass": "inetOrgPerson", "description": data['role'], "mobile": '+' + data['mCode'] + data['mobile'], "mail": data['mail'], "userPassword": data['password'], "uid": data["empID"] } parsed_entry = [(i, bytes(j, encoding='utf-8')) for i, j in entry.items()] con.add_s(dn, parsed_entry) rValue = "Created user : "******"application/json", response=json.dumps(rValue), status=201) else: #missing mandatory fields! Exit with 400 rValue = "Missing mandatory user attributes " + str( missing_attr) return Response(mimetype="application/json", response=json.dumps(rValue), status=400) except ldap.LDAPError as e: mssg = list(e.args)[0]['desc'] rValue = "Error while adding user: "******"application/json", response=json.dumps(rValue), status=400)