예제 #1
0
 def __init__(self):
     super(ModeCustomAttrs, self).__init__()
     # TODO: Inappropriate Intimacy: custom host attributes should not now about
     #       custom user attributes and vice versa. The only reason they now about
     #       each other now is that they are stored in one file.
     self._all_attrs = load_custom_attrs_from_mk_file(
         lock=html.is_transaction())
예제 #2
0
def page_handler() -> None:
    initialize_wato_html_head()

    if not config.wato_enabled:
        raise MKGeneralException(
            _("WATO is disabled. Please set <tt>wato_enabled = True</tt>"
              " in your <tt>multisite.mk</tt> if you want to use WATO."))

    # config.current_customer can not be checked with CRE repos
    if cmk_version.is_managed_edition() and not managed.is_provider(
            config.current_customer):  # type: ignore[attr-defined]
        raise MKGeneralException(
            _("Check_MK can only be configured on "
              "the managers central site."))

    current_mode = html.request.var("mode") or "main"
    mode_permissions, mode_class = _get_mode_permission_and_class(current_mode)

    display_options.load_from_html(html)

    if display_options.disabled(display_options.N):
        html.add_body_css_class("inline")

    # If we do an action, we aquire an exclusive lock on the complete WATO.
    if html.is_transaction():
        with store.lock_checkmk_configuration():
            _wato_page_handler(current_mode, mode_permissions, mode_class)
    else:
        _wato_page_handler(current_mode, mode_permissions, mode_class)
예제 #3
0
파일: users.py 프로젝트: majma24/checkmk
    def _from_vars(self):
        # TODO: Should we turn the both fields below into Optional[UserId]?
        self._user_id = html.request.get_unicode_input(
            "edit")  # missing -> new user
        # This is needed for the breadcrumb computation:
        # When linking from user notification rules page the request variable is "user"
        # instead of "edit". We should also change that variable to "user" on this page,
        # then we can simply use self._user_id.
        if not self._user_id and html.request.has_var("user"):
            self._user_id = html.request.get_str_input_mandatory("user")

        self._cloneid = html.request.get_unicode_input(
            "clone")  # Only needed in 'new' mode
        # TODO: Nuke the field below? It effectively hides facts about _user_id for mypy.
        self._is_new_user = self._user_id is None
        self._users = userdb.load_users(lock=html.is_transaction())
        new_user = userdb.new_user_template('htpasswd')
        if self._user_id is not None:
            self._user = self._users.get(UserId(self._user_id), new_user)
        elif self._cloneid:
            self._user = self._users.get(UserId(self._cloneid), new_user)
        else:
            self._user = new_user
        self._locked_attributes = userdb.locked_attributes(
            self._user.get('connector'))
예제 #4
0
def handle_edit_annotations():
    # Avoid reshowing edit form after edit and reload
    if html.is_transaction() and not html.transaction_valid():
        return False
    if html.request.var("anno_host") and not html.request.var("_delete_annotation"):
        finished = edit_annotation()
    else:
        finished = False

    return finished
예제 #5
0
 def _from_vars(self):
     # TODO: Should we turn the both fields below into Optional[UserId]?
     self._user_id = html.request.get_unicode_input("edit")  # missing -> new user
     self._cloneid = html.request.get_unicode_input("clone")  # Only needed in 'new' mode
     # TODO: Nuke the field below? It effectively hides facts about _user_id for mypy.
     self._is_new_user = self._user_id is None
     self._users = userdb.load_users(lock=html.is_transaction())
     new_user = userdb.new_user_template('htpasswd')
     if self._user_id is not None:
         self._user = self._users.get(UserId(self._user_id), new_user)
     elif self._cloneid:
         self._user = self._users.get(UserId(self._cloneid), new_user)
     else:
         self._user = new_user
     self._locked_attributes = userdb.locked_attributes(self._user.get('connector'))
