void UserGroupDbObject::OnConfigUpdate(void) { UserGroup::Ptr group = static_pointer_cast<UserGroup>(GetObject()); DbQuery query1; query1.Table = DbType::GetByName("UserGroup")->GetTable() + "_members"; query1.Type = DbQueryDelete; query1.Category = DbCatConfig; query1.WhereCriteria = new Dictionary(); query1.WhereCriteria->Set("instance_id", 0); query1.WhereCriteria->Set("contactgroup_id", DbValue::FromObjectInsertID(group)); OnQuery(query1); BOOST_FOREACH(const User::Ptr& user, group->GetMembers()) { DbQuery query2; query2.Table = DbType::GetByName("UserGroup")->GetTable() + "_members"; query2.Type = DbQueryInsert; query2.Category = DbCatConfig; query2.Fields = new Dictionary(); query2.Fields->Set("instance_id", 0); /* DbConnection class fills in real ID */ query2.Fields->Set("contactgroup_id", DbValue::FromObjectInsertID(group)); query2.Fields->Set("contact_object_id", user); OnQuery(query2); } }
bool UserGroup::ResolveGroupMembership(const User::Ptr& user, bool add, int rstack) { if (add && rstack > 20) { Log(LogWarning, "UserGroup") << "Too many nested groups for group '" << GetName() << "': User '" << user->GetName() << "' membership assignment failed."; return false; } Array::Ptr groups = GetGroups(); if (groups && groups->GetLength() > 0) { ObjectLock olock(groups); for (const String& name : groups) { UserGroup::Ptr group = UserGroup::GetByName(name); if (group && !group->ResolveGroupMembership(user, add, rstack + 1)) return false; } } if (add) AddMember(user); else RemoveMember(user); return true; }
bool UserGroup::EvaluateObjectRuleOne(const User::Ptr user, const ObjectRule& rule) { DebugInfo di = rule.GetDebugInfo(); std::ostringstream msgbuf; msgbuf << "Evaluating 'object' rule (" << di << ")"; CONTEXT(msgbuf.str()); Dictionary::Ptr locals = make_shared<Dictionary>(); locals->Set("user", user); if (!rule.EvaluateFilter(locals)) return false; std::ostringstream msgbuf2; msgbuf2 << "Assigning membership for group '" << rule.GetName() << "' to user '" << user->GetName() << "' for rule " << di; Log(LogDebug, "UserGroup", msgbuf2.str()); String group_name = rule.GetName(); UserGroup::Ptr group = UserGroup::GetByName(group_name); if (!group) { Log(LogCritical, "UserGroup", "Invalid membership assignment. Group '" + group_name + "' does not exist."); return false; } /* assign user group membership */ group->ResolveGroupMembership(user, true); /* update groups attribute for apply */ user->AddGroup(group_name); return true; }
Dictionary::Ptr UserGroupDbObject::GetConfigFields(void) const { Dictionary::Ptr fields = new Dictionary(); UserGroup::Ptr group = static_pointer_cast<UserGroup>(GetObject()); fields->Set("alias", group->GetDisplayName()); return fields; }
void User::Stop(bool runtimeRemoved) { ObjectImpl<User>::Stop(runtimeRemoved); Array::Ptr groups = GetGroups(); if (groups) { ObjectLock olock(groups); for (const String& name : groups) { UserGroup::Ptr ug = UserGroup::GetByName(name); if (ug) ug->ResolveGroupMembership(this, false); } } }
void User::OnAllConfigLoaded(void) { ObjectImpl<User>::OnAllConfigLoaded(); UserGroup::EvaluateObjectRules(this); Array::Ptr groups = GetGroups(); if (groups) { groups = groups->ShallowClone(); ObjectLock olock(groups); for (const String& name : groups) { UserGroup::Ptr ug = UserGroup::GetByName(name); if (ug) ug->ResolveGroupMembership(this, true); } } }
BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetCheckableNotificationUserGroups(service)) { Log(LogDebug, "db_ido", "service contactgroups: " + usergroup->GetName()); Dictionary::Ptr fields_contact = make_shared<Dictionary>(); fields_contact->Set("service_id", DbValue::FromObjectInsertID(service)); fields_contact->Set("contactgroup_object_id", usergroup); fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */ DbQuery query_contact; query_contact.Table = GetType()->GetTable() + "_contactgroups"; query_contact.Type = DbQueryInsert; query_contact.Category = DbCatConfig; query_contact.Fields = fields_contact; OnQuery(query_contact); }
BOOST_FOREACH(const UserGroup::Ptr& usergroup, CompatUtility::GetCheckableNotificationUserGroups(host)) { Log(LogDebug, "HostDbObject") << "host contactgroups: " << usergroup->GetName(); Dictionary::Ptr fields_contact = new Dictionary(); fields_contact->Set("host_id", DbValue::FromObjectInsertID(host)); fields_contact->Set("contactgroup_object_id", usergroup); fields_contact->Set("instance_id", 0); /* DbConnection class fills in real ID */ DbQuery query_contact; query_contact.Table = GetType()->GetTable() + "_contactgroups"; query_contact.Type = DbQueryInsert; query_contact.Category = DbCatConfig; query_contact.Fields = fields_contact; OnQuery(query_contact); }