Esempio n. 1
0
def remove_permission(auth_user: AuthUser, username: str, company_id: str):
    fn = 'controllers.company.remove_permission'
    if auth_user.username not in admin_list:
        return {'error': 'unauthorized'}

    cla.log.info(
        f'{fn} - company ({company_id}) removed for user ({username}) by {auth_user.username}'
    )

    company = Company()
    try:
        company.load(company_id)
    except Exception as err:
        cla.log.warning(f'{fn} - unable to update company permission: {err}')
        return {'error': str(err)}

    company.remove_company_acl(username)
    event_data = f'Removed user {username} from Company {company.get_company_name()} permissions list.'
    Event.create_event(
        event_data=event_data,
        event_summary=event_data,
        event_company_id=company_id,
        event_type=EventType.RemoveCompanyPermission,
        contains_pii=True,
    )
    company.save()
Esempio n. 2
0
def remove_permission(auth_user: AuthUser, username: str, company_id: str):
    if auth_user.username not in admin_list:
        return {'error': 'unauthorized'}

    cla.log.info('company ({}) removed for ({}) by {}'.format(
        company_id, username, auth_user.username))

    company = Company()
    try:
        company.load(company_id)
    except Exception as err:
        print('Unable to update company permission: {}'.format(err))
        return {'error': str(err)}

    company.remove_company_acl(username)
    event_data = 'company ({}) removed for ({}) by {}'.format(
        company_id, username, auth_user.username)
    Event.create_event(
        event_data=event_data,
        event_summary=event_data,
        event_company_id=company_id,
        event_type=EventType.RemoveCompanyPermission,
        contains_pii=True,
    )
    company.save()
Esempio n. 3
0
def update_company_whitelist_csv(content, company_id, username=None):
    """
    Adds the CSV of email addresse to this company's whitelist.

    :param content: The content posted to this endpoint (CSV data).
    :type content: string
    :param company_id: The ID of the company to add to the whitelist.
    :type company_id: UUID
    """
    company = Company()
    try:
        company.load(str(company_id))
    except DoesNotExist as err:
        return {'errors': {'company_id': str(err)}}

    company_acl_verify(username, company)

    # Ready email addresses.
    emails = content.split('\n')
    emails = [email for email in emails if '@' in email]
    current_whitelist = company.get_company_whitelist()
    new_whitelist = list(set(current_whitelist + emails))
    company.set_company_whitelist(new_whitelist)
    company.save()
    return company.to_dict()
Esempio n. 4
0
def add_permission(auth_user: AuthUser,
                   username: str,
                   company_id: str,
                   ignore_auth_user=False):
    if not ignore_auth_user and auth_user.username not in admin_list:
        return {'error': 'unauthorized'}

    cla.log.info('company ({}) added for user ({}) by {}'.format(
        company_id, username, auth_user.username))

    company = Company()
    try:
        company.load(company_id)
    except Exception as err:
        print('Unable to update company permission: {}'.format(err))
        return {'error': str(err)}

    company.add_company_acl(username)
    event_data = f'Permissions added to user {username} for Company {company.get_company_name()}'
    Event.create_event(
        event_data=event_data,
        event_summary=event_data,
        event_type=EventType.AddCompanyPermission,
        event_company_id=company_id,
        contains_pii=True,
    )
    company.save()
Esempio n. 5
0
def update_company(company_id, # pylint: disable=too-many-arguments
                   company_name=None,
                   company_manager_id=None,
                   username=None):
    """
    Updates an company and returns the newly updated company in dict format.
    A value of None means the field should not be updated.

    :param company_id: ID of the company to update.
    :type company_id: ID
    :param company_name: New company name.
    :type company_name: string | None
    :param company_manager_id: The ID of the company manager user.
    :type company_manager_id: string
    :return: dict representation of the company object.
    :rtype: dict
    """
    company = Company()
    try:
        company.load(str(company_id))
    except DoesNotExist as err:
        return {'errors': {'company_id': str(err)}}

    company_acl_verify(username, company)

    if company_name is not None:
        company.set_company_name(company_name)
    if company_manager_id is not None:
        val = hug.types.uuid(company_manager_id)
        company.set_company_manager_id(str(val))
    company.save()
    return company.to_dict()
