Files
infix/package/sysrepo-cpp/0015-wrap-sr_nacm_get_user.patch

85 lines
3.0 KiB
Diff

From 3407f98d147ffbf031725102181e1fe3cd874363 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= <tomas.pecka@cesnet.cz>
Date: Tue, 8 Apr 2025 12:52:00 +0200
Subject: [PATCH 15/20] wrap sr_nacm_get_user
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Organization: Wires
This adds a counterpart to setting NACM user. Sometimes, it might be
useful to get it as well.
Change-Id: I534a229eb536d63091904ab4f6268cb9da6dd3db
Signed-off-by: Mattias Walström <lazzer@gmail.com>
---
include/sysrepo-cpp/Session.hpp | 1 +
src/Session.cpp | 9 +++++++++
tests/session.cpp | 7 +++++++
3 files changed, 17 insertions(+)
diff --git a/include/sysrepo-cpp/Session.hpp b/include/sysrepo-cpp/Session.hpp
index b7cad72..1c409b8 100644
--- a/include/sysrepo-cpp/Session.hpp
+++ b/include/sysrepo-cpp/Session.hpp
@@ -100,6 +100,7 @@ public:
void replaceConfig(std::optional<libyang::DataNode> config, const std::optional<std::string>& moduleName = std::nullopt, std::chrono::milliseconds timeout = std::chrono::milliseconds{0});
void setNacmUser(const std::string& user);
+ std::optional<std::string> getNacmUser() const;
[[nodiscard]] Subscription initNacm(
SubscribeOptions opts = SubscribeOptions::Default,
ExceptionHandler handler = nullptr,
diff --git a/src/Session.cpp b/src/Session.cpp
index 40c3df3..2273c79 100644
--- a/src/Session.cpp
+++ b/src/Session.cpp
@@ -706,6 +706,15 @@ void Session::setNacmUser(const std::string& user)
throwIfError(res, "Couldn't set NACM user", m_sess.get());
}
+/**
+ * @brief Get the NACM user for this session.
+ */
+std::optional<std::string> Session::getNacmUser() const
+{
+ auto* username = sr_nacm_get_user(m_sess.get());
+ return username ? std::make_optional<std::string>(username) : std::nullopt;
+}
+
/**
* @brief Initializes NACM callbacks.
*
diff --git a/tests/session.cpp b/tests/session.cpp
index 9a419a1..548b371 100644
--- a/tests/session.cpp
+++ b/tests/session.cpp
@@ -390,10 +390,14 @@ TEST_CASE("session")
auto data = sess.getData("/test_module:denyAllLeaf");
REQUIRE(data.value().findPath("/test_module:denyAllLeaf").value().asTerm().valueStr() == "AHOJ");
+ REQUIRE(!sess.getNacmUser());
+
// check that repeated NACM initialization still works
for (int i = 0; i < 3; ++i) {
auto nacmSub = sess.initNacm();
sess.setNacmUser("nobody");
+ REQUIRE(sess.getNacmUser() == "nobody");
+
data = sess.getData("/test_module:denyAllLeaf");
// After turning on NACM, we can't access the leaf.
REQUIRE(!data);
@@ -408,6 +412,9 @@ TEST_CASE("session")
sysrepo::ErrorWithCode);
}
+ REQUIRE(!!sess.getNacmUser());
+ REQUIRE(sess.getNacmUser() == "nobody");
+
// duplicate NACM initialization should throw
auto nacm = sess.initNacm();
REQUIRE_THROWS_WITH_AS(auto x = sess.initNacm(),
--
2.43.0