ORE Studio 0.0.4
Loading...
Searching...
No Matches
tenant_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_HANDLER_HPP
21#define ORES_IAM_MESSAGING_TENANT_HANDLER_HPP
22
23#include <stdexcept>
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"
36
37namespace ores::iam::messaging {
38
39namespace {
40
41inline auto& tenant_handler_lg() {
42 static auto instance = ores::logging::make_logger(
43 "ores.iam.messaging.tenant_handler");
44 return instance;
45}
46
47} // namespace
48
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;
54
55class tenant_handler {
56public:
57 tenant_handler(ores::nats::service::client& nats,
60 : nats_(nats), ctx_(std::move(ctx)), signer_(std::move(signer)) {}
61
62 void list(ores::nats::message msg) {
63 using namespace ores::logging;
64 BOOST_LOG_SEV(tenant_handler_lg(), debug)
65 << "Handling " << msg.subject;
66 try {
67 repository::tenant_repository repo(ctx_);
68 get_tenants_response resp;
69 resp.tenants = repo.read_latest();
70 BOOST_LOG_SEV(tenant_handler_lg(), debug)
71 << "Completed " << msg.subject;
72 reply(nats_, msg, resp);
73 } catch (const std::exception& e) {
74 BOOST_LOG_SEV(tenant_handler_lg(), error)
75 << msg.subject << " failed: " << e.what();
76 reply(nats_, msg, get_tenants_response{});
77 }
78 }
79
80 void save(ores::nats::message msg) {
81 using namespace ores::logging;
82 BOOST_LOG_SEV(tenant_handler_lg(), debug)
83 << "Handling " << msg.subject;
84 auto req = decode<save_tenant_request>(msg);
85 if (!req) {
86 BOOST_LOG_SEV(tenant_handler_lg(), warn)
87 << "Failed to decode: " << msg.subject;
88 return;
89 }
90 try {
91 auto ctx_expected = ores::service::service::make_request_context(
92 ctx_, msg, std::optional<ores::security::jwt::jwt_authenticator>{signer_});
93 if (!ctx_expected) {
94 error_reply(nats_, msg, ctx_expected.error());
95 return;
96 }
97 const auto& ctx = *ctx_expected;
98 if (!has_permission(ctx, "iam::tenants:write")) {
99 error_reply(nats_, msg, ores::service::error_code::forbidden);
100 return;
101 }
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;
109 reply(nats_, msg,
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()});
116 }
117 }
118
119 void del(ores::nats::message msg) {
120 using namespace ores::logging;
121 BOOST_LOG_SEV(tenant_handler_lg(), debug)
122 << "Handling " << msg.subject;
123 auto req = decode<delete_tenant_request>(msg);
124 if (!req) {
125 BOOST_LOG_SEV(tenant_handler_lg(), warn)
126 << "Failed to decode: " << msg.subject;
127 return;
128 }
129 try {
130 repository::tenant_repository repo(ctx_);
131 boost::uuids::string_generator sg;
132 for (const auto& id_str : req->ids)
133 repo.remove(sg(id_str));
134 BOOST_LOG_SEV(tenant_handler_lg(), debug)
135 << "Completed " << msg.subject;
136 reply(nats_, msg,
137 delete_tenant_response{.success = true});
138 } catch (const std::exception& e) {
139 BOOST_LOG_SEV(tenant_handler_lg(), error)
140 << msg.subject << " failed: " << e.what();
141 reply(nats_, msg, delete_tenant_response{
142 .success = false, .message = e.what()});
143 }
144 }
145
146 void history(ores::nats::message msg) {
147 using namespace ores::logging;
148 BOOST_LOG_SEV(tenant_handler_lg(), debug)
149 << "Handling " << msg.subject;
150 auto req = decode<get_tenant_history_request>(msg);
151 if (!req) {
152 BOOST_LOG_SEV(tenant_handler_lg(), warn)
153 << "Failed to decode: " << msg.subject;
154 return;
155 }
156 try {
157 repository::tenant_repository repo(ctx_);
158 boost::uuids::string_generator sg;
159 auto hist = repo.read_history(sg(req->id));
160 BOOST_LOG_SEV(tenant_handler_lg(), debug)
161 << "Completed " << msg.subject;
162 reply(nats_, msg, get_tenant_history_response{
163 .success = true,
164 .versions = std::move(hist)});
165 } catch (const std::exception& e) {
166 BOOST_LOG_SEV(tenant_handler_lg(), error)
167 << msg.subject << " failed: " << e.what();
168 reply(nats_, msg, get_tenant_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