settings_routes.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "settings_routes.hpp"
  2. #include "../app.hpp"
  3. #include "../stores/settings_store.hpp"
  4. #include "../stores/workspace_store.hpp"
  5. #include <smartbotic/microbit/auth/auth_middleware.hpp>
  6. #include <smartbotic/microbit/smtp/smtp_client.hpp>
  7. #include <smartbotic/microbit/callerai/callerai_client.hpp>
  8. #include <smartbotic/microbit/common/logging.hpp>
  9. #include <nlohmann/json.hpp>
  10. #include <spdlog/spdlog.h>
  11. #include <string>
  12. #include <vector>
  13. namespace smartbotic::microbit {
  14. using json = nlohmann::json;
  15. // Helper: check if user is owner of any workspace
  16. static bool isOwnerOfAnyWorkspace(App& app, const std::string& userId) {
  17. auto workspaces = app.workspaceStore()->listUserWorkspaces(userId);
  18. for (const auto& ws : workspaces) {
  19. auto member = app.workspaceStore()->getMember(ws.id, userId);
  20. if (member && member->role == "owner") {
  21. return true;
  22. }
  23. }
  24. return false;
  25. }
  26. // Helper: mask sensitive fields in settings
  27. static json maskSensitiveFields(const json& settings) {
  28. json masked = settings;
  29. // List of keys whose values should be masked
  30. static const std::vector<std::string> sensitiveKeys = {
  31. "smtp_password", "callerai_api_key", "api_key", "secret", "password"
  32. };
  33. for (const auto& key : sensitiveKeys) {
  34. if (masked.contains(key) && masked[key].is_string()) {
  35. std::string val = masked[key].get<std::string>();
  36. if (val.size() > 4) {
  37. masked[key] = std::string(val.size() - 4, '*') + val.substr(val.size() - 4);
  38. } else if (!val.empty()) {
  39. masked[key] = "****";
  40. }
  41. }
  42. }
  43. return masked;
  44. }
  45. void setupSettingsRoutes(httplib::Server& svr, App& app) {
  46. // GET /api/v1/settings -- get global settings (owner of any workspace)
  47. svr.Get("/api/v1/settings", [&app](const httplib::Request& req, httplib::Response& res) {
  48. try {
  49. auto authCtx = app.authMiddleware()->getAuthContext();
  50. if (!authCtx) {
  51. res.status = 401;
  52. res.set_content(R"({"error":"Unauthorized"})", "application/json");
  53. return;
  54. }
  55. if (!isOwnerOfAnyWorkspace(app, authCtx->userId)) {
  56. res.status = 403;
  57. res.set_content(json{{"error", "Forbidden: requires owner role in at least one workspace"}}.dump(), "application/json");
  58. return;
  59. }
  60. auto settings = app.settingsStore()->getGlobalSettings();
  61. json settingsJson = settings.value_or(json::object());
  62. // Mask sensitive fields before returning
  63. json masked = maskSensitiveFields(settingsJson);
  64. res.status = 200;
  65. res.set_content(json{{"settings", masked}}.dump(), "application/json");
  66. } catch (const std::exception& e) {
  67. spdlog::error("Get settings error: {}", e.what());
  68. res.status = 500;
  69. res.set_content(json{{"error", "Internal server error"}}.dump(), "application/json");
  70. }
  71. });
  72. // PUT /api/v1/settings -- update global settings (owner)
  73. svr.Put("/api/v1/settings", [&app](const httplib::Request& req, httplib::Response& res) {
  74. try {
  75. auto authCtx = app.authMiddleware()->getAuthContext();
  76. if (!authCtx) {
  77. res.status = 401;
  78. res.set_content(R"({"error":"Unauthorized"})", "application/json");
  79. return;
  80. }
  81. if (!isOwnerOfAnyWorkspace(app, authCtx->userId)) {
  82. res.status = 403;
  83. res.set_content(json{{"error", "Forbidden: requires owner role in at least one workspace"}}.dump(), "application/json");
  84. return;
  85. }
  86. auto body = json::parse(req.body);
  87. bool updated = app.settingsStore()->updateGlobalSettings(body);
  88. if (!updated) {
  89. res.status = 500;
  90. res.set_content(json{{"error", "Failed to update settings"}}.dump(), "application/json");
  91. return;
  92. }
  93. // Return the updated (masked) settings
  94. auto settings = app.settingsStore()->getGlobalSettings();
  95. json settingsJson = settings.value_or(json::object());
  96. json masked = maskSensitiveFields(settingsJson);
  97. res.status = 200;
  98. res.set_content(json{{"settings", masked}}.dump(), "application/json");
  99. } catch (const json::parse_error& e) {
  100. res.status = 400;
  101. res.set_content(json{{"error", "Invalid JSON"}}.dump(), "application/json");
  102. } catch (const std::invalid_argument& e) {
  103. res.status = 400;
  104. res.set_content(json{{"error", e.what()}}.dump(), "application/json");
  105. } catch (const std::exception& e) {
  106. spdlog::error("Update settings error: {}", e.what());
  107. res.status = 500;
  108. res.set_content(json{{"error", "Internal server error"}}.dump(), "application/json");
  109. }
  110. });
  111. // POST /api/v1/settings/test-smtp -- test SMTP connection (owner)
  112. svr.Post("/api/v1/settings/test-smtp", [&app](const httplib::Request& req, httplib::Response& res) {
  113. try {
  114. auto authCtx = app.authMiddleware()->getAuthContext();
  115. if (!authCtx) {
  116. res.status = 401;
  117. res.set_content(R"({"error":"Unauthorized"})", "application/json");
  118. return;
  119. }
  120. if (!isOwnerOfAnyWorkspace(app, authCtx->userId)) {
  121. res.status = 403;
  122. res.set_content(json{{"error", "Forbidden: requires owner role in at least one workspace"}}.dump(), "application/json");
  123. return;
  124. }
  125. if (!app.smtpClient()) {
  126. throw std::invalid_argument("SMTP client is not configured");
  127. }
  128. bool success = app.smtpClient()->testConnection();
  129. if (success) {
  130. res.status = 200;
  131. res.set_content(json{{"message", "SMTP connection successful"}}.dump(), "application/json");
  132. } else {
  133. res.status = 500;
  134. res.set_content(json{{"error", "SMTP connection failed"}}.dump(), "application/json");
  135. }
  136. } catch (const std::invalid_argument& e) {
  137. res.status = 400;
  138. res.set_content(json{{"error", e.what()}}.dump(), "application/json");
  139. } catch (const std::exception& e) {
  140. spdlog::error("Test SMTP error: {}", e.what());
  141. res.status = 500;
  142. res.set_content(json{{"error", "Internal server error"}}.dump(), "application/json");
  143. }
  144. });
  145. // POST /api/v1/settings/test-callerai -- test CallerAI connection (owner)
  146. svr.Post("/api/v1/settings/test-callerai", [&app](const httplib::Request& req, httplib::Response& res) {
  147. try {
  148. auto authCtx = app.authMiddleware()->getAuthContext();
  149. if (!authCtx) {
  150. res.status = 401;
  151. res.set_content(R"({"error":"Unauthorized"})", "application/json");
  152. return;
  153. }
  154. if (!isOwnerOfAnyWorkspace(app, authCtx->userId)) {
  155. res.status = 403;
  156. res.set_content(json{{"error", "Forbidden: requires owner role in at least one workspace"}}.dump(), "application/json");
  157. return;
  158. }
  159. if (!app.calleraiClient()) {
  160. throw std::invalid_argument("CallerAI client is not configured");
  161. }
  162. bool success = app.calleraiClient()->testConnection();
  163. if (success) {
  164. res.status = 200;
  165. res.set_content(json{{"message", "CallerAI connection successful"}}.dump(), "application/json");
  166. } else {
  167. res.status = 500;
  168. res.set_content(json{{"error", "CallerAI connection failed"}}.dump(), "application/json");
  169. }
  170. } catch (const std::invalid_argument& e) {
  171. res.status = 400;
  172. res.set_content(json{{"error", e.what()}}.dump(), "application/json");
  173. } catch (const std::exception& e) {
  174. spdlog::error("Test CallerAI error: {}", e.what());
  175. res.status = 500;
  176. res.set_content(json{{"error", "Internal server error"}}.dump(), "application/json");
  177. }
  178. });
  179. }
  180. } // namespace smartbotic::microbit