This is an automated email from the ASF dual-hosted git repository.
linkinstar pushed a commit to branch feat/1.6.0/user
in repository https://gitbox.apache.org/repos/asf/answer.git
The following commit(s) were added to refs/heads/feat/1.6.0/user by this push:
new 6d02706d feat(user): add user suspension functionality
6d02706d is described below
commit 6d02706d4db67e735d0b69554c21b4417f1c7c67
Author: LinkinStars <[email protected]>
AuthorDate: Thu Jun 26 14:41:32 2025 +0800
feat(user): add user suspension functionality
---
cmd/wire_gen.go | 2 +-
docs/docs.go | 58 ++++++++++++++++++++++++++++
docs/swagger.json | 58 ++++++++++++++++++++++++++++
docs/swagger.yaml | 45 +++++++++++++++++++++
internal/base/cron/cron.go | 17 ++++++++
internal/entity/user_entity.go | 4 ++
internal/migrations/v27.go | 14 ++++++-
internal/repo/user/user_backyard_repo.go | 25 +++++++++++-
internal/schema/backyard_user_schema.go | 43 ++++++++++++++++++++-
internal/schema/user_schema.go | 32 ++++++++++-----
internal/service/user_admin/user_backyard.go | 41 +++++++++++++++++++-
internal/service/user_common/user.go | 3 ++
12 files changed, 326 insertions(+), 16 deletions(-)
diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go
index f98deb42..947b6605 100644
--- a/cmd/wire_gen.go
+++ b/cmd/wire_gen.go
@@ -291,7 +291,7 @@ func initApplication(debug bool, serverConf *conf.Server,
dbConf *data.Database,
renderController := controller.NewRenderController()
pluginAPIRouter := router.NewPluginAPIRouter(connectorController,
userCenterController, captchaController, embedController, renderController)
ginEngine := server.NewHTTPServer(debug, staticRouter, answerAPIRouter,
swaggerRouter, uiRouter, authUserMiddleware, avatarMiddleware,
shortIDMiddleware, templateRouter, pluginAPIRouter, uiConf)
- scheduledTaskManager :=
cron.NewScheduledTaskManager(siteInfoCommonService, questionService,
fileRecordService, serviceConf)
+ scheduledTaskManager :=
cron.NewScheduledTaskManager(siteInfoCommonService, questionService,
fileRecordService, userAdminService, serviceConf)
application := newApplication(serverConf, ginEngine,
scheduledTaskManager)
return application, func() {
cleanup2()
diff --git a/docs/docs.go b/docs/docs.go
index 76750aee..48c8b1b4 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -8755,6 +8755,10 @@ const docTemplate = `{
"description": "user status",
"type": "string"
},
+ "suspended_until": {
+ "description": "suspended until timestamp",
+ "type": "integer"
+ },
"username": {
"description": "username",
"type": "string"
@@ -8871,6 +8875,10 @@ const docTemplate = `{
"status_msg": {
"type": "string"
},
+ "suspended_until": {
+ "description": "suspended until timestamp",
+ "type": "integer"
+ },
"username": {
"description": "username",
"type": "string"
@@ -9520,6 +9528,10 @@ const docTemplate = `{
"description": "suspended time",
"type": "integer"
},
+ "suspended_until": {
+ "description": "suspended until time",
+ "type": "integer"
+ },
"user_id": {
"description": "user id",
"type": "string"
@@ -10696,10 +10708,21 @@ const docTemplate = `{
"schema.SiteInterfaceReq": {
"type": "object",
"required": [
+ "default_avatar",
"language",
"time_zone"
],
"properties": {
+ "default_avatar": {
+ "type": "string",
+ "enum": [
+ "system",
+ "gravatar"
+ ]
+ },
+ "gravatar_base_url": {
+ "type": "string"
+ },
"language": {
"type": "string",
"maxLength": 128
@@ -10713,10 +10736,21 @@ const docTemplate = `{
"schema.SiteInterfaceResp": {
"type": "object",
"required": [
+ "default_avatar",
"language",
"time_zone"
],
"properties": {
+ "default_avatar": {
+ "type": "string",
+ "enum": [
+ "system",
+ "gravatar"
+ ]
+ },
+ "gravatar_base_url": {
+ "type": "string"
+ },
"language": {
"type": "string",
"maxLength": 128
@@ -11621,6 +11655,22 @@ const docTemplate = `{
"inactive"
]
},
+ "suspend_duration": {
+ "type": "string",
+ "enum": [
+ "24h",
+ "48h",
+ "72h",
+ "7d",
+ "14d",
+ "1m",
+ "2m",
+ "3m",
+ "6m",
+ "1y",
+ "forever"
+ ]
+ },
"user_id": {
"type": "string"
}
@@ -11650,6 +11700,10 @@ const docTemplate = `{
"status": {
"type": "string"
},
+ "suspended_until": {
+ "description": "suspended until timestamp",
+ "type": "integer"
+ },
"username": {
"type": "string"
},
@@ -11812,6 +11866,10 @@ const docTemplate = `{
"description": "user status",
"type": "string"
},
+ "suspended_until": {
+ "description": "suspended until timestamp",
+ "type": "integer"
+ },
"username": {
"description": "username",
"type": "string"
diff --git a/docs/swagger.json b/docs/swagger.json
index 4682f67c..768869c3 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -8728,6 +8728,10 @@
"description": "user status",
"type": "string"
},
+ "suspended_until": {
+ "description": "suspended until timestamp",
+ "type": "integer"
+ },
"username": {
"description": "username",
"type": "string"
@@ -8844,6 +8848,10 @@
"status_msg": {
"type": "string"
},
+ "suspended_until": {
+ "description": "suspended until timestamp",
+ "type": "integer"
+ },
"username": {
"description": "username",
"type": "string"
@@ -9493,6 +9501,10 @@
"description": "suspended time",
"type": "integer"
},
+ "suspended_until": {
+ "description": "suspended until time",
+ "type": "integer"
+ },
"user_id": {
"description": "user id",
"type": "string"
@@ -10669,10 +10681,21 @@
"schema.SiteInterfaceReq": {
"type": "object",
"required": [
+ "default_avatar",
"language",
"time_zone"
],
"properties": {
+ "default_avatar": {
+ "type": "string",
+ "enum": [
+ "system",
+ "gravatar"
+ ]
+ },
+ "gravatar_base_url": {
+ "type": "string"
+ },
"language": {
"type": "string",
"maxLength": 128
@@ -10686,10 +10709,21 @@
"schema.SiteInterfaceResp": {
"type": "object",
"required": [
+ "default_avatar",
"language",
"time_zone"
],
"properties": {
+ "default_avatar": {
+ "type": "string",
+ "enum": [
+ "system",
+ "gravatar"
+ ]
+ },
+ "gravatar_base_url": {
+ "type": "string"
+ },
"language": {
"type": "string",
"maxLength": 128
@@ -11594,6 +11628,22 @@
"inactive"
]
},
+ "suspend_duration": {
+ "type": "string",
+ "enum": [
+ "24h",
+ "48h",
+ "72h",
+ "7d",
+ "14d",
+ "1m",
+ "2m",
+ "3m",
+ "6m",
+ "1y",
+ "forever"
+ ]
+ },
"user_id": {
"type": "string"
}
@@ -11623,6 +11673,10 @@
"status": {
"type": "string"
},
+ "suspended_until": {
+ "description": "suspended until timestamp",
+ "type": "integer"
+ },
"username": {
"type": "string"
},
@@ -11785,6 +11839,10 @@
"description": "user status",
"type": "string"
},
+ "suspended_until": {
+ "description": "suspended until timestamp",
+ "type": "integer"
+ },
"username": {
"description": "username",
"type": "string"
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index 3d68f8a6..96042f5f 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -873,6 +873,9 @@ definitions:
status:
description: user status
type: string
+ suspended_until:
+ description: suspended until timestamp
+ type: integer
username:
description: username
type: string
@@ -959,6 +962,9 @@ definitions:
type: string
status_msg:
type: string
+ suspended_until:
+ description: suspended until timestamp
+ type: integer
username:
description: username
type: string
@@ -1401,6 +1407,9 @@ definitions:
suspended_at:
description: suspended time
type: integer
+ suspended_until:
+ description: suspended until time
+ type: integer
user_id:
description: user id
type: string
@@ -2214,6 +2223,13 @@ definitions:
type: object
schema.SiteInterfaceReq:
properties:
+ default_avatar:
+ enum:
+ - system
+ - gravatar
+ type: string
+ gravatar_base_url:
+ type: string
language:
maxLength: 128
type: string
@@ -2221,11 +2237,19 @@ definitions:
maxLength: 128
type: string
required:
+ - default_avatar
- language
- time_zone
type: object
schema.SiteInterfaceResp:
properties:
+ default_avatar:
+ enum:
+ - system
+ - gravatar
+ type: string
+ gravatar_base_url:
+ type: string
language:
maxLength: 128
type: string
@@ -2233,6 +2257,7 @@ definitions:
maxLength: 128
type: string
required:
+ - default_avatar
- language
- time_zone
type: object
@@ -2845,6 +2870,20 @@ definitions:
- deleted
- inactive
type: string
+ suspend_duration:
+ enum:
+ - 24h
+ - 48h
+ - 72h
+ - 7d
+ - 14d
+ - 1m
+ - 2m
+ - 3m
+ - 6m
+ - 1y
+ - forever
+ type: string
user_id:
type: string
required:
@@ -2867,6 +2906,9 @@ definitions:
type: integer
status:
type: string
+ suspended_until:
+ description: suspended until timestamp
+ type: integer
username:
type: string
website:
@@ -2984,6 +3026,9 @@ definitions:
status:
description: user status
type: string
+ suspended_until:
+ description: suspended until timestamp
+ type: integer
username:
description: username
type: string
diff --git a/internal/base/cron/cron.go b/internal/base/cron/cron.go
index 98e59dec..1d8008ad 100644
--- a/internal/base/cron/cron.go
+++ b/internal/base/cron/cron.go
@@ -27,6 +27,7 @@ import (
"github.com/apache/answer/internal/service/file_record"
"github.com/apache/answer/internal/service/service_config"
"github.com/apache/answer/internal/service/siteinfo_common"
+ "github.com/apache/answer/internal/service/user_admin"
"github.com/robfig/cron/v3"
"github.com/segmentfault/pacman/log"
)
@@ -36,6 +37,7 @@ type ScheduledTaskManager struct {
siteInfoService siteinfo_common.SiteInfoCommonService
questionService *content.QuestionService
fileRecordService *file_record.FileRecordService
+ userAdminService *user_admin.UserAdminService
serviceConfig *service_config.ServiceConfig
}
@@ -44,12 +46,14 @@ func NewScheduledTaskManager(
siteInfoService siteinfo_common.SiteInfoCommonService,
questionService *content.QuestionService,
fileRecordService *file_record.FileRecordService,
+ userAdminService *user_admin.UserAdminService,
serviceConfig *service_config.ServiceConfig,
) *ScheduledTaskManager {
manager := &ScheduledTaskManager{
siteInfoService: siteInfoService,
questionService: questionService,
fileRecordService: fileRecordService,
+ userAdminService: userAdminService,
serviceConfig: serviceConfig,
}
return manager
@@ -78,6 +82,19 @@ func (s *ScheduledTaskManager) Run() {
log.Error(err)
}
+ // Check for expired user suspensions every 10 minutes
+ _, err = c.AddFunc("*/10 * * * *", func() {
+ ctx := context.Background()
+ log.Infof("checking expired user suspensions")
+ err := s.userAdminService.CheckAndUnsuspendExpiredUsers(ctx)
+ if err != nil {
+ log.Errorf("failed to check expired user suspensions:
%v", err)
+ }
+ })
+ if err != nil {
+ log.Error(err)
+ }
+
if s.serviceConfig.CleanUpUploads {
log.Infof("clean up uploads cron enabled")
diff --git a/internal/entity/user_entity.go b/internal/entity/user_entity.go
index cfc2b6c2..66d61292 100644
--- a/internal/entity/user_entity.go
+++ b/internal/entity/user_entity.go
@@ -36,12 +36,16 @@ const (
UserAdminFlag = 1
)
+// PermanentSuspensionTime is a fixed time representing permanent suspension
(2099-12-31 23:59:59)
+var PermanentSuspensionTime = time.Date(2099, 12, 31, 23, 59, 59, 0, time.UTC)
+
// User user
type User struct {
ID string `xorm:"not null pk autoincr BIGINT(20) id"`
CreatedAt time.Time `xorm:"created TIMESTAMP created_at"`
UpdatedAt time.Time `xorm:"updated TIMESTAMP updated_at"`
SuspendedAt time.Time `xorm:"TIMESTAMP suspended_at"`
+ SuspendedUntil time.Time `xorm:"DATETIME suspended_until"`
DeletedAt time.Time `xorm:"TIMESTAMP deleted_at"`
LastLoginDate time.Time `xorm:"TIMESTAMP last_login_date"`
Username string `xorm:"not null default '' VARCHAR(50) UNIQUE
username"`
diff --git a/internal/migrations/v27.go b/internal/migrations/v27.go
index 7f089a87..a0c4ef8a 100644
--- a/internal/migrations/v27.go
+++ b/internal/migrations/v27.go
@@ -23,6 +23,7 @@ import (
"context"
"encoding/json"
"fmt"
+ "time"
"github.com/apache/answer/internal/base/constant"
"github.com/apache/answer/internal/entity"
@@ -30,7 +31,18 @@ import (
"xorm.io/xorm"
)
+func addSuspendedUntilToUser(ctx context.Context, x *xorm.Engine) error {
+ type User struct {
+ SuspendedUntil *time.Time `xorm:"DATETIME suspended_until"`
+ }
+ return x.Context(ctx).Sync(new(User))
+}
+
func moveUserConfigToInterface(ctx context.Context, x *xorm.Engine) error {
+ if err := addSuspendedUntilToUser(ctx, x); err != nil {
+ return fmt.Errorf("add suspended_until to user failed: %w", err)
+ }
+
// Get old interface config
interfaceSiteInfo := &entity.SiteInfo{Type: constant.SiteTypeInterface}
exist, err := x.Context(ctx).Get(interfaceSiteInfo)
@@ -42,7 +54,7 @@ func moveUserConfigToInterface(ctx context.Context, x
*xorm.Engine) error {
}
interfaceConfig := &schema.SiteInterfaceReq{}
- _ = json.Unmarshal([]byte(interfaceSiteInfo.Content), interfaceSiteInfo)
+ _ = json.Unmarshal([]byte(interfaceSiteInfo.Content), interfaceConfig)
// Get old user config
usersConfig := &entity.SiteInfo{Type: constant.SiteTypeUsers}
diff --git a/internal/repo/user/user_backyard_repo.go
b/internal/repo/user/user_backyard_repo.go
index f5987df9..c93845e9 100644
--- a/internal/repo/user/user_backyard_repo.go
+++ b/internal/repo/user/user_backyard_repo.go
@@ -52,16 +52,20 @@ func NewUserAdminRepo(data *data.Data, authRepo
auth.AuthRepo) user_admin.UserAd
// UpdateUserStatus update user status
func (ur *userAdminRepo) UpdateUserStatus(ctx context.Context, userID string,
userStatus, mailStatus int,
- email string,
+ email string, suspendedUntil time.Time,
) (err error) {
cond := &entity.User{Status: userStatus, MailStatus: mailStatus, EMail:
email}
switch userStatus {
case entity.UserStatusSuspended:
cond.SuspendedAt = time.Now()
+ cond.SuspendedUntil = suspendedUntil
case entity.UserStatusDeleted:
cond.DeletedAt = time.Now()
+ case entity.UserStatusAvailable:
+ // When restoring user status, clear suspended until time to
zero
+ cond.SuspendedUntil = time.Time{}
}
- _, err = ur.data.DB.Context(ctx).ID(userID).Update(cond)
+ _, err = ur.data.DB.Context(ctx).ID(userID).MustCols("status",
"mail_status", "e_mail", "suspended_at", "suspended_until",
"deleted_at").Update(cond)
if err != nil {
return
errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
@@ -184,3 +188,20 @@ func (ur *userAdminRepo) DeletePermanentlyUsers(ctx
context.Context) (err error)
}
return
}
+
+// GetExpiredSuspendedUsers gets all suspended users whose suspension has
expired
+func (ur *userAdminRepo) GetExpiredSuspendedUsers(ctx context.Context) (users
[]*entity.User, err error) {
+ users = make([]*entity.User, 0)
+ now := time.Now()
+
+ err = ur.data.DB.Context(ctx).
+ Where("status = ?", entity.UserStatusSuspended).
+ Where("suspended_until < ?", now).
+ Find(&users)
+
+ if err != nil {
+ return nil,
errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+
+ return users, nil
+}
diff --git a/internal/schema/backyard_user_schema.go
b/internal/schema/backyard_user_schema.go
index 923a20ed..6ec84872 100644
--- a/internal/schema/backyard_user_schema.go
+++ b/internal/schema/backyard_user_schema.go
@@ -21,19 +21,23 @@ package schema
import (
"context"
+ "strings"
+ "time"
+
"github.com/apache/answer/internal/base/constant"
"github.com/apache/answer/internal/base/handler"
"github.com/apache/answer/internal/base/reason"
"github.com/apache/answer/internal/base/translator"
"github.com/apache/answer/internal/base/validator"
+ "github.com/apache/answer/internal/entity"
"github.com/segmentfault/pacman/errors"
- "strings"
)
// UpdateUserStatusReq update user request
type UpdateUserStatusReq struct {
UserID string `validate:"required" json:"user_id"`
Status string `validate:"required,oneof=normal suspended
deleted inactive" json:"status" enums:"normal,suspended,deleted,inactive"`
+ SuspendDuration string `validate:"omitempty,oneof=24h 48h 72h 7d 14d
1m 2m 3m 6m 1y forever" json:"suspend_duration"`
RemoveAllContent bool `validate:"omitempty" json:"remove_all_content"`
LoginUserID string `json:"-"`
}
@@ -43,6 +47,39 @@ func (r *UpdateUserStatusReq) IsSuspended() bool { return
r.Status == constant.U
func (r *UpdateUserStatusReq) IsDeleted() bool { return r.Status ==
constant.UserDeleted }
func (r *UpdateUserStatusReq) IsInactive() bool { return r.Status ==
constant.UserInactive }
+// GetSuspendedUntil calculates the suspended until time based on duration
+func (r *UpdateUserStatusReq) GetSuspendedUntil() time.Time {
+ if !r.IsSuspended() || r.SuspendDuration == "" || r.SuspendDuration ==
"forever" {
+ return entity.PermanentSuspensionTime // permanent suspension
+ }
+
+ now := time.Now()
+ switch r.SuspendDuration {
+ case "24h":
+ return now.Add(24 * time.Hour)
+ case "48h":
+ return now.Add(48 * time.Hour)
+ case "72h":
+ return now.Add(72 * time.Hour)
+ case "7d":
+ return now.Add(7 * 24 * time.Hour)
+ case "14d":
+ return now.Add(14 * 24 * time.Hour)
+ case "1m":
+ return now.AddDate(0, 1, 0)
+ case "2m":
+ return now.AddDate(0, 2, 0)
+ case "3m":
+ return now.AddDate(0, 3, 0)
+ case "6m":
+ return now.AddDate(0, 6, 0)
+ case "1y":
+ return now.AddDate(1, 0, 0)
+ default:
+ return entity.PermanentSuspensionTime // fallback to permanent
+ }
+}
+
// GetUserPageReq get user list page request
type GetUserPageReq struct {
// page
@@ -71,6 +108,8 @@ type GetUserPageResp struct {
DeletedAt int64 `json:"deleted_at"`
// suspended time
SuspendedAt int64 `json:"suspended_at"`
+ // suspended until time
+ SuspendedUntil int64 `json:"suspended_until"`
// username
Username string `json:"username"`
// email
@@ -96,6 +135,8 @@ type GetUserInfoReq struct {
// GetUserInfoResp get user response
type GetUserInfoResp struct {
+ // suspended until
+ SuspendedUntil time.Time `json:"suspended_until"`
}
// UpdateUserRoleReq update user role request
diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.go
index 7c0af1dc..dd65cba9 100644
--- a/internal/schema/user_schema.go
+++ b/internal/schema/user_schema.go
@@ -96,6 +96,8 @@ type UserLoginResp struct {
HavePassword bool `json:"have_password"`
// visit token
VisitToken string `json:"visit_token"`
+ // suspended until timestamp
+ SuspendedUntil int64 `json:"suspended_until"`
}
func (r *UserLoginResp) ConvertFromUserEntity(userInfo *entity.User) {
@@ -104,6 +106,9 @@ func (r *UserLoginResp) ConvertFromUserEntity(userInfo
*entity.User) {
r.LastLoginDate = userInfo.LastLoginDate.Unix()
r.Status = constant.ConvertUserStatus(userInfo.Status,
userInfo.MailStatus)
r.HavePassword = len(userInfo.Pass) > 0
+ if !userInfo.SuspendedUntil.IsZero() && userInfo.SuspendedUntil !=
entity.PermanentSuspensionTime {
+ r.SuspendedUntil = userInfo.SuspendedUntil.Unix()
+ }
}
type GetCurrentLoginUserInfoResp struct {
@@ -119,6 +124,9 @@ func (r *GetCurrentLoginUserInfoResp)
ConvertFromUserEntity(userInfo *entity.Use
if len(r.ColorScheme) == 0 {
r.ColorScheme = constant.ColorSchemeDefault
}
+ if !userInfo.SuspendedUntil.IsZero() && userInfo.SuspendedUntil !=
entity.PermanentSuspensionTime {
+ r.SuspendedUntil = userInfo.SuspendedUntil.Unix()
+ }
}
// GetOtherUserInfoByUsernameResp get user response
@@ -156,6 +164,8 @@ type GetOtherUserInfoByUsernameResp struct {
Location string `json:"location"`
Status string `json:"status"`
StatusMsg string `json:"status_msg,omitempty"`
+ // suspended until timestamp
+ SuspendedUntil int64 `json:"suspended_until"`
}
func (r *GetOtherUserInfoByUsernameResp) ConvertFromUserEntity(userInfo
*entity.User) {
@@ -163,6 +173,9 @@ func (r *GetOtherUserInfoByUsernameResp)
ConvertFromUserEntity(userInfo *entity.
r.CreatedAt = userInfo.CreatedAt.Unix()
r.LastLoginDate = userInfo.LastLoginDate.Unix()
r.Status = constant.ConvertUserStatus(userInfo.Status,
userInfo.MailStatus)
+ if !userInfo.SuspendedUntil.IsZero() && userInfo.SuspendedUntil !=
entity.PermanentSuspensionTime {
+ r.SuspendedUntil = userInfo.SuspendedUntil.Unix()
+ }
if userInfo.MailStatus == entity.EmailStatusToBeVerified {
statusMsgShow, ok := UserStatusShowMsg[11]
if ok {
@@ -348,15 +361,16 @@ type ActionRecordResp struct {
}
type UserBasicInfo struct {
- ID string `json:"id"`
- Username string `json:"username"`
- Rank int `json:"rank"`
- DisplayName string `json:"display_name"`
- Avatar string `json:"avatar"`
- Website string `json:"website"`
- Location string `json:"location"`
- Language string `json:"language"`
- Status string `json:"status"`
+ ID string `json:"id"`
+ Username string `json:"username"`
+ Rank int `json:"rank"`
+ DisplayName string `json:"display_name"`
+ Avatar string `json:"avatar"`
+ Website string `json:"website"`
+ Location string `json:"location"`
+ Language string `json:"language"`
+ Status string `json:"status"`
+ SuspendedUntil int64 `json:"suspended_until"`
}
type GetOtherUserInfoByUsernameReq struct {
diff --git a/internal/service/user_admin/user_backyard.go
b/internal/service/user_admin/user_backyard.go
index f7dc0eae..9992f8f8 100644
--- a/internal/service/user_admin/user_backyard.go
+++ b/internal/service/user_admin/user_backyard.go
@@ -59,7 +59,7 @@ import (
// UserAdminRepo user repository
type UserAdminRepo interface {
- UpdateUserStatus(ctx context.Context, userID string, userStatus,
mailStatus int, email string) (err error)
+ UpdateUserStatus(ctx context.Context, userID string, userStatus,
mailStatus int, email string, suspendedUntil time.Time) (err error)
GetUserInfo(ctx context.Context, userID string) (user *entity.User,
exist bool, err error)
GetUserInfoByEmail(ctx context.Context, email string) (user
*entity.User, exist bool, err error)
GetUserPage(ctx context.Context, page, pageSize int, user *entity.User,
@@ -68,6 +68,7 @@ type UserAdminRepo interface {
AddUsers(ctx context.Context, users []*entity.User) (err error)
UpdateUserPassword(ctx context.Context, userID string, password string)
(err error)
DeletePermanentlyUsers(ctx context.Context) (err error)
+ GetExpiredSuspendedUsers(ctx context.Context) (users []*entity.User,
err error)
}
// UserAdminService user service
@@ -156,7 +157,8 @@ func (us *UserAdminService) UpdateUserStatus(ctx
context.Context, req *schema.Up
userInfo.MailStatus = entity.EmailStatusAvailable
}
- err = us.userRepo.UpdateUserStatus(ctx, userInfo.ID, userInfo.Status,
userInfo.MailStatus, userInfo.EMail)
+ suspendedUntil := req.GetSuspendedUntil()
+ err = us.userRepo.UpdateUserStatus(ctx, userInfo.ID, userInfo.Status,
userInfo.MailStatus, userInfo.EMail, suspendedUntil)
if err != nil {
return err
}
@@ -525,6 +527,9 @@ func (us *UserAdminService) GetUserPage(ctx
context.Context, req *schema.GetUser
} else if u.Status == entity.UserStatusSuspended {
t.Status = constant.UserSuspended
t.SuspendedAt = u.SuspendedAt.Unix()
+ if !u.SuspendedUntil.IsZero() && u.SuspendedUntil !=
entity.PermanentSuspensionTime {
+ t.SuspendedUntil = u.SuspendedUntil.Unix()
+ }
} else if u.MailStatus == entity.EmailStatusToBeVerified {
t.Status = constant.UserInactive
} else {
@@ -625,3 +630,35 @@ func (us *UserAdminService) DeletePermanently(ctx
context.Context, req *schema.D
return errors.BadRequest(reason.RequestFormatError)
}
+
+// CheckAndUnsuspendExpiredUsers checks for users whose suspension has expired
and restores them to normal status
+func (us *UserAdminService) CheckAndUnsuspendExpiredUsers(ctx context.Context)
error {
+ // Find all suspended users whose suspension time has expired
+ expiredUsers, err := us.userRepo.GetExpiredSuspendedUsers(ctx)
+ if err != nil {
+ return err
+ }
+
+ now := time.Now()
+ for _, user := range expiredUsers {
+ // Check if suspension has expired (not permanent and time has
passed)
+ if user.Status == entity.UserStatusSuspended &&
+ !user.SuspendedUntil.IsZero() &&
+ user.SuspendedUntil.Before(now) {
+
+ log.Infof("Unsuspending user %s (ID: %s) - suspension
expired at %v",
+ user.Username, user.ID, user.SuspendedUntil)
+
+ // Update user status to normal
+ err = us.userRepo.UpdateUserStatus(ctx, user.ID,
entity.UserStatusAvailable,
+ entity.EmailStatusAvailable, user.EMail,
time.Time{})
+ if err != nil {
+ log.Errorf("Failed to unsuspend user %s (ID:
%s): %v",
+ user.Username, user.ID, err)
+ continue
+ }
+ }
+ }
+
+ return nil
+}
diff --git a/internal/service/user_common/user.go
b/internal/service/user_common/user.go
index 3df99261..166bf955 100644
--- a/internal/service/user_common/user.go
+++ b/internal/service/user_common/user.go
@@ -180,6 +180,9 @@ func (us *UserCommon) FormatUserBasicInfo(ctx
context.Context, userInfo *entity.
userBasicInfo.Location = userInfo.Location
userBasicInfo.Language = userInfo.Language
userBasicInfo.Status = constant.ConvertUserStatus(userInfo.Status,
userInfo.MailStatus)
+ if !userInfo.SuspendedUntil.IsZero() && userInfo.SuspendedUntil !=
entity.PermanentSuspensionTime {
+ userBasicInfo.SuspendedUntil = userInfo.SuspendedUntil.Unix()
+ }
if userBasicInfo.Status == constant.UserDeleted {
userBasicInfo.Avatar = ""
userBasicInfo.DisplayName = "user" +
converter.DeleteUserDisplay(userInfo.ID)