diff --git a/cmd/cancel2FA.go b/cmd/cancel2FA.go index c7dc5270..888b0f06 100644 --- a/cmd/cancel2FA.go +++ b/cmd/cancel2FA.go @@ -24,6 +24,7 @@ var Cancel2FACmd = &cobra.Command{ utils.Log.Errorf("failed to cancel 2FA: %+v", err) } else { utils.Log.Info("2FA canceled") + DelAdminCacheOnline() } } }, diff --git a/cmd/user.go b/cmd/user.go new file mode 100644 index 00000000..1e8504ae --- /dev/null +++ b/cmd/user.go @@ -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) +} diff --git a/internal/op/user.go b/internal/op/user.go index a0bbc3ed..79e73db8 100644 --- a/internal/op/user.go +++ b/internal/op/user.go @@ -113,3 +113,18 @@ func Cancel2FAById(id uint) error { } 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 +} diff --git a/server/handles/auth.go b/server/handles/auth.go index 9bbbf3e8..acc8c95b 100644 --- a/server/handles/auth.go +++ b/server/handles/auth.go @@ -26,6 +26,7 @@ type LoginReq struct { OtpCode string `json:"otp_code"` } +// Login Deprecated func Login(c *gin.Context) { // check count of login ip := c.ClientIP() diff --git a/server/handles/user.go b/server/handles/user.go index adb29cdb..b06b4254 100644 --- a/server/handles/user.go +++ b/server/handles/user.go @@ -120,3 +120,13 @@ func Cancel2FAById(c *gin.Context) { } 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) +} diff --git a/server/router.go b/server/router.go index f36097a3..871658c1 100644 --- a/server/router.go +++ b/server/router.go @@ -84,6 +84,7 @@ func admin(g *gin.RouterGroup) { user.POST("/update", handles.UpdateUser) user.POST("/cancel_2fa", handles.Cancel2FAById) user.POST("/delete", handles.DeleteUser) + user.POST("/del_cache", handles.DelUserCache) storage := g.Group("/storage") storage.GET("/list", handles.ListStorages)