A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
feat(role): log out user when user was changed role
LinkinStar5088c3a9efc3
diff --git a/cmd/answer/wire_gen.go b/cmd/answer/wire_gen.goindex fcb4ca2a..e0747ba3 100644--- a/cmd/answer/wire_gen.go+++ b/cmd/answer/wire_gen.go@@ -179,7 +179,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,reportBackyardService := report_backyard.NewReportBackyardService(reportRepo, userCommon, commonRepo, answerRepo, questionRepo, commentCommonRepo, reportHandle, configRepo)controller_backyardReportController := controller_backyard.NewReportController(reportBackyardService)userBackyardRepo := user.NewUserBackyardRepo(dataData, authRepo)- userBackyardService := user_backyard.NewUserBackyardService(userBackyardRepo, userRoleRelService)+ userBackyardService := user_backyard.NewUserBackyardService(userBackyardRepo, userRoleRelService, authService)userBackyardController := controller_backyard.NewUserBackyardController(userBackyardService)reasonRepo := reason.NewReasonRepo(configRepo)reasonService := reason2.NewReasonService(reasonRepo)diff --git a/internal/base/constant/constant.go b/internal/base/constant/constant.goindex c36a0f7f..68f4316c 100644--- a/internal/base/constant/constant.go+++ b/internal/base/constant/constant.go@@ -11,6 +11,7 @@ const (AdminTokenCacheKey = "answer:admin:token:"AdminTokenCacheTime = 7 * 24 * time.HourAcceptLanguageFlag = "Accept-Language"+ UserTokenMappingCacheKey = "answer:user-token:mapping:")const (@@ -57,4 +58,3 @@ const (SiteTypeWrite = "write"SiteTypeLegal = "legal")-diff --git a/internal/repo/auth/auth.go b/internal/repo/auth/auth.goindex 9f5a1813..6a5dc61e 100644--- a/internal/repo/auth/auth.go+++ b/internal/repo/auth/auth.go@@ -10,6 +10,7 @@ import ("github.com/answerdev/answer/internal/entity""github.com/answerdev/answer/internal/service/auth""github.com/segmentfault/pacman/errors"+ "github.com/segmentfault/pacman/log")// authRepo auth repository@@ -42,6 +43,9 @@ func (ar *authRepo) SetUserCacheInfo(ctx context.Context, accessToken string, usif err != nil {return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()}+ if err := ar.AddUserTokenMapping(ctx, userInfo.UserID, accessToken); err != nil {+ log.Error(err)+ }return nil}@@ -130,6 +134,45 @@ func (ar *authRepo) RemoveBackyardUserCacheInfo(ctx context.Context, accessTokenreturn nil}+// AddUserTokenMapping add user token mapping+func (ar *authRepo) AddUserTokenMapping(ctx context.Context, userID, accessToken string) (err error) {+ key := constant.UserTokenMappingCacheKey + userID+ resp, _ := ar.data.Cache.GetString(ctx, key)+ mapping := make(map[string]bool, 0)+ if len(resp) > 0 {+ _ = json.Unmarshal([]byte(resp), &mapping)+ }+ mapping[accessToken] = true+ content, _ := json.Marshal(mapping)+ return ar.data.Cache.SetString(ctx, key, string(content), constant.UserTokenCacheTime)+}++// RemoveAllUserTokens Log out all users under this user id+func (ar *authRepo) RemoveAllUserTokens(ctx context.Context, userID string) {+ key := constant.UserTokenMappingCacheKey + userID+ resp, _ := ar.data.Cache.GetString(ctx, key)+ mapping := make(map[string]bool, 0)+ if len(resp) > 0 {+ _ = json.Unmarshal([]byte(resp), &mapping)+ log.Debugf("find %d user tokens by user id %s", len(mapping), userID)+ }++ for token := range mapping {+ if err := ar.RemoveUserCacheInfo(ctx, token); err != nil {+ log.Error(err)+ } else {+ log.Debugf("del user %s token success")+ }+ }+ if err := ar.RemoveUserStatus(ctx, userID); err != nil {+ log.Error(err)+ }+ if err := ar.data.Cache.Del(ctx, key); err != nil {+ log.Error(err)+ }+ return+}+// NewAuthRepo new repositoryfunc NewAuthRepo(data *data.Data) auth.AuthRepo {return &authRepo{diff --git a/internal/service/auth/auth.go b/internal/service/auth/auth.goindex 1f77d174..9f3bbb75 100644--- a/internal/service/auth/auth.go+++ b/internal/service/auth/auth.go@@ -19,6 +19,8 @@ type AuthRepo interface {GetBackyardUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error)SetBackyardUserCacheInfo(ctx context.Context, accessToken string, userInfo *entity.UserCacheInfo) errorRemoveBackyardUserCacheInfo(ctx context.Context, accessToken string) (err error)+ AddUserTokenMapping(ctx context.Context, userID, accessToken string) (err error)+ RemoveAllUserTokens(ctx context.Context, userID string)}// AuthService kit service@@ -78,6 +80,16 @@ func (as *AuthService) RemoveUserCacheInfo(ctx context.Context, accessToken strireturn as.authRepo.RemoveUserCacheInfo(ctx, accessToken)}+// AddUserTokenMapping add user token mapping+func (as *AuthService) AddUserTokenMapping(ctx context.Context, userID, accessToken string) (err error) {+ return as.authRepo.AddUserTokenMapping(ctx, userID, accessToken)+}++// RemoveAllUserTokens Log out all users under this user id+func (as *AuthService) RemoveAllUserTokens(ctx context.Context, userID string) {+ as.authRepo.RemoveAllUserTokens(ctx, userID)+}+//cmsfunc (as *AuthService) GetCmsUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error) {diff --git a/internal/service/user_backyard/user_backyard.go b/internal/service/user_backyard/user_backyard.goindex f3689a56..8bf0e2e7 100644--- a/internal/service/user_backyard/user_backyard.go+++ b/internal/service/user_backyard/user_backyard.go@@ -12,6 +12,7 @@ import ("github.com/answerdev/answer/internal/base/reason""github.com/answerdev/answer/internal/entity""github.com/answerdev/answer/internal/schema"+ "github.com/answerdev/answer/internal/service/auth""github.com/answerdev/answer/internal/service/role""github.com/jinzhu/copier""github.com/segmentfault/pacman/errors"@@ -30,16 +31,19 @@ type UserBackyardRepo interface {type UserBackyardService struct {userRepo UserBackyardRepouserRoleRelService *role.UserRoleRelService+ authService *auth.AuthService}// NewUserBackyardService new user backyard servicefunc NewUserBackyardService(userRepo UserBackyardRepo,userRoleRelService *role.UserRoleRelService,+ authService *auth.AuthService,) *UserBackyardService {return &UserBackyardService{userRepo: userRepo,userRoleRelService: userRoleRelService,+ authService: authService,}}@@ -80,7 +84,14 @@ func (us *UserBackyardService) UpdateUserRole(ctx context.Context, req *schema.Uif req.UserID == req.LoginUserID {return errors.BadRequest(reason.UserCannotUpdateYourRole)}- return us.userRoleRelService.SaveUserRole(ctx, req.UserID, req.RoleID)++ err = us.userRoleRelService.SaveUserRole(ctx, req.UserID, req.RoleID)+ if err != nil {+ return err+ }++ us.authService.RemoveAllUserTokens(ctx, req.UserID)+ return}// GetUserInfo get user one