From d3d823c85fe5ab307a5b4e3751294319f1597f29 Mon Sep 17 00:00:00 2001 From: Asai Neko Date: Thu, 25 Dec 2025 20:42:34 +0800 Subject: [PATCH] Add user update service Signed-off-by: Asai Neko --- service/user/update.go | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/service/user/update.go b/service/user/update.go index e69de29..87e2b72 100644 --- a/service/user/update.go +++ b/service/user/update.go @@ -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", + }) +}