def send_clean_files_email(datasource_id, user_email, full_name, initiated_by):
    try:
        if not datasource_id:
            return "Invalid datasource! Aborting..."
        db_session = db_connection().get_session()
        datasource = db_session.query(DataSource).filter(
            DataSource.datasource_id == datasource_id).first()
        login_user = db_session.query(LoginUser).filter(
            LoginUser.domain_id == datasource.domain_id,
            LoginUser.email == initiated_by).first()
        admin_user = login_user.first_name + " " + login_user.last_name

        template_name = "clean_files"
        template_parameters = {
            "user_name": full_name,
            "admin_user": admin_user,
            "user_first_name": full_name.split(" ")[0]
        }
        rendered_html = get_rendered_html(template_name, template_parameters)
        email_subject = "Please log in to Adya to manage your G Suite data"
        aws_utils.send_email([user_email], email_subject, rendered_html)
        return True
    except Exception as e:
        Logger().exception("Exception occurred sending clean files email")
        return False
def send_welcome_email(login_user):
    try:
        if not login_user:
            return "Invalid user! Aborting..."

        template_name = "welcome"
        template_parameters = {
            "first_name": login_user.first_name,
            "last_name": login_user.last_name,
            "email": login_user.email
        }
        rendered_html = get_rendered_html(template_name, template_parameters)
        user_list = [template_parameters['email']]
        email_subject = "Welcome to Adya!"
        aws_utils.send_email(user_list, email_subject, rendered_html)
    except Exception as e:
        Logger().exception("Exception occurred sending welcome email!")
def send_app_install_policy_violate_email(user_email, policy, application,
                                          is_reverted):
    try:
        template_name = "app_install_policy_violation"
        template_parameters = {
            "policy_name": policy.name,
            "app_name": application["display_text"],
            "user_name": application["user_email"],
            "is_reverted": is_reverted
        }
        rendered_html = get_rendered_html(template_name, template_parameters)
        email_subject = "[Adya] A policy is violated in GSuite account"
        aws_utils.send_email([user_email], email_subject, rendered_html)
        return True
    except Exception as e:
        Logger().exception(
            "Exception occured while sending app install policy violation email"
        )
        return False
def send_gdrive_scan_completed_email(auth_token, datasource):
    try:
        if not datasource:
            return "Invalid datasource! Aborting..."

        session = db_connection().get_session()
        login_user = session.query(LoginUser).filter(
            and_(LoginUser.auth_token == auth_token,
                 LoginUser.is_enabled == True)).first()
        if not login_user:
            Logger().info("No user to send an email to, so aborting...")
            return
        login_user_first_name = login_user.first_name
        template_name = "gdrive_scan_completed"
        template_parameters = get_gdrive_scan_summary(datasource,
                                                      login_user_first_name,
                                                      auth_token, None)
        rendered_html = get_rendered_html(template_name, template_parameters)

        # only to get admin users
        # all_admin_user_for_a_domain = session.query(DomainUser).filter(and_(DomainUser.datasource_id == datasource.datasource_id,
        #                                                                    DomainUser.is_admin == True)).all()

        user_list = set()
        # if all_admin_user_for_a_domain:
        #     for user in all_admin_user_for_a_domain:
        #         user_list.add(user.email)
        user_list.add(login_user.email)

        user_list = list(user_list)
        Logger().info(
            "send_gdrive_scan_completed_email : user email list : {}".format(
                user_list))

        email_subject = "[Adya] Your GSuite account is ready for review"
        aws_utils.send_email(user_list, email_subject, rendered_html)
    except Exception as e:
        Logger().exception(
            "Exception occurred sending gdrive scan completed email")
def send_new_user_policy_violate_email(user_email, policy, new_user):
    try:
        datasource_name = (policy.name).split("::")[0] if policy.name else None
        user_type = "Administrator" if new_user['is_admin'] else (
            "external_user" if
            (new_user['member_type']
             == constants.EntityExposureType.EXTERNAL.value) else None)
        template_name = "new_administrator_policy_violation" if (user_type == "Administrator") else \
                            ("add_external_user_policy_violation" if (user_type == "external_user") else None)
        template_parameters = {
            "policy_name": policy.name,
            "user_email": new_user["email"],
            "datasource_name": datasource_name
        }
        rendered_html = get_rendered_html(template_name, template_parameters)
        email_subject = "[Adya] A policy is violated in {} account".format(
            datasource_name)
        aws_utils.send_email([user_email], email_subject, rendered_html)
        return True
    except Exception as e:
        Logger().exception(
            "Exception occured while sending new user policy violation email")
        return False
