def test_search_db(temp_database, database): ret, msg = db.insert_into_database(temp_database, database, NAME="searchname") assert ret != False ret, msg = db.search_database(temp_database, database, "NAME", "searchname") assert ret == True ret, msg = db.search_database(temp_database, database, "NAME", "definitelynotfound") assert ret != True
def toggle_status(emailid): ''' This function will toggle the VMO Status for a particular user. :param emailid: The emailid id of the user you want to toggle :return: redirect to "/" with a Status Code of 302 ''' if WEBDEBUG: print_details(request) print ("Toggling VMO Status for: "+emailid) ret, msg = db.search_database(dbname, "users", "Alias", emailid) print("Current Value of VMO Status: "+msg['Active']) if ret: value = msg['Active'] if value == "True": value = "False" else: value = "True" updatestring = "Active='" + value +"'" print (msg['CallHandlerObjectId']) ret, msg = db.update_database(dbname, "users", updatestring, "Alias='" + emailid + "'") if value == "True": print ("Submitting a request to set an alert on: "+emailid) else: print ("Submitting a request to turn off the alert on: "+emailid) apistring = mailip + "/monitor" print(apistring) headers = { 'Content-Type': 'application/json' } user = {} user['email'] = emailid user['status'] = value print(user) try: resp = requests.post(apistring, data=json.dumps(user), headers=headers) except requests.exceptions.RequestException as e: flash("ERROR: Error when attempting to toggle status of user : "******"ERROR: Synchronization failed to ("+apistring+"): "+" Status Code ("+str(resp.status_code)+"): "+str(resp.reason)) return redirect("/", code=302)
def synchronize_dbs(): """ This function call will synchonize the database with the UCXN Server :return: """ # Make a call to the voice mail application to get all users apistring = vmip+"/ucxn/users" print ("API String: "+apistring) try: resp = requests.get(apistring) print (resp) except requests.exceptions.RequestException as e: flash("ERROR: Error when attempting to synchronize database: "+str(e)) return if resp.status_code != 200: flash ("ERROR: Synchronization failed to ("+apistring+"): "+" Status Code ("+str(resp.status_code)+"): "+str(resp.reason)) return print (resp) data=resp.json() print(str(data)) # For each user in the data received, see if the record already exists in our database and if so synchronize the records for user in data: emailid = user['Alias'] ret, msg = db.search_database(dbname, "users", "Alias", emailid) # If we found the user check the out of office greeting: updatestring = "" if ret: print ("Found existing record") if msg['Extension'] != user['Extension']: updatestring += "Extension='"+user['Extension']+"'" print ("vmo db AlternateGreetingEnabled = "+msg['AlternateGreetingEnabled']) print ("ucxn db AlternateGreetingEnabled = "+user['AlternateGreetingEnabled']) if msg['AlternateGreetingEnabled'] != user['AlternateGreetingEnabled']: if updatestring != "": updatestring += " and " updatestring += "AlternateGreetingEnabled='" + user['AlternateGreetingEnabled']+"'" if updatestring != "": print ("Updating record: "+emailid) ret, msg = db.update_database(dbname, "users", updatestring, "Alias='" + emailid + "'") print ("Return = "+str(ret) + " "+str(msg)) else: print ("Did not find existing record") ret, msg = db.insert_into_database(dbname, "users", ObjectId=user['ObjectId'], Alias=user['Alias'], Extension=user['Extension'],CallHandlerObjectID=user['CallHandlerObjectId'],AlternateGreetingEnabled=user['AlternateGreetingEnabled'],Active="True") return
def getendpointid(): """ This function will determine if a endpoint id already exists in the database. @return: id of the deviceid """ if WEBDEBUG: print_details(request) if 'deviceid' not in request.args: return jsonify({"result": "no parameter"}) ret,msg = db.search_database(dbname, "device", "name", request.args['deviceid']) return jsonify({"result":msg})
def getemaildomain(): """ This function will see if an email domain is in the white list database @return: id of the record """ if WEBDEBUG: print_details(request) if 'domain' not in request.args: return jsonify({"result": "no parameter"}) ret,msg = db.search_database(dbname, "domain", "name", request.args['domain']) return jsonify({"result": msg})
def statusguestaccount(): """ This function will return the status of the guest account provided by the email address @return: status """ if WEBDEBUG: print_details(request) if 'email' not in request.args: return jsonify({"result": "no parameter"}) else: emailaddress = request.args['email'] ret, msg = db.search_database(dbname, "guest", "name", emailaddress) return jsonify({"result":msg})
def updatestatusguestaccount(): """ This function will update the status of the guest account @return: """ if WEBDEBUG: print_details(request) # determine if both the emailid and status is provided in the request, if not, return the error value if ('emailid' in request.args) and ('status' in request.args): emailid = request.args['emailid'] print("Emailid: "+emailid) # check to see if the status is completed. If so, then we are going to ecxpect the guest password if request.args['status']=="completed": print("Status passed to function is completed") # If guest password is not passed to the parameter, then return an error message if ('guestpassword' not in request.args): print("Guestpassword is not passed to the function") return jsonify({"result":"no guest password"}) else: print("Guest Password:"******"STATUS='" + request.args['status'] + "', GUESTPASSWORD='******'guestpassword']+"'" # Search the database record to return the room id so we can alert the user. ret, msg = db.search_database(dbname, "guest", "name", emailid) # If guest record is found then send the message to the teams room if ret: print(str(msg)) ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "--------------------------------------------------------") ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "Congratulations, Your account was successfully created!") ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "Guest Credentials are located below:") ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "ssid: **_platinum-guest_**") ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "username: **_"+emailid+"_**") ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "password: **_"+request.args['guestpassword']+"_**") else: print("(Email id Found)") return jsonify({"result": "emailid not found"}) else: # Update the status of the record. updatestring = "STATUS='" + request.args['status'] + "'" ret, msg = db.update_database(dbname, "guest", updatestring, "NAME='" + request.args['emailid'] + "'") print(str(msg)) return jsonify({"result":ret}) else: return jsonify({"result":"wrong paramters"})
def generateguestaccount(): """ This function will trigger the process of a creation of a guest account. @return: """ if WEBDEBUG: print_details(request) # This function must be called with both the deviceid parameter and the teamsid parameter if ('deviceid' in request.args) and ('teamsid' in request.args): # Determine if deviceid is in white list database. If not, then the device isnt authorized to generate # guest account deviceid=request.args['deviceid'] ret, msg = db.search_database(dbname, "device", "name", deviceid) if not ret: return jsonify({"result": "deviceid not authorized"}) # Determine if the teamsid is a valid WebEx Teams user. If not, then quit ret= teamsapi.getemailfromid(teamsurl, teamstoken, request.args['teamsid']) if ret=="": return jsonify({"result": "teamsid not found"}) # Determine if emaildomain is in white list database, if not, then the email domain isn't authorized to generate # a guest account emailaddress=ret[0] emaildomain=emailaddress.split("@")[1] ret, msg = db.search_database(dbname, "domain", "name", emaildomain) if (not ret): return jsonify({"result": "email domain not authorized"}) # Now that we know we are authorized, serach the guest table to determine if a guest account has already # been initiated ret, msg = db.search_database(dbname, "guest", "name", emailaddress) if (not ret): # Trigger the initiation of the guest account creation # Create a new teamsroom to communicate the status back to the user ret=teamsapi.createteamsroom(teamsurl, teamstoken,"Platinum Onboard Guest Wireless "+str(date.today())+" - "+emailaddress) if ret == '': print("Unable to create the teams room") else: roomId = ret # Add the user to the teams room that we just created ret=teamsapi.adduserstoroom(teamsurl, teamstoken,roomId,emailaddress) if ret == '': print("Unable to add people to the teams room") teamsapi.sendmessagetoroom(teamsurl, teamstoken, roomId, "--------------------------------------------------------") teamsapi.sendmessagetoroom(teamsurl, teamstoken, roomId, "**Welcome to the Platinum Onboard Service**") teamsapi.sendmessagetoroom(teamsurl, teamstoken, roomId, "We have initiated the creation of the guest wireless account for "+emailaddress) teamsapi.sendmessagetoroom(teamsurl, teamstoken, roomId, "Please give us a few moments until your account is provisioned") # Insert a new guest record into the database. ret, msg = db.insert_into_database(dbname, "guest", NAME=emailaddress, DEVICE=deviceid, STATUS="initiated", TEAMSROOMID=roomId) if (not ret): # There was an issue with inserting the record. This is a problem since we should role back the creation. print("Unable to insert the record") return (jsonify({"result":msg})) else: print("Successful insertion of record") # This was successful # Send a request to the provisioning server print("Triggering Guest Creation of '"+emailaddress+"' from device '"+deviceid+"' to "+provisionip) apistring = "http://"+provisionip+"/api/check-guest.php?emailid="+emailaddress print("Sending API to trigger guest creation: "+apistring) # Post the API call to the provisioning engine resp = requests.post(apistring) print(str(resp)) return jsonify({"result": "success", "record_id": msg}) else: # User already has a guest account initiated: print(str(msg)) print("User " + emailaddress + " already has a account initiated!") # Search the database for the record. ret, msg = db.search_database(dbname, "guest", "name", emailaddress) # If the status is initiated, then let the user know via the teams room, that we already initiated # a guest account. if (msg['status'] == 'initiated'): ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "--------------------------------------------------------") ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "Your account request was already initiated!") print("Initiated") return jsonify({"result": "initiated"}) else: # Otherwise, the guest account is created, so end back the data concerning the wireless credentials ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "--------------------------------------------------------") ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "Your account was already provisioned:") ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "Guest Credentials are located below:") ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "ssid: **_platinum-guest_**") ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "username: **_" + emailaddress + "_**") ret = teamsapi.sendmessagetoroom(teamsurl, teamstoken, msg['teamsroomid'], "password: **_" + msg['guestpassword'] + "_**") print("Guest Password is: "+ msg['guestpassword']) return jsonify({"result": "completed", "password": msg['guestpassword']}) else: print("Wrong Parameters") return jsonify({"result": "wrong paramters"})
def setstatus(): ''' This function will implement the setstatus function. The setstatus will be used anytime the mail server wants to change the status of the a user. For example, if they go from Out Of Office = True or Out Of Office = False. :return: {"result":"True"} and Status Code 200 if the request was successful {"result":"Invalid JSON"} and Status Code 400 if the inbound JSON is incorrect {"result":"Not Found"} and Status Code 404 if the user was not found in the database {"result":str(e)} and Status Code 403 if a generic error occurred and the 'e' is the error message {"result": "Internal Error"} and any Status Code if another web error occured when communicating to the mail gateway ''' if WEBDEBUG: print_details(request) req_data = request.get_json(force=True, silent=True) # Check to see if the email and status fields are included in the inbound JSON try: email = req_data['email'] status = req_data['status'] except (KeyError, TypeError, ValueError): return jsonify({"result":"Invalid JSON"}),400 # Check to see if the OOO message is included try: message = req_data['message'] except (KeyError, TypeError, ValueError): print("Incoming Message doesn't include a OOO message") else: print("Incoming Message does include a OOO message") print("The message is: '"+message+"'") print("Setting the status for: "+email+" to: "+ status) # Search the database table for the user ret, msg = db.search_database(dbname, "users", "Alias", email) if not ret: return jsonify({"result":"Not Found"}),404 # Create the API call to send to UCXN apistring = vmip+"/ucxn/users/"+msg['CallHandlerObjectId']+"/greeting" headers = { 'Content-Type': 'application/json' } jsonmsg = {} jsonmsg['action']=status jsonmsg['extension']=msg['Extension'] if message: jsonmsg['message']=message print (apistring) print (jsonmsg) # Try to post the message to the UCXN server try: resp = requests.post(apistring,data=json.dumps(jsonmsg),headers=headers,timeout=10) except requests.exceptions.RequestException as e: flash("ERROR: Error when attempting to set status: "+str(e)) return jsonify({"result":str(e)}),403 print (resp.status_code) if resp.status_code == 200: data=resp.json() print(str(data)) updatestring = "AlternateGreetingEnabled='" + status +"'" ret, msg = db.update_database(dbname, "users", updatestring, "Alias='" + email + "'") data = db.search_db(dbname, "users") return jsonify({"result":"True"}),200 else: print("ItExecuted") flash ("ERROR: Unable to set status failed to ("+apistring+"): "+" Status Code ("+str(resp.status_code)+"): "+str(resp.reason)) return jsonify({"result": "Internal Error"}),resp.status_code