fix: PATCH /me logic
This commit is contained in:
@@ -22,9 +22,7 @@ class AuthController : public drogon::HttpController<AuthController>
|
||||
"/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(
|
||||
|
||||
+21
-27
@@ -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<long long>());
|
||||
response["user"]["first_name"] = result[0]["first_name"].as<std::string>();
|
||||
response["user"]["last_name"] = result[0]["last_name"].as<std::string>();
|
||||
response["user"]["email"] = result[0]["email"].as<std::string>();
|
||||
response["user"]["fiscal_code"] = result[0]["fiscal_code"].as<std::string>();
|
||||
response["user"]["phone"] = result[0]["phone"].as<std::string>();
|
||||
response["user"]["points"] = result[0]["points"].as<int>();
|
||||
response["user"]["must_change_password"] =
|
||||
result[0]["must_change_password"].as<bool>();
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user