Esempio n. 6
0
def create_company(auth_user: AuthUser,
                   company_name: str = None,
                   company_manager_id=None,
                   company_manager_user_name=None,
                   company_manager_user_email=None,
                   user_id=None,
                   response=None):
    """
    Creates an company and returns the newly created company in dict format.

    :param auth_user: The authenticated user
    :type auth_user: object
    :param company_name: The company name.
    :type company_name: string
    :param company_manager_id: The ID of the company manager user.
    :type company_manager_id: string
    :param company_manager_user_name: The user name of the company manager user.
    :type company_manager_user_name: string
    :param company_manager_user_email: The user email of the company manager user.
    :type company_manager_user_email: string
    :return: dict representation of the company object.
    :rtype: dict
    """
    fn = 'controllers.company.create_company'
    manager = cla.controllers.user.get_or_create_user(auth_user)

    for company in get_companies():
        if company.get("company_name") == company_name:
            cla.log.error({"error": "Company already exists"})
            response.status = HTTP_409
            return {"status_code": HTTP_409,
                    "data": {"error": "Company already exists.",
                             "company_id": company.get("company_id")}
                    }

    cla.log.debug(f'{fn} - creating company with name: {company_name}')
    company = Company()
    company.set_company_id(str(uuid.uuid4()))
    company.set_company_name(company_name)
    company.set_company_manager_id(manager.get_user_id())
    company.set_company_acl(manager.get_lf_username())
    company.save()
    cla.log.debug(f'{fn} - created company with name: {company_name} with company_id: {company.get_company_id()}')

    # Create audit trail for company
    event_data = f'User {auth_user.username} created Company {company.get_company_name()} ' \
                 f'with company_id: {company.get_company_id()}.'
    event_summary = f'User {auth_user.username} created Company {company.get_company_name()}.'
    Event.create_event(
        event_type=EventType.CreateCompany,
        event_company_id=company.get_company_id(),
        event_data=event_data,
        event_summary=event_summary,
        event_user_id=user_id,
        contains_pii=False,
    )

    return {"status_code": HTTP_200, "data": company.to_dict()}
Esempio n. 7
0
def company(company_table):
    """ create project """
    with patch(PATCH_METHOD) as req:
        req.return_value = {}
        company_instance = Company()
        company_instance.set_company_id("foo_company_id")
        company_instance.set_company_name("foo_company_name")
        company_instance.save()
        yield company_instance
Esempio n. 8
0
def create_company(auth_user,
                   company_name=None,
                   company_manager_id=None,
                   company_manager_user_name=None,
                   company_manager_user_email=None,
                   user_id=None,
                   response=None):
    """
    Creates an company and returns the newly created company in dict format.

    :param company_name: The company name.
    :type company_name: string
    :param company_manager_id: The ID of the company manager user.
    :type company_manager_id: string
    :param company_manager_user_name: The user name of the company manager user.
    :type company_manager_user_name: string
    :param company_manager_user_email: The user email of the company manager user.
    :type company_manager_user_email: string
    :return: dict representation of the company object.
    :rtype: dict
    """

    manager = cla.controllers.user.get_or_create_user(auth_user)

    for company in get_companies():
        if company.get("company_name") == company_name:
            cla.log.error({"error": "Company already exists"})
            response.status = HTTP_409
            return {
                "status_code": HTTP_409,
                "data": {
                    "error": "Company already exists.",
                    "company_id": company.get("company_id")
                }
            }

    company = Company()
    company.set_company_id(str(uuid.uuid4()))
    company.set_company_name(company_name)
    company.set_company_manager_id(manager.get_user_id())
    company.set_company_acl(manager.get_lf_username())

    company.save()

    # Create audit trail for company
    event_data = 'Company-{} created'.format(company.get_company_name())
    Event.create_event(
        event_type=EventType.CreateCompany,
        event_company_id=company.get_company_id(),
        event_data=event_data,
        event_user_id=user_id,
        contains_pii=False,
    )

    return {"status_code": HTTP_200, "data": company.to_dict()}
Esempio n. 9
0
def company_instance():
    """
    Mock Company instance
    """
    with patch(PATCH_METHOD) as req:
        req.return_value = COMPANY_TABLE_DATA
        instance = Company()
        instance.set_company_id("uuid")
        instance.set_company_name("co")
        instance.set_company_external_id("external id")
        instance.save()
        yield instance
Esempio n. 10
0
def update_company(
        company_id: str,  # pylint: disable=too-many-arguments
        company_name: str = None,
        company_manager_id: str = None,
        username: str = None):
    """
    Updates an company and returns the newly updated company in dict format.
    A value of None means the field should not be updated.

    :param company_id: ID of the company to update.
    :type company_id: str
    :param company_name: New company name.
    :type company_name: string | None
    :param company_manager_id: The ID of the company manager user.
    :type company_manager_id: str
    :param username: The username of the existing company manager user who performs the company update.
    :type username: str
    :return: dict representation of the company object.
    :rtype: dict
    """
    company = Company()
    try:
        company.load(str(company_id))
    except DoesNotExist as err:
        return {'errors': {'company_id': str(err)}}

    company_acl_verify(username, company)
    update_str = ""

    if company_name is not None:
        company.set_company_name(company_name)
        update_str += f"The company name was updated to {company_name}. "
    if company_manager_id is not None:
        val = hug.types.uuid(company_manager_id)
        company.set_company_manager_id(str(val))
        update_str += f"The company company manager id was updated to {val}"

    company.save()

    # Audit update event
    event_data = update_str
    Event.create_event(
        event_data=event_data,
        event_summary=event_data,
        event_type=EventType.UpdateCompany,
        event_company_id=company_id,
        contains_pii=False,
    )
    return company.to_dict()
Esempio n. 11
0
def remove_permission(auth_user: AuthUser, username: str, company_id: str):
    if auth_user.username not in admin_list:
        return {'error': 'unauthorized'}

    cla.log.info('company ({}) removed for ({}) by {}'.format(company_id, username, auth_user.username))

    company = Company()
    try:
        company.load(company_id)
    except Exception as err:
        print('Unable to update company permission: {}'.format(err))
        return {'error': str(err)}

    company.remove_company_acl(username)
    company.save()