Exemple #6
0
def write_to_csv(auth_token, payload):
    source = payload["sourceType"]
    name = payload["resourceName"]
    type = payload["resourceType"]
    owner = payload["ownerEmailId"]
    exposure_type = payload["exposureType"]
    parent_folder = payload["parentFolder"]
    modified_date = payload["selectedDate"]
    logged_in_user = payload["logged_in_user"]
    selected_fields = payload['selectedFields']

    db_session = db_connection().get_session()
    existing_user = db_utils.get_user_session(auth_token)
    domain_id = existing_user.domain_id

    resources_query, resource_alias, parent_alias = fetch_filtered_resources(
        db_session,
        auth_token,
        exposure_type=exposure_type,
        resource_type=type,
        prefix=name,
        owner_email_id=owner,
        parent_folder=parent_folder,
        selected_date=modified_date,
        source_type=source)

    column_fields = []
    column_headers = []

    if 'source_type' in selected_fields:
        column_fields.append(DataSource.datasource_type)
        column_headers.append("Source")
    if 'resource_name' in selected_fields:
        column_fields.append(resource_alias.resource_name)
        column_headers.append("Name")
    if 'resource_type' in selected_fields:
        column_fields.append(resource_alias.resource_type)
        column_headers.append("Type")
    if 'resource_owner_id' in selected_fields:
        column_fields.append(resource_alias.resource_owner_id)
        column_headers.append("Owner")
    if 'exposure_type' in selected_fields:
        column_fields.append(resource_alias.exposure_type)
        column_headers.append("Exposure Type")
    if 'parent_name' in selected_fields:
        column_fields.append(parent_alias.resource_name)
        column_headers.append("Parent Folder")
    if 'last_modified_time' in selected_fields:
        column_fields.append(resource_alias.last_modified_time)
        column_headers.append("Modified On or Before")

    resources = resources_query.with_entities(*column_fields).filter(
        DataSource.datasource_id == resource_alias.datasource_id).all()

    temp_csv = utils.convert_data_to_csv(resources, column_headers)
    bucket_name = "adyaapp-" + constants.DEPLOYMENT_ENV + "-data"
    now = datetime.strftime(datetime.utcnow(), "%Y-%m-%d-%H-%M-%S")
    #now = str(datetime.utcnow())
    key = domain_id + "/export/resource-" + now + ".csv"
    temp_url = aws_utils.upload_file_in_s3_bucket(bucket_name, key, temp_csv)

    if temp_url:
        email_subject = "[Adya] Your requested report is ready for download"
        link = "<a href=" + temp_url + ">link</a>"
        email_head = "<p>Hi " + existing_user.first_name + ",</p></br></br>"
        email_body = "<p>Your requested report is now ready for download from this " + link + ".</p></br></br><p>In case of any questions, please send a mail to [email protected]</p></br></br>"
        email_signature = "<p>Thanks,</p></br><p>Team Adya</p>"
        rendered_html = email_head + email_body + email_signature
        aws_utils.send_email([logged_in_user], email_subject, rendered_html)
        # adya_emails.send_csv_export_email(logged_in_user, domain_id, temp_url)
    else:
        Logger().exception(
            "Failed to generate url. Please contact administrator")
