77 "context"
88 "net/mail"
99 "strconv"
10+ "strings"
1011
12+ "github.com/bytedance/gg/gptr"
1113 "github.com/bytedance/gg/gslice"
1214
1315 "github.com/coze-dev/coze-loop/backend/infra/middleware/session"
@@ -17,31 +19,64 @@ import (
1719 "github.com/coze-dev/coze-loop/backend/modules/foundation/domain/user/entity"
1820 "github.com/coze-dev/coze-loop/backend/modules/foundation/domain/user/service"
1921 "github.com/coze-dev/coze-loop/backend/modules/foundation/pkg/errno"
22+ "github.com/coze-dev/coze-loop/backend/pkg/conf"
2023 "github.com/coze-dev/coze-loop/backend/pkg/errorx"
2124 "github.com/coze-dev/coze-loop/backend/pkg/lang/conv"
2225 "github.com/coze-dev/coze-loop/backend/pkg/lang/ptr"
2326 "github.com/coze-dev/coze-loop/backend/pkg/lang/slices"
27+ "github.com/coze-dev/coze-loop/backend/pkg/logs"
2428)
2529
26- type UserApplicationImpl struct {
27- userService service.IUserService
28- }
29-
3030func NewUserApplication (
3131 userService service.IUserService ,
32- ) user.UserService {
33- return & UserApplicationImpl {
34- userService : userService ,
32+ configFactory conf.IConfigLoaderFactory ,
33+ ) (user.UserService , error ) {
34+ ua := & UserApplicationImpl {
35+ userService : userService ,
36+ registerController : userRegisterController {},
37+ }
38+ if loader , err := configFactory .NewConfigLoader ("foundation.yaml" ); err == nil {
39+ ua .registerController .configLoader = loader
3540 }
41+ return ua , nil
3642}
3743
38- func (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 )
44+ type UserApplicationImpl struct {
45+ userService service.IUserService
46+ registerController userRegisterController
47+ }
48+
49+ type userRegisterController struct {
50+ // configLoader weak dependency, might be nil
51+ configLoader conf.IConfigLoader
52+ }
53+
54+ type userRegisterControlConfig struct {
55+ Block bool `mapstructure:"block"`
56+ AllowedEmails string `mapstructure:"allowed_emails"`
57+ }
58+
59+ func (u * userRegisterController ) allowRegister (ctx context.Context , email string ) bool {
60+ if u .configLoader == nil {
61+ return true
62+ }
63+
64+ const keyUserRegisterControl = "user_register_control"
65+ var config userRegisterControlConfig
66+ if err := u .configLoader .UnmarshalKey (ctx , keyUserRegisterControl , & config ); err != nil {
67+ logs .CtxWarn (ctx , "load user_register_control config fail, err: %v" , err )
68+ return false
69+ }
70+
71+ if ! config .Block {
72+ return true
4173 }
74+ return slices .Contains (strings .Split (config .AllowedEmails , ";" ), email )
75+ }
4276
43- if _ , err = mail .ParseAddress (* request .Email ); err != nil {
44- return nil , errorx .NewByCode (errno .CommonInvalidParamCode , errorx .WithExtraMsg ("email is invalid" ))
77+ func (u * UserApplicationImpl ) Register (ctx context.Context , request * user.UserRegisterRequest ) (r * user.UserRegisterResponse , err error ) {
78+ if err := u .validateRegisterReq (ctx , request ); err != nil {
79+ return nil , err
4580 }
4681
4782 userDO , err := u .userService .Create (ctx , & service.CreateUserRequest {
@@ -66,6 +101,22 @@ func (u *UserApplicationImpl) Register(ctx context.Context, request *user.UserRe
66101 return r , nil
67102}
68103
104+ func (u * UserApplicationImpl ) validateRegisterReq (ctx context.Context , request * user.UserRegisterRequest ) error {
105+ if request .Email == nil || request .Password == nil {
106+ return errorx .NewByCode (errno .CommonInvalidParamCode )
107+ }
108+
109+ if _ , err := mail .ParseAddress (gptr .Indirect (request .Email )); err != nil {
110+ return errorx .NewByCode (errno .CommonInvalidParamCode , errorx .WithExtraMsg ("email is invalid" ))
111+ }
112+
113+ if ! u .registerController .allowRegister (ctx , request .GetEmail ()) {
114+ return errorx .NewByCode (errno .UserRegistrationControlBlockCode )
115+ }
116+
117+ return nil
118+ }
119+
69120func (u * UserApplicationImpl ) ResetPassword (ctx context.Context , request * user.ResetPasswordRequest ) (r * user.ResetPasswordResponse , err error ) {
70121 r = user .NewResetPasswordResponse ()
71122
0 commit comments