From 98588304591f6a4af1dba566d74b49d6836b0c6c Mon Sep 17 00:00:00 2001 From: ReLoia Date: Mon, 15 Jun 2026 11:16:39 +0200 Subject: [PATCH] fix: PATCH /me logic --- controllers/AuthController.h | 4 +-- services/AuthService.cc | 48 ++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/controllers/AuthController.h b/controllers/AuthController.h index 9c044e4..725b661 100644 --- a/controllers/AuthController.h +++ b/controllers/AuthController.h @@ -22,9 +22,7 @@ class AuthController : public drogon::HttpController "/api/v1/auth/change-password", drogon::Post); ADD_METHOD_TO(AuthController::me, "/api/v1/me", drogon::Get); - ADD_METHOD_TO(AuthController::editUser, - "/api/v1/me", - drogon::Patch); + ADD_METHOD_TO(AuthController::editUser, "/api/v1/me", drogon::Patch); METHOD_LIST_END void registerCitizen( diff --git a/services/AuthService.cc b/services/AuthService.cc index e947d6b..fb7a147 100644 --- a/services/AuthService.cc +++ b/services/AuthService.cc @@ -239,37 +239,31 @@ Json::Value AuthService::changePassword(const auth::UserContext &user, return success(); } -Json::Value AuthService::currentUser(const auth::UserContext &user) -{ + Json::Value AuthService::editUser(const auth::UserContext &user, Json::Value::const_iterator::reference body) { + const auto allowedFields = {"first_name", "last_name", "phone"}; auto db = drogon::app().getDbClient(); - const auto result = db->execSqlSync( - "SELECT id, first_name, last_name, email, fiscal_code, phone, points, " - "must_change_password FROM users WHERE id = ?", - user.id); + bool hasUpdates = false; - auto response = success(); - if (!result.empty()) - { - response["user"]["id"] = Json::Int64(result[0]["id"].as()); - response["user"]["first_name"] = result[0]["first_name"].as(); - response["user"]["last_name"] = result[0]["last_name"].as(); - response["user"]["email"] = result[0]["email"].as(); - response["user"]["fiscal_code"] = result[0]["fiscal_code"].as(); - response["user"]["phone"] = result[0]["phone"].as(); - response["user"]["points"] = result[0]["points"].as(); - response["user"]["must_change_password"] = - result[0]["must_change_password"].as(); - response["user"]["role"] = user.role; - for (const auto &role : user.roles) - { - response["user"]["roles"].append(role); - } - for (const auto &permission : user.permissions) - { - response["user"]["permissions"].append(permission); + for (const auto& field : allowedFields) { + if (body.isMember(field)) { + if (!body[field].isString()) { + return failure("invalid_field_type"); + } + + db->execSqlSync( + "UPDATE users SET " + std::string(field) + " = ? WHERE id = ?", + body[field].asString(), + user.id + ); + hasUpdates = true; } } - return response; + + if (!hasUpdates) { + return failure("missing_fields"); + } + + return success(); } Json::Value AuthService::editUser(const auth::UserContext &user, Json::Value::const_iterator::reference body) {