From e495960d0e8beb2db304eec1af0041778fddf916 Mon Sep 17 00:00:00 2001 From: JustSong Date: Mon, 15 May 2023 23:30:29 +0800 Subject: [PATCH] fix: do not send channel's secret to frontend --- controller/channel.go | 4 ++-- model/channel.go | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/controller/channel.go b/controller/channel.go index 6e60384..13fcaef 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -83,7 +83,7 @@ func GetChannel(c *gin.Context) { }) return } - channel_, err := model.GetChannelById(id, userId) + channel_, err := model.GetChannelById(id, userId, false) if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, @@ -183,7 +183,7 @@ func UpdateChannel(c *gin.Context) { }) return } - oldChannel, err := model.GetChannelById(channel_.Id, userId) + oldChannel, err := model.GetChannelById(channel_.Id, userId, true) if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, diff --git a/model/channel.go b/model/channel.go index 9df706a..a060d3e 100644 --- a/model/channel.go +++ b/model/channel.go @@ -45,12 +45,17 @@ type BriefChannel struct { Description string `json:"description"` } -func GetChannelById(id int, userId int) (*Channel, error) { +func GetChannelById(id int, userId int, selectAll bool) (*Channel, error) { if id == 0 || userId == 0 { return nil, errors.New("id 或 userId 为空!") } c := Channel{Id: id, UserId: userId} - err := DB.Where(c).First(&c).Error + var err error + if selectAll { + err = DB.Where(c).First(&c).Error + } else { + err = DB.Omit("secret").Where(c).First(&c).Error + } return &c, err }