Exemple #1
0
def add(event, context):
    print("PackageAddRequest", event, context)
    resp = lib_common_auth.validateToken(event)
    if resp != 0:
        return resp

    print(event)

    resp = lib_common_package.validateAddPackageRequest(event)
    if resp != 0:
        return resp

    try:
        dbcon.ping(reconnect=True, attempts=3, delay=5)
        cursor = dbcon.cursor()
        sql = "INSERT INTO package (user, package) VALUES (%s, %s)"
        val = (event['data']['user'], event['data']['package'])
        cursor.execute(sql, val)
        package = cursor.lastrowid

        ploads = {'token': event['data']['token'], 'package': cursor.lastrowid}
        r = requests.get(lib_common_http.endpoints['email']['package'],
                         json=ploads)
        ro = json.loads(r.text)
        print("EmailPackage", ro)
        if ro["statusCode"] == 200:
            response = {
                "statusCode": 200,
                "body": {
                    "package": package,
                    "mail": "S",
                    "data": ro['body']
                }
            }
        else:
            response = {
                "statusCode": 206,
                "body": {
                    "package": package,
                    "mail": "F",
                    "data": ro['body']
                }
            }

    except:  # catch *all* exceptions
        print("ERROR", ploads)
        e = sys.exc_info()[0]
        response = {"statusCode": 423, "body": e}

    return response
Exemple #2
0
def getByUser(event, context):
    resp = lib_common_auth.validateToken(event)
    if resp != 0:
        return resp

    resp = lib_common_employee.validateGetByUserRequest(event)
    if resp != 0:
        return resp

    try:
        con = ldap.initialize(f'ldap://{env_ldapnode}')
        con.simple_bind_s(env_ldapuser, env_ldappass)
        ldap_base = env_ldapbase
        query = f"(& (objectclass=user) (sAMAccountName={event['data']['user']}))"
        result = con.search_s(ldap_base, ldap.SCOPE_SUBTREE, query)
        con.unbind_s()
        results = []
        for dn, user in result:
            x = {
                "cn":
                lib_common_employee.getPropertyValue(user, "cn"),
                "user":
                lib_common_employee.getPropertyValue(user, "sAMAccountName"),
                "email":
                lib_common_employee.getPropertyValue(user, "mail"),
                "mobile":
                lib_common_employee.getPropertyValue(user, "mobile")
            }
            results.append(x)
            print("EMPLOYEE", x)
        if len(results) > 0:
            response = {
                "statusCode": 200,
                "body": {
                    "total": len(results),
                    "employees": results
                }
            }
        else:
            return lib_common_http.getNotFoundErrorResponse()

    except Exception as e:
        response = {"statusCode": 404, "body": e}

    return response
Exemple #3
0
def getByName(event, context):
    resp = lib_common_auth.validateToken(event)
    if resp != 0:
        return resp

    resp = lib_common_employee.validateGetByNameRequest(event)
    if resp != 0:
        return resp

    try:
        con = ldap.initialize(f'ldap://{env_ldapnode}')
        con.simple_bind_s(env_ldapuser, env_ldappass)
        ldap_base = env_ldapbase
        query = f"(& (objectclass=user) (cn=*{event['data']['name'].replace(' ','*')}*))"
        result = con.search_s(ldap_base, ldap.SCOPE_SUBTREE, query)
        con.unbind_s()
        results = []
        for dn, user in result:
            x = {
                "cn":
                lib_common_employee.getPropertyValue(user, "cn"),
                "user":
                lib_common_employee.getPropertyValue(user, "sAMAccountName"),
                "email":
                lib_common_employee.getPropertyValue(user, "mail"),
                "mobile":
                lib_common_employee.getPropertyValue(user, "mobile")
            }
            results.append(x)

        response = {
            "statusCode": 200,
            "body": {
                "total": len(results),
                "employees": results
            }
        }
    except Exception as e:
        response = {"statusCode": 404, "body": e}

    return response
Exemple #4
0
def get(event, context):
    resp = lib_common_auth.validateToken(event)
    if resp != 0:
        return resp

    resp = lib_common_package.validateGetPackageRequest(event)
    if resp != 0:
        return resp
    try:
        dbcon.ping(reconnect=True, attempts=3, delay=5)
        cursor = dbcon.cursor(dictionary=True)
        sql = f'SELECT id, user, package, unix_timestamp(created) as date from package where id={event["data"]["package"]}'
        cursor.execute(sql)
        package = cursor.fetchone()
        print("GetPackageOrigin", package)
        if len(package) > 0:
            response = {"statusCode": 200, "body": {"package": package}}
        else:
            return lib_common_http.getNotFoundErrorResponse()
    except:
        e = sys.exc_info()[0]
        response = {"statusCode": 424, "body": e}

    return response
def package(event, context):

    resp = lib_common_auth.validateToken(event)
    if resp != 0:
        return resp

    resp = lib_common_email.validatePackageRequest(event)
    if resp != 0:
        return resp

    pkg = {}
    usr = {}

    # Get Package
    try:
        ploads = {
            'token': event['data']['token'],
            'package': event['data']['package']
        }
        r = requests.get(lib_common_http.endpoints['package']['get'],
                         json=ploads)
        ro = json.loads(r.text)
        print("Email_GetPackage", ro)
        if ro["statusCode"] == 200:
            resp = lib_common_package.validateGetPackageResponse(ro)
            if resp != 0:
                return resp
            else:
                pkg = ro["body"]["package"]
        else:
            return lib_common_http.getNotFoundErrorResponse()
    except Exception as e:
        print("Email_GetPackage_ERROR", ploads)
        return {"statusCode": 400, "body": e}

    # Get User
    try:
        ploads = {'token': event['data']['token'], 'user': pkg['user']}
        r = requests.get(lib_common_http.endpoints['employee']['getbyuser'],
                         json=ploads)
        ro = json.loads(r.text)
        print("Email_GetUser", ro)
        if ro["statusCode"] == 200:
            resp = lib_common_employee.validateGetByUserResponse(ro)
            if resp != 0:
                return resp
            else:
                usr = ro["body"]["employees"][0]
        else:
            return lib_common_http.getNotFoundErrorResponse()
    except Exception as e:
        print("Email_GetUser_ERROR", ploads)
        return {"statusCode": 454, "body": e}

    # Build and send mail
    resp = lib_common_email.sendPackageMail({"pkg": pkg, "usr": usr})

    if resp == 0:
        response = {"statusCode": 200, "body": "Message sent"}
    else:
        response = {"statusCode": 484, "body": resp}

    return response