(ctx context.Context, licenseService *enterprise.LicenseService, stores *store.Store, workspaceID, email string, checkDomainSetting bool)
| 759 | } |
| 760 | |
| 761 | func 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 | |
| 803 | func extractDomain(input string) string { |
| 804 | pattern := `[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+` |
no test coverage detected