]> granicus.if.org Git - icinga2/blob - lib/icinga/usergroup.cpp
Merge pull request #6294 from Icinga/feature/unique-groups-api
[icinga2] / lib / icinga / usergroup.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 "icinga/usergroup.hpp"
21 #include "icinga/usergroup-ti.cpp"
22 #include "config/objectrule.hpp"
23 #include "config/configitem.hpp"
24 #include "base/configtype.hpp"
25 #include "base/objectlock.hpp"
26 #include "base/logger.hpp"
27 #include "base/context.hpp"
28 #include "base/workqueue.hpp"
29
30 using namespace icinga;
31
32 REGISTER_TYPE(UserGroup);
33
34 INITIALIZE_ONCE([]() {
35         ObjectRule::RegisterType("UserGroup");
36 });
37
38 bool UserGroup::EvaluateObjectRule(const User::Ptr& user, const ConfigItem::Ptr& group)
39 {
40         String groupName = group->GetName();
41
42         CONTEXT("Evaluating rule for group '" + groupName + "'");
43
44         ScriptFrame frame(true);
45         if (group->GetScope())
46                 group->GetScope()->CopyTo(frame.Locals);
47         frame.Locals->Set("user", user);
48
49         if (!group->GetFilter()->Evaluate(frame).GetValue().ToBool())
50                 return false;
51
52         Log(LogDebug, "UserGroup")
53                 << "Assigning membership for group '" << groupName << "' to user '" << user->GetName() << "'";
54
55         Array::Ptr groups = user->GetGroups();
56
57         if (groups && !groups->Contains(groupName))
58                 groups->Add(groupName);
59
60         return true;
61 }
62
63 void UserGroup::EvaluateObjectRules(const User::Ptr& user)
64 {
65         CONTEXT("Evaluating group membership for user '" + user->GetName() + "'");
66
67         for (const ConfigItem::Ptr& group : ConfigItem::GetItems(UserGroup::TypeInstance))
68         {
69                 if (!group->GetFilter())
70                         continue;
71
72                 EvaluateObjectRule(user, group);
73         }
74 }
75
76 std::set<User::Ptr> UserGroup::GetMembers() const
77 {
78         boost::mutex::scoped_lock lock(m_UserGroupMutex);
79         return m_Members;
80 }
81
82 void UserGroup::AddMember(const User::Ptr& user)
83 {
84         user->AddGroup(GetName());
85
86         boost::mutex::scoped_lock lock(m_UserGroupMutex);
87         m_Members.insert(user);
88 }
89
90 void UserGroup::RemoveMember(const User::Ptr& user)
91 {
92         boost::mutex::scoped_lock lock(m_UserGroupMutex);
93         m_Members.erase(user);
94 }
95
96 bool UserGroup::ResolveGroupMembership(const User::Ptr& user, bool add, int rstack) {
97
98         if (add && rstack > 20) {
99                 Log(LogWarning, "UserGroup")
100                         << "Too many nested groups for group '" << GetName() << "': User '"
101                         << user->GetName() << "' membership assignment failed.";
102
103                 return false;
104         }
105
106         Array::Ptr groups = GetGroups();
107
108         if (groups && groups->GetLength() > 0) {
109                 ObjectLock olock(groups);
110
111                 for (const String& name : groups) {
112                         UserGroup::Ptr group = UserGroup::GetByName(name);
113
114                         if (group && !group->ResolveGroupMembership(user, add, rstack + 1))
115                                 return false;
116                 }
117         }
118
119         if (add)
120                 AddMember(user);
121         else
122                 RemoveMember(user);
123
124         return true;
125 }
126