String UserDbObject::CalculateConfigHash(const Dictionary::Ptr& configFields) const { String hashData = DbObject::CalculateConfigHash(configFields); User::Ptr user = static_pointer_cast<User>(GetObject()); Array::Ptr groups = user->GetGroups(); if (groups) hashData += DbObject::HashValue(groups); return SHA256(hashData); }
bool UserGroup::EvaluateObjectRule(const User::Ptr& user, const ConfigItem::Ptr& group) { String group_name = group->GetName(); CONTEXT("Evaluating rule for group '" + group_name + "'"); ScriptFrame frame; if (group->GetScope()) group->GetScope()->CopyTo(frame.Locals); frame.Locals->Set("user", user); if (!group->GetFilter()->Evaluate(frame).GetValue().ToBool()) return false; Log(LogDebug, "UserGroup") << "Assigning membership for group '" << group_name << "' to user '" << user->GetName() << "'"; Array::Ptr groups = user->GetGroups(); groups->Add(group_name); return true; }
void UserDbObject::OnConfigUpdateHeavy(void) { User::Ptr user = static_pointer_cast<User>(GetObject()); /* groups */ Array::Ptr groups = user->GetGroups(); std::vector<DbQuery> queries; DbQuery query1; query1.Table = DbType::GetByName("UserGroup")->GetTable() + "_members"; query1.Type = DbQueryDelete; query1.Category = DbCatConfig; query1.WhereCriteria = new Dictionary(); query1.WhereCriteria->Set("contact_object_id", user); queries.push_back(query1); if (groups) { ObjectLock olock(groups); for (const String& groupName : groups) { UserGroup::Ptr group = UserGroup::GetByName(groupName); DbQuery query2; query2.Table = DbType::GetByName("UserGroup")->GetTable() + "_members"; query2.Type = DbQueryInsert | DbQueryUpdate; 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); query2.WhereCriteria = new Dictionary(); query2.WhereCriteria->Set("instance_id", 0); /* DbConnection class fills in real ID */ query2.WhereCriteria->Set("contactgroup_id", DbValue::FromObjectInsertID(group)); query2.WhereCriteria->Set("contact_object_id", user); queries.push_back(query2); } } DbObject::OnMultipleQueries(queries); queries.clear(); DbQuery query2; query2.Table = "contact_addresses"; query2.Type = DbQueryDelete; query2.Category = DbCatConfig; query2.WhereCriteria = new Dictionary(); query2.WhereCriteria->Set("contact_id", DbValue::FromObjectInsertID(user)); queries.push_back(query2); Dictionary::Ptr vars = user->GetVars(); if (vars) { /* This is sparta. */ for (int i = 1; i <= 6; i++) { Dictionary::Ptr fields = new Dictionary(); String key = "address" + Convert::ToString(i); if (!vars->Contains(key)) continue; String val = vars->Get(key); fields->Set("contact_id", DbValue::FromObjectInsertID(user)); fields->Set("address_number", i); fields->Set("address", val); fields->Set("instance_id", 0); /* DbConnection class fills in real ID */ DbQuery query; query.Type = DbQueryInsert; query.Table = "contact_addresses"; query.Category = DbCatConfig; query.Fields = fields; queries.push_back(query); } } DbObject::OnMultipleQueries(queries); }