Compare commits
2
Commits
@@ -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(
|
||||
|
||||
@@ -15,6 +15,7 @@ Base path: `/api/v1`.
|
||||
- `POST /auth/login/password`: password login fallback.
|
||||
- `POST /auth/change-password`: change the authenticated user's password.
|
||||
- `GET /me`: return the authenticated user.
|
||||
- `PATCH /me`: edit user informations.
|
||||
|
||||
Protected endpoints use:
|
||||
|
||||
|
||||
@@ -385,6 +385,21 @@
|
||||
],
|
||||
"type": "object"
|
||||
},
|
||||
"EditUserRequest": {
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"first_name": {
|
||||
"type": "string"
|
||||
},
|
||||
"last_name": {
|
||||
"type": "string"
|
||||
},
|
||||
"phone": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
},
|
||||
"EmailRequest": {
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
@@ -2390,6 +2405,48 @@
|
||||
"tags": [
|
||||
"Auth"
|
||||
]
|
||||
},
|
||||
"patch": {
|
||||
"description": "Updates fields of the authenticated user's profile.",
|
||||
"operationId": "AuthController.editUser",
|
||||
"parameters": [ ],
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/EditUserRequest"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/OkResponse"
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "User profile updated successfully."
|
||||
},
|
||||
"400": {
|
||||
"description": "Invalid parameter type or structural error."
|
||||
},
|
||||
"401": {
|
||||
"$ref": "#/components/responses/Unauthorized"
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{
|
||||
"bearerAuth": [ ]
|
||||
}
|
||||
],
|
||||
"summary": "Edit current user profile",
|
||||
"tags": [
|
||||
"Auth"
|
||||
]
|
||||
}
|
||||
},
|
||||
"/api/v1/me/rewards": {
|
||||
|
||||
+20
-26
@@ -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& field : allowedFields) {
|
||||
if (body.isMember(field)) {
|
||||
if (!body[field].isString()) {
|
||||
return failure("invalid_field_type");
|
||||
}
|
||||
for (const auto &permission : user.permissions)
|
||||
{
|
||||
response["user"]["permissions"].append(permission);
|
||||
|
||||
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