예제 #6
0
    def _from_vars(self):
        self._user_id = html.get_unicode_input("edit")  # missing -> new user
        self._cloneid = html.get_unicode_input("clone")  # Only needed in 'new' mode
        self._is_new_user = self._user_id is None

        self._users = userdb.load_users(lock=html.is_transaction())

        if self._is_new_user:
            if self._cloneid:
                self._user = self._users.get(self._cloneid, userdb.new_user_template('htpasswd'))
            else:
                self._user = userdb.new_user_template('htpasswd')
        else:
            self._user = self._users.get(self._user_id, userdb.new_user_template('htpasswd'))

        self._locked_attributes = userdb.locked_attributes(self._user.get('connector'))
예제 #7
0
파일: ldap.py 프로젝트: tklecker/checkmk
    def _from_vars(self):
        self._connection_id = html.request.get_ascii_input("id")
        self._connection_cfg = {}
        self._connections = load_connection_config(lock=html.is_transaction())

        if self._connection_id is None:
            clone_id = html.request.var("clone")
            if clone_id is not None:
                self._connection_cfg = self._get_connection_cfg_and_index(clone_id)[0]

            self._new = True
            return

        self._new = False
        self._connection_cfg, self._connection_nr = self._get_connection_cfg_and_index(
            self._connection_id)
예제 #8
0
    def _from_vars(self):
        self._name = html.request.get_ascii_input("edit")  # missing -> new custom attr
        self._new = self._name is None

        # TODO: Inappropriate Intimacy: custom host attributes should not now about
        #       custom user attributes and vice versa. The only reason they now about
        #       each other now is that they are stored in one file.
        self._all_attrs = load_custom_attrs_from_mk_file(lock=html.is_transaction())

        if not self._new:
            matching_attrs = [a for a in self._attrs if a['name'] == self._name]
            if not matching_attrs:
                raise MKUserError(None, _('The attribute does not exist.'))
            self._attr: Dict[str, Any] = matching_attrs[0]
        else:
            self._attr = {}
예제 #9
0
def _wato_page_handler(current_mode: str,
                       mode_permissions: Optional[List[PermissionName]],
                       mode_class: Type[WatoMode]) -> None:
    try:
        init_wato_datastructures(with_wato_lock=not html.is_transaction())
    except Exception:
        # Snapshot must work in any case
        if current_mode == 'snapshot':
            pass
        else:
            raise

    # Check general permission for this mode
    if mode_permissions is not None and not config.user.may("wato.seeall"):
        _ensure_mode_permissions(mode_permissions)

    mode = mode_class()

    # Do actions (might switch mode)
    if html.is_transaction():
        try:
            config.user.need_permission("wato.edit")

            # Even if the user has seen this mode because auf "seeall",
            # he needs an explicit access permission for doing changes:
            if config.user.may("wato.seeall"):
                if mode_permissions:
                    _ensure_mode_permissions(mode_permissions)

            if cmk.gui.watolib.read_only.is_enabled(
            ) and not cmk.gui.watolib.read_only.may_override():
                raise MKUserError(None, cmk.gui.watolib.read_only.message())

            result = mode.action()
            if isinstance(result, (tuple, str, bool)):
                raise MKGeneralException(
                    f"WatoMode \"{current_mode}\" returns unsupported return value: {result!r}"
                )

            # We assume something has been modified and increase the config generation ID by one.
            update_config_generation()

            if config.wato_use_git:
                do_git_commit()

            # Handle two cases:
            # a) Don't render the page content after action
            #    (a confirm dialog is displayed by the action, or a non-HTML content was sent)
            # b) Redirect to another page
            if isinstance(result, FinalizeRequest):
                raise result

        except MKUserError as e:
            html.add_user_error(e.varname, str(e))

        except MKAuthException as e:
            reason = e.args[0]
            html.add_user_error(None, reason)

    breadcrumb = make_main_menu_breadcrumb(
        mode.main_menu()) + mode.breadcrumb()
    page_menu = mode.page_menu(breadcrumb)
    wato_html_head(title=mode.title(),
                   breadcrumb=breadcrumb,
                   page_menu=page_menu,
                   show_body_start=display_options.enabled(display_options.H),
                   show_top_heading=display_options.enabled(display_options.T))

    if not html.is_transaction() or (cmk.gui.watolib.read_only.is_enabled() and
                                     cmk.gui.watolib.read_only.may_override()):
        _show_read_only_warning()

    # Show outcome of failed action on this page
    if html.has_user_errors():
        html.show_user_errors()

    # Show outcome of previous page (that redirected to this one)
    for message in get_flashed_messages():
        html.show_message(message)

    # Show content
    mode.handle_page()

    if is_sidebar_reload_needed():
        html.reload_whole_page()

    wato_html_footer(show_body_end=display_options.enabled(display_options.H))
