Exemple #1
0
def selection_id():
    if not html.has_var('selection'):
        sel_id = lib.gen_id()
        html.set_var('selection', sel_id)
        return sel_id
    else:
        sel_id = html.var('selection')
        # Avoid illegal file access by introducing .. or /
        if not re.match("^[-0-9a-zA-Z]+$", sel_id):
            new_id = lib.gen_id()
            html.set_var('selection', new_id)
            return new_id
        else:
            return sel_id
Exemple #2
0
def selection_id():
    if not html.has_var('selection'):
        sel_id = lib.gen_id()
        html.set_var('selection', sel_id)
        return sel_id
    else:
        sel_id = html.var('selection')
        # Avoid illegal file access by introducing .. or /
        if not re.match("^[-0-9a-zA-Z]+$", sel_id):
            new_id = lib.gen_id()
            html.set_var('selection', new_id)
            return new_id
        else:
            return sel_id
Exemple #3
0
def page_notify():
    if not config.user.may("general.notify"):
        raise MKAuthException(
            _("You are not allowed to use the notification module."))

    html.header(_('Notify Users'), stylesheets=['pages', 'status', 'views'])

    html.begin_context_buttons()
    html.context_button(_('User Config'), 'wato.py?mode=users', 'users')
    html.end_context_buttons()

    def validate(msg):
        if not msg.get('text'):
            raise MKUserError('text', _('You need to provide a text.'))

        if not msg.get('methods'):
            raise MKUserError(
                'methods',
                _('Please select at least one notification method.'))

        valid_methods = notify_methods.keys()
        for method in msg['methods']:
            if method not in valid_methods:
                raise MKUserError('methods',
                                  _('Invalid notitification method selected.'))

        # On manually entered list of users validate the names
        if type(msg['dest']) == tuple and msg['dest'][0] == 'list':
            existing = config.multisite_users.keys()
            for user_id in msg['dest'][1]:
                if user_id not in existing:
                    raise MKUserError(
                        'dest',
                        _('A user with the id "%s" does not exist.') % user_id)

        # FIXME: More validation

    msg = forms.edit_dictionary(
        vs_notify,
        {},
        title=_('Notify Users'),
        buttontext=_('Send Notification'),
        validate=validate,
        method='POST',
    )

    if msg:
        msg['id'] = lib.gen_id()
        msg['time'] = time.time()

        # construct the list of recipients
        recipients = []

        if type(msg['dest']) == str:
            dest_what = msg['dest']
        else:
            dest_what = msg['dest'][0]

        if dest_what == 'broadcast':
            recipients = config.multisite_users.keys()

        elif dest_what == 'online':
            recipients = userdb.get_online_user_ids()

        elif dest_what == 'list':
            recipients = msg['dest'][1]

        num_recipients = len(recipients)

        num_success = {}
        for method in msg['methods']:
            num_success[method] = 0

        # Now loop all notitification methods to send the notifications
        errors = {}
        for user_id in recipients:
            for method in msg['methods']:
                try:
                    handler = notify_methods[method]['handler']
                    handler(user_id, msg)
                    num_success[method] = num_success[method] + 1
                except MKInternalError, e:
                    errors.setdefault(method, []).append((user_id, e))

        message = _('The notification has been sent via<br>')
        message += "<table>"
        for method in msg['methods']:
            message += "<tr><td>%s</td><td>to %d of %d recipients</td></tr>" %\
                            (notify_methods[method]["title"], num_success[method], num_recipients)
        message += "</table>"

        message += _('<p>Sent notification to: %s</p>') % ', '.join(recipients)
        message += '<a href="%s">%s</a>' % (html.makeuri(
            []), _('Back to previous page'))
        html.message(HTML(message))

        if errors:
            error_message = ""
            for method, method_errors in errors.items():
                error_message += _(
                    "Failed to send %s notifications to the following users:"
                ) % method
                error_message += "<table>"
                for user, exception in method_errors:
                    error_message += "<tr><td><tt>%s</tt></td><td>%s</td></tr>" % (
                        user, html.attrencode(exception))
                error_message += "</table><br>"
            html.show_error(HTML(error_message))
