From db9c55835f411dd82acff2f0564207ecc95770c1 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Sat, 7 Feb 2015 23:04:01 +0100 Subject: [PATCH] Require at least one user for notification objects (user or as member of user_groups) fixes #8067 --- lib/icinga/icinga-type.conf | 1 + lib/icinga/notification.cpp | 19 +++++++++++++++++++ lib/icinga/notification.hpp | 1 + 3 files changed, 21 insertions(+) diff --git a/lib/icinga/icinga-type.conf b/lib/icinga/icinga-type.conf index fc91e2c73..6cf117ecf 100644 --- a/lib/icinga/icinga-type.conf +++ b/lib/icinga/icinga-type.conf @@ -107,6 +107,7 @@ %type Notification { %validator "ValidateNotificationFilters" + %validator "ValidateNotificationUsers" %require "host_name", %attribute %name(Host) "host_name", diff --git a/lib/icinga/notification.cpp b/lib/icinga/notification.cpp index ccb346547..4e86dfec8 100644 --- a/lib/icinga/notification.cpp +++ b/lib/icinga/notification.cpp @@ -34,6 +34,7 @@ using namespace icinga; REGISTER_TYPE(Notification); REGISTER_SCRIPTFUNCTION(ValidateNotificationFilters, &Notification::ValidateFilters); +REGISTER_SCRIPTFUNCTION(ValidateNotificationUsers, &Notification::ValidateUsers); INITIALIZE_ONCE(&Notification::StaticInitialize); boost::signals2::signal Notification::OnNextNotificationChanged; @@ -500,6 +501,24 @@ int icinga::FilterArrayToInt(const Array::Ptr& typeFilters, int defaultValue) return resultTypeFilter; } +void Notification::ValidateUsers(const String& location, const Notification::Ptr& object) +{ + std::set allUsers; + + std::set users = object->GetUsers(); + std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin())); + + BOOST_FOREACH(const UserGroup::Ptr& ug, object->GetUserGroups()) { + std::set members = ug->GetMembers(); + std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin())); + } + + if (allUsers.empty()) { + BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " + + location + ": No users/user_groups specified.", object->GetDebugInfo())); + } +} + void Notification::ValidateFilters(const String& location, const Notification::Ptr& object) { int sfilter = FilterArrayToInt(object->GetStates(), 0); diff --git a/lib/icinga/notification.hpp b/lib/icinga/notification.hpp index 5bafdaf85..5c0b20c1d 100644 --- a/lib/icinga/notification.hpp +++ b/lib/icinga/notification.hpp @@ -109,6 +109,7 @@ public: static void RegisterApplyRuleHandler(void); + static void ValidateUsers(const String& location, const Notification::Ptr& object); static void ValidateFilters(const String& location, const Notification::Ptr& object); static void EvaluateApplyRules(const intrusive_ptr& host);