Skip to content

Commit 3407f98

Browse files
committed
wrap sr_nacm_get_user
This adds a counterpart to setting NACM user. Sometimes, it might be useful to get it as well. Change-Id: I534a229eb536d63091904ab4f6268cb9da6dd3db
1 parent b960058 commit 3407f98

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

include/sysrepo-cpp/Session.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class Session {
100100
void replaceConfig(std::optional<libyang::DataNode> config, const std::optional<std::string>& moduleName = std::nullopt, std::chrono::milliseconds timeout = std::chrono::milliseconds{0});
101101

102102
void setNacmUser(const std::string& user);
103+
std::optional<std::string> getNacmUser() const;
103104
[[nodiscard]] Subscription initNacm(
104105
SubscribeOptions opts = SubscribeOptions::Default,
105106
ExceptionHandler handler = nullptr,

src/Session.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,15 @@ void Session::setNacmUser(const std::string& user)
706706
throwIfError(res, "Couldn't set NACM user", m_sess.get());
707707
}
708708

709+
/**
710+
* @brief Get the NACM user for this session.
711+
*/
712+
std::optional<std::string> Session::getNacmUser() const
713+
{
714+
auto* username = sr_nacm_get_user(m_sess.get());
715+
return username ? std::make_optional<std::string>(username) : std::nullopt;
716+
}
717+
709718
/**
710719
* @brief Initializes NACM callbacks.
711720
*

tests/session.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,14 @@ TEST_CASE("session")
390390
auto data = sess.getData("/test_module:denyAllLeaf");
391391
REQUIRE(data.value().findPath("/test_module:denyAllLeaf").value().asTerm().valueStr() == "AHOJ");
392392

393+
REQUIRE(!sess.getNacmUser());
394+
393395
// check that repeated NACM initialization still works
394396
for (int i = 0; i < 3; ++i) {
395397
auto nacmSub = sess.initNacm();
396398
sess.setNacmUser("nobody");
399+
REQUIRE(sess.getNacmUser() == "nobody");
400+
397401
data = sess.getData("/test_module:denyAllLeaf");
398402
// After turning on NACM, we can't access the leaf.
399403
REQUIRE(!data);
@@ -408,6 +412,9 @@ TEST_CASE("session")
408412
sysrepo::ErrorWithCode);
409413
}
410414

415+
REQUIRE(!!sess.getNacmUser());
416+
REQUIRE(sess.getNacmUser() == "nobody");
417+
411418
// duplicate NACM initialization should throw
412419
auto nacm = sess.initNacm();
413420
REQUIRE_THROWS_WITH_AS(auto x = sess.initNacm(),

0 commit comments

Comments
 (0)