Exemple #4
0
def page_notify():
    if not config.may("general.notify"):
        raise MKAuthException(_("You are not allowed to use the notification module."))

    html.header(_('Notify Users'), stylesheets = ['pages', 'status', 'views'])

    html.begin_context_buttons()
    html.context_button(_('User Config'), 'wato.py?mode=users', 'users')
    html.end_context_buttons()

    def validate(msg):
        if not msg.get('text'):
            raise MKUserError('text', _('You need to provide a text.'))

        if not msg.get('methods'):
            raise MKUserError('methods', _('Please select at least one notification method.'))

        valid_methods = notify_methods.keys()
        for method in msg['methods']:
            if method not in valid_methods:
                raise MKUserError('methods', _('Invalid notitification method selected.'))

        # On manually entered list of users validate the names
        if type(msg['dest']) == tuple and msg['dest'][0] == 'list':
            existing = config.multisite_users.keys()
            for user_id in msg['dest'][1]:
                if user_id not in existing:
                    raise MKUserError('dest', _('A user with the id "%s" does not exist.') % user_id)

        # FIXME: More validation

    msg = forms.edit_dictionary(vs_notify, {},
        title = _('Notify Users'),
        buttontext = _('Send Notification'),
        validate = validate,
        method = 'POST',
    )

    if msg:
        msg['id']   = lib.gen_id()
        msg['time'] = time.time()

        # construct the list of recipients
        recipients = []

        if type(msg['dest']) == str:
            dest_what = msg['dest']
        else:
            dest_what = msg['dest'][0]

        if dest_what == 'broadcast':
            recipients = config.multisite_users.keys()

        elif dest_what == 'online':
            recipients = userdb.get_online_user_ids()

        elif dest_what == 'list':
            recipients = msg['dest'][1]

        num_recipients = len(recipients)
        num_success = 0
        num_failed  = 0

        # Now loop all notitification methods to send the notifications
        for user_id in recipients:
            for method in msg['methods']:
                try:
                    handler = notify_methods[method]['handler']
                    handler(user_id, msg)
                    num_success += 1
                except MKInternalError, e:
                    num_failed += 1
                    html.show_error(_('Failed to send %s notification to %s: %s') % (method, user_id, e))

        msg = _('The notification has been sent to %d of %d recipients.') % (num_success, num_recipients)
        msg += ' <a href="%s">%s</a>' % (html.makeuri([]), _('Back to previous page'))

        msg += '<p>Sent notification to: %s</p>' % ', '.join(recipients)

        html.message(msg)
Exemple #5
0
def selection_id():
    if not html.has_var('selection'):
        sel_id = lib.gen_id()
        html.add_var('selection', sel_id)
    return html.var('selection')
Exemple #6
0
def page_notify():
    if not config.user.may("general.notify"):
        raise MKAuthException(_("You are not allowed to use the notification module."))

    html.header(_('Notify Users'), stylesheets = ['pages', 'status', 'views'])

    html.begin_context_buttons()
    html.context_button(_('User Config'), 'wato.py?mode=users', 'users')
    html.end_context_buttons()

    def validate(msg):
        if not msg.get('text'):
            raise MKUserError('text', _('You need to provide a text.'))

        if not msg.get('methods'):
            raise MKUserError('methods', _('Please select at least one notification method.'))

        valid_methods = notify_methods.keys()
        for method in msg['methods']:
            if method not in valid_methods:
                raise MKUserError('methods', _('Invalid notitification method selected.'))

        # On manually entered list of users validate the names
        if type(msg['dest']) == tuple and msg['dest'][0] == 'list':
            existing = config.multisite_users.keys()
            for user_id in msg['dest'][1]:
                if user_id not in existing:
                    raise MKUserError('dest', _('A user with the id "%s" does not exist.') % user_id)

        # FIXME: More validation

    msg = forms.edit_dictionary(vs_notify, {},
        title = _('Notify Users'),
        buttontext = _('Send Notification'),
        validate = validate,
        method = 'POST',
    )

    if msg:
        msg['id']   = lib.gen_id()
        msg['time'] = time.time()

        # construct the list of recipients
        recipients = []

        if type(msg['dest']) == str:
            dest_what = msg['dest']
        else:
            dest_what = msg['dest'][0]

        if dest_what == 'broadcast':
            recipients = config.multisite_users.keys()

        elif dest_what == 'online':
            recipients = userdb.get_online_user_ids()

        elif dest_what == 'list':
            recipients = msg['dest'][1]

        num_recipients = len(recipients)

        num_success = {}
        for method in msg['methods']:
            num_success[method] = 0

        # Now loop all notitification methods to send the notifications
        errors = {}
        for user_id in recipients:
            for method in msg['methods']:
                try:
                    handler = notify_methods[method]['handler']
                    handler(user_id, msg)
                    num_success[method] = num_success[method] + 1
                except MKInternalError, e:
                    errors.setdefault(method, []).append((user_id, e))

        message = _('The notification has been sent via<br>')
        message += "<table>"
        for method in msg['methods']:
            message += "<tr><td>%s</td><td>to %d of %d recipients</td></tr>" %\
                            (notify_methods[method]["title"], num_success[method], num_recipients)
        message += "</table>"

        message += _('<p>Sent notification to: %s</p>') % ', '.join(recipients)
        message += '<a href="%s">%s</a>' % (html.makeuri([]), _('Back to previous page'))
        html.message(HTML(message))

        if errors:
            error_message = ""
            for method, method_errors in errors.items():
                error_message += _("Failed to send %s notifications to the following users:") % method
                error_message += "<table>"
                for user, exception in method_errors:
                    error_message += "<tr><td><tt>%s</tt></td><td>%s</td></tr>" % (user, html.attrencode(exception))
                error_message += "</table><br>"
            html.show_error(HTML(error_message))