Add user update service

Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
2025-12-25 20:42:34 +08:00
parent bfeb46a61f
commit d3d823c85f

View File

@@ -0,0 +1,47 @@
package user
import (
"net/http"
"nixcn-cms/data"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func Update(c *gin.Context) {
var ReqInfo data.UserUpdateInput
c.BindJSON(&ReqInfo)
// New user model
user := new(data.User)
userId, ok := c.Get("user_id")
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{
"status": "can not found user id",
})
return
}
// Get user info
user.GetByUserId(userId.(uuid.UUID))
// Reject permission 0 user
if user.PermissionLevel == 0 {
c.JSON(http.StatusForbidden, gin.H{
"status": "premission denied",
})
return
}
// Cant change user type under permission 2
if user.PermissionLevel < 2 {
ReqInfo.Type = nil
}
// Update user info
user.UpdateByUserID(userId.(uuid.UUID), &ReqInfo)
c.JSON(http.StatusOK, gin.H{
"status": "success",
})
}