fix: admin 审批链路查询条件缺失与网关路由缺失
SearchUserVerifications 查询条件硬编码了 user_id=0 导致 admin 查询 始终返回空列表,改为条件构建模式。envoy 缺少 /api/v1/admin 前缀 路由导致审批接口 404。新增 users-rpc 启动时从环境变量自动初始化 管理员账户。
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"juwan-backend/app/user_verifications/rpc/internal/models"
|
||||
"juwan-backend/app/user_verifications/rpc/internal/models/predicate"
|
||||
"juwan-backend/app/user_verifications/rpc/internal/models/userverifications"
|
||||
"juwan-backend/app/user_verifications/rpc/internal/svc"
|
||||
"juwan-backend/app/user_verifications/rpc/pb"
|
||||
@@ -31,7 +32,18 @@ func (l *SearchUserVerificationsLogic) SearchUserVerifications(in *pb.SearchUser
|
||||
logx.Errorf("Limit exceeds max limit: %d", in.Limit)
|
||||
return nil, errors.New("limit exceeds max limit")
|
||||
}
|
||||
verifications, err := l.svcCtx.UserVeriModelRO.Query().Where(userverifications.UserIDEQ(in.UserId)).
|
||||
predicates := make([]predicate.UserVerifications, 0, 3)
|
||||
if in.UserId != 0 {
|
||||
predicates = append(predicates, userverifications.UserIDEQ(in.UserId))
|
||||
}
|
||||
if in.Role != "" {
|
||||
predicates = append(predicates, userverifications.RoleEQ(in.Role))
|
||||
}
|
||||
if in.Status != "" {
|
||||
predicates = append(predicates, userverifications.StatusEQ(in.Status))
|
||||
}
|
||||
|
||||
verifications, err := l.svcCtx.UserVeriModelRO.Query().Where(predicates...).
|
||||
Offset(int(in.Page * in.Limit)).
|
||||
Limit(int(in.Limit)).
|
||||
All(l.ctx)
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
"juwan-backend/app/users/rpc/internal/models/users"
|
||||
"juwan-backend/app/users/rpc/internal/svc"
|
||||
"juwan-backend/common/utils/pwdUtils"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
func initAdmin(svcCtx *svc.ServiceContext) {
|
||||
username := strings.TrimSpace(os.Getenv("ADMIN_USERNAME"))
|
||||
password := strings.TrimSpace(os.Getenv("ADMIN_PASSWORD"))
|
||||
email := strings.TrimSpace(os.Getenv("ADMIN_EMAIL"))
|
||||
if username == "" || password == "" || email == "" {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
exists, _ := svcCtx.UsersModelRW.Users.Query().Where(users.UsernameEQ(username)).Exist(ctx)
|
||||
if exists {
|
||||
return
|
||||
}
|
||||
|
||||
hashedPassword, err := pwdUtils.HashPassword(password)
|
||||
if err != nil {
|
||||
logx.Errorf("hash admin password: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := svcCtx.Snowflake.NextId(ctx, &snowflake.NextIdReq{})
|
||||
if err != nil {
|
||||
logx.Errorf("generate admin user ID: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = svcCtx.UsersModelRW.Users.Create().
|
||||
SetID(resp.Id).
|
||||
SetUsername(username).
|
||||
SetPasswordHash(hashedPassword).
|
||||
SetEmail(email).
|
||||
SetPhone("").
|
||||
SetBio("").
|
||||
SetAvatar("").
|
||||
SetCurrentRole("consumer").
|
||||
SetNickname(username).
|
||||
SetIsAdmin(true).
|
||||
Save(ctx)
|
||||
if err != nil {
|
||||
logx.Errorf("create admin user: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
logx.Infof("initialized admin user: %s", username)
|
||||
}
|
||||
@@ -29,7 +29,8 @@ func (l *UpdateUsersLogic) UpdateUsers(in *pb.UpdateUsersReq) (*pb.UpdateUsersRe
|
||||
SetNillableAvatar(in.Avatar).
|
||||
SetNillableBio(in.Bio).
|
||||
SetNillableCurrentRole(in.CurrentRole).
|
||||
SetNillablePasswordHash(in.PasswordHash)
|
||||
SetNillablePasswordHash(in.PasswordHash).
|
||||
SetNillableIsAdmin(in.IsAdmin)
|
||||
if len(in.VerifiedRoles) > 0 {
|
||||
verifiedRoles := types.TextArray{Elements: append([]string{}, in.VerifiedRoles...), Valid: true}
|
||||
updater.SetVerifiedRoles(verifiedRoles)
|
||||
|
||||
@@ -25,6 +25,7 @@ func main() {
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c, conf.UseEnv())
|
||||
ctx := svc.NewServiceContext(c)
|
||||
initAdmin(ctx)
|
||||
|
||||
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
|
||||
pb.RegisterUsercenterServer(grpcServer, server.NewUsercenterServer(ctx))
|
||||
|
||||
Reference in New Issue
Block a user