ORE Studio 0.0.4
Loading...
Searching...
No Matches
tenant_status_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_STATUS_HANDLER_HPP
21#define ORES_IAM_MESSAGING_TENANT_STATUS_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_status_protocol.hpp"
32#include "ores.iam.core/service/tenant_status_service.hpp"
33
34namespace ores::iam::messaging {
35
36namespace {
37
38inline auto& tenant_status_handler_lg() {
39 static auto instance = ores::logging::make_logger(
40 "ores.iam.messaging.tenant_status_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;
51
52class tenant_status_handler {
53public:
54 tenant_status_handler(ores::nats::service::client& nats,
57 : nats_(nats), ctx_(std::move(ctx)), signer_(std::move(signer)) {}
58
59 void list(ores::nats::message msg) {
60 using namespace ores::logging;
61 BOOST_LOG_SEV(tenant_status_handler_lg(), debug)
62 << "Handling " << msg.subject;
63 try {
64 service::tenant_status_service svc(ctx_);
65 BOOST_LOG_SEV(tenant_status_handler_lg(), debug)
66 << "Completed " << msg.subject;
67 reply(nats_, msg, get_tenant_statuses_response{
68 .statuses = svc.list_statuses()});
69 } catch (const std::exception& e) {
70 BOOST_LOG_SEV(tenant_status_handler_lg(), error)
71 << msg.subject << " failed: " << e.what();
72 reply(nats_, msg, get_tenant_statuses_response{});
73 }
74 }
75
76 void save(ores::nats::message msg) {
77 using namespace ores::logging;
78 BOOST_LOG_SEV(tenant_status_handler_lg(), debug)
79 << "Handling " << msg.subject;
80 auto req = decode<save_tenant_status_request>(msg);
81 if (!req) {
82 BOOST_LOG_SEV(tenant_status_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_status_service svc(ctx);
99 stamp(req->data, ctx);
100 svc.save_status(req->data);
101 BOOST_LOG_SEV(tenant_status_handler_lg(), debug)
102 << "Completed " << msg.subject;
103 reply(nats_, msg,
104 save_tenant_status_response{.success = true});
105 } catch (const std::exception& e) {
106 BOOST_LOG_SEV(tenant_status_handler_lg(), error)
107 << msg.subject << " failed: " << e.what();
108 reply(nats_, msg, save_tenant_status_response{
109 .success = false, .message = e.what()});
110 }
111 }
112
113 void del(ores::nats::message msg) {
114 using namespace ores::logging;
115 BOOST_LOG_SEV(tenant_status_handler_lg(), debug)
116 << "Handling " << msg.subject;
117 auto req = decode<delete_tenant_status_request>(msg);
118 if (!req) {
119 BOOST_LOG_SEV(tenant_status_handler_lg(), warn)
120 << "Failed to decode: " << msg.subject;
121 return;
122 }
123 auto ctx_expected = ores::service::service::make_request_context(
124 ctx_, msg, std::optional<ores::security::jwt::jwt_authenticator>{signer_});
125 if (!ctx_expected) {
126 error_reply(nats_, msg, ctx_expected.error());
127 return;
128 }
129 const auto& ctx = *ctx_expected;
130 if (!has_permission(ctx, "iam::tenants:delete")) {
131 error_reply(nats_, msg, ores::service::error_code::forbidden);
132 return;
133 }
134 try {
135 service::tenant_status_service svc(ctx);
136 svc.remove_status(req->status);
137 BOOST_LOG_SEV(tenant_status_handler_lg(), debug)
138 << "Completed " << msg.subject;
139 reply(nats_, msg,
140 delete_tenant_status_response{.success = true});
141 } catch (const std::exception& e) {
142 BOOST_LOG_SEV(tenant_status_handler_lg(), error)
143 << msg.subject << " failed: " << e.what();
144 reply(nats_, msg, delete_tenant_status_response{
145 .success = false, .message = e.what()});
146 }
147 }
148
149 void history(ores::nats::message msg) {
150 using namespace ores::logging;
151 BOOST_LOG_SEV(tenant_status_handler_lg(), debug)
152 << "Handling " << msg.subject;
153 auto req = decode<get_tenant_status_history_request>(msg);
154 if (!req) {
155 BOOST_LOG_SEV(tenant_status_handler_lg(), warn)
156 << "Failed to decode: " << msg.subject;
157 return;
158 }
159 try {
160 service::tenant_status_service svc(ctx_);
161 auto hist = svc.get_status_history(req->status);
162 BOOST_LOG_SEV(tenant_status_handler_lg(), debug)
163 << "Completed " << msg.subject;
164 reply(nats_, msg, get_tenant_status_history_response{
165 .success = true,
166 .history = std::move(hist)});
167 } catch (const std::exception& e) {
168 BOOST_LOG_SEV(tenant_status_handler_lg(), error)
169 << msg.subject << " failed: " << e.what();
170 reply(nats_, msg, get_tenant_status_history_response{
171 .success = false, .message = e.what()});
172 }
173 }
174
175private:
179};
180
181} // namespace ores::iam::messaging
182#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