]> granicus.if.org Git - icinga2/blob - lib/livestatus/contactgroupstable.cpp
Merge branch 'support/2.8'
[icinga2] / lib / livestatus / contactgroupstable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
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 "livestatus/contactgroupstable.hpp"
21 #include "icinga/usergroup.hpp"
22 #include "base/configtype.hpp"
23
24 using namespace icinga;
25
26 ContactGroupsTable::ContactGroupsTable()
27 {
28         AddColumns(this);
29 }
30
31 void ContactGroupsTable::AddColumns(Table *table, const String& prefix,
32         const Column::ObjectAccessor& objectAccessor)
33 {
34         table->AddColumn(prefix + "name", Column(&ContactGroupsTable::NameAccessor, objectAccessor));
35         table->AddColumn(prefix + "alias", Column(&ContactGroupsTable::AliasAccessor, objectAccessor));
36         table->AddColumn(prefix + "members", Column(&ContactGroupsTable::MembersAccessor, objectAccessor));
37 }
38
39 String ContactGroupsTable::GetName() const
40 {
41         return "contactgroups";
42 }
43
44 String ContactGroupsTable::GetPrefix() const
45 {
46         return "contactgroup";
47 }
48
49 void ContactGroupsTable::FetchRows(const AddRowFunction& addRowFn)
50 {
51         for (const UserGroup::Ptr& ug : ConfigType::GetObjectsByType<UserGroup>()) {
52                 if (!addRowFn(ug, LivestatusGroupByNone, Empty))
53                         return;
54         }
55 }
56
57 Value ContactGroupsTable::NameAccessor(const Value& row)
58 {
59         UserGroup::Ptr user_group = static_cast<UserGroup::Ptr>(row);
60
61         if (!user_group)
62                 return Empty;
63
64         return user_group->GetName();
65 }
66
67 Value ContactGroupsTable::AliasAccessor(const Value& row)
68 {
69         UserGroup::Ptr user_group = static_cast<UserGroup::Ptr>(row);
70
71         if (!user_group)
72                 return Empty;
73
74         return user_group->GetName();
75 }
76
77 Value ContactGroupsTable::MembersAccessor(const Value& row)
78 {
79         UserGroup::Ptr user_group = static_cast<UserGroup::Ptr>(row);
80
81         if (!user_group)
82                 return Empty;
83
84         ArrayData result;
85
86         for (const User::Ptr& user : user_group->GetMembers()) {
87                 result.push_back(user->GetName());
88         }
89
90         return new Array(std::move(result));
91 }