Exemplo n.º 1
0
 def page(self) -> AjaxPageResult:
     check_csrf_token()
     layout_var = request.get_str_input_mandatory("layout", "{}")
     layout_config = json.loads(layout_var)
     active_config.bi_layouts["templates"].update(layout_config)
     BILayoutManagement.save_layouts()
     return {}
Exemplo n.º 2
0
    def page(self):
        check_csrf_token()
        if not user.may("general.configure_sidebar"):
            raise MKGeneralException(
                _("You are not allowed to change the sidebar."))

        addname = request.var("name")

        if addname is None or addname not in snapin_registry:
            raise MKUserError(None, _("Invalid sidebar element %s") % addname)

        if addname in _used_snapins():
            raise MKUserError(None,
                              _("Element %s is already enabled") % addname)

        user_config = UserSidebarConfig(user, active_config.sidebar)
        snapin = UserSidebarSnapin.from_snapin_type_id(addname)
        user_config.add_snapin(snapin)
        user_config.save()

        with output_funnel.plugged():
            try:
                url = SidebarRenderer().render_snapin(snapin)
            finally:
                snapin_code = output_funnel.drain()

        return {
            "name": addname,
            "url": url,
            "content": snapin_code,
            "refresh": snapin.snapin_type.refresh_regularly(),
            "restart": snapin.snapin_type.refresh_on_restart(),
        }
Exemplo n.º 3
0
    def page(self):
        check_csrf_token()
        response.set_content_type("application/json")
        if not user.may("general.configure_sidebar"):
            return None

        snapin_id = request.var("name")
        if snapin_id is None:
            return None

        state = request.var("state")
        if state not in [
                SnapinVisibility.OPEN.value, SnapinVisibility.CLOSED.value,
                "off"
        ]:
            raise MKUserError("state", "Invalid state: %s" % state)

        user_config = UserSidebarConfig(user, active_config.sidebar)

        try:
            snapin = user_config.get_snapin(snapin_id)
        except KeyError:
            return None

        if state == "off":
            user_config.remove_snapin(snapin)
        else:
            snapin.visible = SnapinVisibility(state)

        user_config.save()
        return None
Exemplo n.º 4
0
    def page(self):
        check_csrf_token()
        ajax_request = self.webapi_request()

        site_id_val = ajax_request.get("site")
        if not site_id_val:
            raise MKUserError(None, "The site_id is missing")
        site_id = site_id_val
        if site_id not in sitenames():
            raise MKUserError(None, _("The requested site does not exist"))

        status = (cmk.gui.sites.states().get(site_id,
                                             cmk.gui.sites.SiteStatus({})).get(
                                                 "state", "unknown"))
        if status == "dead":
            raise MKGeneralException(
                _("The site is marked as dead. Not trying to replicate."))

        site = get_site_config(site_id)
        assert user.id is not None
        result = self._synchronize_profile(site_id, site, user.id)

        if result is not True:
            assert result is not False
            _add_profile_replication_change(site_id, result)
            raise MKGeneralException(result)

        return _("Replication completed successfully.")
Exemplo n.º 5
0
 def page(self) -> AjaxPageResult:
     check_csrf_token()
     _set_user_attribute(
         "ui_sidebar_position",
         None if _sidebar_position_id(_get_sidebar_position()) == "left"
         else "left",
     )
     return {}
Exemplo n.º 6
0
 def page(self) -> AjaxPageResult:
     try:
         check_csrf_token()
         name = request.get_str_input_mandatory("name")
         url = makeuri_contextless(request, [("name", name)],
                                   "dashboard.py")
         validate_start_url(url, "")
         _set_user_attribute("start_url", repr(url))
     except Exception:
         raise MKUserError(None, _("Failed to set start URL"))
     return {}
Exemplo n.º 7
0
    def page(self):
        check_csrf_token()
        user.need_permission("wato.activate")

        api_request = self.webapi_request()
        # ? type of activate_until is unclear
        activate_until = api_request.get("activate_until")
        if not activate_until:
            raise MKUserError("activate_until",
                              _('Missing parameter "%s".') % "activate_until")

        manager = activate_changes.ActivateChangesManager()
        manager.load()
        # ? type of api_request is unclear
        affected_sites_request = ensure_str(  # pylint: disable= six-ensure-str-bin-call
            api_request.get("sites", "").strip())
        if not affected_sites_request:
            affected_sites = manager.dirty_and_active_activation_sites()
        else:
            affected_sites = [
                SiteId(s) for s in affected_sites_request.split(",")
            ]

        comment: Optional[str] = api_request.get("comment", "").strip()

        activate_foreign = api_request.get("activate_foreign", "0") == "1"

        valuespec = _vs_activation("", manager.has_foreign_changes())
        if valuespec:
            valuespec.validate_value(
                {
                    "comment": comment,
                    "foreign": activate_foreign,
                },
                "activate",
            )

        if comment == "":
            comment = None

        activation_id = manager.start(
            sites=affected_sites,
            activate_until=ensure_str(activate_until),  # pylint: disable= six-ensure-str-bin-call
            comment=comment,
            activate_foreign=activate_foreign,
        )

        return {
            "activation_id": activation_id,
        }
