53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
package user
|
|
|
|
import (
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/cryptography"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func Update(c *gin.Context) {
|
|
// New user model
|
|
user := new(data.User)
|
|
userIdOrig, ok := c.Get("user_id")
|
|
if !ok {
|
|
c.JSON(403, gin.H{"status": "userid error"})
|
|
return
|
|
}
|
|
userId, err := uuid.Parse(userIdOrig.(string))
|
|
if err != nil {
|
|
c.JSON(500, gin.H{"status": "failed to parse uuid"})
|
|
}
|
|
|
|
var ReqInfo data.User
|
|
c.BindJSON(&ReqInfo)
|
|
|
|
// Get user info
|
|
user.GetByUserId(userId)
|
|
|
|
// Reject permission 0 user
|
|
if user.PermissionLevel == 0 {
|
|
c.JSON(403, gin.H{"status": "premission denied"})
|
|
return
|
|
}
|
|
|
|
user.Avatar = ReqInfo.Avatar
|
|
user.Email = ReqInfo.Email
|
|
user.Nickname = ReqInfo.Nickname
|
|
user.Subtitle = ReqInfo.Subtitle
|
|
|
|
if ReqInfo.Bio != "" {
|
|
if !cryptography.IsBase64Std(ReqInfo.Bio) {
|
|
c.JSON(400, gin.H{"status": "invalid base64"})
|
|
}
|
|
}
|
|
user.Bio = ReqInfo.Bio
|
|
|
|
// Update user info
|
|
user.UpdateByUserID(userId)
|
|
|
|
c.JSON(200, gin.H{"status": "success"})
|
|
}
|