示例#1
0
    def _serializer(group):
        # type: (Dict[str, str]) -> Any
        ident = group['id']
        extensions = {}
        if "customer" in group:
            customer_id = group["customer"]
            extensions[
                "customer"] = "global" if customer_id is None else customer_id
        elif is_managed_edition():
            extensions["customer"] = managed.default_customer_id()

        return constructors.domain_object(
            domain_type=name,
            identifier=ident,
            title=group['alias'],
            members={
                'title':
                constructors.object_property(
                    name='title',
                    value=group['alias'],
                    prop_format='string',
                    base=constructors.object_href(name, ident),
                ),
            },
            extensions=extensions,
        )
示例#2
0
def complement_customer(details):
    if not is_managed_edition():
        return details

    if "customer" in details:
        customer_id = details["customer"]
        details["customer"] = "global" if managed.is_global(customer_id) else customer_id
    else:  # special case where customer is set to customer_default_id which results in no-entry
        details["customer"] = managed.default_customer_id()
    return details
示例#3
0
    def _serializer(group):
        # type: (Dict[str, str]) -> Any
        ident = group["id"]
        extensions = {}
        if "customer" in group:
            customer_id = group["customer"]
            extensions["customer"] = "global" if customer_id is None else customer_id
        elif is_managed_edition():
            extensions["customer"] = managed.default_customer_id()

        extensions["alias"] = group["alias"]
        return constructors.domain_object(
            domain_type=name,
            identifier=ident,
            title=group["alias"] or ident,
            extensions=extensions,
        )
示例#4
0
def update_customer_info(attributes, customer_id, remove_provider=False):
    """Update the attributes with the correct customer_id

    Args:
        attributes:
            the attributes of the to save/edit instance
        customer_id:
            the internal customer id
        remove_provider:
            Bool which decides if the customer entry should be removed if set to the customer_default_id

    """
    # None is a valid customer_id used for 'Global' configuration
    if remove_provider and customer_id == managed.default_customer_id():
        attributes.pop("customer", None)
        return attributes

    attributes["customer"] = customer_id
    return attributes
    def _convert_aggregation_to_bi(self, aggr):
        node = self._convert_node_to_bi(aggr["node"])
        option_keys: List[Tuple[str, Any]] = [
            ("ID", None),
            ("node_visualization", {}),
            ("hard_states", False),
            ("downtime_aggr_warn", False),
            ("disabled", False),
        ]

        if cmk_version.is_managed_edition():
            option_keys.append(("customer", managed.default_customer_id()))

        # Create dict with all aggregation options
        options = {}
        for option, default_value in option_keys:
            options[option] = aggr.get(option, default_value)

        return (options, self._convert_aggregation_groups(aggr["groups"])) + node
