chore: update token store when user updated
This commit is contained in:
+74
-5
@@ -78,19 +78,88 @@ func TokenStoreInit() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TokenStoreAddItem(item *TokenStoreItem) {
|
func TokenStoreAddItem(item TokenStoreItem) {
|
||||||
(*item).Refresh()
|
item.Refresh()
|
||||||
s.Mutex.RLock()
|
s.Mutex.RLock()
|
||||||
s.Map[(*item).Key()] = item
|
s.Map[item.Key()] = &item
|
||||||
s.Mutex.RUnlock()
|
s.Mutex.RUnlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TokenStoreRemoveItem(item *TokenStoreItem) {
|
func TokenStoreRemoveItem(item TokenStoreItem) {
|
||||||
s.Mutex.RLock()
|
s.Mutex.RLock()
|
||||||
delete(s.Map, (*item).Key())
|
delete(s.Map, item.Key())
|
||||||
s.Mutex.RUnlock()
|
s.Mutex.RUnlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TokenStoreUpdateUser(cleanUser *model.User, originUser *model.User) {
|
||||||
|
// TODO: check if this token is shared!
|
||||||
|
if cleanUser.WeChatTestAccountId == originUser.WeChatTestAccountId {
|
||||||
|
cleanUser.WeChatTestAccountId = ""
|
||||||
|
}
|
||||||
|
if cleanUser.WeChatTestAccountSecret == originUser.WeChatTestAccountSecret {
|
||||||
|
cleanUser.WeChatTestAccountSecret = ""
|
||||||
|
}
|
||||||
|
if cleanUser.WeChatTestAccountId != "" || cleanUser.WeChatTestAccountSecret != "" {
|
||||||
|
oldWeChatTestAccountTokenStoreItem := WeChatTestAccountTokenStoreItem{
|
||||||
|
AppID: originUser.WeChatTestAccountId,
|
||||||
|
AppSecret: originUser.WeChatTestAccountSecret,
|
||||||
|
}
|
||||||
|
newWeChatTestAccountTokenStoreItem := oldWeChatTestAccountTokenStoreItem
|
||||||
|
if cleanUser.WeChatTestAccountId != "" {
|
||||||
|
newWeChatTestAccountTokenStoreItem.AppID = cleanUser.WeChatTestAccountId
|
||||||
|
}
|
||||||
|
if cleanUser.WeChatTestAccountSecret != "" {
|
||||||
|
newWeChatTestAccountTokenStoreItem.AppSecret = cleanUser.WeChatTestAccountSecret
|
||||||
|
}
|
||||||
|
TokenStoreRemoveItem(&oldWeChatTestAccountTokenStoreItem)
|
||||||
|
TokenStoreAddItem(&newWeChatTestAccountTokenStoreItem)
|
||||||
|
}
|
||||||
|
if cleanUser.WeChatCorpAccountId == originUser.WeChatCorpAccountId {
|
||||||
|
cleanUser.WeChatCorpAccountId = ""
|
||||||
|
}
|
||||||
|
if cleanUser.WeChatCorpAccountAgentId == originUser.WeChatCorpAccountAgentId {
|
||||||
|
cleanUser.WeChatCorpAccountAgentId = ""
|
||||||
|
}
|
||||||
|
if cleanUser.WeChatCorpAccountSecret == originUser.WeChatCorpAccountSecret {
|
||||||
|
cleanUser.WeChatCorpAccountSecret = ""
|
||||||
|
}
|
||||||
|
if cleanUser.WeChatCorpAccountId != "" || cleanUser.WeChatCorpAccountAgentId != "" || cleanUser.WeChatCorpAccountSecret != "" {
|
||||||
|
oldWeChatCorpAccountTokenStoreItem := WeChatCorpAccountTokenStoreItem{
|
||||||
|
CorpId: cleanUser.WeChatCorpAccountId,
|
||||||
|
CorpSecret: cleanUser.WeChatCorpAccountSecret,
|
||||||
|
AgentId: cleanUser.WeChatCorpAccountAgentId,
|
||||||
|
}
|
||||||
|
newWeChatCorpAccountTokenStoreItem := oldWeChatCorpAccountTokenStoreItem
|
||||||
|
if cleanUser.WeChatCorpAccountId != "" {
|
||||||
|
newWeChatCorpAccountTokenStoreItem.CorpId = cleanUser.WeChatCorpAccountId
|
||||||
|
}
|
||||||
|
if cleanUser.WeChatCorpAccountSecret != "" {
|
||||||
|
newWeChatCorpAccountTokenStoreItem.CorpSecret = cleanUser.WeChatCorpAccountSecret
|
||||||
|
}
|
||||||
|
if cleanUser.WeChatCorpAccountAgentId != "" {
|
||||||
|
newWeChatCorpAccountTokenStoreItem.AgentId = cleanUser.WeChatCorpAccountAgentId
|
||||||
|
}
|
||||||
|
TokenStoreRemoveItem(&oldWeChatCorpAccountTokenStoreItem)
|
||||||
|
TokenStoreAddItem(&newWeChatCorpAccountTokenStoreItem)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TokenStoreRemoveUser user must be filled
|
||||||
|
func TokenStoreRemoveUser(user *model.User) {
|
||||||
|
// TODO: check if this token is shared!
|
||||||
|
testAccountTokenStoreItem := WeChatTestAccountTokenStoreItem{
|
||||||
|
AppID: user.WeChatTestAccountId,
|
||||||
|
AppSecret: user.WeChatTestAccountSecret,
|
||||||
|
}
|
||||||
|
TokenStoreRemoveItem(&testAccountTokenStoreItem)
|
||||||
|
corpAccountTokenStoreItem := WeChatCorpAccountTokenStoreItem{
|
||||||
|
CorpId: user.WeChatCorpAccountId,
|
||||||
|
CorpSecret: user.WeChatCorpAccountSecret,
|
||||||
|
AgentId: user.WeChatCorpAccountAgentId,
|
||||||
|
}
|
||||||
|
TokenStoreRemoveItem(&corpAccountTokenStoreItem)
|
||||||
|
}
|
||||||
|
|
||||||
func TokenStoreGetToken(key string) string {
|
func TokenStoreGetToken(key string) string {
|
||||||
s.Mutex.RLock()
|
s.Mutex.RLock()
|
||||||
defer s.Mutex.RUnlock()
|
defer s.Mutex.RUnlock()
|
||||||
|
|||||||
+33
-6
@@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"message-pusher/channel"
|
||||||
"message-pusher/common"
|
"message-pusher/common"
|
||||||
"message-pusher/model"
|
"message-pusher/model"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -376,13 +377,35 @@ func UpdateSelf(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
originUser, err := model.GetUserById(c.GetInt("id"), true)
|
||||||
cleanUser := model.User{
|
if err != nil {
|
||||||
Id: c.GetInt("id"),
|
c.JSON(http.StatusOK, gin.H{
|
||||||
Username: user.Username,
|
"success": false,
|
||||||
Password: user.Password,
|
"message": err.Error(),
|
||||||
DisplayName: user.DisplayName,
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
cleanUser := model.User{
|
||||||
|
Id: c.GetInt("id"),
|
||||||
|
Username: user.Username,
|
||||||
|
Password: user.Password,
|
||||||
|
WeChatTestAccountId: user.WeChatTestAccountId,
|
||||||
|
WeChatTestAccountSecret: user.WeChatTestAccountSecret,
|
||||||
|
WeChatTestAccountTemplateId: user.WeChatTestAccountTemplateId,
|
||||||
|
WeChatTestAccountOpenId: user.WeChatTestAccountOpenId,
|
||||||
|
WeChatTestAccountVerificationToken: user.WeChatTestAccountVerificationToken,
|
||||||
|
WeChatCorpAccountId: user.WeChatCorpAccountId,
|
||||||
|
WeChatCorpAccountSecret: user.WeChatCorpAccountSecret,
|
||||||
|
WeChatCorpAccountAgentId: user.WeChatCorpAccountAgentId,
|
||||||
|
WeChatCorpAccountUserId: user.WeChatCorpAccountUserId,
|
||||||
|
WeChatCorpAccountClientType: user.WeChatCorpAccountClientType,
|
||||||
|
LarkWebhookURL: user.LarkWebhookURL,
|
||||||
|
LarkWebhookSecret: user.LarkWebhookSecret,
|
||||||
|
DingWebhookURL: user.DingWebhookURL,
|
||||||
|
DingWebhookSecret: user.DingWebhookSecret,
|
||||||
|
}
|
||||||
|
channel.TokenStoreUpdateUser(&cleanUser, originUser)
|
||||||
|
|
||||||
if user.Password == "$I_LOVE_U" {
|
if user.Password == "$I_LOVE_U" {
|
||||||
user.Password = "" // rollback to what it should be
|
user.Password = "" // rollback to what it should be
|
||||||
cleanUser.Password = ""
|
cleanUser.Password = ""
|
||||||
@@ -428,6 +451,7 @@ func DeleteUser(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
channel.TokenStoreRemoveUser(originUser)
|
||||||
err = model.DeleteUserById(id)
|
err = model.DeleteUserById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
@@ -440,6 +464,9 @@ func DeleteUser(c *gin.Context) {
|
|||||||
|
|
||||||
func DeleteSelf(c *gin.Context) {
|
func DeleteSelf(c *gin.Context) {
|
||||||
id := c.GetInt("id")
|
id := c.GetInt("id")
|
||||||
|
user := model.User{Id: id}
|
||||||
|
user.FillUserById()
|
||||||
|
channel.TokenStoreRemoveUser(&user)
|
||||||
err := model.DeleteUserById(id)
|
err := model.DeleteUserById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
|||||||
+2
-4
@@ -71,8 +71,7 @@ func GetUserById(id int, selectAll bool) (*User, error) {
|
|||||||
|
|
||||||
func DeleteUserById(id int) (err error) {
|
func DeleteUserById(id int) (err error) {
|
||||||
user := User{Id: id}
|
user := User{Id: id}
|
||||||
err = DB.Delete(&user).Error
|
return user.Delete()
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) Insert() error {
|
func (user *User) Insert() error {
|
||||||
@@ -100,8 +99,7 @@ func (user *User) Update(updatePassword bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (user *User) Delete() error {
|
func (user *User) Delete() error {
|
||||||
var err error
|
err := DB.Delete(user).Error
|
||||||
err = DB.Delete(user).Error
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user