feat(user): add user modification API and handler
implement user modification functionality including: - new endpoint /userMgr/modify - database update function DBUpdateUser - request/response structures - handler with proper authentication and validation - updated API documentation - marked as completed in README checklist
This commit is contained in:
+57
-7
@@ -25,6 +25,12 @@ type CreateUserRequest struct {
|
||||
}
|
||||
|
||||
type ModifyUserRequest struct {
|
||||
UserID int `json:"userID"`
|
||||
Username string `json:"username"`
|
||||
Passwd string `json:"passwd"`
|
||||
}
|
||||
|
||||
type ModifyUserTypeRequest struct {
|
||||
UserID int `json:"userID"`
|
||||
Type string `json:"newType"`
|
||||
}
|
||||
@@ -315,11 +321,10 @@ func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
postLog.Info(fmt.Sprintf("[CreateUserHandler] User \"%s\" created successfully with ID: %d", req.Username, userID))
|
||||
}
|
||||
|
||||
func ModifyUserTypeHandler (w http.ResponseWriter, r *http.Request) {
|
||||
_, err := Auth(w, r, http.MethodPost, "superuser")
|
||||
func ModifyUserHandler (w http.ResponseWriter, r *http.Request) {
|
||||
_, err := Auth(w, r, http.MethodPost, "user", "admin", "superuser")
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[ModifyUserHandler] Auth failed: %v", err))
|
||||
SendErrorResponse(w, http.StatusUnauthorized, fmt.Sprintf("Auth failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -338,23 +343,68 @@ func ModifyUserTypeHandler (w http.ResponseWriter, r *http.Request) {
|
||||
postLog.Warning(fmt.Sprintf("[ModifyUserHandler] Invalid request format: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
if req.UserID == 0 {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "UserID is required")
|
||||
postLog.Warning("[ModifyUserHandler] ModifyUser failed: UserID is empty")
|
||||
return
|
||||
}
|
||||
|
||||
if req.Username == "" {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Username is required")
|
||||
postLog.Warning("[ModifyUserHandler] ModifyUser failed: username is empty")
|
||||
return
|
||||
}
|
||||
|
||||
if err := DBUpdateUser(req.UserID, req.Username, req.Passwd); err != nil {
|
||||
SendErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
postLog.Error(fmt.Sprintf("[ModifyUserHandler] Failed to update user [%d]: %v", req.UserID, err))
|
||||
return
|
||||
}
|
||||
SendSuccessResponse(w, "User updated successfully", nil)
|
||||
postLog.Info(fmt.Sprintf("[ModifyUserHandler] User [%d]%s updated successfully to: %s", req.UserID, GetUsernameByID(req.UserID), req.Username))
|
||||
}
|
||||
|
||||
func ModifyUserTypeHandler (w http.ResponseWriter, r *http.Request) {
|
||||
_, err := Auth(w, r, http.MethodPost, "superuser")
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[ModifyUserTypeHandler] Auth failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Failed to read request body")
|
||||
postLog.Warning(fmt.Sprintf("[ModifyUserTypeHandler] Failed to read request body: %v", err))
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
var req ModifyUserTypeRequest
|
||||
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Invalid request format")
|
||||
postLog.Warning(fmt.Sprintf("[ModifyUserTypeHandler] Invalid request format: %v", err))
|
||||
return
|
||||
}
|
||||
if req.UserID == 0 {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "UserID is required")
|
||||
postLog.Warning("[ModifyUserTypeHandler] ModifyUserType failed: UserID is empty")
|
||||
return
|
||||
}
|
||||
if req.Type != "admin" && req.Type != "visitor" && req.Type != "superuser" {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Invalid type: must be 'admin' or 'visitor' or 'superuser'")
|
||||
postLog.Warning(fmt.Sprintf("[ModifyUserHandler] ModifyUser failed: invalid type: %s", req.Type))
|
||||
postLog.Warning(fmt.Sprintf("[ModifyUserTypeHandler] ModifyUserType failed: invalid type: %s", req.Type))
|
||||
return
|
||||
}
|
||||
if err := DBUpdateUserType(req.UserID, req.Type); err != nil {
|
||||
SendErrorResponse(w, http.StatusInternalServerError, err.Error())
|
||||
postLog.Error(fmt.Sprintf("[ModifyUserHandler] Failed to update user type [%d]: %v", req.UserID, err))
|
||||
postLog.Error(fmt.Sprintf("[ModifyUserTypeHandler] Failed to update user type [%d]: %v", req.UserID, err))
|
||||
return
|
||||
}
|
||||
SendSuccessResponse(w, "User type updated successfully", nil)
|
||||
postLog.Info(fmt.Sprintf("[ModifyUserHandler] User [%d]%s type updated successfully to: %s", req.UserID, GetUsernameByID(req.UserID), req.Type))
|
||||
postLog.Info(fmt.Sprintf("[ModifyUserTypeHandler] User [%d]%s type updated successfully to: %s", req.UserID, GetUsernameByID(req.UserID), req.Type))
|
||||
}
|
||||
|
||||
func RemoveUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user