Exemplo n.º 1
0
 def create_entity(entity_info: Dict[str, Any]):
     """Create an Entity."""
     entity = EntityModel()
     entity.business_identifier = entity_info.get('businessIdentifier',
                                                  None)
     entity = entity.flush()
     entity.commit()
     return Entity(entity)
Exemplo n.º 2
0
def factory_entity_model(business_identifier='CP1234567',
                         business_number='791861073BC0001',
                         name='Foobar, Inc.', pass_code=None):
    """Return a valid entity object with the provided fields."""
    entity = EntityModel(business_identifier=business_identifier,
                         business_number=business_number,
                         name=name,
                         pass_code=pass_code)
    entity.save()
    return entity
Exemplo n.º 3
0
    def update_contact(business_identifier, contact_info: Dict[str, Any]):
        """Update a business contact for the specified Entity."""
        entity = EntityModel.find_by_business_identifier(business_identifier)
        if entity is None:
            raise BusinessException(Error.DATA_NOT_FOUND, None)
        contact = ContactModel.find_by_entity_id(entity.id)
        contact.email = contact_info.get('emailAddress', contact.email)
        contact.phone = contact_info.get('phoneNumber', contact.phone)
        contact.phone_extension = contact_info.get('extension',
                                                   contact.phone_extension)
        contact = contact.flush()
        contact.commit()

        entity = EntityModel.find_by_business_identifier(business_identifier)
        return Entity(entity)
Exemplo n.º 4
0
    def update_entity(business_identifier: str,
                      entity_info: dict,
                      token_info: Dict = None):
        """Update an entity from the given dictionary.

        Completely replaces the entity including the business identifier
        """
        if not entity_info or not business_identifier:
            return None
        # todo No memberhsip created at this point. check_auth wont work.ideally we shud put the logic in here
        # check_auth(token_info, one_of_roles=allowed_roles, business_identifier=business_identifier)
        entity = EntityModel.find_by_business_identifier(business_identifier)
        if entity is None or entity.corp_type_code is None:
            raise BusinessException(Error.DATA_NOT_FOUND, None)
        # if entity.corp_type_code != token_info.get('corp_type', None):
        #    raise BusinessException(Error.INVALID_USER_CREDENTIALS, None)

        is_system = token_info and Role.SYSTEM.value in token_info.get(
            'realm_access').get('roles')
        if is_system:
            if entity_info.get('passCode') is not None:
                entity_info['passCode'] = passcode_hash(
                    entity_info['passCode'])

        entity.update_from_dict(**camelback2snake(entity_info))
        entity.commit()

        entity = Entity(entity)
        return entity
Exemplo n.º 5
0
 def get_contact_for_business(business_identifier):
     """Get the contact for a business identified by the given id."""
     entity = EntityModel.find_by_business_identifier(business_identifier)
     if entity is None:
         return None
     contact = ContactModel.find_by_entity_id(entity.id)
     return contact
Exemplo n.º 6
0
    def reset_passcode(business_identifier: str,
                       email_addresses: str = None,
                       token_info: Dict = None):
        """Reset the entity passcode and send email."""
        check_auth(token_info,
                   one_of_roles=ALL_ALLOWED_ROLES,
                   business_identifier=business_identifier)
        current_app.logger.debug(
            'reset passcode identifier:{}; token:{}'.format(
                business_identifier, token_info))
        entity: EntityModel = EntityModel.find_by_business_identifier(
            business_identifier)
        # generate passcode and set
        new_pass_code = ''.join(
            secrets.choice(string.digits) for i in range(9))

        entity.pass_code = passcode_hash(new_pass_code)
        entity.pass_code_claimed = False
        entity.save()

        if email_addresses:
            mailer_payload = dict(emailAddresses=email_addresses,
                                  passCode=new_pass_code,
                                  businessIdentifier=business_identifier,
                                  businessName=entity.name,
                                  isStaffInitiated=Role.STAFF.value
                                  in token_info.get('roles'))
            publish_to_mailer(notification_type='resetPasscode',
                              business_identifier=business_identifier,
                              data=mailer_payload)

        entity = Entity(entity)
        return entity
Exemplo n.º 7
0
    def reset_passcode(business_identifier: str, email_addresses: str = None, **kwargs):
        """Reset the entity passcode and send email."""
        user_from_context: UserContext = kwargs['user_context']
        check_auth(one_of_roles=ALL_ALLOWED_ROLES, business_identifier=business_identifier)
        current_app.logger.debug(f'reset passcode identifier:{business_identifier}')
        entity: EntityModel = EntityModel.find_by_business_identifier(business_identifier)
        # generate passcode and set
        new_pass_code = ''.join(secrets.choice(string.digits) for i in range(9))

        entity.pass_code = passcode_hash(new_pass_code)
        entity.pass_code_claimed = False
        entity.save()
        if email_addresses:
            mailer_payload = dict(
                emailAddresses=email_addresses,
                passCode=new_pass_code,
                businessIdentifier=business_identifier,
                businessName=entity.name,
                isStaffInitiated=user_from_context.is_staff()
            )
            publish_to_mailer(
                notification_type='resetPasscode', business_identifier=business_identifier, data=mailer_payload
            )

        entity = Entity(entity)
        return entity
