perf: delete user cache after cancel `2FA`

pull/4939/head
Andy Hsu 2023-08-06 20:47:58 +08:00
parent 1d06a0019f
commit 30415cefbe
6 changed files with 80 additions and 0 deletions

View File

@ -24,6 +24,7 @@ var Cancel2FACmd = &cobra.Command{
utils.Log.Errorf("failed to cancel 2FA: %+v", err) utils.Log.Errorf("failed to cancel 2FA: %+v", err)
} else { } else {
utils.Log.Info("2FA canceled") utils.Log.Info("2FA canceled")
DelAdminCacheOnline()
} }
} }
}, },

52
cmd/user.go Normal file
View File

@ -0,0 +1,52 @@
package cmd
import (
"crypto/tls"
"fmt"
"time"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/op"
"github.com/alist-org/alist/v3/internal/setting"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/go-resty/resty/v2"
)
func DelAdminCacheOnline() {
admin, err := op.GetAdmin()
if err != nil {
utils.Log.Errorf("[del_admin_cache] get admin error: %+v", err)
return
}
DelUserCacheOnline(admin.Username)
}
func DelUserCacheOnline(username string) {
client := resty.New().SetTimeout(3 * time.Second).SetTLSClientConfig(&tls.Config{InsecureSkipVerify: conf.Conf.TlsInsecureSkipVerify})
token := setting.GetStr(conf.Token)
port := conf.Conf.Scheme.HttpPort
u := fmt.Sprintf("http://localhost:%d/api/admin/user/del_cache", port)
if port == -1 {
if conf.Conf.Scheme.HttpsPort == -1 {
utils.Log.Infof("[del_user_cache] no open port")
return
}
u = fmt.Sprintf("https://localhost:%d/api/admin/user/del_cache", conf.Conf.Scheme.HttpsPort)
}
res, err := client.R().SetHeader("Authorization", token).SetQueryParam("username", username).Post(u)
if err != nil {
utils.Log.Errorf("[del_user_cache] del cache error: %+v", err)
return
}
if res.StatusCode() != 200 {
utils.Log.Errorf("[del_user_cache] del cache error: %+v", res.String())
return
}
code := utils.Json.Get(res.Body(), "code").ToInt()
msg := utils.Json.Get(res.Body(), "message").ToString()
if code != 200 {
utils.Log.Errorf("[del_user_cache] del cache error: %s", msg)
return
}
utils.Log.Infof("[del_user_cache] del user [%s] cache success", username)
}

View File

@ -113,3 +113,18 @@ func Cancel2FAById(id uint) error {
} }
return Cancel2FAByUser(user) return Cancel2FAByUser(user)
} }
func DelUserCache(username string) error {
user, err := GetUserByName(username)
if err != nil {
return err
}
if user.IsAdmin() {
adminUser = nil
}
if user.IsGuest() {
guestUser = nil
}
userCache.Del(username)
return nil
}

View File

@ -26,6 +26,7 @@ type LoginReq struct {
OtpCode string `json:"otp_code"` OtpCode string `json:"otp_code"`
} }
// Login Deprecated
func Login(c *gin.Context) { func Login(c *gin.Context) {
// check count of login // check count of login
ip := c.ClientIP() ip := c.ClientIP()

View File

@ -120,3 +120,13 @@ func Cancel2FAById(c *gin.Context) {
} }
common.SuccessResp(c) common.SuccessResp(c)
} }
func DelUserCache(c *gin.Context) {
username := c.Query("username")
err := op.DelUserCache(username)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}

View File

@ -84,6 +84,7 @@ func admin(g *gin.RouterGroup) {
user.POST("/update", handles.UpdateUser) user.POST("/update", handles.UpdateUser)
user.POST("/cancel_2fa", handles.Cancel2FAById) user.POST("/cancel_2fa", handles.Cancel2FAById)
user.POST("/delete", handles.DeleteUser) user.POST("/delete", handles.DeleteUser)
user.POST("/del_cache", handles.DelUserCache)
storage := g.Group("/storage") storage := g.Group("/storage")
storage.GET("/list", handles.ListStorages) storage.GET("/list", handles.ListStorages)