36 #include <boost/algorithm/string.hpp>
43 : config_file_(auth_file)
44 , xmlnne_(config_file_)
54 DEBUG(
"Will load tree");
56 additional_config = additional_config.get_child(
"root");
59 const auto &users_tree = additional_config.get_child_optional(
"users");
63 const auto &groups_tree =
64 additional_config.get_child_optional(
"group_mapping");
68 const auto &credentials_tree =
69 additional_config.get_child_optional(
"credentials");
73 const auto &schedules_tree =
74 additional_config.get_child_optional(
"schedules");
78 const auto &schedule_mapping_tree =
79 additional_config.get_child_optional(
"schedules_mapping");
80 if (schedule_mapping_tree)
85 catch (std::exception &e)
87 ERROR(
"Exception: " << e.what());
88 std::throw_with_nested(
98 auto cred = it->second;
112 auto cred = it->second;
128 auto cred = it->second;
146 std::throw_with_nested(
152 const boost::property_tree::ptree &group_mapping)
156 for (
const auto &group_info : group_mapping)
158 const boost::property_tree::ptree &node = group_info.second;
159 const std::string &group_name = node.get<std::string>(
"group");
161 xmlnne_(
"map", group_info.first);
167 for (
const auto &membership : node)
169 if (membership.first !=
"user")
171 std::string user_name = membership.second.data();
175 ERROR(
"Unknown user " << user_name);
178 grp->member_add(user);
185 std::vector<GroupPtr> ret;
188 for (
const auto &map_entry :
groups_)
190 ret.push_back(map_entry.second);
198 std::vector<GroupPtr> grps;
199 auto lambda_cmp = [&](
UserPtr user) ->
bool {
200 return user->username() == u->username();
203 for (
const auto &grp_map :
groups_)
206 std::tie(std::ignore, grp) = grp_map;
208 if (std::find_if(grp->members().begin(), grp->members().end(), lambda_cmp) !=
209 grp->members().end())
223 for (
auto &profile : profiles)
226 result = merger.
merge(result, profile);
228 if (result->schedule_count())
234 const boost::property_tree::ptree &credentials)
237 for (
const auto &mapping : credentials)
239 const std::string &node_name = mapping.first;
240 const boost::property_tree::ptree &node = mapping.second;
244 std::string user_id = node.get<std::string>(
"user");
248 config_file_,
"Credentials defined for undefined user " + user_id);
254 auto opt_child = node.get_child_optional(
"WiegandCard");
257 std::string card_id = opt_child->get<std::string>(
"card_id");
258 int bits = opt_child->get<
int>(
"bits");
266 else if (opt_child = node.get_child_optional(
"PINCode"))
269 std::string pin = opt_child->get<std::string>(
"pin");
276 else if (opt_child = node.get_child_optional(
"WiegandCardPin"))
278 std::string card_id = opt_child->get<std::string>(
"card_id");
279 std::string pin = opt_child->get<std::string>(
"pin");
280 int bits = opt_child->get<
int>(
"bits");
282 auto c = std::make_shared<Cred::RFIDCard>();
287 auto p = std::make_shared<Cred::PinCode>();
290 credential = std::make_shared<Cred::RFIDCardPin>(c, p);
292 assert_cast<Cred::RFIDCardPinPtr>(credential);
295 credential->id(cred_id++);
297 credential->owner(user);
301 credential->alias(opt_child->get<std::string>(
"id",
""));
307 const boost::property_tree::ptree &schedules)
314 const boost::property_tree::ptree &schedules_mapping)
316 for (
const auto &mapping_entry : schedules_mapping)
318 const std::string &node_name = mapping_entry.first;
319 const boost::property_tree::ptree &node = mapping_entry.second;
324 std::list<std::string> schedule_names;
325 std::list<std::string> user_names;
326 std::list<std::string> group_names;
327 std::list<std::string> credential_names;
328 std::string target_door;
329 target_door = node.get<std::string>(
"door",
"");
330 auto door(std::make_shared<Leosac::Auth::Door>());
331 door->alias(target_door);
335 for (
const auto &mapping_data : node)
337 if (mapping_data.first ==
"door")
339 if (mapping_data.first ==
"schedule")
340 schedule_names.push_back(mapping_data.second.data());
341 if (mapping_data.first ==
"user")
342 user_names.push_back(mapping_data.second.data());
343 if (mapping_data.first ==
"group")
344 group_names.push_back(mapping_data.second.data());
345 if (mapping_data.first ==
"credential")
346 credential_names.push_back(mapping_data.second.data());
350 for (
const auto &schedule_name : schedule_names)
358 if (!door->alias().empty())
361 for (
const auto &user_name : user_names)
367 for (
const auto &group_name : group_names)
372 for (
const auto &cred_id : credential_names)
374 DEBUG(
"CRED = " << cred_id);
376 sm->add_credential(assert_cast<Cred::CredentialPtr>(cred));
389 for (
const auto &user : users)
391 const std::string &node_name = user.first;
392 const boost::property_tree::ptree &node = user.second;
396 std::string username = node.get<std::string>(
"name");
397 std::string firstname = node.get<std::string>(
"firstname",
"");
398 std::string lastname = node.get<std::string>(
"lastname",
"");
399 std::string email = node.get<std::string>(
"email",
"");
401 if (username ==
"UNKNOWN_USER")
403 "'UNKNOWN_USER' is a reserved name. Do not use.");
405 UserPtr uptr(std::make_unique<User>(user_id++));
406 uptr->username(username);
407 uptr->firstname(firstname);
408 uptr->lastname(lastname);
415 if (
users_.count(username))
417 WARN(
"User " << username <<
" was already defined. Will overwrite.");
424 const boost::property_tree::ptree &node)
429 v.
set_end_date(node.get<std::string>(
"validity_end",
""));
447 if (!credential->alias().empty())
451 WARN(
"Credential with ID = " << credential->alias()
452 <<
" already exist and "
453 "will be overwritten.");
463 std::vector<GroupPtr> grps;
464 std::vector<IAccessProfilePtr> profiles;
468 ASSERT_LOG(cred->owner().get_eager(),
"Sanity check failed.");
470 auto cred_owner = cred->owner().get_eager();
471 if (!cred->validity().is_valid())
473 INFO(
"Credentials is invalid. It was disabled or out of its validity "
478 if (cred_owner && !cred_owner->is_valid())
480 INFO(
"The user (" << cred_owner->username()
481 <<
") is disabled or out of its validity period.");
492 for (
auto &grp : grps)
505 ASSERT_LOG(mapping,
"Mapping cannot be null");
506 ASSERT_LOG(profile,
"Profile cannot be null");
508 auto schedule =
LEOSAC_ENFORCE(mapping->schedule().get_eager().lock(),
509 "Cannot retrieve schedule from mapping");
511 if (mapping->doors().size())
514 for (
auto lazy_weak_door : mapping->doors())
517 "Cannot get Door from mapping");
518 profile->addAccessSchedule(
519 std::make_shared<AuthTarget>(door_ptr->alias()),
520 std::static_pointer_cast<const Leosac::Tools::ISchedule>(schedule));
526 profile->addAccessSchedule(
528 std::static_pointer_cast<const Leosac::Tools::ISchedule>(schedule));
536 auto profile(std::make_shared<SimpleAccessProfile>());
539 if (mapping->has_user(u->id()))
541 add_schedule_from_mapping_to_profile(mapping, profile);
551 auto profile(std::make_shared<SimpleAccessProfile>());
554 if (mapping->has_group(g->id()))
556 add_schedule_from_mapping_to_profile(mapping, profile);
567 auto profile(std::make_shared<SimpleAccessProfile>());
570 if (mapping->has_cred(c->id()))
572 add_schedule_from_mapping_to_profile(mapping, profile);