def publish_activity(activity: Activity): # pylint:disable=unused-argument """Publish the activity asynchronously, using the given details.""" try: # find user_id if haven't passed in if not activity.actor_id and g and 'jwt_oidc_token_info' in g: user: UserModel = UserModel.find_by_jwt_token() activity.actor_id = user.id if user else None data = { 'actorId': activity.actor_id, 'action': activity.action, 'itemType': 'ACCOUNT', 'itemName': activity.name, 'itemId': activity.id, 'itemValue': activity.value, 'orgId': activity.org_id, 'remoteAddr': fetch_remote_addr(), 'createdAt': f'{datetime.now()}' } source = 'https://api.auth.bcregistry.gov.bc.ca/v1/accounts' payload = { 'specversion': '1.x-wip', 'type': 'bc.registry.auth.activity', 'source': source, 'id': str(uuid.uuid1()), 'time': f'{datetime.now()}', 'datacontenttype': 'application/json', 'data': data } publish_response(payload=payload, client_name=CONFIG.NATS_ACTIVITY_CLIENT_NAME, subject=CONFIG.NATS_ACTIVITY_SUBJECT) except Exception as err: # noqa: B902 # pylint: disable=broad-except capture_message('Activity Queue Publish Event Error:' + str(err), level='error') current_app.logger.error('Activity Queue Publish Event Error:', exc_info=True)
def get_ip_if_allowed(): """ Get the remote address (IP address) of the current Flask context, if the project's privacy settings allow it. Behind the scenes, this calls back to the FlaskPlugin from SQLAlchemy-Continuum in order to maintain forward compatibility """ ip_logging_allowed = False try: if g.project.logging_preference == LoggingMode.RECORD_IP: ip_logging_allowed = True # If ip recording WAS enabled prior to this transaction, # we record the IP for this one last transaction old_logging_mode = get_history(g.project, "logging_preference")[2] if old_logging_mode and old_logging_mode[0] == LoggingMode.RECORD_IP: ip_logging_allowed = True except AttributeError: # g.project doesn't exist, it's being created or this action is outside # the scope of a project. Use the default logging mode to decide if LoggingMode.default() == LoggingMode.RECORD_IP: ip_logging_allowed = True if ip_logging_allowed: return fetch_remote_addr() else: return None
def create_activity(cls, obj): """Create activity records if the model is versioned.""" if isinstance(obj, VersionedModel) and not current_app.config.get( 'DISABLE_ACTIVITY_LOGS'): activity = activity_plugin.activity_cls(verb='update', object=obj, data={ 'user_name': cls._get_user_name(), 'remote_addr': fetch_remote_addr() }) db.session.add(activity)
def create_activity(cls, obj, is_delete=False): """Create activity records if the model is versioned.""" if isinstance(obj, VersionedModel) and not current_app.config.get( 'DISABLE_ACTIVITY_LOGS'): if is_delete: verb = 'delete' else: verb = 'update' if obj.modified_by is not None else 'create' activity = activity_plugin.activity_cls( verb=verb, object=obj, data={ 'user_name': g.jwt_oidc_token_info.get('preferred_username', None) if g and 'jwt_oidc_token_info' in g else None, 'remote_addr': fetch_remote_addr() }) db.session.add(activity)