88 "net/mail"
99 "strconv"
1010
11+ "github.com/bytedance/gg/gptr"
1112 "github.com/bytedance/gg/gslice"
1213
1314 "github.com/coze-dev/coze-loop/backend/infra/middleware/session"
@@ -17,31 +18,62 @@ import (
1718 "github.com/coze-dev/coze-loop/backend/modules/foundation/domain/user/entity"
1819 "github.com/coze-dev/coze-loop/backend/modules/foundation/domain/user/service"
1920 "github.com/coze-dev/coze-loop/backend/modules/foundation/pkg/errno"
21+ "github.com/coze-dev/coze-loop/backend/pkg/conf"
2022 "github.com/coze-dev/coze-loop/backend/pkg/errorx"
2123 "github.com/coze-dev/coze-loop/backend/pkg/lang/conv"
2224 "github.com/coze-dev/coze-loop/backend/pkg/lang/ptr"
2325 "github.com/coze-dev/coze-loop/backend/pkg/lang/slices"
26+ "github.com/coze-dev/coze-loop/backend/pkg/logs"
2427)
2528
2629type UserApplicationImpl struct {
27- userService service.IUserService
30+ userService service.IUserService
31+ registerController userRegisterController
32+ }
33+
34+ type userRegisterController struct {
35+ configLoader conf.IConfigLoader
36+ }
37+
38+ type userRegisterControlConfig struct {
39+ Block bool `mapstructure:"block"`
40+ AllowedEmails []string `mapstructure:"allowed_emails"`
41+ }
42+
43+ func (u * userRegisterController ) allowRegister (ctx context.Context , email string ) bool {
44+ const keyUserRegisterControl = "user_register_control"
45+
46+ var config userRegisterControlConfig
47+ if err := u .configLoader .UnmarshalKey (ctx , keyUserRegisterControl , & config ); err != nil {
48+ logs .CtxWarn (ctx , "load user_register_control config fail, err: %v" , err )
49+ return false
50+ }
51+
52+ if ! config .Block {
53+ return true
54+ }
55+ return slices .Contains (config .AllowedEmails , email )
2856}
2957
3058func NewUserApplication (
3159 userService service.IUserService ,
32- ) user.UserService {
60+ configFactory conf.IConfigLoaderFactory ,
61+ ) (user.UserService , error ) {
62+ loader , err := configFactory .NewConfigLoader ("foundation.yaml" )
63+ if err != nil {
64+ return nil , err
65+ }
3366 return & UserApplicationImpl {
3467 userService : userService ,
35- }
68+ registerController : userRegisterController {
69+ configLoader : loader ,
70+ },
71+ }, nil
3672}
3773
3874func (u * UserApplicationImpl ) Register (ctx context.Context , request * user.UserRegisterRequest ) (r * user.UserRegisterResponse , err error ) {
39- if request .Email == nil || request .Password == nil {
40- return nil , errorx .NewByCode (errno .CommonInvalidParamCode )
41- }
42-
43- if _ , err = mail .ParseAddress (* request .Email ); err != nil {
44- return nil , errorx .NewByCode (errno .CommonInvalidParamCode , errorx .WithExtraMsg ("email is invalid" ))
75+ if err := u .validateRegisterReq (ctx , request ); err != nil {
76+ return nil , err
4577 }
4678
4779 userDO , err := u .userService .Create (ctx , & service.CreateUserRequest {
@@ -66,6 +98,22 @@ func (u *UserApplicationImpl) Register(ctx context.Context, request *user.UserRe
6698 return r , nil
6799}
68100
101+ func (u * UserApplicationImpl ) validateRegisterReq (ctx context.Context , request * user.UserRegisterRequest ) error {
102+ if request .Email == nil || request .Password == nil {
103+ return errorx .NewByCode (errno .CommonInvalidParamCode )
104+ }
105+
106+ if _ , err := mail .ParseAddress (gptr .Indirect (request .Email )); err != nil {
107+ return errorx .NewByCode (errno .CommonInvalidParamCode , errorx .WithExtraMsg ("email is invalid" ))
108+ }
109+
110+ if ! u .registerController .allowRegister (ctx , request .GetEmail ()) {
111+ return errorx .NewByCode (errno .UserRegistrationControlBlockCode )
112+ }
113+
114+ return nil
115+ }
116+
69117func (u * UserApplicationImpl ) ResetPassword (ctx context.Context , request * user.ResetPasswordRequest ) (r * user.ResetPasswordResponse , err error ) {
70118 r = user .NewResetPasswordResponse ()
71119
0 commit comments