Esempio n. 12
0
def create_company(auth_user,
                   company_name=None,
                   company_manager_id=None,
                   company_manager_user_name=None,
                   company_manager_user_email=None,
                   user_id=None):
    """
    Creates an company and returns the newly created company in dict format.

    :param company_name: The company name.
    :type company_name: string
    :param company_manager_id: The ID of the company manager user.
    :type company_manager_id: string
    :param company_manager_user_name: The user name of the company manager user.
    :type company_manager_user_name: string
    :param company_manager_user_email: The user email of the company manager user.
    :type company_manager_user_email: string
    :return: dict representation of the company object.
    :rtype: dict
    """

    manager = cla.controllers.user.get_or_create_user(auth_user)

    for company in get_companies():
        if company.get("company_name") == company_name:
            cla.log.error({"error": "Company already exists"})
            return {
                "status_code": HTTP_409,
                "data": {
                    "error": "Company already exists.",
                    "company_id": company.get("company_id")
                }
            }

    company = Company()
    company.set_company_id(str(uuid.uuid4()))
    company.set_company_name(company_name)
    company.set_company_manager_id(manager.get_user_id())
    company.set_company_acl(manager.get_lf_username())

    company.save()

    return {"status_code": HTTP_200, "data": company.to_dict()}
Esempio n. 13
0
def invite_cla_manager(contributor_id, contributor_name, contributor_email,
                       cla_manager_name, cla_manager_email, project_name,
                       company_name):
    """
    Sends email to the specified CLA Manager to sign up through the Corporate
    console and adds the requested user to the Approved List request queue.

    :param contributor_id: The id of the user inviting the CLA Manager
    :param contributor_name: The name of the user inviting the CLA Manager
    :param contributor_email: The email address that this user wants to be added to the Approved List. Must exist in the user's list of emails.
    :param cla_manager_name: The name of the CLA manager
    :param cla_manager_email: The email address of the CLA manager
    :param project_name: The name of the project
    :param company_name: The name of the organization/company
    """
    user = User()
    try:
        user.load(contributor_id)
    except DoesNotExist as err:
        msg = f'unable to load user by id: {contributor_id} for inviting company admin - error: {err}'
        cla.log.warning(msg)
        return {
            'errors': {
                'user_id': contributor_id,
                'message': msg,
                'error': str(err)
            }
        }

    project = Project()
    try:
        project.load_project_by_name(project_name)
    except DoesNotExist as err:
        msg = f'unable to load project by name: {project_name} for inviting company admin - error: {err}'
        cla.log.warning(msg)
        return {
            'errors': {
                'project_name': project_name,
                'message': msg,
                'error': str(err)
            }
        }
    company = Company()
    try:
        company.load_company_by_name(company_name)
    except DoesNotExist as err:
        msg = f'unable to load company by name: {company_name} - error: {err}'
        cla.log.warning(msg)
        company.set_company_id(str(uuid.uuid4()))
        company.set_company_name(company_name)
        company.save()

    # Add user lfusername if exists
    username = None
    if user.get_lf_username():
        username = user.get_lf_username()
    elif user.get_user_name():
        username = user.get_user_name()
    if username:
        company.add_company_acl(username)
        company.save()

    # create company invite
    company_invite = CompanyInvite()
    company_invite.set_company_invite_id(str(uuid.uuid4()))
    company_invite.set_requested_company_id(company.get_company_id())
    company_invite.set_user_id(user.get_user_id())
    company_invite.save()

    # We'll use the user's provided contributor name - if not provided use what we have in the DB
    if contributor_name is None:
        contributor_name = user.get_user_name()

    log_msg = (
        f'sent email to CLA Manager: {cla_manager_name} with email {cla_manager_email} '
        f'for project {project_name} and company {company_name} '
        f'to user {contributor_name} with email {contributor_email}')
    # Send email to the admin. set account_exists=False since the admin needs to sign up through the Corporate Console.
    cla.log.info(log_msg)
    send_email_to_cla_manager(project, contributor_name, contributor_email,
                              cla_manager_name, cla_manager_email,
                              company_name, False)

    # update ccla_whitelist_request
    ccla_whitelist_request = CCLAWhitelistRequest()
    ccla_whitelist_request.set_request_id(str(uuid.uuid4()))
    ccla_whitelist_request.set_company_name(company_name)
    ccla_whitelist_request.set_project_name(project_name)
    ccla_whitelist_request.set_user_github_id(contributor_id)
    ccla_whitelist_request.set_user_github_username(contributor_name)
    ccla_whitelist_request.set_user_emails(set([contributor_email]))
    ccla_whitelist_request.set_request_status("pending")
    ccla_whitelist_request.save()

    Event.create_event(
        event_user_id=contributor_id,
        event_project_name=project_name,
        event_data=log_msg,
        event_summary=log_msg,
        event_type=EventType.InviteAdmin,
        contains_pii=True,
    )