]> granicus.if.org Git - icinga2/blob - lib/icinga/usergroup.cpp
Split DynamicObject::OnConfigLoaded into two separate events
[icinga2] / lib / icinga / usergroup.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 "icinga/usergroup.hpp"
21 #include "config/objectrule.hpp"
22 #include "config/configitem.hpp"
23 #include "base/dynamictype.hpp"
24 #include "base/objectlock.hpp"
25 #include "base/logger.hpp"
26 #include "base/context.hpp"
27 #include "base/workqueue.hpp"
28 #include <boost/foreach.hpp>
29
30 using namespace icinga;
31
32 REGISTER_TYPE(UserGroup);
33
34 INITIALIZE_ONCE(&UserGroup::RegisterObjectRuleHandler);
35
36 void UserGroup::RegisterObjectRuleHandler(void)
37 {
38         ObjectRule::RegisterType("UserGroup");
39 }
40
41 bool UserGroup::EvaluateObjectRule(const User::Ptr& user, const ConfigItem::Ptr& group)
42 {
43         String group_name = group->GetName();
44
45         CONTEXT("Evaluating rule for group '" + group_name + "'");
46
47         Dictionary::Ptr locals = new Dictionary();
48         locals->Set("__parent", group->GetScope());
49         locals->Set("user", user);
50
51         if (!group->GetFilter()->Evaluate(locals))
52                 return false;
53
54         Log(LogDebug, "UserGroup")
55             << "Assigning membership for group '" << group_name << "' to user '" << user->GetName() << "'";
56
57         Array::Ptr groups = user->GetGroups();
58         groups->Add(group_name);
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         BOOST_FOREACH(const ConfigItem::Ptr& group, ConfigItem::GetItems("UserGroup"))
68         {
69                 if (!group->GetFilter())
70                         continue;
71
72                 EvaluateObjectRule(user, group);
73         }
74 }
75
76 std::set<User::Ptr> UserGroup::GetMembers(void) 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                 BOOST_FOREACH(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