20#ifndef ORES_IAM_MESSAGING_TENANT_HANDLER_HPP
21#define ORES_IAM_MESSAGING_TENANT_HANDLER_HPP
24#include <boost/uuid/nil_generator.hpp>
25#include <boost/uuid/random_generator.hpp>
26#include <boost/uuid/string_generator.hpp>
27#include "ores.logging/make_logger.hpp"
28#include "ores.nats/domain/message.hpp"
29#include "ores.nats/service/client.hpp"
30#include "ores.database/domain/context.hpp"
31#include "ores.security/jwt/jwt_authenticator.hpp"
32#include "ores.service/messaging/handler_helpers.hpp"
33#include "ores.service/service/request_context.hpp"
34#include "ores.iam.api/messaging/tenant_protocol.hpp"
35#include "ores.iam.core/repository/tenant_repository.hpp"
37namespace ores::iam::messaging {
41inline auto& tenant_handler_lg() {
42 static auto instance = ores::logging::make_logger(
43 "ores.iam.messaging.tenant_handler");
49using ores::service::messaging::reply;
50using ores::service::messaging::decode;
51using ores::service::messaging::stamp;
52using ores::service::messaging::error_reply;
53using ores::service::messaging::has_permission;
54using ores::service::messaging::log_handler_entry;
62 : nats_(nats), ctx_(
std::move(ctx)), signer_(
std::move(signer)) {}
65 [[maybe_unused]]
const auto correlation_id =
66 log_handler_entry(tenant_handler_lg(), msg);
68 repository::tenant_repository repo(ctx_);
69 get_tenants_response resp;
70 resp.tenants = repo.read_latest();
71 BOOST_LOG_SEV(tenant_handler_lg(), debug)
73 reply(nats_, msg, resp);
74 }
catch (
const std::exception& e) {
75 BOOST_LOG_SEV(tenant_handler_lg(), error)
76 << msg.
subject <<
" failed: " << e.what();
77 reply(nats_, msg, get_tenants_response{});
82 [[maybe_unused]]
const auto correlation_id =
83 log_handler_entry(tenant_handler_lg(), msg);
84 auto req = decode<save_tenant_request>(msg);
86 BOOST_LOG_SEV(tenant_handler_lg(), warn)
87 <<
"Failed to decode: " << msg.
subject;
91 auto ctx_expected = ores::service::service::make_request_context(
92 ctx_, msg, std::optional<ores::security::jwt::jwt_authenticator>{signer_});
94 error_reply(nats_, msg, ctx_expected.error());
97 const auto& ctx = *ctx_expected;
98 if (!has_permission(ctx,
"iam::tenants:write")) {
102 if (req->data.id.is_nil())
103 req->data.id = boost::uuids::random_generator()();
104 repository::tenant_repository repo(ctx);
105 stamp(req->data, ctx);
106 repo.write(req->data);
107 BOOST_LOG_SEV(tenant_handler_lg(), debug)
108 <<
"Completed " << msg.
subject;
110 save_tenant_response{.success =
true});
111 }
catch (
const std::exception& e) {
112 BOOST_LOG_SEV(tenant_handler_lg(), error)
113 << msg.
subject <<
" failed: " << e.what();
114 reply(nats_, msg, save_tenant_response{
115 .success =
false, .message = e.what()});
120 [[maybe_unused]]
const auto correlation_id =
121 log_handler_entry(tenant_handler_lg(), msg);
122 auto req = decode<delete_tenant_request>(msg);
124 BOOST_LOG_SEV(tenant_handler_lg(), warn)
125 <<
"Failed to decode: " << msg.
subject;
129 repository::tenant_repository repo(ctx_);
130 boost::uuids::string_generator sg;
131 for (
const auto& id_str : req->ids)
132 repo.remove(sg(id_str));
133 BOOST_LOG_SEV(tenant_handler_lg(), debug)
134 <<
"Completed " << msg.
subject;
136 delete_tenant_response{.success =
true});
137 }
catch (
const std::exception& e) {
138 BOOST_LOG_SEV(tenant_handler_lg(), error)
139 << msg.
subject <<
" failed: " << e.what();
140 reply(nats_, msg, delete_tenant_response{
141 .success =
false, .message = e.what()});
146 [[maybe_unused]]
const auto correlation_id =
147 log_handler_entry(tenant_handler_lg(), msg);
148 auto req = decode<get_tenant_history_request>(msg);
150 BOOST_LOG_SEV(tenant_handler_lg(), warn)
151 <<
"Failed to decode: " << msg.
subject;
155 repository::tenant_repository repo(ctx_);
156 boost::uuids::string_generator sg;
157 auto hist = repo.read_history(sg(req->id));
158 BOOST_LOG_SEV(tenant_handler_lg(), debug)
159 <<
"Completed " << msg.
subject;
160 reply(nats_, msg, get_tenant_history_response{
162 .versions = std::move(hist)});
163 }
catch (
const std::exception& e) {
164 BOOST_LOG_SEV(tenant_handler_lg(), error)
165 << msg.
subject <<
" failed: " << e.what();
166 reply(nats_, msg, get_tenant_history_response{
167 .success =
false, .message = e.what()});
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
@ forbidden
The caller is authenticated but lacks the required permission.
Context for the operations on a postgres database.
Definition context.hpp:47
A received NATS message.
Definition message.hpp:40
std::string subject
The subject the message was published to.
Definition message.hpp:44
NATS client: connection, pub/sub, request/reply, and JetStream.
Definition client.hpp:73
JWT authentication primitive.
Definition jwt_authenticator.hpp:45