Require at least one user for notification objects (user or as member of user_groups)

fixes #8067
This commit is contained in:
Michael Friedrich 2015-02-07 23:04:01 +01:00
parent 9302a6d03c
commit db9c55835f
3 changed files with 21 additions and 0 deletions

View File

@ -107,6 +107,7 @@
%type Notification { %type Notification {
%validator "ValidateNotificationFilters" %validator "ValidateNotificationFilters"
%validator "ValidateNotificationUsers"
%require "host_name", %require "host_name",
%attribute %name(Host) "host_name", %attribute %name(Host) "host_name",

View File

@ -34,6 +34,7 @@ using namespace icinga;
REGISTER_TYPE(Notification); REGISTER_TYPE(Notification);
REGISTER_SCRIPTFUNCTION(ValidateNotificationFilters, &Notification::ValidateFilters); REGISTER_SCRIPTFUNCTION(ValidateNotificationFilters, &Notification::ValidateFilters);
REGISTER_SCRIPTFUNCTION(ValidateNotificationUsers, &Notification::ValidateUsers);
INITIALIZE_ONCE(&Notification::StaticInitialize); INITIALIZE_ONCE(&Notification::StaticInitialize);
boost::signals2::signal<void (const Notification::Ptr&, double, const MessageOrigin&)> Notification::OnNextNotificationChanged; boost::signals2::signal<void (const Notification::Ptr&, double, const MessageOrigin&)> Notification::OnNextNotificationChanged;
@ -500,6 +501,24 @@ int icinga::FilterArrayToInt(const Array::Ptr& typeFilters, int defaultValue)
return resultTypeFilter; return resultTypeFilter;
} }
void Notification::ValidateUsers(const String& location, const Notification::Ptr& object)
{
std::set<User::Ptr> allUsers;
std::set<User::Ptr> users = object->GetUsers();
std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
BOOST_FOREACH(const UserGroup::Ptr& ug, object->GetUserGroups()) {
std::set<User::Ptr> 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) void Notification::ValidateFilters(const String& location, const Notification::Ptr& object)
{ {
int sfilter = FilterArrayToInt(object->GetStates(), 0); int sfilter = FilterArrayToInt(object->GetStates(), 0);

View File

@ -109,6 +109,7 @@ public:
static void RegisterApplyRuleHandler(void); 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 ValidateFilters(const String& location, const Notification::Ptr& object);
static void EvaluateApplyRules(const intrusive_ptr<Host>& host); static void EvaluateApplyRules(const intrusive_ptr<Host>& host);