Пример #1
0
def create_general_notification(username,
                                target_users,
                                header,
                                link_url,
                                message,
                                notification_type=NotificationType.ALERT):
    """
    Generate a general notification -- not based on mongo obj.

    :param obj: The object.
    :type obj: class which inherits from
               :class:`crits.core.crits_mongoengine.CritsBaseAttributes`
    :param username: The user creating the notification.
    :type username: str
    :param target_users: The list of users who will get the notification.
    :type target_users: list(str)
    :param header: The notification header message.
    :type header: list(str)
    :param link_url: A link URL for the header, specify None if there is no link.
    :type link_url: str
    :param message: The notification message.
    :type message: str
    :param notification_type: The notification type (e.g. alert, error).
    :type notification_type: str
    """

    if notification_type not in NotificationType.ALL:
        notification_type = NotificationType.ALERT

    n = Notification()
    n.analyst = username
    n.notification_type = notification_type
    n.notification = message
    n.header = header
    n.link_url = link_url

    for target_user in target_users:
        # Check to make sure the user actually exists
        user = CRITsUser.objects(username=target_user).first()
        if user is not None:
            n.users.append(target_user)

    # don't notify the user creating this notification
    n.users = [u for u in n.users if u != username]
    if not len(n.users):
        return
    try:
        n.save()
    except ValidationError:
        pass

    # Signal potentially waiting threads that notification information is available
    for user in n.users:
        notification_lock = NotificationLockManager.get_notification_lock(user)
        notification_lock.acquire()

        try:
            notification_lock.notifyAll()
        finally:
            notification_lock.release()
Пример #2
0
def create_general_notification(
    username, target_users, header, link_url, message, notification_type=NotificationType.ALERT
):
    """
    Generate a general notification -- not based on mongo obj.

    :param obj: The object.
    :type obj: class which inherits from
               :class:`crits.core.crits_mongoengine.CritsBaseAttributes`
    :param username: The user creating the notification.
    :type username: str
    :param target_users: The list of users who will get the notification.
    :type target_users: list(str)
    :param header: The notification header message.
    :type header: list(str)
    :param link_url: A link URL for the header, specify None if there is no link.
    :type link_url: str
    :param message: The notification message.
    :type message: str
    :param notification_type: The notification type (e.g. alert, error).
    :type notification_type: str
    """

    if notification_type not in NotificationType.ALL:
        notification_type = NotificationType.ALERT

    n = Notification()
    n.analyst = username
    n.notification_type = notification_type
    n.notification = message
    n.header = header
    n.link_url = link_url

    for target_user in target_users:
        # Check to make sure the user actually exists
        user = CRITsUser.objects(username=target_user).first()
        if user is not None:
            n.users.append(target_user)

    # don't notify the user creating this notification
    n.users = [u for u in n.users if u != username]
    if not len(n.users):
        return
    try:
        n.save()
    except ValidationError:
        pass

    # Signal potentially waiting threads that notification information is available
    for user in n.users:
        notification_lock = NotificationLockManager.get_notification_lock(user)
        notification_lock.acquire()

        try:
            notification_lock.notifyAll()
        finally:
            notification_lock.release()
Пример #3
0
def create_notification(obj, username, message, source_filter=None,
                        notification_type=NotificationType.ALERT):
    """
    Generate a notification -- based on mongo obj.

    :param obj: The object.
    :type obj: class which inherits from
               :class:`crits.core.crits_mongoengine.CritsBaseAttributes`
    :param username: The user creating the notification.
    :type username: str
    :param message: The notification message.
    :type message: str
    :param source_filter: Filter on who can see this notification.
    :type source_filter: list(str)
    :param notification_type: The notification type (e.g. alert, error).
    :type notification_type: str
    """

    n = Notification()
    n.analyst = username
    obj_type = obj._meta['crits_type']

    if notification_type not in NotificationType.ALL:
        notification_type = NotificationType.ALERT

    n.notification_type = notification_type

    if obj_type == 'Comment':
        n.obj_id = obj.obj_id
        n.obj_type = obj.obj_type
        n.notification = "%s added a comment: %s" % (username, obj.comment)
    else:
        n.notification = message
        n.obj_id = obj.id
        n.obj_type = obj_type

    if hasattr(obj, 'source'):
        sources = [s.name for s in obj.source]

        if obj_type == 'Comment':
            # for comments, use the sources from the object that it is linked to
            # instead of the comments's sources
            referenced_obj = class_from_id(n.obj_type, n.obj_id)
            sources = [s.name for s in referenced_obj.source]

        subscribed_users = get_subscribed_users(n.obj_type, n.obj_id, sources)

        # Filter on users that have access to the source of the object
        for subscribed_user in subscribed_users:
            allowed_sources = user_sources(subscribed_user)

            for allowed_source in allowed_sources:
                if allowed_source in sources:
                    if source_filter is None or allowed_source in source_filter:
                        n.users.append(subscribed_user)
                        break
    else:
        n.users = get_subscribed_users(n.obj_type, n.obj_id, [])

    if obj_type == 'Comment':
        for u in obj.users:
            if u not in n.users:
                n.users.append(u)

    # don't notify the user creating this notification
    n.users = [u for u in n.users if u != username]
    if not len(n.users):
        return
    try:
        n.save()
    except ValidationError:
        pass

    # Signal potentially waiting threads that notification information is available
    for user in n.users:
        notification_lock = NotificationLockManager.get_notification_lock(user)
        notification_lock.acquire()

        try:
            notification_lock.notifyAll()
        finally:
            notification_lock.release()