diff --git a/channel/token-store.go b/channel/token-store.go index e5da0ca..f261106 100644 --- a/channel/token-store.go +++ b/channel/token-store.go @@ -152,6 +152,7 @@ func TokenStoreAddChannel(channel *model.Channel) { } item := channel2item(channel) if item != nil { + // Do not check IsShared here, cause its useless TokenStoreAddItem(item) } } @@ -161,7 +162,7 @@ func TokenStoreRemoveChannel(channel *model.Channel) { return } item := channel2item(channel) - if item != nil { + if item != nil && !item.IsShared() { TokenStoreRemoveItem(item) } } @@ -170,6 +171,7 @@ func TokenStoreUpdateChannel(newChannel *model.Channel, oldChannel *model.Channe if oldChannel.Type != model.TypeWeChatTestAccount && oldChannel.Type != model.TypeWeChatCorpAccount { return } + // Why so complicated? Because the given channel maybe incomplete. if oldChannel.Type == model.TypeWeChatTestAccount { // Only keep changed parts if newChannel.AppId == oldChannel.AppId { diff --git a/channel/wechat-corp-account.go b/channel/wechat-corp-account.go index fe601ab..7593db7 100644 --- a/channel/wechat-corp-account.go +++ b/channel/wechat-corp-account.go @@ -32,8 +32,9 @@ func (i *WeChatCorpAccountTokenStoreItem) Key() string { func (i *WeChatCorpAccountTokenStoreItem) IsShared() bool { appId := fmt.Sprintf("%s|%s", i.CorpId, i.AgentId) - return model.DB.Where("type = ? and app_id = ? and secret = ?", - model.TypeWeChatCorpAccount, appId, i.AgentSecret).Find(&model.Channel{}).RowsAffected != 1 + var count int64 = 0 + model.DB.Model(&model.Channel{}).Where("type = ? and app_id = ? and secret = ?", model.TypeWeChatCorpAccount, appId, i.AgentSecret).Count(&count) + return count > 1 } func (i *WeChatCorpAccountTokenStoreItem) IsFilled() bool { diff --git a/channel/wechat-test-account.go b/channel/wechat-test-account.go index 032314d..519d51f 100644 --- a/channel/wechat-test-account.go +++ b/channel/wechat-test-account.go @@ -29,8 +29,9 @@ func (i *WeChatTestAccountTokenStoreItem) Key() string { } func (i *WeChatTestAccountTokenStoreItem) IsShared() bool { - return model.DB.Where("type = ? and app_id = ? and secret = ?", - model.TypeWeChatTestAccount, i.AppID, i.AppSecret).Find(&model.Channel{}).RowsAffected != 1 + var count int64 = 0 + model.DB.Model(&model.Channel{}).Where("type = ? and app_id = ? and secret = ?", model.TypeWeChatTestAccount, i.AppID, i.AppSecret).Count(&count) + return count > 1 } func (i *WeChatTestAccountTokenStoreItem) IsFilled() bool { diff --git a/controller/user.go b/controller/user.go index 79fdee1..3159985 100644 --- a/controller/user.go +++ b/controller/user.go @@ -604,6 +604,7 @@ func ManageUser(c *gin.Context) { }) return } + channel.TokenStoreRemoveUser(&user) case "promote": if myRole != common.RoleRootUser { c.JSON(http.StatusOK, gin.H{