예제 #1
0
def is_alias_used(my_what: Union[GroupType, Literal["roles", "timeperiods"]],
                  my_name: GroupName,
                  my_alias: str) -> Tuple[bool, Optional[str]]:
    # Host / Service / Contact groups
    all_groups = load_group_information()
    for what, groups in all_groups.items():
        for gid, group in groups.items():
            if group["alias"] == my_alias and (my_what != what
                                               or my_name != gid):
                return False, _(
                    "This alias is already used in the %s group %s.") % (what,
                                                                         gid)

    # Timeperiods
    timeperiods = cmk.gui.watolib.timeperiods.load_timeperiods()
    for key, value in timeperiods.items():
        if timeperiod_spec_alias(value) == my_alias and (
                my_what != "timeperiods" or my_name != key):
            return False, _(
                "This alias is already used in timeperiod %s.") % key

    # Roles
    roles = userdb_utils.load_roles()
    for key, value in roles.items():
        if value.get("alias") == my_alias and (my_what != "roles"
                                               or my_name != key):
            return False, _("This alias is already used in the role %s.") % key

    return True, None
예제 #2
0
    def _to_valuespec(self, tp_spec):
        if not tp_spec:
            return {}

        exceptions = []
        for exception_name, time_ranges in tp_spec.items():
            if exception_name not in defines.weekday_ids() + [
                    "alias", "exclude"
            ]:
                exceptions.append(
                    (exception_name,
                     self._time_ranges_to_valuespec(time_ranges)))

        vs_spec = {
            "name":
            unique_default_name_suggestion(self._name or "time_period",
                                           list(self._timeperiods.keys())),
            "alias":
            timeperiod_spec_alias(tp_spec),
            "weekdays":
            self._weekdays_to_valuespec(tp_spec),
            "exclude":
            tp_spec.get("exclude", []),
            "exceptions":
            sorted(exceptions),
        }

        return vs_spec
예제 #3
0
def is_alias_used(my_what, my_name, my_alias):
    # Host / Service / Contact groups
    all_groups = load_group_information()
    for what, groups in all_groups.items():
        for gid, group in groups.items():
            if group['alias'] == my_alias and (my_what != what
                                               or my_name != gid):
                return False, _(
                    "This alias is already used in the %s group %s.") % (what,
                                                                         gid)

    # Timeperiods
    timeperiods = cmk.gui.watolib.timeperiods.load_timeperiods()
    for key, value in timeperiods.items():
        if timeperiod_spec_alias(value) == my_alias and (
                my_what != "timeperiods" or my_name != key):
            return False, _(
                "This alias is already used in timeperiod %s.") % key

    # Roles
    roles = userdb.load_roles()
    for key, value in roles.items():
        if value.get("alias") == my_alias and (my_what != "roles"
                                               or my_name != key):
            return False, _("This alias is already used in the role %s.") % key

    return True, None
예제 #4
0
 def _find_usages_in_other_timeperiods(self, tpname: str) -> List[TimeperiodUsage]:
     used_in: List[TimeperiodUsage] = []
     for tpn, tp in watolib.timeperiods.load_timeperiods().items():
         if tpname in tp.get("exclude", []):
             used_in.append(("%s: %s (%s)" %
                             (_("Timeperiod"), timeperiod_spec_alias(tp, tpn), _("excluded")),
                             watolib.folder_preserving_link([("mode", "edit_timeperiod"),
                                                             ("edit", tpn)])))
     return used_in
예제 #5
0
    def _other_timeperiod_choices(self):
        """List of timeperiods that can be used for exclusions

        We offer the list of all other timeperiods - but only those that do not exclude the current
        timeperiod (in order to avoid cycles)"""
        other_tps = []

        for tpname, tp in self._timeperiods.items():
            if not self._timeperiod_excludes(tpname):
                other_tps.append((tpname, timeperiod_spec_alias(tp, tpname)))

        return sorted(other_tps, key=lambda a: a[1].lower())