예제 #10
0
def _wato_page_handler(current_mode: str,
                       mode_permissions: List[PermissionName],
                       mode_class: Type[WatoMode]) -> None:
    try:
        init_wato_datastructures(with_wato_lock=not html.is_transaction())
    except Exception:
        # Snapshot must work in any case
        if current_mode == 'snapshot':
            pass
        else:
            raise

    # Check general permission for this mode
    if mode_permissions is not None and not config.user.may("wato.seeall"):
        _ensure_mode_permissions(mode_permissions)

    mode = mode_class()

    # Do actions (might switch mode)
    action_message: Optional[str] = None
    if html.is_transaction():
        try:
            config.user.need_permission("wato.edit")

            # Even if the user has seen this mode because auf "seeall",
            # he needs an explicit access permission for doing changes:
            if config.user.may("wato.seeall"):
                if mode_permissions:
                    _ensure_mode_permissions(mode_permissions)

            if cmk.gui.watolib.read_only.is_enabled(
            ) and not cmk.gui.watolib.read_only.may_override():
                raise MKUserError(None, cmk.gui.watolib.read_only.message())

            result = mode.action()
            if isinstance(result, tuple):
                newmode, action_message = result
            else:
                newmode = result

            # We assume something has been modified and increase the config generation ID by one.
            update_config_generation()

            # If newmode is False, then we shall immediately abort.
            # This is e.g. the case, if the page outputted non-HTML
            # data, such as a tarball (in the export function). We must
            # be sure not to output *any* further data in that case.
            if newmode is False:
                return

            # if newmode is not None, then the mode has been changed
            if newmode is not None:
                assert not isinstance(newmode, bool)
                if newmode == "":  # no further information: configuration dialog, etc.
                    if action_message:
                        html.show_message(action_message)
                        wato_html_footer()
                    return
                mode_permissions, mode_class = _get_mode_permission_and_class(
                    newmode)
                current_mode = newmode
                mode = mode_class()
                html.request.set_var("mode",
                                     newmode)  # will be used by makeuri

                # Check general permissions for the new mode
                if mode_permissions is not None and not config.user.may(
                        "wato.seeall"):
                    for pname in mode_permissions:
                        if '.' not in pname:
                            pname = "wato." + pname
                        config.user.need_permission(pname)

        except MKUserError as e:
            action_message = "%s" % e
            html.add_user_error(e.varname, action_message)

        except MKAuthException as e:
            reason = e.args[0]
            action_message = reason
            html.add_user_error(None, reason)

    breadcrumb = make_main_menu_breadcrumb(
        mode.main_menu()) + mode.breadcrumb()
    page_menu = mode.page_menu(breadcrumb)
    wato_html_head(title=mode.title(),
                   breadcrumb=breadcrumb,
                   page_menu=page_menu,
                   show_body_start=display_options.enabled(display_options.H),
                   show_top_heading=display_options.enabled(display_options.T))

    if not html.is_transaction() or (cmk.gui.watolib.read_only.is_enabled() and
                                     cmk.gui.watolib.read_only.may_override()):
        _show_read_only_warning()

    # Show outcome of action
    if html.has_user_errors():
        html.show_user_errors()
    elif action_message:
        html.show_message(action_message)

    # Show content
    mode.handle_page()

    if is_sidebar_reload_needed():
        html.reload_sidebar()

    if config.wato_use_git and html.is_transaction():
        do_git_commit()

    wato_html_footer(show_footer=display_options.enabled(display_options.Z),
                     show_body_end=display_options.enabled(display_options.H))