PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
user_context.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
14#pragma once
15
16#include "user.h"
17#include <chrono>
18#include <optional>
19#include <string>
20
21namespace kcenon::pacs::security {
22
30public:
31 using clock = std::chrono::steady_clock;
32 using time_point = clock::time_point;
33
39 user_context(User user, std::string session_id)
40 : user_(std::move(user)), session_id_(std::move(session_id)),
41 created_at_(clock::now()), last_activity_(clock::now()) {}
42
47 User system_user;
48 system_user.id = "system";
49 system_user.username = "system";
50 system_user.roles = {Role::System};
51 system_user.active = true;
52 return user_context(std::move(system_user), "system-internal");
53 }
54
58 static user_context anonymous_context(const std::string &session_id) {
59 User anon_user;
60 anon_user.id = "anonymous";
61 anon_user.username = "anonymous";
62 anon_user.roles = {}; // No roles = no permissions
63 anon_user.active = true;
64 return user_context(std::move(anon_user), session_id);
65 }
66
67 // Accessors
68 [[nodiscard]] const User &user() const noexcept { return user_; }
69 [[nodiscard]] const std::string &session_id() const noexcept {
70 return session_id_;
71 }
72 [[nodiscard]] time_point created_at() const noexcept { return created_at_; }
73 [[nodiscard]] time_point last_activity() const noexcept {
74 return last_activity_;
75 }
76
80 [[nodiscard]] bool is_valid() const noexcept { return user_.active; }
81
85 [[nodiscard]] bool has_role(Role role) const { return user_.has_role(role); }
86
90 void touch() { last_activity_ = clock::now(); }
91
92 // Optional: Source information for audit
93 void set_source_ae_title(std::string ae) {
94 source_ae_title_ = std::move(ae);
95 }
96 [[nodiscard]] const std::optional<std::string> &
97 source_ae_title() const noexcept {
98 return source_ae_title_;
99 }
100
101 void set_source_ip(std::string ip) { source_ip_ = std::move(ip); }
102 [[nodiscard]] const std::optional<std::string> &source_ip() const noexcept {
103 return source_ip_;
104 }
105
106private:
108 std::string session_id_;
111 std::optional<std::string> source_ae_title_;
112 std::optional<std::string> source_ip_;
113};
114
115} // namespace kcenon::pacs::security
Represents the security context for a user session.
std::chrono::steady_clock clock
const std::optional< std::string > & source_ae_title() const noexcept
const std::optional< std::string > & source_ip() const noexcept
time_point created_at() const noexcept
time_point last_activity() const noexcept
user_context(User user, std::string session_id)
Construct a user context.
bool is_valid() const noexcept
Check if the context is valid (user is active)
std::optional< std::string > source_ae_title_
static user_context system_context()
Create a system context for internal operations.
bool has_role(Role role) const
Check if user has a specific role.
std::optional< std::string > source_ip_
void set_source_ae_title(std::string ae)
static user_context anonymous_context(const std::string &session_id)
Create an anonymous context with minimal permissions.
const std::string & session_id() const noexcept
void touch()
Update last activity timestamp.
const User & user() const noexcept
Role
User roles in the PACS system.
Definition role.h:25
@ System
Internal system operations.
Represents a user in the system.
Definition user.h:26
bool has_role(Role role) const
Check if user has a specific role.
Definition user.h:35
std::vector< Role > roles
Definition user.h:29
User definition for RBAC.