feat: update template
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"server/internal/db"
|
||||
"server/internal/db/sqlc"
|
||||
"server/internal/middleware"
|
||||
"server/internal/model/auth"
|
||||
"server/internal/model/common"
|
||||
"server/internal/model/request"
|
||||
"server/internal/model/response"
|
||||
@@ -31,7 +32,7 @@ func NewUserService(store *db.Store, jwt *middleware.JWTMiddleware, cache *cache
|
||||
}
|
||||
}
|
||||
|
||||
// clearUserCache
|
||||
// clearUserCache 清理单个用户缓存 权限 info 和 鉴权状态
|
||||
func (s *UserService) clearUserCache(ctx context.Context, id int32) error {
|
||||
if err := s.cache.Del(ctx, cachekey.UserApiPermissions(id)); err != nil {
|
||||
return err
|
||||
@@ -41,6 +42,9 @@ func (s *UserService) clearUserCache(ctx context.Context, id int32) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.cache.Del(ctx, cachekey.UserAuthState(id)); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -64,19 +68,29 @@ func (s *UserService) clearRefreshToken(ctx context.Context, id int32) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// forceLogout 强制下线:bump 版本 + 清缓存(含 auth_state)+ 清刷新令牌
|
||||
func (s *UserService) forceLogout(ctx context.Context, id int32) error {
|
||||
if err := s.store.IncrementUserTokenVersion(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = s.clearUserCache(ctx, id)
|
||||
_ = s.clearRefreshToken(ctx, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *UserService) GetCurrentUser(ctx context.Context, id int32, isAdmin bool) (*response.UserInfo, error) {
|
||||
return cache.GetOrSetJSON[*response.UserInfo](ctx, s.cache, cachekey.UserInfo(id), 0, func() (*response.UserInfo, error) {
|
||||
g, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
var (
|
||||
user sqlc.GetUserByIDRow
|
||||
user sqlc.GetUserRow
|
||||
roles []sqlc.SysRole
|
||||
menus []sqlc.SysMenu
|
||||
permissions []*string
|
||||
)
|
||||
|
||||
g.Go(func() error {
|
||||
u, err := s.store.GetUserByID(ctx, id)
|
||||
u, err := s.store.GetUser(ctx, sqlc.GetUserParams{ID: id})
|
||||
if err != nil {
|
||||
return dberr.MapNoRows(err, errs.ErrUserNotFound)
|
||||
}
|
||||
@@ -136,8 +150,8 @@ func (s *UserService) GetCurrentUser(ctx context.Context, id int32, isAdmin bool
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 如果用户被禁用 返回错误 超管不用管状态
|
||||
if user.ID != 1 && user.Status != 1 {
|
||||
// 判断用户不为超管 且状态为0表示用户已被禁用
|
||||
if !auth.IsAdmin(user.ID) && user.Status == 0 {
|
||||
return nil, errs.ErrUserDisabled
|
||||
}
|
||||
|
||||
@@ -174,7 +188,7 @@ func (s *UserService) List(ctx context.Context, p request.SearchUserParams) (*co
|
||||
|
||||
func (s *UserService) ListRoles(ctx context.Context, id int32) ([]sqlc.SysRole, error) {
|
||||
// 先查询用户是否存在
|
||||
_, err := s.store.GetUserByID(ctx, id)
|
||||
_, err := s.store.GetUser(ctx, sqlc.GetUserParams{ID: id})
|
||||
if err != nil {
|
||||
return nil, dberr.MapNoRows(err, errs.ErrUserNotFound)
|
||||
}
|
||||
@@ -203,6 +217,19 @@ func (s *UserService) Create(ctx context.Context, req request.CreateUserRequest)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *UserService) KickUser(ctx context.Context, id int32) error {
|
||||
return s.forceLogout(ctx, id)
|
||||
}
|
||||
|
||||
func (s *UserService) KickAllUsers(ctx context.Context) error {
|
||||
if err := s.store.IncrementTokenVersionForAllUsers(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
// 清理所有的token和状态
|
||||
_ = s.cache.DelByPrefix(ctx, cachekey.AuthRefreshPattern)
|
||||
return s.cache.DelByPrefix(ctx, cachekey.UserAuthStatePattern)
|
||||
}
|
||||
|
||||
func (s *UserService) Update(ctx context.Context, id int32, req request.UpdateUserRequest) error {
|
||||
user := sqlc.UpdateUserParams{
|
||||
Username: req.Username,
|
||||
@@ -227,7 +254,8 @@ func (s *UserService) Update(ctx context.Context, id int32, req request.UpdateUs
|
||||
|
||||
// 如果将用户的状态修改为0,则清除用户刷新令牌
|
||||
if req.Status != nil && *req.Status == 0 {
|
||||
_ = s.clearRefreshToken(ctx, id)
|
||||
// 踢下线
|
||||
_ = s.forceLogout(ctx, id)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -235,7 +263,7 @@ func (s *UserService) Update(ctx context.Context, id int32, req request.UpdateUs
|
||||
|
||||
func (s *UserService) SetRoles(ctx context.Context, userID int32, req request.SetUserRolesRequest) error {
|
||||
// 先查询用户是否存在
|
||||
_, err := s.store.GetUserByID(ctx, userID)
|
||||
_, err := s.store.GetUser(ctx, sqlc.GetUserParams{ID: userID})
|
||||
if err != nil {
|
||||
return dberr.MapNoRows(err, errs.ErrUserNotFound)
|
||||
}
|
||||
@@ -292,13 +320,15 @@ func (s *UserService) UpdatePassword(ctx context.Context, id int32, req request.
|
||||
}
|
||||
|
||||
// 下线当前用户
|
||||
_ = s.clearRefreshToken(ctx, id)
|
||||
if err = s.forceLogout(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *UserService) Delete(ctx context.Context, id int32) error {
|
||||
isAdmin := middleware.UserIsAdmin(id)
|
||||
isAdmin := auth.IsAdmin(id)
|
||||
|
||||
if isAdmin {
|
||||
return errs.ErrCannotDeleteSuperAdmin
|
||||
|
||||
Reference in New Issue
Block a user