1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "db_ido/userdbobject.hpp"
#include "db_ido/usergroupdbobject.hpp"
#include "db_ido/dbtype.hpp"
#include "db_ido/dbvalue.hpp"
#include "icinga/user.hpp"
#include "icinga/notification.hpp"
#include "base/convert.hpp"
#include "base/objectlock.hpp"
#include "base/logger.hpp"
using namespace icinga;
REGISTER_DBTYPE(User, "contact", DbObjectTypeContact, "contact_object_id", UserDbObject);
UserDbObject::UserDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
: DbObject(type, name1, name2)
{ }
Dictionary::Ptr UserDbObject::GetConfigFields() const
{
User::Ptr user = static_pointer_cast<User>(GetObject());
int typeFilter = user->GetTypeFilter();
int stateFilter = user->GetStateFilter();
return new Dictionary({
{ "alias", user->GetDisplayName() },
{ "email_address", user->GetEmail() },
{ "pager_address", user->GetPager() },
{ "host_timeperiod_object_id", user->GetPeriod() },
{ "service_timeperiod_object_id", user->GetPeriod() },
{ "host_notifications_enabled", user->GetEnableNotifications() },
{ "service_notifications_enabled", user->GetEnableNotifications() },
{ "can_submit_commands", 1 },
{ "notify_service_recovery", (typeFilter & NotificationRecovery) ? 1 : 0 },
{ "notify_service_warning", (stateFilter & StateFilterWarning) ? 1 : 0 },
{ "notify_service_unknown", (stateFilter & StateFilterUnknown) ? 1 : 0 },
{ "notify_service_critical", (stateFilter & StateFilterCritical) ? 1 : 0 },
{ "notify_service_flapping", (typeFilter & (NotificationFlappingStart | NotificationFlappingEnd)) ? 1 : 0 },
{ "notify_service_downtime", (typeFilter & (NotificationDowntimeStart | NotificationDowntimeEnd | NotificationDowntimeRemoved)) ? 1 : 0 },
{ "notify_host_recovery", (typeFilter & NotificationRecovery) ? 1 : 0 },
{ "notify_host_down", (stateFilter & StateFilterDown) ? 1 : 0 },
{ "notify_host_flapping", (typeFilter & (NotificationFlappingStart | NotificationFlappingEnd)) ? 1 : 0 },
{ "notify_host_downtime", (typeFilter & (NotificationDowntimeStart | NotificationDowntimeEnd | NotificationDowntimeRemoved)) ? 1 : 0 }
});
}
Dictionary::Ptr UserDbObject::GetStatusFields() const
{
User::Ptr user = static_pointer_cast<User>(GetObject());
return new Dictionary({
{ "host_notifications_enabled", user->GetEnableNotifications() },
{ "service_notifications_enabled", user->GetEnableNotifications() },
{ "last_host_notification", DbValue::FromTimestamp(user->GetLastNotification()) },
{ "last_service_notification", DbValue::FromTimestamp(user->GetLastNotification()) }
});
}
void UserDbObject::OnConfigUpdateHeavy()
{
User::Ptr user = static_pointer_cast<User>(GetObject());
/* groups */
Array::Ptr groups = user->GetGroups();
std::vector<DbQuery> queries;
DbQuery query1;
query1.Table = DbType::GetByName("UserGroup")->GetTable() + "_members";
query1.Type = DbQueryDelete;
query1.Category = DbCatConfig;
query1.WhereCriteria = new Dictionary({
{ "contact_object_id", user }
});
queries.emplace_back(std::move(query1));
if (groups) {
ObjectLock olock(groups);
for (String groupName : groups) {
UserGroup::Ptr group = UserGroup::GetByName(groupName);
DbQuery query2;
query2.Table = DbType::GetByName("UserGroup")->GetTable() + "_members";
query2.Type = DbQueryInsert | DbQueryUpdate;
query2.Category = DbCatConfig;
query2.Fields = new Dictionary({
{ "instance_id", 0 }, /* DbConnection class fills in real ID */
{ "contactgroup_id", DbValue::FromObjectInsertID(group) },
{ "contact_object_id", user }
});
query2.WhereCriteria = new Dictionary({
{ "instance_id", 0 }, /* DbConnection class fills in real ID */
{ "contactgroup_id", DbValue::FromObjectInsertID(group) },
{ "contact_object_id", user }
});
queries.emplace_back(std::move(query2));
}
}
DbObject::OnMultipleQueries(queries);
queries.clear();
DbQuery query2;
query2.Table = "contact_addresses";
query2.Type = DbQueryDelete;
query2.Category = DbCatConfig;
query2.WhereCriteria = new Dictionary({
{ "contact_id", DbValue::FromObjectInsertID(user) }
});
queries.emplace_back(std::move(query2));
Dictionary::Ptr vars = user->GetVars();
if (vars) { /* This is sparta. */
for (int i = 1; i <= 6; i++) {
String key = "address" + Convert::ToString(i);
if (!vars->Contains(key))
continue;
String val = vars->Get(key);
DbQuery query;
query.Type = DbQueryInsert;
query.Table = "contact_addresses";
query.Category = DbCatConfig;
query.Fields = new Dictionary({
{ "contact_id", DbValue::FromObjectInsertID(user) },
{ "address_number", i },
{ "address", val },
{ "instance_id", 0 } /* DbConnection class fills in real ID */
});
queries.emplace_back(std::move(query));
}
}
DbObject::OnMultipleQueries(queries);
}
String UserDbObject::CalculateConfigHash(const Dictionary::Ptr& configFields) const
{
String hashData = DbObject::CalculateConfigHash(configFields);
User::Ptr user = static_pointer_cast<User>(GetObject());
Array::Ptr groups = user->GetGroups();
if (groups) {
groups = groups->ShallowClone();
ObjectLock oLock (groups);
std::sort(groups->Begin(), groups->End());
hashData += DbObject::HashValue(groups);
}
return SHA256(hashData);
}
|