Ejemplo n.º 1
0
def load_user(user_id):
    user = get_by_id(user_id, USER_COLLECTION)

    if "OfficeID" in user and user["OfficeID"]:
        office = get_by_id(user["OfficeID"], CLIENT_OFFICE_COLLECTION)
        if office:
            user["OfficeName"] = office["Name"]

    if "DepartmentID" in user and user["DepartmentID"]:
        department = get_by_id(user["DepartmentID"],
                               CLIENT_DEPARTMENT_COLLECTION)
        if department:
            user["DepartmentName"] = department["Name"]

    return make_response_obj(user)
Ejemplo n.º 2
0
def generate_password_reset_token(user=None):

    #Default is 1 hour
    expires_in = 3600

    if "ClientID" in user and user["ClientID"]:
        client = get_by_id(user["ClientID"], CLIENT_COLLECTION)

        if "ResetLinkTimeoutInMinutes" in client:
            expires_in = int(client["ResetLinkTimeoutInMinutes"]) * 60

    s = Serializer(get_secret_key(), expires_in=expires_in)
    return s.dumps({'id': user["EntityID"]})
Ejemplo n.º 3
0
def update_user(data):
    check = get_user_by_email(data["Email"])

    user = get_by_id(data["EntityID"], USER_COLLECTION)

    if check.count() > 0:
        ''' If an user exists with input email and is not the same person. Alert as duplicate user'''
        existing_user = list(check)[0]
        debug(logger, existing_user["EntityID"])
        debug(logger, user["EntityID"])
        debug(logger, existing_user["Email"])
        debug(logger, user["Email"])

        if existing_user["EntityID"] != user["EntityID"] and existing_user[
                "Email"] == data["Email"]:
            return {
                "error": True,
                "msg": "An user with this email already exists",
                "errorCode": DUPLICATE_USER_ERROR_CODE
            }

    if "FirstName" in data:
        user["FirstName"] = data["FirstName"]

    if "LastName" in data:
        user["LastName"] = data["LastName"]

    if "Phone" in data:
        user["Phone"] = data["Phone"]

    if "Email" in data:
        user["Email"] = data["Email"]

    if "FirstName" in data and "LastName" in data:
        user["DisplayName"] = data["FirstName"] + " " + data["LastName"]

    if "PermissionLevel" in data:
        user["PermissionLevel"] = data["PermissionLevel"]

    if "IsAcceptedLicense" in data:
        user["IsAcceptedLicense"] = data["IsAcceptedLicense"]

    ret = save(user, get_logged_in_user_id(), arrow.utcnow(), USER_COLLECTION)

    if ret == None:
        return {"error": True, "msg": "Error when saving user"}
    else:
        return make_response_obj(ret)
Ejemplo n.º 4
0
def update_profile(data, user_id):

    user = get_by_id(user_id, USER_COLLECTION)

    if not user:
        return {"error": True, "msg": "Can not find user"}, 400

    previous_email = user["Email"]
    email_changed = False
    if is_user_email_changed(data, user):
        if "Password" not in data:
            return {
                "error": True,
                "msg": "Password required to change email"
            }, 400

        if user["HashedPassword"] != get_encrypted_str(data["Password"]):
            return {
                "error": True,
                "msg": "Invalid user password.",
                "errorCode": INVALID_CURRENT_PASSWORD_ERROR_CODE
            }, 400

        email_changed = True
        check = get_user_by_email(data["Email"])

        if check.count() > 0:
            return {
                "error": True,
                "msg": "An user with this email already exists",
                "errorCode": DUPLICATE_USER_ERROR_CODE
            }, 404

    ret = update_profile_attributes(user, data)

    profile_data = get_profile(ret["EntityID"])[0]

    if email_changed == True:
        send_email_changed_notification_to_user(user, previous_email)
        send_email_changed_notification_to_admins(user, previous_email)

        token = base64_encode(data["Email"].lower() + ":" +
                              get_encrypted_str(data["Password"]))
        profile_data["AuthToken"] = token

    return profile_data, 200
