MCPcopy Create free account
hub / github.com/bytebase/bytebase / validateEmailWithDomains

Function validateEmailWithDomains

backend/api/v1/user_service.go:761–801  ·  view source on GitHub ↗
(ctx context.Context, licenseService *enterprise.LicenseService, stores *store.Store, workspaceID, email string, checkDomainSetting bool)

Source from the content-addressed store, hash-verified

759}
760
761func validateEmailWithDomains(ctx context.Context, licenseService *enterprise.LicenseService, stores *store.Store, workspaceID, email string, checkDomainSetting bool) error {
762 if err := validateEndUserEmail(email); err != nil {
763 return err
764 }
765 if licenseService.IsFeatureEnabled(ctx, workspaceID, v1pb.PlanFeature_FEATURE_USER_EMAIL_DOMAIN_RESTRICTION) != nil {
766 // nolint:nilerr
767 // feature not enabled, only validate email and skip domain restriction.
768 if err := common.ValidateEmail(email); err != nil {
769 return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid email: %v", err.Error()))
770 }
771 return nil
772 }
773 setting, err := stores.GetWorkspaceProfileSetting(ctx, workspaceID)
774 if err != nil {
775 return connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to find workspace setting"))
776 }
777
778 var allowedDomains []string
779 if checkDomainSetting || setting.EnforceIdentityDomain {
780 allowedDomains = setting.Domains
781 }
782
783 // Check if the email is valid.
784 if err := common.ValidateEmail(email); err != nil {
785 return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("invalid email: %v", err.Error()))
786 }
787 // Enforce domain restrictions.
788 if len(allowedDomains) > 0 {
789 ok := false
790 for _, v := range allowedDomains {
791 if strings.HasSuffix(email, fmt.Sprintf("@%s", v)) {
792 ok = true
793 break
794 }
795 }
796 if !ok {
797 return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("email %q does not belong to domains %v", email, allowedDomains))
798 }
799 }
800 return nil
801}
802
803func extractDomain(input string) string {
804 pattern := `[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+`

Callers 6

CreateUserMethod · 0.85
UpdateEmailMethod · 0.85
CreateGroupMethod · 0.85
SwitchWorkspaceMethod · 0.85

Calls 6

ValidateEmailFunction · 0.92
validateEndUserEmailFunction · 0.85
IsFeatureEnabledMethod · 0.80
ErrorfMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected