20#ifndef ORES_IAM_MESSAGING_TENANT_TYPE_HANDLER_HPP
21#define ORES_IAM_MESSAGING_TENANT_TYPE_HANDLER_HPP
24#include "ores.logging/make_logger.hpp"
25#include "ores.nats/domain/message.hpp"
26#include "ores.nats/service/client.hpp"
27#include "ores.database/domain/context.hpp"
28#include "ores.security/jwt/jwt_authenticator.hpp"
29#include "ores.service/messaging/handler_helpers.hpp"
30#include "ores.service/service/request_context.hpp"
31#include "ores.iam.api/messaging/tenant_type_protocol.hpp"
32#include "ores.iam.core/service/tenant_type_service.hpp"
34namespace ores::iam::messaging {
38inline auto& tenant_type_handler_lg() {
39 static auto instance = ores::logging::make_logger(
40 "ores.iam.messaging.tenant_type_handler");
46using ores::service::messaging::reply;
47using ores::service::messaging::decode;
48using ores::service::messaging::stamp;
49using ores::service::messaging::error_reply;
50using ores::service::messaging::has_permission;
51using ores::service::messaging::log_handler_entry;
54class tenant_type_handler {
59 : nats_(nats), ctx_(
std::move(ctx)), signer_(
std::move(signer)) {}
62 [[maybe_unused]]
const auto correlation_id =
63 log_handler_entry(tenant_type_handler_lg(), msg);
65 service::tenant_type_service svc(ctx_);
66 BOOST_LOG_SEV(tenant_type_handler_lg(), debug)
68 reply(nats_, msg, get_tenant_types_response{
69 .types = svc.list_types()});
70 }
catch (
const std::exception& e) {
71 BOOST_LOG_SEV(tenant_type_handler_lg(), error)
72 << msg.
subject <<
" failed: " << e.what();
73 reply(nats_, msg, get_tenant_types_response{});
78 [[maybe_unused]]
const auto correlation_id =
79 log_handler_entry(tenant_type_handler_lg(), msg);
80 auto req = decode<save_tenant_type_request>(msg);
82 BOOST_LOG_SEV(tenant_type_handler_lg(), warn)
83 <<
"Failed to decode: " << msg.
subject;
87 auto ctx_expected = ores::service::service::make_request_context(
88 ctx_, msg, std::optional<ores::security::jwt::jwt_authenticator>{signer_});
90 error_reply(nats_, msg, ctx_expected.error());
93 const auto& ctx = *ctx_expected;
94 if (!has_permission(ctx,
"iam::tenants:create")) {
98 service::tenant_type_service svc(ctx);
99 stamp(req->data, ctx);
100 svc.save_type(req->data);
101 BOOST_LOG_SEV(tenant_type_handler_lg(), debug)
102 <<
"Completed " << msg.
subject;
104 save_tenant_type_response{.success =
true});
105 }
catch (
const std::exception& e) {
106 BOOST_LOG_SEV(tenant_type_handler_lg(), error)
107 << msg.
subject <<
" failed: " << e.what();
108 reply(nats_, msg, save_tenant_type_response{
109 .success =
false, .message = e.what()});
114 [[maybe_unused]]
const auto correlation_id =
115 log_handler_entry(tenant_type_handler_lg(), msg);
116 auto req = decode<delete_tenant_type_request>(msg);
118 BOOST_LOG_SEV(tenant_type_handler_lg(), warn)
119 <<
"Failed to decode: " << msg.
subject;
122 auto ctx_expected = ores::service::service::make_request_context(
123 ctx_, msg, std::optional<ores::security::jwt::jwt_authenticator>{signer_});
125 error_reply(nats_, msg, ctx_expected.error());
128 const auto& ctx = *ctx_expected;
129 if (!has_permission(ctx,
"iam::tenants:delete")) {
134 service::tenant_type_service svc(ctx);
135 svc.remove_type(req->type);
136 BOOST_LOG_SEV(tenant_type_handler_lg(), debug)
137 <<
"Completed " << msg.
subject;
139 delete_tenant_type_response{.success =
true});
140 }
catch (
const std::exception& e) {
141 BOOST_LOG_SEV(tenant_type_handler_lg(), error)
142 << msg.
subject <<
" failed: " << e.what();
143 reply(nats_, msg, delete_tenant_type_response{
144 .success =
false, .message = e.what()});
149 [[maybe_unused]]
const auto correlation_id =
150 log_handler_entry(tenant_type_handler_lg(), msg);
151 auto req = decode<get_tenant_type_history_request>(msg);
153 BOOST_LOG_SEV(tenant_type_handler_lg(), warn)
154 <<
"Failed to decode: " << msg.
subject;
158 service::tenant_type_service svc(ctx_);
159 auto hist = svc.get_type_history(req->type);
160 BOOST_LOG_SEV(tenant_type_handler_lg(), debug)
161 <<
"Completed " << msg.
subject;
162 reply(nats_, msg, get_tenant_type_history_response{
164 .history = std::move(hist)});
165 }
catch (
const std::exception& e) {
166 BOOST_LOG_SEV(tenant_type_handler_lg(), error)
167 << msg.
subject <<
" failed: " << e.what();
168 reply(nats_, msg, get_tenant_type_history_response{
169 .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