]> granicus.if.org Git - icinga2/blob - lib/icinga/usergroup.cpp
Move the VMFrame class to libbase
[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         ScriptFrame frame;
48         if (group->GetScope())
49                 group->GetScope()->CopyTo(frame.Locals);
50         frame.Locals->Set("user", user);
51
52         if (!group->GetFilter()->Evaluate(frame))
53                 return false;
54
55         Log(LogDebug, "UserGroup")
56             << "Assigning membership for group '" << group_name << "' to user '" << user->GetName() << "'";
57
58         Array::Ptr groups = user->GetGroups();
59         groups->Add(group_name);
60
61         return true;
62 }
63
64 void UserGroup::EvaluateObjectRules(const User::Ptr& user)
65 {
66         CONTEXT("Evaluating group membership for user '" + user->GetName() + "'");
67
68         BOOST_FOREACH(const ConfigItem::Ptr& group, ConfigItem::GetItems("UserGroup"))
69         {
70                 if (!group->GetFilter())
71                         continue;
72
73                 EvaluateObjectRule(user, group);
74         }
75 }
76
77 std::set<User::Ptr> UserGroup::GetMembers(void) const
78 {
79         boost::mutex::scoped_lock lock(m_UserGroupMutex);
80         return m_Members;
81 }
82
83 void UserGroup::AddMember(const User::Ptr& user)
84 {
85         user->AddGroup(GetName());
86
87         boost::mutex::scoped_lock lock(m_UserGroupMutex);
88         m_Members.insert(user);
89 }
90
91 void UserGroup::RemoveMember(const User::Ptr& user)
92 {
93         boost::mutex::scoped_lock lock(m_UserGroupMutex);
94         m_Members.erase(user);
95 }
96
97 bool UserGroup::ResolveGroupMembership(const User::Ptr& user, bool add, int rstack) {
98
99         if (add && rstack > 20) {
100                 Log(LogWarning, "UserGroup")
101                     << "Too many nested groups for group '" << GetName() << "': User '"
102                     << user->GetName() << "' membership assignment failed.";
103
104                 return false;
105         }
106
107         Array::Ptr groups = GetGroups();
108
109         if (groups && groups->GetLength() > 0) {
110                 ObjectLock olock(groups);
111
112                 BOOST_FOREACH(const String& name, groups) {
113                         UserGroup::Ptr group = UserGroup::GetByName(name);
114
115                         if (group && !group->ResolveGroupMembership(user, add, rstack + 1))
116                                 return false;
117                 }
118         }
119
120         if (add)
121                 AddMember(user);
122         else
123                 RemoveMember(user);
124
125         return true;
126 }
127