PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
security_endpoints.cpp
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
18
20
22 crow::SimpleApp &app, std::shared_ptr<rest_server_context> ctx) {
23
24 // POST /api/v1/security/users - Create a new user
25 CROW_ROUTE(app, "/api/v1/security/users")
26 .methods(crow::HTTPMethod::POST)([ctx](const crow::request &req) {
27 crow::response res;
28 res.add_header("Content-Type", "application/json");
29
30 if (!ctx->security_manager) {
31 res.body = make_error_json("SECURITY_UNAVAILABLE",
32 "Security manager not configured");
33 res.code = 503;
34 return res;
35 }
36
37 auto x = crow::json::load(req.body);
38 if (!x) {
39 res.body = make_error_json("INVALID_JSON", "Invalid JSON body");
40 res.code = 400;
41 return res;
42 }
43
44 if (!x.has("username") || !x.has("id")) {
45 res.body =
46 make_error_json("MISSING_FIELDS", "Username and ID are required");
47 res.code = 400;
48 return res;
49 }
50
52 user.id = x["id"].s();
53 user.username = x["username"].s();
54 user.active = true; // Default to active
55
56 auto result = ctx->security_manager->create_user(user);
57 if (result.is_err()) {
58 res.body = make_error_json(
59 "CREATE_FAILED", "Failed to create user"); // In real app, expose
60 // inner error safely
61 res.code = 500;
62 } else {
63 res.body = make_success_json("User created");
64 res.code = 201;
65 }
66 return res;
67 });
68
69 // POST /api/v1/security/users/<id>/roles - Assign role to user
70 CROW_ROUTE(app, "/api/v1/security/users/<string>/roles")
71 .methods(crow::HTTPMethod::POST)([ctx](const crow::request &req,
72 std::string user_id) {
73 crow::response res;
74 res.add_header("Content-Type", "application/json");
75
76 if (!ctx->security_manager) {
77 res.body = make_error_json("SECURITY_UNAVAILABLE",
78 "Security manager not configured");
79 res.code = 503;
80 return res;
81 }
82
83 auto x = crow::json::load(req.body);
84 if (!x || !x.has("role")) {
85 res.body = make_error_json("INVALID_REQUEST", "Role is required");
86 res.code = 400;
87 return res;
88 }
89
90 std::string role_str = x["role"].s();
91 auto role_opt = kcenon::pacs::security::parse_role(role_str);
92 if (!role_opt) {
93 res.body = make_error_json("INVALID_ROLE", "Invalid role specified");
94 res.code = 400;
95 return res;
96 }
97
98 auto result = ctx->security_manager->assign_role(user_id, *role_opt);
99 if (result.is_err()) {
100 // Could distinguish user not found vs other errors
101 res.body = make_error_json("ASSIGN_FAILED", "Failed to assign role");
102 res.code = 500;
103 } else {
104 res.body = make_success_json("Role assigned");
105 res.code = 200;
106 }
107 return res;
108 });
109}
110
111} // namespace kcenon::pacs::web::endpoints
Core RBAC logic.
std::optional< Role > parse_role(std::string_view str)
Parse Role from string.
Definition role.h:50
void register_security_endpoints_impl(crow::SimpleApp &app, std::shared_ptr< rest_server_context > ctx)
Register security endpoints with the Crow app.
std::string make_error_json(std::string_view code, std::string_view message)
Create JSON error response body with details.
Definition rest_types.h:79
std::string make_success_json(std::string_view message="OK")
Create success response with optional message.
Definition rest_types.h:91
Common types and utilities for REST API.
Role definitions for RBAC.
Security API endpoints for REST server.
Represents a user in the system.
Definition user.h:26
User definition for RBAC.