2013-07-31 09:09:39 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2013-07-31 09:09:39 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "db_ido/userdbobject.hpp"
|
2015-11-11 11:55:20 +01:00
|
|
|
#include "db_ido/usergroupdbobject.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#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"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2013-07-31 09:09:39 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-08-02 17:12:07 +02:00
|
|
|
REGISTER_DBTYPE(User, "contact", DbObjectTypeContact, "contact_object_id", UserDbObject);
|
2013-07-31 09:09:39 +02:00
|
|
|
|
2013-08-01 13:20:30 +02:00
|
|
|
UserDbObject::UserDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
|
|
|
|
: DbObject(type, name1, name2)
|
2013-07-31 09:09:39 +02:00
|
|
|
{ }
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Dictionary::Ptr UserDbObject::GetConfigFields() const
|
2013-07-31 09:09:39 +02:00
|
|
|
{
|
|
|
|
User::Ptr user = static_pointer_cast<User>(GetObject());
|
|
|
|
|
2016-03-15 14:41:24 +01:00
|
|
|
int typeFilter = user->GetTypeFilter();
|
|
|
|
int stateFilter = user->GetStateFilter();
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
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 },
|
2018-01-15 14:35:30 +01:00
|
|
|
{ "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 }
|
2018-01-11 11:17:38 +01:00
|
|
|
});
|
2013-07-31 09:09:39 +02:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Dictionary::Ptr UserDbObject::GetStatusFields() const
|
2013-07-31 09:09:39 +02:00
|
|
|
{
|
|
|
|
User::Ptr user = static_pointer_cast<User>(GetObject());
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
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()) }
|
|
|
|
});
|
2013-07-31 09:09:39 +02:00
|
|
|
}
|
2013-08-08 08:30:19 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void UserDbObject::OnConfigUpdateHeavy()
|
2013-10-01 17:16:36 +02:00
|
|
|
{
|
|
|
|
User::Ptr user = static_pointer_cast<User>(GetObject());
|
|
|
|
|
2015-11-11 11:55:20 +01:00
|
|
|
/* groups */
|
|
|
|
Array::Ptr groups = user->GetGroups();
|
|
|
|
|
2016-08-14 22:24:51 +02:00
|
|
|
std::vector<DbQuery> queries;
|
|
|
|
|
|
|
|
DbQuery query1;
|
|
|
|
query1.Table = DbType::GetByName("UserGroup")->GetTable() + "_members";
|
|
|
|
query1.Type = DbQueryDelete;
|
|
|
|
query1.Category = DbCatConfig;
|
2018-01-11 11:17:38 +01:00
|
|
|
query1.WhereCriteria = new Dictionary({
|
|
|
|
{ "contact_object_id", user }
|
|
|
|
});
|
2017-11-30 08:19:58 +01:00
|
|
|
queries.emplace_back(std::move(query1));
|
2016-08-14 22:24:51 +02:00
|
|
|
|
2015-11-11 11:55:20 +01:00
|
|
|
if (groups) {
|
|
|
|
ObjectLock olock(groups);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& groupName : groups) {
|
2015-11-11 11:55:20 +01:00
|
|
|
UserGroup::Ptr group = UserGroup::GetByName(groupName);
|
|
|
|
|
2016-08-14 22:24:51 +02:00
|
|
|
DbQuery query2;
|
|
|
|
query2.Table = DbType::GetByName("UserGroup")->GetTable() + "_members";
|
|
|
|
query2.Type = DbQueryInsert | DbQueryUpdate;
|
|
|
|
query2.Category = DbCatConfig;
|
2018-01-11 11:17:38 +01:00
|
|
|
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 }
|
|
|
|
});
|
2017-11-30 08:19:58 +01:00
|
|
|
queries.emplace_back(std::move(query2));
|
2015-11-11 11:55:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 22:24:51 +02:00
|
|
|
DbObject::OnMultipleQueries(queries);
|
|
|
|
|
|
|
|
queries.clear();
|
|
|
|
|
|
|
|
DbQuery query2;
|
|
|
|
query2.Table = "contact_addresses";
|
|
|
|
query2.Type = DbQueryDelete;
|
|
|
|
query2.Category = DbCatConfig;
|
2018-01-11 11:17:38 +01:00
|
|
|
query2.WhereCriteria = new Dictionary({
|
|
|
|
{ "contact_id", DbValue::FromObjectInsertID(user) }
|
|
|
|
});
|
2017-11-30 08:19:58 +01:00
|
|
|
queries.emplace_back(std::move(query2));
|
2013-10-01 17:16:36 +02:00
|
|
|
|
2014-04-03 10:30:11 +02:00
|
|
|
Dictionary::Ptr vars = user->GetVars();
|
2013-10-01 17:16:36 +02:00
|
|
|
|
2014-04-03 10:30:11 +02:00
|
|
|
if (vars) { /* This is sparta. */
|
2013-10-01 17:16:36 +02:00
|
|
|
for (int i = 1; i <= 6; i++) {
|
|
|
|
String key = "address" + Convert::ToString(i);
|
|
|
|
|
2014-11-04 12:18:07 +01:00
|
|
|
if (!vars->Contains(key))
|
2013-10-01 17:16:36 +02:00
|
|
|
continue;
|
|
|
|
|
2014-11-04 12:18:07 +01:00
|
|
|
String val = vars->Get(key);
|
|
|
|
|
2013-10-01 17:16:36 +02:00
|
|
|
DbQuery query;
|
|
|
|
query.Type = DbQueryInsert;
|
|
|
|
query.Table = "contact_addresses";
|
2015-08-17 11:03:41 +02:00
|
|
|
query.Category = DbCatConfig;
|
2018-01-11 11:17:38 +01:00
|
|
|
query.Fields = new Dictionary({
|
|
|
|
{ "contact_id", DbValue::FromObjectInsertID(user) },
|
|
|
|
{ "address_number", i },
|
|
|
|
{ "address", val },
|
|
|
|
{ "instance_id", 0 } /* DbConnection class fills in real ID */
|
|
|
|
|
|
|
|
});
|
2017-11-30 08:19:58 +01:00
|
|
|
queries.emplace_back(std::move(query));
|
2013-10-01 17:16:36 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-14 22:24:51 +02:00
|
|
|
|
|
|
|
DbObject::OnMultipleQueries(queries);
|
2013-10-01 17:16:36 +02:00
|
|
|
}
|
2016-08-24 11:35:12 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
hashData += DbObject::HashValue(groups);
|
|
|
|
|
2016-08-25 07:37:41 +02:00
|
|
|
return SHA256(hashData);
|
2016-08-24 11:35:12 +02:00
|
|
|
}
|