feat(user): add user type modification endpoint
- Implement new endpoint `/userMgr/modifyType` for updating user types - Add database function DBUpdateUserType to handle type updates - Include request validation and proper error handling - Update API documentation with new endpoint details
This commit is contained in:
+47
@@ -24,6 +24,11 @@ type CreateUserRequest struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type ModifyUserRequest struct {
|
||||
UserID int `json:"userID"`
|
||||
Type string `json:"newType"`
|
||||
}
|
||||
|
||||
type RemoveUserRequest struct {
|
||||
TargetUserID int `json:"targetUserID"`
|
||||
}
|
||||
@@ -310,6 +315,48 @@ 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")
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[ModifyUserHandler] 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("[ModifyUserHandler] Failed to read request body: %v", err))
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
var req ModifyUserRequest
|
||||
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Invalid request format")
|
||||
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.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))
|
||||
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))
|
||||
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))
|
||||
}
|
||||
|
||||
func RemoveUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := Auth(w, r, http.MethodPost, "superuser")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user