Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
108 lines
3.2 KiB
C++
108 lines
3.2 KiB
C++
// RightsRequest: one data-subject request lifecycle. States run received,
|
|
// verified, approved, in_progress, fulfilled, rejected, expired. Due date is
|
|
// created + 30 days; one extension moves it to + 60 days. Open-request counts
|
|
// per tenant are capped at kMaxOpenRequestsPerTenant by the caller-owned
|
|
// RequestBook below.
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "../caps.h"
|
|
#include "subject.h"
|
|
|
|
namespace molo::audit::rights {
|
|
|
|
enum class RequestKind : std::uint8_t { kAccess = 0, kExport = 1, kErasure = 2 };
|
|
|
|
enum class RequestState : std::uint8_t {
|
|
kReceived = 0,
|
|
kVerified = 1,
|
|
kApproved = 2,
|
|
kInProgress = 3,
|
|
kFulfilled = 4,
|
|
kRejected = 5,
|
|
kExpired = 6,
|
|
};
|
|
|
|
inline constexpr std::int64_t kDueDays = 30;
|
|
inline constexpr std::int64_t kExtendedDueDays = 60;
|
|
inline constexpr std::int64_t kDaySeconds = 86400;
|
|
|
|
struct RightsRequest {
|
|
std::string id;
|
|
Subject subject;
|
|
RequestKind kind = RequestKind::kAccess;
|
|
RequestState state = RequestState::kReceived;
|
|
std::int64_t created_ts = 0;
|
|
std::int64_t due_ts = 0; // created + 30d, or + 60d once extended
|
|
bool extended = false;
|
|
|
|
bool Open() const {
|
|
return state == RequestState::kReceived || state == RequestState::kVerified ||
|
|
state == RequestState::kApproved || state == RequestState::kInProgress;
|
|
}
|
|
};
|
|
|
|
class RequestBook {
|
|
public:
|
|
RightsRequest& Open(std::string id, Subject subject, RequestKind kind, std::int64_t now) {
|
|
if (OpenCount(subject.tenant) >= kMaxOpenRequestsPerTenant)
|
|
throw std::runtime_error("tenant exceeds kMaxOpenRequestsPerTenant");
|
|
RightsRequest r;
|
|
r.id = id;
|
|
r.subject = std::move(subject);
|
|
r.kind = kind;
|
|
r.created_ts = now;
|
|
r.due_ts = now + kDueDays * kDaySeconds;
|
|
reqs_[r.id] = r;
|
|
return reqs_[r.id];
|
|
}
|
|
|
|
void Transition(const std::string& id, RequestState to) {
|
|
auto it = reqs_.find(id);
|
|
if (it == reqs_.end()) throw std::runtime_error("unknown rights request: " + id);
|
|
it->second.state = to;
|
|
}
|
|
|
|
void ExtendOnce(const std::string& id) {
|
|
auto it = reqs_.find(id);
|
|
if (it == reqs_.end()) throw std::runtime_error("unknown rights request: " + id);
|
|
if (it->second.extended) throw std::runtime_error("request already extended: " + id);
|
|
it->second.extended = true;
|
|
it->second.due_ts = it->second.created_ts + kExtendedDueDays * kDaySeconds;
|
|
}
|
|
|
|
// Marks overdue open requests expired. Returns the expired ids.
|
|
std::vector<std::string> ExpireOverdue(std::int64_t now) {
|
|
std::vector<std::string> out;
|
|
for (auto& [id, r] : reqs_)
|
|
if (r.Open() && now > r.due_ts) {
|
|
r.state = RequestState::kExpired;
|
|
out.push_back(id);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::size_t OpenCount(const std::string& tenant) const {
|
|
std::size_t n = 0;
|
|
for (auto& [_, r] : reqs_)
|
|
if (r.subject.tenant == tenant && r.Open()) ++n;
|
|
return n;
|
|
}
|
|
|
|
const RightsRequest& Get(const std::string& id) const {
|
|
auto it = reqs_.find(id);
|
|
if (it == reqs_.end()) throw std::runtime_error("unknown rights request: " + id);
|
|
return it->second;
|
|
}
|
|
|
|
private:
|
|
std::map<std::string, RightsRequest> reqs_;
|
|
};
|
|
|
|
} // namespace molo::audit::rights
|