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:
parent
fb5b2f52c7
commit
ef5b49ebd8
File diff suppressed because it is too large
Load Diff
@ -112,3 +112,7 @@ func (m *UserServiceMock) GenerateResetToken(user *models.User) (*models.User, e
|
||||
func (m *UserServiceMock) FlushCache() {
|
||||
m.Called()
|
||||
}
|
||||
|
||||
func (m *UserServiceMock) FlushUserCache(s string) {
|
||||
m.Called(s)
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"github.com/emvi/logbuch"
|
||||
"github.com/gorilla/mux"
|
||||
conf "github.com/muety/wakapi/config"
|
||||
"github.com/muety/wakapi/middlewares"
|
||||
"github.com/muety/wakapi/models"
|
||||
"github.com/muety/wakapi/models/view"
|
||||
"github.com/muety/wakapi/services"
|
||||
@ -31,13 +32,20 @@ func NewLoginHandler(userService services.IUserService, mailService services.IMa
|
||||
func (h *LoginHandler) RegisterRoutes(router *mux.Router) {
|
||||
router.Path("/login").Methods(http.MethodGet).HandlerFunc(h.GetIndex)
|
||||
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.MethodPost).HandlerFunc(h.PostSignup)
|
||||
router.Path("/set-password").Methods(http.MethodGet).HandlerFunc(h.GetSetPassword)
|
||||
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.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) {
|
||||
@ -108,6 +116,9 @@ func (h *LoginHandler) PostLogout(w http.ResponseWriter, r *http.Request) {
|
||||
loadTemplates()
|
||||
}
|
||||
|
||||
if user := middlewares.GetPrincipal(r); user != nil {
|
||||
h.userSrvc.FlushUserCache(user.ID)
|
||||
}
|
||||
http.SetCookie(w, h.config.GetClearCookie(models.AuthCookieKey))
|
||||
http.Redirect(w, r, fmt.Sprintf("%s/", h.config.Server.BasePath), http.StatusFound)
|
||||
}
|
||||
|
@ -286,7 +286,7 @@ func (h *SettingsHandler) actionUpdateSharing(w http.ResponseWriter, r *http.Req
|
||||
var err error
|
||||
user := middlewares.GetPrincipal(r)
|
||||
|
||||
defer h.userSrvc.FlushCache()
|
||||
defer h.userSrvc.FlushUserCache(user.ID)
|
||||
|
||||
user.ShareProjects, err = strconv.ParseBool(r.PostFormValue("share_projects"))
|
||||
user.ShareLanguages, err = strconv.ParseBool(r.PostFormValue("share_languages"))
|
||||
|
@ -141,4 +141,5 @@ type IUserService interface {
|
||||
MigrateMd5Password(*models.User, *models.Login) (*models.User, error)
|
||||
GenerateResetToken(*models.User) (*models.User, error)
|
||||
FlushCache()
|
||||
FlushUserCache(string)
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ func (srv *UserService) GetUserById(userId string) (*models.User, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
srv.cache.Set(u.ID, u, cache.DefaultExpiration)
|
||||
srv.cache.SetDefault(u.ID, u)
|
||||
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) {
|
||||
srv.cache.Flush()
|
||||
srv.FlushUserCache(user.ID)
|
||||
srv.notifyUpdate(user)
|
||||
return srv.repository.Update(user)
|
||||
}
|
||||
|
||||
func (srv *UserService) ResetApiKey(user *models.User) (*models.User, error) {
|
||||
srv.cache.Flush()
|
||||
srv.FlushUserCache(user.ID)
|
||||
user.ApiKey = uuid.NewV4().String()
|
||||
return srv.Update(user)
|
||||
}
|
||||
|
||||
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 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) {
|
||||
srv.cache.Flush()
|
||||
srv.FlushUserCache(user.ID)
|
||||
user.Password = login.Password
|
||||
if hash, err := utils.HashBcrypt(user.Password, srv.config.Security.PasswordSalt); err != nil {
|
||||
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 {
|
||||
srv.cache.Flush()
|
||||
srv.FlushUserCache(user.ID)
|
||||
|
||||
user.ReportsWeekly = false
|
||||
srv.notifyUpdate(user)
|
||||
@ -222,6 +222,10 @@ func (srv *UserService) FlushCache() {
|
||||
srv.cache.Flush()
|
||||
}
|
||||
|
||||
func (srv *UserService) FlushUserCache(userId string) {
|
||||
srv.cache.Delete(userId)
|
||||
}
|
||||
|
||||
func (srv *UserService) notifyUpdate(user *models.User) {
|
||||
srv.eventBus.Publish(hub.Message{
|
||||
Name: config.EventUserUpdate,
|
||||
|
Loading…
Reference in New Issue
Block a user