Compare commits
2
Commits
c5fede30ad
...
5beb64c1e6
@@ -22,9 +22,7 @@ class AuthController : public drogon::HttpController<AuthController>
|
|||||||
"/api/v1/auth/change-password",
|
"/api/v1/auth/change-password",
|
||||||
drogon::Post);
|
drogon::Post);
|
||||||
ADD_METHOD_TO(AuthController::me, "/api/v1/me", drogon::Get);
|
ADD_METHOD_TO(AuthController::me, "/api/v1/me", drogon::Get);
|
||||||
ADD_METHOD_TO(AuthController::editUser,
|
ADD_METHOD_TO(AuthController::editUser, "/api/v1/me", drogon::Patch);
|
||||||
"/api/v1/me",
|
|
||||||
drogon::Patch);
|
|
||||||
METHOD_LIST_END
|
METHOD_LIST_END
|
||||||
|
|
||||||
void registerCitizen(
|
void registerCitizen(
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ Base path: `/api/v1`.
|
|||||||
- `POST /auth/login/password`: password login fallback.
|
- `POST /auth/login/password`: password login fallback.
|
||||||
- `POST /auth/change-password`: change the authenticated user's password.
|
- `POST /auth/change-password`: change the authenticated user's password.
|
||||||
- `GET /me`: return the authenticated user.
|
- `GET /me`: return the authenticated user.
|
||||||
|
- `PATCH /me`: edit user informations.
|
||||||
|
|
||||||
Protected endpoints use:
|
Protected endpoints use:
|
||||||
|
|
||||||
|
|||||||
@@ -385,6 +385,21 @@
|
|||||||
],
|
],
|
||||||
"type": "object"
|
"type": "object"
|
||||||
},
|
},
|
||||||
|
"EditUserRequest": {
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"first_name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"last_name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"phone": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
"EmailRequest": {
|
"EmailRequest": {
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
"properties": {
|
"properties": {
|
||||||
@@ -2390,6 +2405,48 @@
|
|||||||
"tags": [
|
"tags": [
|
||||||
"Auth"
|
"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": {
|
"/api/v1/me/rewards": {
|
||||||
|
|||||||
+20
-26
@@ -239,37 +239,31 @@ Json::Value AuthService::changePassword(const auth::UserContext &user,
|
|||||||
return success();
|
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();
|
auto db = drogon::app().getDbClient();
|
||||||
const auto result = db->execSqlSync(
|
bool hasUpdates = false;
|
||||||
"SELECT id, first_name, last_name, email, fiscal_code, phone, points, "
|
|
||||||
"must_change_password FROM users WHERE id = ?",
|
|
||||||
user.id);
|
|
||||||
|
|
||||||
auto response = success();
|
for (const auto& field : allowedFields) {
|
||||||
if (!result.empty())
|
if (body.isMember(field)) {
|
||||||
{
|
if (!body[field].isString()) {
|
||||||
response["user"]["id"] = Json::Int64(result[0]["id"].as<long long>());
|
return failure("invalid_field_type");
|
||||||
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)
|
|
||||||
{
|
db->execSqlSync(
|
||||||
response["user"]["permissions"].append(permission);
|
"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) {
|
Json::Value AuthService::editUser(const auth::UserContext &user, Json::Value::const_iterator::reference body) {
|
||||||
|
|||||||
Reference in New Issue
Block a user