1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

chore: clear user cache upon logout

This commit is contained in:
Ferdinand Mütsch 2023-01-02 14:53:21 +01:00
parent fb5b2f52c7
commit ef5b49ebd8
6 changed files with 763 additions and 742 deletions

File diff suppressed because it is too large Load Diff

View File

@ -112,3 +112,7 @@ func (m *UserServiceMock) GenerateResetToken(user *models.User) (*models.User, e
func (m *UserServiceMock) FlushCache() { func (m *UserServiceMock) FlushCache() {
m.Called() m.Called()
} }
func (m *UserServiceMock) FlushUserCache(s string) {
m.Called(s)
}

View File

@ -5,6 +5,7 @@ import (
"github.com/emvi/logbuch" "github.com/emvi/logbuch"
"github.com/gorilla/mux" "github.com/gorilla/mux"
conf "github.com/muety/wakapi/config" conf "github.com/muety/wakapi/config"
"github.com/muety/wakapi/middlewares"
"github.com/muety/wakapi/models" "github.com/muety/wakapi/models"
"github.com/muety/wakapi/models/view" "github.com/muety/wakapi/models/view"
"github.com/muety/wakapi/services" "github.com/muety/wakapi/services"
@ -31,13 +32,20 @@ func NewLoginHandler(userService services.IUserService, mailService services.IMa
func (h *LoginHandler) RegisterRoutes(router *mux.Router) { func (h *LoginHandler) RegisterRoutes(router *mux.Router) {
router.Path("/login").Methods(http.MethodGet).HandlerFunc(h.GetIndex) router.Path("/login").Methods(http.MethodGet).HandlerFunc(h.GetIndex)
router.Path("/login").Methods(http.MethodPost).HandlerFunc(h.PostLogin) router.Path("/login").Methods(http.MethodPost).HandlerFunc(h.PostLogin)
router.Path("/logout").Methods(http.MethodPost).HandlerFunc(h.PostLogout)
router.Path("/signup").Methods(http.MethodGet).HandlerFunc(h.GetSignup) router.Path("/signup").Methods(http.MethodGet).HandlerFunc(h.GetSignup)
router.Path("/signup").Methods(http.MethodPost).HandlerFunc(h.PostSignup) router.Path("/signup").Methods(http.MethodPost).HandlerFunc(h.PostSignup)
router.Path("/set-password").Methods(http.MethodGet).HandlerFunc(h.GetSetPassword) router.Path("/set-password").Methods(http.MethodGet).HandlerFunc(h.GetSetPassword)
router.Path("/set-password").Methods(http.MethodPost).HandlerFunc(h.PostSetPassword) router.Path("/set-password").Methods(http.MethodPost).HandlerFunc(h.PostSetPassword)
router.Path("/reset-password").Methods(http.MethodGet).HandlerFunc(h.GetResetPassword) router.Path("/reset-password").Methods(http.MethodGet).HandlerFunc(h.GetResetPassword)
router.Path("/reset-password").Methods(http.MethodPost).HandlerFunc(h.PostResetPassword) router.Path("/reset-password").Methods(http.MethodPost).HandlerFunc(h.PostResetPassword)
authMiddleware := middlewares.NewAuthenticateMiddleware(h.userSrvc).
WithRedirectTarget(defaultErrorRedirectTarget()).
WithOptionalFor([]string{"/logout"})
logoutRouter := router.PathPrefix("/logout").Subrouter()
logoutRouter.Use(authMiddleware.Handler)
logoutRouter.Path("").Methods(http.MethodPost).HandlerFunc(h.PostLogout)
} }
func (h *LoginHandler) GetIndex(w http.ResponseWriter, r *http.Request) { func (h *LoginHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
@ -108,6 +116,9 @@ func (h *LoginHandler) PostLogout(w http.ResponseWriter, r *http.Request) {
loadTemplates() loadTemplates()
} }
if user := middlewares.GetPrincipal(r); user != nil {
h.userSrvc.FlushUserCache(user.ID)
}
http.SetCookie(w, h.config.GetClearCookie(models.AuthCookieKey)) http.SetCookie(w, h.config.GetClearCookie(models.AuthCookieKey))
http.Redirect(w, r, fmt.Sprintf("%s/", h.config.Server.BasePath), http.StatusFound) http.Redirect(w, r, fmt.Sprintf("%s/", h.config.Server.BasePath), http.StatusFound)
} }

View File

@ -286,7 +286,7 @@ func (h *SettingsHandler) actionUpdateSharing(w http.ResponseWriter, r *http.Req
var err error var err error
user := middlewares.GetPrincipal(r) user := middlewares.GetPrincipal(r)
defer h.userSrvc.FlushCache() defer h.userSrvc.FlushUserCache(user.ID)
user.ShareProjects, err = strconv.ParseBool(r.PostFormValue("share_projects")) user.ShareProjects, err = strconv.ParseBool(r.PostFormValue("share_projects"))
user.ShareLanguages, err = strconv.ParseBool(r.PostFormValue("share_languages")) user.ShareLanguages, err = strconv.ParseBool(r.PostFormValue("share_languages"))

View File

@ -141,4 +141,5 @@ type IUserService interface {
MigrateMd5Password(*models.User, *models.Login) (*models.User, error) MigrateMd5Password(*models.User, *models.Login) (*models.User, error)
GenerateResetToken(*models.User) (*models.User, error) GenerateResetToken(*models.User) (*models.User, error)
FlushCache() FlushCache()
FlushUserCache(string)
} }

View File

@ -67,7 +67,7 @@ func (srv *UserService) GetUserById(userId string) (*models.User, error) {
return nil, err return nil, err
} }
srv.cache.Set(u.ID, u, cache.DefaultExpiration) srv.cache.SetDefault(u.ID, u)
return u, nil return u, nil
} }
@ -167,19 +167,19 @@ func (srv *UserService) CreateOrGet(signup *models.Signup, isAdmin bool) (*model
} }
func (srv *UserService) Update(user *models.User) (*models.User, error) { func (srv *UserService) Update(user *models.User) (*models.User, error) {
srv.cache.Flush() srv.FlushUserCache(user.ID)
srv.notifyUpdate(user) srv.notifyUpdate(user)
return srv.repository.Update(user) return srv.repository.Update(user)
} }
func (srv *UserService) ResetApiKey(user *models.User) (*models.User, error) { func (srv *UserService) ResetApiKey(user *models.User) (*models.User, error) {
srv.cache.Flush() srv.FlushUserCache(user.ID)
user.ApiKey = uuid.NewV4().String() user.ApiKey = uuid.NewV4().String()
return srv.Update(user) return srv.Update(user)
} }
func (srv *UserService) SetWakatimeApiCredentials(user *models.User, apiKey string, apiUrl string) (*models.User, error) { func (srv *UserService) SetWakatimeApiCredentials(user *models.User, apiKey string, apiUrl string) (*models.User, error) {
srv.cache.Flush() srv.FlushUserCache(user.ID)
if apiKey != user.WakatimeApiKey { if apiKey != user.WakatimeApiKey {
if u, err := srv.repository.UpdateField(user, "wakatime_api_key", apiKey); err != nil { if u, err := srv.repository.UpdateField(user, "wakatime_api_key", apiKey); err != nil {
@ -195,7 +195,7 @@ func (srv *UserService) SetWakatimeApiCredentials(user *models.User, apiKey stri
} }
func (srv *UserService) MigrateMd5Password(user *models.User, login *models.Login) (*models.User, error) { func (srv *UserService) MigrateMd5Password(user *models.User, login *models.Login) (*models.User, error) {
srv.cache.Flush() srv.FlushUserCache(user.ID)
user.Password = login.Password user.Password = login.Password
if hash, err := utils.HashBcrypt(user.Password, srv.config.Security.PasswordSalt); err != nil { if hash, err := utils.HashBcrypt(user.Password, srv.config.Security.PasswordSalt); err != nil {
return nil, err return nil, err
@ -210,7 +210,7 @@ func (srv *UserService) GenerateResetToken(user *models.User) (*models.User, err
} }
func (srv *UserService) Delete(user *models.User) error { func (srv *UserService) Delete(user *models.User) error {
srv.cache.Flush() srv.FlushUserCache(user.ID)
user.ReportsWeekly = false user.ReportsWeekly = false
srv.notifyUpdate(user) srv.notifyUpdate(user)
@ -222,6 +222,10 @@ func (srv *UserService) FlushCache() {
srv.cache.Flush() srv.cache.Flush()
} }
func (srv *UserService) FlushUserCache(userId string) {
srv.cache.Delete(userId)
}
func (srv *UserService) notifyUpdate(user *models.User) { func (srv *UserService) notifyUpdate(user *models.User) {
srv.eventBus.Publish(hub.Message{ srv.eventBus.Publish(hub.Message{
Name: config.EventUserUpdate, Name: config.EventUserUpdate,