Exemplo n.º 8
0
    def page(self) -> AjaxPageResult:
        check_csrf_token()
        themes = [theme for theme, _title in theme_choices()]
        current_theme = theme.get()
        try:
            theme_index = themes.index(current_theme)
        except ValueError:
            raise MKUserError(None, _("Could not determine current theme."))

        if len(themes) == theme_index + 1:
            new_theme = themes[0]
        else:
            new_theme = themes[theme_index + 1]

        _set_user_attribute("ui_theme", new_theme)
        return {}
Exemplo n.º 9
0
    def page(self):
        check_csrf_token()
        if not user.may("wato.diag_host"):
            raise MKAuthException(_("You are not permitted to perform this action."))

        if not transactions.check_transaction():
            raise MKAuthException(_("Invalid transaction"))

        api_request = self.webapi_request()

        hostname = api_request.get("host")
        if not hostname:
            raise MKGeneralException(_("The hostname is missing."))

        host = Host.host(hostname)

        if not host:
            raise MKGeneralException(_("The given host does not exist."))
        if host.is_cluster():
            raise MKGeneralException(_("This view does not support cluster hosts."))

        host.need_permission("read")

        _test = api_request.get("_test")
        if not _test:
            raise MKGeneralException(_("The test is missing."))

        # Execute a specific test
        if _test not in dict(ModeDiagHost.diag_host_tests()):
            raise MKGeneralException(_("Invalid test."))

        # TODO: Use ModeDiagHost._vs_rules() for processing/validation?
        args: List[str] = [""] * 13
        for idx, what in enumerate(
            [
                "ipaddress",
                "snmp_community",
                "agent_port",
                "snmp_timeout",
                "snmp_retries",
                "tcp_connect_timeout",
            ]
        ):
            args[idx] = api_request.get(what, "")

        if api_request.get("snmpv3_use"):
            snmpv3_use = {
                "0": "noAuthNoPriv",
                "1": "authNoPriv",
                "2": "authPriv",
            }.get(api_request.get("snmpv3_use", ""), "")

            args[7] = snmpv3_use
            if snmpv3_use != "noAuthNoPriv":
                snmpv3_auth_proto = {
                    str(DropdownChoice.option_id("md5")): "md5",
                    str(DropdownChoice.option_id("sha")): "sha",
                }.get(api_request.get("snmpv3_auth_proto", ""), "")

                args[8] = snmpv3_auth_proto
                args[9] = api_request.get("snmpv3_security_name", "")
                args[10] = api_request.get("snmpv3_security_password", "")

                if snmpv3_use == "authPriv":
                    snmpv3_privacy_proto = {
                        str(DropdownChoice.option_id("DES")): "DES",
                        str(DropdownChoice.option_id("AES")): "AES",
                    }.get(api_request.get("snmpv3_privacy_proto", ""), "")

                    args[11] = snmpv3_privacy_proto

                    args[12] = api_request.get("snmpv3_privacy_password", "")
            else:
                args[9] = api_request.get("snmpv3_security_name", "")

        result = diag_host(
            host.site_id(),
            hostname,
            _test,
            *args,
        )
        return {
            "next_transid": transactions.fresh_transid(),
            "status_code": result.return_code,
            "output": result.response,
        }
Exemplo n.º 10
0
 def page(self) -> AjaxPageResult:
     check_csrf_token()
     layout_id = request.var("layout_id")
     active_config.bi_layouts["templates"].pop(layout_id)
     BILayoutManagement.save_layouts()
     return {}
Exemplo n.º 11
0
 def page(self) -> AjaxPageResult:
     check_csrf_token()
     for_aggregation = request.var("aggregation_name")
     active_config.bi_layouts["aggregations"].pop(for_aggregation)
     BILayoutManagement.save_layouts()
     return {}
Exemplo n.º 12
0
 def page(self):
     check_csrf_token()
     response.set_content_type("application/json")
     user_config = UserSidebarConfig(user, active_config.sidebar)
     user_config.folded = request.var("fold") == "yes"
     user_config.save()