def send_permission_change_policy_violate_email(user_email, policy, resource,
                                                new_permissions,
                                                violated_permissions,
                                                new_permissions_left):
    try:
        db_session = db_connection().get_session()
        resource_owner = db_session.query(DomainUser).filter(
            resource["datasource_id"] == DomainUser.datasource_id,
            DomainUser.email == resource["resource_owner_id"]).first()
        template_name = "permission_change_policy_violation"
        permissions = []
        for permission in new_permissions:
            user_name = permission["email"]
            permission_str = user_name + " (" + constants.permission_friendly_name_map[
                permission["permission_type"]] + ")"
            permissions.append(permission_str)

        violated_perm = []
        if violated_permissions:
            for violated_permission in violated_permissions:
                user_name = violated_permission["email"]
                violated_perm_str = user_name + " (" + constants.permission_friendly_name_map[
                    violated_permission["permission_type"]] + ")"
                violated_perm.append(violated_perm_str)

        new_perm_left = []
        for perm_left in new_permissions_left:
            user_name = perm_left["email"]
            permission_str = user_name + " (" + constants.permission_friendly_name_map[
                perm_left["permission_type"]] + ")"
            new_perm_left.append(permission_str)

        template_parameters = {
            "policy_name":
            policy.name,
            "document_name":
            resource["resource_name"],
            "modifying_user":
            resource["last_modifying_user_email"],
            "owner_name":
            resource_owner.first_name,
            "permissions":
            permissions,
            "revert_back":
            True if violated_permissions else False,
            "violated_permissions":
            violated_perm,
            "len_violated_permissions":
            True if (violated_permissions and len(violated_permissions) > 0)
            else False,
            "new_permissions_left":
            new_perm_left,
            "new_permissions_str":
            "New permissions for this document are - "
            if len(new_perm_left) > 0 else ""
        }
        rendered_html = get_rendered_html(template_name, template_parameters)
        email_subject = "[Adya] A policy is violated in your GSuite account"
        aws_utils.send_email([user_email], email_subject, rendered_html)
        return True
    except Exception as e:
        Logger().exception(
            "Exception occured while sending policy violation email")
        return False
def write_to_csv(auth_token, payload):
    source = payload["datasource_id"]
    type = payload["type"]
    name = payload["full_name"]
    email = payload["email"]
    is_admin = payload["is_admin"]
    member_type = payload["member_type"]
    logged_in_user = payload["logged_in_user"]
    selected_fields = payload['selectedFields']

    db_session = db_connection().get_session()
    existing_user = db_utils.get_user_session(auth_token)
    domain_id = existing_user.domain_id

    domain_datasource_ids = []
    if not source:
        datasources = db_session.query(DataSource).filter(DataSource.domain_id == domain_id).all()
        for datasource in datasources:
            domain_datasource_ids.append(datasource.datasource_id)
    else:
        domain_datasource_ids = [source]
    
    users_query = db_session.query(DomainUser).join(DataSource).filter(DomainUser.datasource_id.in_(domain_datasource_ids))
    users_query = filter_on_get_user_list(users_query, full_name=name, email=email, member_type=member_type, datasource_id=source,
                    is_admin=is_admin, type=type)

    column_fields = []
    column_headers = []

    if 'datasource_id' in selected_fields:
        column_fields.append(DataSource.datasource_type)
        column_headers.append('Source')
    if 'full_name' in selected_fields:
        column_name = case([(DomainUser.full_name != None, DomainUser.full_name),],
            else_ = DomainUser.first_name + " " + DomainUser.last_name)
        column_fields.append(column_name)
        column_headers.append('Name')
    if 'email' in selected_fields:
        column_fields.append(DomainUser.email)
        column_headers.append('Email')
    if '' in selected_fields:
        column_fields.append(DomainUser.photo_url)
        column_headers.append('Avatar')
    if 'type' in selected_fields:
        column_fields.append(DomainUser.type)
        column_headers.append('Type')
    if 'last_login' in selected_fields:
        column_fields.append(DomainUser.last_login_time)
        column_headers.append('Last Login')
    if 'is_admin' in selected_fields:
        column_fields.append(DomainUser.is_admin)
        column_headers.append('Is Admin')
    if 'member_type' in selected_fields:
        column_fields.append(DomainUser.member_type)
        column_headers.append('Exposure Type')
    
    users = users_query.with_entities(*column_fields).all()

    temp_csv = utils.convert_data_to_csv(users, column_headers)
    bucket_name = "adyaapp-" + constants.DEPLOYMENT_ENV + "-data"
    now = datetime.strftime(datetime.utcnow(), "%Y-%m-%d-%H-%M-%S")
    key = domain_id + "/export/user-" + now + ".csv"
    temp_url = aws_utils.upload_file_in_s3_bucket(bucket_name, key, temp_csv)

    if temp_url:
        email_subject = "[Adya] Your requested report is ready for download"
        link = "<a href=" + temp_url + ">link</a>"
        email_head = "<p>Hi " + existing_user.first_name + ",</p></br></br>"
        email_body = "<p>Your requested report is now ready for download from this " + link + ".</p></br></br><p>In case of any questions, please send a mail to [email protected]</p></br></br>"
        email_signature = "<p>Thanks,</p></br><p>Team Adya</p>"
        rendered_html = email_head + email_body + email_signature
        aws_utils.send_email([logged_in_user], email_subject, rendered_html)
    else:
        Logger().exception("Failed to generate url. Please contact administrator")