예제 #6
0
    def page(self):
        with table_element("timeperiods",
                           empty_text=_("There are no timeperiods defined yet.")) as table:
            for name, timeperiod in sorted(self._timeperiods.items()):
                table.row()

                table.cell(_("Actions"), css="buttons")
                if name in watolib.timeperiods.builtin_timeperiods():
                    html.i(_("(builtin)"))
                else:
                    self._action_buttons(name)

                table.text_cell(_("Name"), name)
                table.text_cell(_("Alias"), timeperiod_spec_alias(timeperiod))
예제 #7
0
파일: users.py 프로젝트: surajrb/checkmk
    def _show_user_list(self):
        visible_custom_attrs = [(name, attr)
                                for name, attr in userdb.get_user_attributes()
                                if attr.show_in_table()]

        users = userdb.load_users()

        entries = users.items()

        html.begin_form("bulk_delete_form", method="POST")

        roles = userdb.load_roles()
        timeperiods = watolib.timeperiods.load_timeperiods()
        contact_groups = load_contact_group_information()

        with table_element("users",
                           None,
                           empty_text=_("No users are defined yet.")) as table:
            online_threshold = time.time() - config.user_online_maxage
            for uid, user in sorted(
                    entries, key=lambda x: x[1].get("alias", x[0]).lower()):
                table.row()

                # Checkboxes
                table.cell(html.render_input(
                    "_toggle_group",
                    type_="button",
                    class_="checkgroup",
                    onclick="cmk.selection.toggle_all_rows();",
                    value='X'),
                           sortable=False,
                           css="checkbox")

                if uid != config.user.id:
                    html.checkbox(
                        "_c_user_%s" %
                        six.ensure_str(base64.b64encode(uid.encode("utf-8"))))

                user_connection_id = cleanup_connection_id(
                    user.get('connector'))
                connection = get_connection(user_connection_id)

                # Buttons
                table.cell(_("Actions"), css="buttons")
                if connection:  # only show edit buttons when the connector is available and enabled
                    edit_url = watolib.folder_preserving_link([("mode",
                                                                "edit_user"),
                                                               ("edit", uid)])
                    html.icon_button(edit_url, _("Properties"), "edit")

                    clone_url = watolib.folder_preserving_link([
                        ("mode", "edit_user"), ("clone", uid)
                    ])
                    html.icon_button(clone_url,
                                     _("Create a copy of this user"), "clone")

                delete_url = make_action_link([("mode", "users"),
                                               ("_delete", uid)])
                html.icon_button(delete_url, _("Delete"), "delete")

                notifications_url = watolib.folder_preserving_link([
                    ("mode", "user_notifications"), ("user", uid)
                ])
                if watolib.load_configuration_settings().get(
                        "enable_rulebased_notifications"):
                    html.icon_button(
                        notifications_url,
                        _("Custom notification table of this user"),
                        "notifications")

                # ID
                table.cell(_("ID"), uid)

                # Online/Offline
                if config.save_user_access_times:
                    last_seen = user.get('last_seen', 0)
                    if last_seen >= online_threshold:
                        title = _('Online')
                        img_txt = 'online'
                    elif last_seen != 0:
                        title = _('Offline')
                        img_txt = 'offline'
                    elif last_seen == 0:
                        title = _('Never logged in')
                        img_txt = 'inactive'

                    title += ' (%s %s)' % (render.date(last_seen),
                                           render.time_of_day(last_seen))
                    table.cell(_("Act."))
                    html.icon(title, img_txt)

                    table.cell(_("Last seen"))
                    if last_seen != 0:
                        html.write_text("%s %s" %
                                        (render.date(last_seen),
                                         render.time_of_day(last_seen)))
                    else:
                        html.write_text(_("Never logged in"))

                if cmk_version.is_managed_edition():
                    table.cell(_("Customer"), managed.get_customer_name(user))

                # Connection
                if connection:
                    table.cell(
                        _("Connection"), '%s (%s)' %
                        (connection.short_title(), user_connection_id))
                    locked_attributes = userdb.locked_attributes(
                        user_connection_id)
                else:
                    table.cell(
                        _("Connection"),
                        "%s (%s) (%s)" %
                        (_("UNKNOWN"), user_connection_id, _("disabled")),
                        css="error")
                    locked_attributes = []

                # Authentication
                if "automation_secret" in user:
                    auth_method = _("Automation")
                elif user.get("password") or 'password' in locked_attributes:
                    auth_method = _("Password")
                else:
                    auth_method = "<i>%s</i>" % _("none")
                table.cell(_("Authentication"), auth_method)

                table.cell(_("State"))
                if user.get("locked", False):
                    html.icon(_('The login is currently locked'),
                              'user_locked')

                if "disable_notifications" in user and isinstance(
                        user["disable_notifications"], bool):
                    disable_notifications_opts = {
                        "disable": user["disable_notifications"]
                    }
                else:
                    disable_notifications_opts = user.get(
                        "disable_notifications", {})

                if disable_notifications_opts.get("disable", False):
                    html.icon(_('Notifications are disabled'),
                              'notif_disabled')

                # Full name / Alias
                table.text_cell(_("Alias"), user.get("alias", ""))

                # Email
                table.text_cell(_("Email"), user.get("email", ""))

                # Roles
                table.cell(_("Roles"))
                if user.get("roles", []):
                    role_links = [(watolib.folder_preserving_link([
                        ("mode", "edit_role"), ("edit", role)
                    ]), roles[role].get("alias")) for role in user["roles"]]
                    html.write_html(
                        HTML(", ").join(
                            html.render_a(alias, href=link)
                            for (link, alias) in role_links))

                # contact groups
                table.cell(_("Contact groups"))
                cgs = user.get("contactgroups", [])
                if cgs:
                    cg_aliases = [
                        contact_groups[c]['alias']
                        if c in contact_groups else c for c in cgs
                    ]
                    cg_urls = [
                        watolib.folder_preserving_link([("mode",
                                                         "edit_contact_group"),
                                                        ("edit", c)])
                        for c in cgs
                    ]
                    html.write_html(
                        HTML(", ").join(
                            html.render_a(content, href=url)
                            for (content, url) in zip(cg_aliases, cg_urls)))
                else:
                    html.i(_("none"))

                #table.cell(_("Sites"))
                #html.write(vs_authorized_sites().value_to_text(user.get("authorized_sites",
                #                                                vs_authorized_sites().default_value())))

                # notifications
                if not watolib.load_configuration_settings().get(
                        "enable_rulebased_notifications"):
                    table.cell(_("Notifications"))
                    if not cgs:
                        html.i(_("not a contact"))
                    elif not user.get("notifications_enabled", True):
                        html.write_text(_("disabled"))
                    elif user.get("host_notification_options", "") == "" and \
                         user.get("service_notification_options", "") == "":
                        html.write_text(_("all events disabled"))
                    else:
                        tp = user.get("notification_period", "24X7")
                        if tp not in timeperiods:
                            tp = tp + _(" (invalid)")
                        elif tp not in watolib.timeperiods.builtin_timeperiods(
                        ):
                            url = watolib.folder_preserving_link([
                                ("mode", "edit_timeperiod"), ("edit", tp)
                            ])
                            tp = html.render_a(timeperiod_spec_alias(
                                timeperiods[tp], tp),
                                               href=url)
                        else:
                            tp = timeperiod_spec_alias(timeperiods[tp], tp)
                        html.write(tp)

                # the visible custom attributes
                for name, attr in visible_custom_attrs:
                    vs = attr.valuespec()
                    table.cell(escaping.escape_attribute(_u(vs.title())))
                    html.write(
                        vs.value_to_text(user.get(name, vs.default_value())))

        html.button("_bulk_delete_users",
                    _("Bulk Delete"),
                    "submit",
                    style="margin-top:10px")
        html.hidden_fields()
        html.end_form()

        if not load_contact_group_information():
            url = "wato.py?mode=contact_groups"
            html.open_div(class_="info")
            html.write(
                _("Note: you haven't defined any contact groups yet. If you <a href='%s'>"
                  "create some contact groups</a> you can assign users to them und thus "
                  "make them monitoring contacts. Only monitoring contacts can receive "
                  "notifications.") % url)
            html.write(
                " you can assign users to them und thus "
                "make them monitoring contacts. Only monitoring contacts can receive "
                "notifications.")
            html.close_div()