1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "db_ido/userdbobject.hpp"
21 #include "db_ido/dbtype.hpp"
22 #include "db_ido/dbvalue.hpp"
23 #include "icinga/user.hpp"
24 #include "icinga/notification.hpp"
25 #include "base/convert.hpp"
26 #include "base/objectlock.hpp"
27 #include "base/logger.hpp"
28 #include <boost/foreach.hpp>
30 using namespace icinga;
32 REGISTER_DBTYPE(User, "contact", DbObjectTypeContact, "contact_object_id", UserDbObject);
34 UserDbObject::UserDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
35 : DbObject(type, name1, name2)
38 Dictionary::Ptr UserDbObject::GetConfigFields(void) const
40 Dictionary::Ptr fields = make_shared<Dictionary>();
41 User::Ptr user = static_pointer_cast<User>(GetObject());
43 fields->Set("alias", user->GetDisplayName());
44 fields->Set("email_address", user->GetEmail());
45 fields->Set("pager_address", user->GetPager());
46 fields->Set("host_timeperiod_object_id", user->GetPeriod());
47 fields->Set("service_timeperiod_object_id", user->GetPeriod());
48 fields->Set("host_notifications_enabled", user->GetEnableNotifications());
49 fields->Set("service_notifications_enabled", user->GetEnableNotifications());
50 fields->Set("can_submit_commands", 1);
51 fields->Set("notify_service_recovery", (user->GetStateFilter() & NotificationRecovery) != 0);
52 fields->Set("notify_service_warning", (user->GetStateFilter() & NotificationProblem) != 0);
53 fields->Set("notify_service_unknown", (user->GetStateFilter() & NotificationProblem) != 0);
54 fields->Set("notify_service_critical", (user->GetStateFilter() & NotificationProblem) != 0);
55 fields->Set("notify_service_flapping", (user->GetStateFilter() & (NotificationFlappingStart | NotificationFlappingEnd)) != 0);
56 fields->Set("notify_service_downtime", (user->GetStateFilter() & (NotificationDowntimeStart | NotificationDowntimeEnd | NotificationDowntimeRemoved)) != 0);
57 fields->Set("notify_host_recovery", (user->GetStateFilter() & NotificationRecovery) != 0);
58 fields->Set("notify_host_down", (user->GetStateFilter() & NotificationProblem) != 0);
59 fields->Set("notify_host_unreachable", (user->GetStateFilter() & NotificationProblem) != 0);
60 fields->Set("notify_host_flapping", (user->GetStateFilter() & (NotificationFlappingStart | NotificationFlappingEnd)) != 0);
61 fields->Set("notify_host_downtime", (user->GetStateFilter() & (NotificationDowntimeStart | NotificationDowntimeEnd | NotificationDowntimeRemoved)) != 0);
66 Dictionary::Ptr UserDbObject::GetStatusFields(void) const
68 Dictionary::Ptr fields = make_shared<Dictionary>();
69 User::Ptr user = static_pointer_cast<User>(GetObject());
71 fields->Set("host_notifications_enabled", user->GetEnableNotifications());
72 fields->Set("service_notifications_enabled", user->GetEnableNotifications());
73 fields->Set("last_host_notification", DbValue::FromTimestamp(user->GetLastNotification()));
74 fields->Set("last_service_notification", DbValue::FromTimestamp(user->GetLastNotification()));
75 fields->Set("modified_attributes", user->GetModifiedAttributes());
76 fields->Set("modified_host_attributes", Empty);
77 fields->Set("modified_service_attributes", Empty);
82 void UserDbObject::OnConfigUpdate(void)
84 Dictionary::Ptr fields = make_shared<Dictionary>();
85 User::Ptr user = static_pointer_cast<User>(GetObject());
87 /* contact addresses */
88 Log(LogDebug, "UserDbObject")
89 << "contact addresses for '" << user->GetName() << "'";
91 Dictionary::Ptr vars = user->GetVars();
93 if (vars) { /* This is sparta. */
94 for (int i = 1; i <= 6; i++) {
95 String key = "address" + Convert::ToString(i);
97 if (!vars->Contains(key))
100 String val = vars->Get(key);
102 fields->Set("contact_id", DbValue::FromObjectInsertID(user));
103 fields->Set("address_number", i);
104 fields->Set("address", val);
105 fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
108 query.Type = DbQueryInsert;
109 query.Table = "contact_addresses";
110 query.Fields = fields;
116 bool UserDbObject::IsStatusAttribute(const String& attribute) const
118 return (attribute == "last_notification");