ORE Studio 0.0.4
Loading...
Searching...
No Matches
context.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 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_DATABASE_CONTEXT_HPP
21#define ORES_DATABASE_CONTEXT_HPP
22
23#include <vector>
24#include <optional>
25#include <sqlgen/postgres.hpp>
26#include <boost/uuid/uuid.hpp>
27#include "ores.database/domain/tenant_aware_pool.hpp"
28#include "ores.utility/uuid/tenant_id.hpp"
29
30namespace ores::database {
31
47class context {
48public:
49 using connection_type = sqlgen::postgres::Connection;
51
55 explicit context(sqlgen::ConnectionPool<connection_type> connection_pool,
56 sqlgen::postgres::Credentials credentials,
58 std::string actor = "",
59 std::string service_account = "")
60 : connection_pool_(std::move(connection_pool), credentials,
61 std::move(tenant_id), std::move(actor), service_account),
62 credentials_(std::move(credentials)),
63 service_account_(std::move(service_account)) {}
64
68 explicit context(sqlgen::ConnectionPool<connection_type> connection_pool,
69 sqlgen::postgres::Credentials credentials,
71 boost::uuids::uuid party_id,
72 std::vector<boost::uuids::uuid> visible_party_ids,
73 std::string actor = "",
74 std::string service_account = "")
75 : connection_pool_(std::move(connection_pool), credentials,
76 std::move(tenant_id), party_id,
77 std::move(visible_party_ids), std::move(actor),
79 credentials_(std::move(credentials)),
80 service_account_(std::move(service_account)) {}
81
85 connection_pool_type& connection_pool() { return connection_pool_; }
86
90 const sqlgen::postgres::Credentials& credentials() const { return credentials_; }
91
96 return connection_pool_.tenant_id();
97 }
98
102 std::optional<boost::uuids::uuid> party_id() const {
103 return connection_pool_.party_id();
104 }
105
109 const std::vector<boost::uuids::uuid>& visible_party_ids() const {
110 return connection_pool_.visible_party_ids();
111 }
112
119 const std::string& actor() const { return connection_pool_.actor(); }
120
128 const std::string& service_account() const { return service_account_; }
129
133 const sqlgen::ConnectionPool<connection_type>& underlying_pool() const {
134 return connection_pool_.underlying_pool();
135 }
136
143 const std::vector<std::string>& roles() const { return roles_; }
144
151 [[nodiscard]] context with_roles(std::vector<std::string> roles) const {
152 auto copy = *this;
153 copy.roles_ = std::move(roles);
154 return copy;
155 }
156
163 std::string actor) const {
164 return context(connection_pool_.underlying_pool(), credentials_,
165 std::move(tenant_id), std::move(actor), service_account_);
166 }
167
173 [[nodiscard]] context with_party(
175 boost::uuids::uuid party_id,
176 std::vector<boost::uuids::uuid> visible_party_ids,
177 std::string actor) const {
178 return context(connection_pool_.underlying_pool(), credentials_,
179 std::move(tenant_id), party_id,
180 std::move(visible_party_ids), std::move(actor),
181 service_account_);
182 }
183
184private:
185 connection_pool_type connection_pool_;
186 sqlgen::postgres::Credentials credentials_;
187 std::string service_account_;
188 std::vector<std::string> roles_;
189};
190
191}
192
193#endif
STL namespace.
Context for the operations on a postgres database.
Definition context.hpp:47
const std::string & service_account() const
Gets the service account for this context.
Definition context.hpp:128
connection_pool_type & connection_pool()
Gets the tenant-aware connection pool.
Definition context.hpp:85
std::optional< boost::uuids::uuid > party_id() const
Gets the party ID for this context, if set.
Definition context.hpp:102
const sqlgen::postgres::Credentials & credentials() const
Gets the credentials for this context.
Definition context.hpp:90
context(sqlgen::ConnectionPool< connection_type > connection_pool, sqlgen::postgres::Credentials credentials, utility::uuid::tenant_id tenant_id, std::string actor="", std::string service_account="")
Constructs a tenant-only context.
Definition context.hpp:55
const std::vector< boost::uuids::uuid > & visible_party_ids() const
Gets the visible party IDs for this context.
Definition context.hpp:109
const std::vector< std::string > & roles() const
Gets the permission codes carried in this context.
Definition context.hpp:143
context with_party(utility::uuid::tenant_id tenant_id, boost::uuids::uuid party_id, std::vector< boost::uuids::uuid > visible_party_ids, std::string actor) const
Creates a new context with tenant and party isolation.
Definition context.hpp:173
const std::string & actor() const
Gets the current actor (end-user) for this context.
Definition context.hpp:119
const utility::uuid::tenant_id & tenant_id() const
Gets the tenant ID for this context.
Definition context.hpp:95
const sqlgen::ConnectionPool< connection_type > & underlying_pool() const
Gets the underlying raw connection pool.
Definition context.hpp:133
context with_roles(std::vector< std::string > roles) const
Returns a copy of this context with the given permission codes.
Definition context.hpp:151
context with_tenant(utility::uuid::tenant_id tenant_id, std::string actor) const
Creates a new context with a different tenant ID (no party).
Definition context.hpp:162
context(sqlgen::ConnectionPool< connection_type > connection_pool, sqlgen::postgres::Credentials credentials, utility::uuid::tenant_id tenant_id, boost::uuids::uuid party_id, std::vector< boost::uuids::uuid > visible_party_ids, std::string actor="", std::string service_account="")
Constructs a tenant-and-party-aware context.
Definition context.hpp:68
const sqlgen::ConnectionPool< Connection > & underlying_pool() const
Gets the underlying connection pool.
Definition tenant_aware_pool.hpp:283
std::optional< boost::uuids::uuid > party_id() const
Gets the current party ID, if set.
Definition tenant_aware_pool.hpp:261
const std::vector< boost::uuids::uuid > & visible_party_ids() const
Gets the visible party IDs.
Definition tenant_aware_pool.hpp:266
const std::string & actor() const
Gets the current actor (username), if set.
Definition tenant_aware_pool.hpp:273
const utility::uuid::tenant_id & tenant_id() const
Gets the current tenant ID.
Definition tenant_aware_pool.hpp:256
A strongly-typed wrapper around a UUID representing a tenant identifier.
Definition tenant_id.hpp:66