Exemple #1
0
def factory_activity_log_model(actor: str,
                               action: str,
                               item_type: str = 'Account',
                               item_name='Foo Bar',
                               item_id=10,
                               org_id=10,
                               remote_addr=''):
    """Create a Log Model."""
    activity_log = ActivityLogModel(actor_id=actor,
                                    action=action,
                                    item_name=item_name,
                                    item_id=item_id,
                                    item_type=item_type,
                                    remote_addr=remote_addr,
                                    org_id=org_id)
    activity_log.save()
Exemple #2
0
    def fetch_activity_logs(org_id: int, **kwargs):  # pylint: disable=too-many-locals
        """Search all activity logs."""
        user_from_context: UserContext = kwargs['user_context']
        item_name = kwargs.get('item_name')
        item_type = kwargs.get('item_type')
        action = kwargs.get('action')
        check_auth(one_of_roles=(ADMIN, STAFF), org_id=org_id)
        logs = {'activity_logs': []}
        page: int = int(kwargs.get('page'))
        limit: int = int(kwargs.get('limit'))
        search_args = (item_name, item_type, action, page, limit)

        current_app.logger.debug('<fetch_activity logs ')
        results, count = ActivityLogModel.fetch_activity_logs_for_account(
            org_id, *search_args)
        is_staff_access = user_from_context.is_staff()
        for result in results:
            activity_log: ActivityLogModel = result[0]
            log_dict = ActivityLogSchema(
                exclude=('actor_id', )).dump(activity_log)

            if user := result[1]:
                actor = ActivityLog._mask_user_name(is_staff_access, user)
                log_dict['actor'] = actor
            log_dict['action'] = ActivityLog._build_string(activity_log)
            logs['activity_logs'].append(log_dict)
Exemple #3
0
    def fetch_activity_logs(org_id: int, token_info: Dict = None, **kwargs):  # pylint: disable=too-many-locals
        """Search all activity logs."""
        item_name = kwargs.get('item_name')
        item_type = kwargs.get('item_type')
        action = kwargs.get('action')
        check_auth(token_info, one_of_roles=(ADMIN, STAFF), org_id=org_id)
        logs = {'activity_logs': []}
        page: int = int(kwargs.get('page'))
        limit: int = int(kwargs.get('limit'))
        search_args = (item_name,
                       item_type,
                       action,
                       page,
                       limit)

        current_app.logger.debug('<fetch_activity logs ')
        results, count = ActivityLogModel.fetch_activity_logs_for_account(org_id, *search_args)
        is_staff_access = g.jwt_oidc_token_info and 'staff' in \
            g.jwt_oidc_token_info.get('realm_access', {}).get('roles', None)
        for result in results:
            activity_log: ActivityLogModel = result[0]

            log_dict = ActivityLogSchema(exclude=('actor_id',)).dump(activity_log)

            if user := result[1]:
                actor = ActivityLog._mask_user_name(is_staff_access, user)
                log_dict['actor'] = actor
            logs['activity_logs'].append(log_dict)