Ejemplo n.º 5
0
def login(data):
    if not data:
        return {"error": True, "msg": "Request was not understood"}, 500
    if not "username" in data or len(data["username"]) <= 0:
        return {"error": True, "msg": "Username/Password is wrong"}, 401
    if not "password" in data:
        return {"error": True, "msg": "Username/Password is wrong"}, 401

    check = get_list_by_query(
        {
            "Email": {
                "$regex": escape_email_for_plus_sign(data["username"]),
                "$options": "i"
            },
            "HashedPassword": get_encrypted_str(data["password"]),
            "Active": True,
            "Latest": True
        }, USER_COLLECTION)
    array = list(check)
    if check.count() > 0:
        user = array[0]
        debug(logger, "User found.")

        if "ClientID" in user and user["ClientID"]:
            client = get_by_id(user["ClientID"], CLIENT_COLLECTION)
            if not client["Active"]:
                debug(logger,
                      "User associated with invalid client. Cant login.")
                return {}, 404

        if "IsResetLink" in data:
            if is_valid_token(data["password"]):
                return make_response_obj(update_login_data(array[0])), 200
            else:
                error(logger, "Invalid token")
                return {
                    "error": True,
                    "msg": "Invalid token.",
                    "errorCode": INVALID_PWD_RESET_TOKEN_ERROR_CODE
                }, 200
        else:
            return make_response_obj(update_login_data(array[0])), 200
    else:
        debug(logger, "User not found.")
        return {}, 404
Ejemplo n.º 6
0
def cancel_password_change(data):
    if "UserAccountID" not in data:
        return {
            "error": True,
            "msg": "Cannot perform action. No user data"
        }, 400

    check = get_by_id(data["UserAccountID"], USER_COLLECTION)
    if not check:
        return {"error": True, "msg": "User does not exist"}, 403

    item = dict(check)

    changed_on = arrow.utcnow()
    item["ChangePasswordOnLogin"] = False
    ret = save(item, data["UserAccountID"], changed_on, USER_COLLECTION)
    if ret == None:
        return {"error": True, "msg": "Saving error"}, 404

    return {"Status": 0}, 200
Ejemplo n.º 7
0
def change_password(data):
    if "UserAccountID" not in data:
        return {
            "error": True,
            "msg": "Cannot perform action. No user data"
        }, 400
    if "NewPassword" not in data:
        return {
            "error": True,
            "msg": "Cannot perform action. Please supply the new password"
        }, 400
    if "CurrentPassword" not in data:
        return {
            "error": True,
            "msg": "Cannot perform action. Please supply current password"
        }, 400

    check = get_by_id(data["UserAccountID"], USER_COLLECTION)
    if not check:
        return {"error": True, "msg": "User does not exist"}, 403

    item = dict(check)

    if item["HashedPassword"] != get_encrypted_str(data["CurrentPassword"]):
        return {
            "error": True,
            "msg": "Invalid current password.",
            "errorCode": INVALID_CURRENT_PASSWORD_ERROR_CODE
        }, 400
    else:
        changed_on = arrow.utcnow()
        item["HashedPassword"] = get_encrypted_str(data["NewPassword"])
        item["ChangePasswordOnLogin"] = False
        ret = save(item, data["UserAccountID"], changed_on, USER_COLLECTION)
        if ret == None:
            return {"error": True, "msg": "Saving error"}, 404
        token = base64_encode(ret["Email"].lower() + ":" +
                              item["HashedPassword"])

        return {"Status": 0, "AuthToken": token}, 200
Ejemplo n.º 8
0
def get_profile(user_id):
    user = get_by_id(user_id, USER_COLLECTION)

    if not user:
        return {"error": True, "msg": "user does not exist"}, 404

    return_json = {
        "UserAccountID":
        user["EntityID"],
        "Email":
        user["Email"],
        "ClientID":
        user["ClientID"],
        "PermissionLevel":
        user["PermissionLevel"],
        "ChangePasswordOnLogin":
        user["ChangePasswordOnLogin"],
        "IsAegisAdministrator":
        user["IsAegisAdministrator"]
        if "IsAegisAdministrator" in user else False,
        "IsAcceptedLicense":
        user["IsAcceptedLicense"] if "IsAcceptedLicense" in user else False
    }

    return_json = populate_display_name(user, return_json)
    return_json = populate_first_name(user, return_json)
    return_json = populate_last_name(user, return_json)
    return_json = populate_email_hash(user, return_json)

    if "OfficeID" in user:
        return_json["OfficeID"] = user["OfficeID"]

    if "DepartmentID" in user:
        return_json["DepartmentID"] = user["DepartmentID"]

    if "LastViewedClientID" in user:
        return_json["LastViewedClientID"] = user["LastViewedClientID"]

    return make_response_obj(return_json), 200
Ejemplo n.º 9
0
		def update_ocr_flag(flag=False):
			file_entry = get_by_id(file_entity_id, FILE_COLLECTION)
			file_entry["OCR"] = flag
			save(file_entry, get_void_uuid(), arrow.utcnow(), FILE_COLLECTION)