示例#6
0
    def action(self):
        if not html.check_transaction():
            return "users"

        if self._is_new_user:
            self._user_id = UserID(allow_empty=False).from_html_vars("user_id")
            user_attrs = {}
        else:
            self._user_id = html.get_unicode_input("edit").strip()
            user_attrs = self._users[self._user_id]

        # Full name
        user_attrs["alias"] = html.get_unicode_input("alias").strip()

        # Locking
        user_attrs["locked"] = html.get_checkbox("locked")
        increase_serial = False

        if self._user_id in self._users and self._users[
                self._user_id]["locked"] != user_attrs["locked"] and user_attrs["locked"]:
            increase_serial = True  # when user is being locked now, increase the auth serial

        # Authentication: Password or Secret
        auth_method = html.request.var("authmethod")
        if auth_method == "secret":
            secret = html.request.var("_auth_secret", "").strip()
            user_attrs["automation_secret"] = secret
            user_attrs["password"] = hash_password(secret)
            increase_serial = True  # password changed, reflect in auth serial

        else:
            password = html.request.var("_password_" + self._pw_suffix(), '').strip()
            password2 = html.request.var("_password2_" + self._pw_suffix(), '').strip()

            # We compare both passwords only, if the user has supplied
            # the repeation! We are so nice to our power users...
            # Note: this validation is done before the main-validiation later on
            # It doesn't make any sense to put this block into the main validation function
            if password2 and password != password2:
                raise MKUserError("_password2", _("The both passwords do not match."))

            # Detect switch back from automation to password
            if "automation_secret" in user_attrs:
                del user_attrs["automation_secret"]
                if "password" in user_attrs:
                    del user_attrs["password"]  # which was the encrypted automation password!

            if password:
                user_attrs["password"] = hash_password(password)
                user_attrs["last_pw_change"] = int(time.time())
                increase_serial = True  # password changed, reflect in auth serial

            # PW change enforcement
            user_attrs["enforce_pw_change"] = html.get_checkbox("enforce_pw_change")
            if user_attrs["enforce_pw_change"]:
                increase_serial = True  # invalidate all existing user sessions, enforce relogon

        # Increase serial (if needed)
        if increase_serial:
            user_attrs["serial"] = user_attrs.get("serial", 0) + 1

        # Email address
        user_attrs["email"] = EmailAddressUnicode().from_html_vars("email")

        idle_timeout = watolib.get_vs_user_idle_timeout().from_html_vars("idle_timeout")
        user_attrs["idle_timeout"] = idle_timeout
        if idle_timeout is not None:
            user_attrs["idle_timeout"] = idle_timeout
        elif idle_timeout is None and "idle_timeout" in user_attrs:
            del user_attrs["idle_timeout"]

        # Pager
        user_attrs["pager"] = html.request.var("pager", '').strip()

        if cmk.is_managed_edition():
            customer = self._vs_customer.from_html_vars("customer")
            self._vs_customer.validate_value(customer, "customer")

            if customer != managed.default_customer_id():
                user_attrs["customer"] = customer
            elif "customer" in user_attrs:
                del user_attrs["customer"]

        vs_sites = self._vs_sites()
        authorized_sites = vs_sites.from_html_vars("authorized_sites")
        vs_sites.validate_value(authorized_sites, "authorized_sites")

        if authorized_sites is not None:
            user_attrs["authorized_sites"] = authorized_sites
        elif "authorized_sites" in user_attrs:
            del user_attrs["authorized_sites"]

        # Roles
        user_attrs["roles"] = [
            role for role in self._roles.keys() if html.get_checkbox("role_" + role)
        ]

        # Language configuration
        set_lang = html.get_checkbox("_set_lang")
        language = html.request.var("language")
        if set_lang:
            if language == "":
                language = None
            user_attrs["language"] = language
        elif not set_lang and "language" in user_attrs:
            del user_attrs["language"]

        # Contact groups
        cgs = []
        for c in self._contact_groups:
            if html.get_checkbox("cg_" + c):
                cgs.append(c)
        user_attrs["contactgroups"] = cgs

        # Notification settings are only active if we do *not* have
        # rule based notifications!
        if not self._rbn_enabled():
            # Notifications
            user_attrs["notifications_enabled"] = html.get_checkbox("notifications_enabled")

            ntp = html.request.var("notification_period")
            if ntp not in self._timeperiods:
                ntp = "24X7"
            user_attrs["notification_period"] = ntp

            for what, opts in [("host", "durfs"), ("service", "wucrfs")]:
                user_attrs[what + "_notification_options"] = "".join(
                    [opt for opt in opts if html.get_checkbox(what + "_" + opt)])

            value = watolib.get_vs_flexible_notifications().from_html_vars("notification_method")
            user_attrs["notification_method"] = value
        else:
            user_attrs["fallback_contact"] = html.get_checkbox("fallback_contact")

        # Custom user attributes
        for name, attr in userdb.get_user_attributes():
            value = attr.valuespec().from_html_vars('ua_' + name)
            user_attrs[name] = value

        # Generate user "object" to update
        user_object = {self._user_id: {"attributes": user_attrs, "is_new_user": self._is_new_user}}
        # The following call validates and updated the users
        edit_users(user_object)
        return "users"