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:
@@ -346,6 +346,14 @@ func DBQuerySpecificUser(userID int) (User, error) { // Query user by ID
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func DBUpdateUserType (userID int, newType string) error {
|
||||
_, err := db.Exec("UPDATE userLogin SET type = ? WHERE userID = ?", newType, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update user type: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DBAddFrpcInstance(instance FrpcInstance) error {
|
||||
_, err := frpcDB.Exec("INSERT INTO frpcInstances (userID, name, bootAtStart, runUser, configPath, createdAt) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
instance.UserID, instance.Name, instance.BootAtStart, instance.RunUser, instance.ConfigPath, time.Now().Format(time.RFC3339))
|
||||
|
||||
+32
@@ -277,6 +277,38 @@ X-Timestamp: 1704067200000
|
||||
|
||||
---
|
||||
|
||||
## Modify User Type
|
||||
|
||||
**Endpoint**: `/userMgr/modifyType`
|
||||
**Method**: POST
|
||||
**Content-Type**: application/json
|
||||
**Auth Required**: Yes (token)
|
||||
**Permission Level**: Superuser
|
||||
|
||||
**Request Headers**:
|
||||
```
|
||||
X-Token: your_token
|
||||
X-Timestamp: 1704067200000
|
||||
```
|
||||
|
||||
**Request Body**:
|
||||
```json
|
||||
{
|
||||
"userID": 2,
|
||||
"newType": "superadmin"
|
||||
}
|
||||
```
|
||||
|
||||
**Response**:
|
||||
```json
|
||||
{
|
||||
"success": true,
|
||||
"message": "User type updated successfully"
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## List Users
|
||||
|
||||
**Endpoint**: `/userMgr/list`
|
||||
|
||||
@@ -21,7 +21,7 @@ func setupRoutes() {
|
||||
http.HandleFunc("/userMgr/create", CreateUserHandler)
|
||||
http.HandleFunc("/userMgr/remove", RemoveUserHandler)
|
||||
http.HandleFunc("/userMgr/list", ListUserHandler)
|
||||
// http.HandleFunc("/userMgr/modify", ModifyUserHandler)
|
||||
http.HandleFunc("/userMgr/modifyType", ModifyUserTypeHandler)
|
||||
|
||||
http.HandleFunc("/sessionMgr/list", ListActiveSessionsHandler)
|
||||
http.HandleFunc("/sessionMgr/remove", RemoveSessionHandler)
|
||||
|
||||
+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