Exemplo n.º 8
0
    def update_entity(business_identifier: str, entity_info: dict, **kwargs):
        """Update an entity from the given dictionary.

        Completely replaces the entity including the business identifier
        """
        if not entity_info or not business_identifier:
            return None
        user_from_context: UserContext = kwargs['user_context']
        # todo No memberhsip created at this point. check_auth wont work.ideally we shud put the logic in here
        # check_auth(token_info, one_of_roles=allowed_roles, business_identifier=business_identifier)
        entity = EntityModel.find_by_business_identifier(business_identifier)
        if entity is None or entity.corp_type_code is None:
            raise BusinessException(Error.DATA_NOT_FOUND, None)
        # if entity.corp_type_code != token_info.get('corp_type', None):
        #    raise BusinessException(Error.INVALID_USER_CREDENTIALS, None)

        if user_from_context.is_system():
            if entity_info.get('passCode') is not None:
                entity_info['passCode'] = passcode_hash(entity_info['passCode'])

        # Small mapping from state -> status. EX in LEAR: Business.State.HISTORICAL
        if 'state' in entity_info:
            entity_info['status'] = entity_info['state']
            del entity_info['state']

        entity.update_from_dict(**camelback2snake(entity_info))
        entity.commit()

        entity = Entity(entity)
        return entity
Exemplo n.º 9
0
    def save_entity(entity_info: dict):
        """Create/update an entity from the given dictionary."""
        if not entity_info:
            return None

        existing_entity = EntityModel.find_by_business_identifier(entity_info['businessIdentifier'])
        if existing_entity is None:
            entity_model = EntityModel.create_from_dict(entity_info)
        else:
            # TODO temporary allow update passcode, should replace with reset passcode endpoint.
            entity_info['passCode'] = passcode_hash(entity_info['passCode'])
            existing_entity.update_from_dict(**camelback2snake(entity_info))
            entity_model = existing_entity
            entity_model.commit()

        entity = Entity(entity_model)
        return entity
Exemplo n.º 10
0
    def find_by_entity_id(entity_id):
        """Find and return an existing Entity with the provided id."""
        if entity_id is None:
            return None

        entity_model = EntityModel.find_by_entity_id(entity_id)
        if not entity_model:
            return None

        return Entity(entity_model)
Exemplo n.º 11
0
def factory_entity_service(business_identifier='CP1234567',
                           business_number='791861073BC0001',
                           name='Foobar, Inc.'):
    """Produce a templated entity model."""
    entity = EntityModel.create_from_dict({
        'business_identifier': business_identifier,
        'business_number': business_number,
        'name': name
    })
    entity.save()
    entity_service = EntityService(entity)
    return entity_service
Exemplo n.º 12
0
    def find_by_business_identifier(cls, business_identifier: str = None):
        """Given a business identifier, this will return the corresponding entity or None."""
        if not business_identifier:
            return None

        entity_model = EntityModel.find_by_business_identifier(
            business_identifier)

        if not entity_model:
            raise BusinessException(Error.DATA_NOT_FOUND, None)

        entity = Entity(entity_model)
        return entity
Exemplo n.º 13
0
    def find_by_business_identifier(cls, business_identifier: str = None):
        """Given a business identifier, this will return the corresponding entity or None."""
        if not business_identifier:
            return None

        entity_model = EntityModel.find_by_business_identifier(
            business_identifier)

        if not entity_model:
            return None

        entity = Entity(entity_model)
        return entity
Exemplo n.º 14
0
    def find_by_business_identifier(cls, business_identifier: str = None, token_info: Dict = None,
                                    allowed_roles: Tuple = None, skip_auth: bool = False):
        """Given a business identifier, this will return the corresponding entity or None."""
        if not business_identifier:
            return None
        entity_model = EntityModel.find_by_business_identifier(business_identifier)

        if not entity_model:
            return None

        if not skip_auth:
            check_auth(token_info, one_of_roles=allowed_roles, business_identifier=business_identifier)

        entity = Entity(entity_model)
        return entity
Exemplo n.º 15
0
 def create_entity(entity_info: dict):
     """Create an Entity."""
     entity = EntityModel.create_from_dict(entity_info)
     entity.commit()
     return Entity(entity)