ORE Studio 0.0.4
Loading...
Searching...
No Matches
tenant_type_handler.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_IAM_MESSAGING_TENANT_TYPE_HANDLER_HPP
21#define ORES_IAM_MESSAGING_TENANT_TYPE_HANDLER_HPP
22
23#include <stdexcept>
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"
33
34namespace ores::iam::messaging {
35
36namespace {
37
38inline auto& tenant_type_handler_lg() {
39 static auto instance = ores::logging::make_logger(
40 "ores.iam.messaging.tenant_type_handler");
41 return instance;
42}
43
44} // namespace
45
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;
52using namespace ores::logging;
53
54class tenant_type_handler {
55public:
56 tenant_type_handler(ores::nats::service::client& nats,
59 : nats_(nats), ctx_(std::move(ctx)), signer_(std::move(signer)) {}
60
61 void list(ores::nats::message msg) {
62 [[maybe_unused]] const auto correlation_id =
63 log_handler_entry(tenant_type_handler_lg(), msg);
64 try {
65 service::tenant_type_service svc(ctx_);
66 BOOST_LOG_SEV(tenant_type_handler_lg(), debug)
67 << "Completed " << msg.subject;
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{});
74 }
75 }
76
77 void save(ores::nats::message msg) {
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);
81 if (!req) {
82 BOOST_LOG_SEV(tenant_type_handler_lg(), warn)
83 << "Failed to decode: " << msg.subject;
84 return;
85 }
86 try {
87 auto ctx_expected = ores::service::service::make_request_context(
88 ctx_, msg, std::optional<ores::security::jwt::jwt_authenticator>{signer_});
89 if (!ctx_expected) {
90 error_reply(nats_, msg, ctx_expected.error());
91 return;
92 }
93 const auto& ctx = *ctx_expected;
94 if (!has_permission(ctx, "iam::tenants:create")) {
95 error_reply(nats_, msg, ores::service::error_code::forbidden);
96 return;
97 }
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;
103 reply(nats_, msg,
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()});
110 }
111 }
112
113 void remove(ores::nats::message msg) {
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);
117 if (!req) {
118 BOOST_LOG_SEV(tenant_type_handler_lg(), warn)
119 << "Failed to decode: " << msg.subject;
120 return;
121 }
122 auto ctx_expected = ores::service::service::make_request_context(
123 ctx_, msg, std::optional<ores::security::jwt::jwt_authenticator>{signer_});
124 if (!ctx_expected) {
125 error_reply(nats_, msg, ctx_expected.error());
126 return;
127 }
128 const auto& ctx = *ctx_expected;
129 if (!has_permission(ctx, "iam::tenants:delete")) {
130 error_reply(nats_, msg, ores::service::error_code::forbidden);
131 return;
132 }
133 try {
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;
138 reply(nats_, msg,
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()});
145 }
146 }
147
148 void history(ores::nats::message msg) {
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);
152 if (!req) {
153 BOOST_LOG_SEV(tenant_type_handler_lg(), warn)
154 << "Failed to decode: " << msg.subject;
155 return;
156 }
157 try {
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{
163 .success = true,
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()});
170 }
171 }
172
173private:
177};
178
179} // namespace ores::iam::messaging
180#endif
STL namespace.
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