]> granicus.if.org Git - icinga2/blob - lib/db_ido/usergroupdbobject.cpp
Fix Zone::IsGlobal()
[icinga2] / lib / db_ido / usergroupdbobject.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "db_ido/usergroupdbobject.hpp"
21 #include "db_ido/dbtype.hpp"
22 #include "db_ido/dbvalue.hpp"
23 #include "base/objectlock.hpp"
24 #include "base/initialize.hpp"
25 #include "base/dynamictype.hpp"
26 #include <boost/foreach.hpp>
27
28 using namespace icinga;
29
30 REGISTER_DBTYPE(UserGroup, "contactgroup", DbObjectTypeContactGroup, "contactgroup_object_id", UserGroupDbObject);
31
32 UserGroupDbObject::UserGroupDbObject(const DbType::Ptr& type, const String& name1, const String& name2)
33         : DbObject(type, name1, name2)
34 { }
35
36 Dictionary::Ptr UserGroupDbObject::GetConfigFields(void) const
37 {
38         Dictionary::Ptr fields = make_shared<Dictionary>();
39         UserGroup::Ptr group = static_pointer_cast<UserGroup>(GetObject());
40
41         fields->Set("alias", group->GetDisplayName());
42
43         return fields;
44 }
45
46 Dictionary::Ptr UserGroupDbObject::GetStatusFields(void) const
47 {
48         return Empty;
49 }
50
51 void UserGroupDbObject::OnConfigUpdate(void)
52 {
53         UserGroup::Ptr group = static_pointer_cast<UserGroup>(GetObject());
54
55         DbQuery query1;
56         query1.Table = DbType::GetByName("UserGroup")->GetTable() + "_members";
57         query1.Type = DbQueryDelete;
58         query1.Category = DbCatConfig;
59         query1.WhereCriteria = make_shared<Dictionary>();
60         query1.WhereCriteria->Set("instance_id", 0);
61         query1.WhereCriteria->Set("contactgroup_id", DbValue::FromObjectInsertID(group));
62         OnQuery(query1);
63
64         BOOST_FOREACH(const User::Ptr& user, group->GetMembers()) {
65                 DbQuery query2;
66                 query2.Table = DbType::GetByName("UserGroup")->GetTable() + "_members";
67                 query2.Type = DbQueryInsert;
68                 query2.Category = DbCatConfig;
69                 query2.Fields = make_shared<Dictionary>();
70                 query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */
71                 query2.Fields->Set("contactgroup_id", DbValue::FromObjectInsertID(group));
72                 query2.Fields->Set("contact_object_id", user);
73                 OnQuery(query2);
74         }
75 }