diff --git a/ui-ngx/src/app/core/services/utils.service.ts b/ui-ngx/src/app/core/services/utils.service.ts index 174f6f941e..59ce20c4bc 100644 --- a/ui-ngx/src/app/core/services/utils.service.ts +++ b/ui-ngx/src/app/core/services/utils.service.ts @@ -477,14 +477,4 @@ export class UtilsService { el.parentNode.removeChild(el); } } - - public validateEmail(control: AbstractControl): ValidationErrors | null { - const email = control.value; - const nativeEmailError = Validators.email(control); - if (nativeEmailError !== null) { - return nativeEmailError; - } - const passesDomainCheck = /\.[^.\s]{2,}$/.test(email); - return passesDomainCheck ? null : {email: true}; - } } diff --git a/ui-ngx/src/app/core/utils.ts b/ui-ngx/src/app/core/utils.ts index 8ef3dc074e..c824aa26fc 100644 --- a/ui-ngx/src/app/core/utils.ts +++ b/ui-ngx/src/app/core/utils.ts @@ -33,6 +33,7 @@ import { } from '@shared/models/js-function.models'; import { DomSanitizer } from '@angular/platform-browser'; import { SecurityContext } from '@angular/core'; +import { AbstractControl, ValidationErrors, Validators } from '@angular/forms'; const varsRegex = /\${([^}]*)}/g; @@ -988,3 +989,14 @@ export const trimDefaultValues = (input: Record, defaults: Record { + const email = control.value; + const nativeEmailError = Validators.email(control); + if (nativeEmailError !== null) { + return nativeEmailError; + } + const passesDomainCheck = /\.[^.\s]{2,}$/.test(email); + return passesDomainCheck ? null : {email: true}; +}; + diff --git a/ui-ngx/src/app/modules/home/components/entity/contact-based.component.ts b/ui-ngx/src/app/modules/home/components/entity/contact-based.component.ts index ff052313ff..2db5dd3c97 100644 --- a/ui-ngx/src/app/modules/home/components/entity/contact-based.component.ts +++ b/ui-ngx/src/app/modules/home/components/entity/contact-based.component.ts @@ -24,6 +24,7 @@ import { EntityComponent } from './entity.component'; import { EntityTableConfig } from '@home/models/entity/entities-table-config.models'; import { CountryData } from '@shared/models/country.models'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; +import { validateEmail } from '@app/core/utils'; @Directive() export abstract class ContactBasedComponent> extends EntityComponent implements AfterViewInit { @@ -50,7 +51,7 @@ export abstract class ContactBasedComponent> exten entityForm.addControl('address', this.fb.control(entity ? entity.address : '', [])); entityForm.addControl('address2', this.fb.control(entity ? entity.address2 : '', [])); entityForm.addControl('phone', this.fb.control(entity ? entity.phone : '', [Validators.maxLength(255)])); - entityForm.addControl('email', this.fb.control(entity ? entity.email : '', [Validators.email])); + entityForm.addControl('email', this.fb.control(entity ? entity.email : '', [validateEmail])); return entityForm; } diff --git a/ui-ngx/src/app/modules/home/pages/admin/security-settings.component.ts b/ui-ngx/src/app/modules/home/pages/admin/security-settings.component.ts index 7f644b7441..c6470ee983 100644 --- a/ui-ngx/src/app/modules/home/pages/admin/security-settings.component.ts +++ b/ui-ngx/src/app/modules/home/pages/admin/security-settings.component.ts @@ -32,13 +32,12 @@ import { JwtSettings, SecuritySettings } from '@shared/models/settings.models'; import { AdminService } from '@core/http/admin.service'; import { HasConfirmForm } from '@core/guards/confirm-on-exit.guard'; import { mergeMap, tap } from 'rxjs/operators'; -import { randomAlphanumeric } from '@core/utils'; +import { randomAlphanumeric, validateEmail } from '@core/utils'; import { AuthService } from '@core/auth/auth.service'; import { DialogService } from '@core/services/dialog.service'; import { TranslateService } from '@ngx-translate/core'; import { Observable, of } from 'rxjs'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; -import { UtilsService } from "@core/services/utils.service"; @Component({ selector: 'tb-security-settings', @@ -62,7 +61,6 @@ export class SecuritySettingsComponent extends PageComponent implements HasConfi private dialogService: DialogService, private translate: TranslateService, private fb: UntypedFormBuilder, - private utils: UtilsService, private destroyRef: DestroyRef) { super(store); this.buildSecuritySettingsForm(); @@ -78,7 +76,7 @@ export class SecuritySettingsComponent extends PageComponent implements HasConfi buildSecuritySettingsForm() { this.securitySettingsFormGroup = this.fb.group({ maxFailedLoginAttempts: [null, [Validators.min(0)]], - userLockoutNotificationEmail: ['', [this.utils.validateEmail]], + userLockoutNotificationEmail: ['', [validateEmail]], userActivationTokenTtl: [24, [Validators.required, Validators.min(1), Validators.max(24)]], passwordResetTokenTtl: [24, [Validators.required, Validators.min(1), Validators.max(24)]], mobileSecretKeyLength: [null, [Validators.min(1)]], diff --git a/ui-ngx/src/app/modules/home/pages/profile/profile.component.ts b/ui-ngx/src/app/modules/home/pages/profile/profile.component.ts index a097b470ca..3a8b99f166 100644 --- a/ui-ngx/src/app/modules/home/pages/profile/profile.component.ts +++ b/ui-ngx/src/app/modules/home/pages/profile/profile.component.ts @@ -28,12 +28,11 @@ import { environment as env } from '@env/environment'; import { TranslateService } from '@ngx-translate/core'; import { ActionSettingsChangeLanguage } from '@core/settings/settings.actions'; import { ActivatedRoute } from '@angular/router'; -import { isDefinedAndNotNull, isNotEmptyStr } from '@core/utils'; +import { isDefinedAndNotNull, isNotEmptyStr, validateEmail } from '@core/utils'; import { getCurrentAuthUser } from '@core/auth/auth.selectors'; import { AuthService } from '@core/auth/auth.service'; import { UnitSystem, UnitSystems } from '@shared/models/unit.models'; import { UnitService } from '@core/services/unit.service'; -import { UtilsService } from "@core/services/utils.service"; @Component({ selector: 'tb-profile', @@ -55,7 +54,6 @@ export class ProfileComponent extends PageComponent implements OnInit, HasConfir private authService: AuthService, private translate: TranslateService, private unitService: UnitService, - private utils: UtilsService, private fb: UntypedFormBuilder) { super(store); this.authUser = getCurrentAuthUser(this.store); @@ -68,7 +66,7 @@ export class ProfileComponent extends PageComponent implements OnInit, HasConfir private buildProfileForm() { this.profile = this.fb.group({ - email: ['', [Validators.required, this.utils.validateEmail]], + email: ['', [Validators.required, validateEmail]], firstName: [''], lastName: [''], phone: [''], diff --git a/ui-ngx/src/app/modules/home/pages/security/authentication-dialog/email-auth-dialog.component.ts b/ui-ngx/src/app/modules/home/pages/security/authentication-dialog/email-auth-dialog.component.ts index 769463c63b..e69ff94bd2 100644 --- a/ui-ngx/src/app/modules/home/pages/security/authentication-dialog/email-auth-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/pages/security/authentication-dialog/email-auth-dialog.component.ts @@ -28,7 +28,7 @@ import { TwoFactorAuthProviderType } from '@shared/models/two-factor-auth.models'; import { MatStepper } from '@angular/material/stepper'; -import { UtilsService } from "@core/services/utils.service"; +import { validateEmail } from '@app/core/utils'; export interface EmailAuthDialogData { email: string; @@ -52,14 +52,13 @@ export class EmailAuthDialogComponent extends DialogComponent, protected router: Router, private twoFaService: TwoFactorAuthenticationService, - private utils: UtilsService, @Inject(MAT_DIALOG_DATA) public data: EmailAuthDialogData, public dialogRef: MatDialogRef, public fb: UntypedFormBuilder) { super(store, router, dialogRef); this.emailConfigForm = this.fb.group({ - email: [this.data.email, [Validators.required, this.utils.validateEmail]] + email: [this.data.email, [Validators.required, validateEmail]] }); this.emailVerificationForm = this.fb.group({ diff --git a/ui-ngx/src/app/modules/home/pages/user/user.component.ts b/ui-ngx/src/app/modules/home/pages/user/user.component.ts index 0c9447e9d3..18318045ce 100644 --- a/ui-ngx/src/app/modules/home/pages/user/user.component.ts +++ b/ui-ngx/src/app/modules/home/pages/user/user.component.ts @@ -14,7 +14,7 @@ /// limitations under the License. /// -import { ChangeDetectorRef, Component, Inject, OnInit, Optional } from '@angular/core'; +import { ChangeDetectorRef, Component, Inject, Optional } from '@angular/core'; import { select, Store } from '@ngrx/store'; import { AppState } from '@core/core.state'; import { EntityComponent } from '../../components/entity/entity.component'; @@ -23,18 +23,17 @@ import { User } from '@shared/models/user.model'; import { selectAuth } from '@core/auth/auth.selectors'; import { map } from 'rxjs/operators'; import { Authority } from '@shared/models/authority.enum'; -import { isDefinedAndNotNull } from '@core/utils'; +import { isDefinedAndNotNull, validateEmail } from '@core/utils'; import { EntityTableConfig } from '@home/models/entity/entities-table-config.models'; import { ActionNotificationShow } from '@app/core/notification/notification.actions'; import { TranslateService } from '@ngx-translate/core'; -import { UtilsService } from "@core/services/utils.service"; @Component({ selector: 'tb-user', templateUrl: './user.component.html', styleUrls: ['./user.component.scss'] }) -export class UserComponent extends EntityComponent implements OnInit{ +export class UserComponent extends EntityComponent{ authority = Authority; @@ -47,16 +46,11 @@ export class UserComponent extends EntityComponent implements OnInit{ @Optional() @Inject('entity') protected entityValue: User, @Optional() @Inject('entitiesTableConfig') protected entitiesTableConfigValue: EntityTableConfig, public fb: UntypedFormBuilder, - private utils: UtilsService, protected cd: ChangeDetectorRef, protected translate: TranslateService) { super(store, fb, entityValue, entitiesTableConfigValue, cd); } - ngOnInit(): void { - this.entityForm.controls.email.addValidators(this.utils.validateEmail); - } - hideDelete() { if (this.entitiesTableConfig) { return !this.entitiesTableConfig.deleteEnabled(this.entity); @@ -76,7 +70,7 @@ export class UserComponent extends EntityComponent implements OnInit{ buildForm(entity: User): UntypedFormGroup { return this.fb.group( { - email: [entity ? entity.email : '', [Validators.required]], + email: [entity ? entity.email : '', [Validators.required,validateEmail]], firstName: [entity ? entity.firstName : ''], lastName: [entity ? entity.lastName : ''], phone: [entity ? entity.phone : ''], diff --git a/ui-ngx/src/app/modules/login/pages/login/login.component.ts b/ui-ngx/src/app/modules/login/pages/login/login.component.ts index 89d5c8d8cd..e553a22acb 100644 --- a/ui-ngx/src/app/modules/login/pages/login/login.component.ts +++ b/ui-ngx/src/app/modules/login/pages/login/login.component.ts @@ -24,7 +24,7 @@ import { HttpErrorResponse } from '@angular/common/http'; import { Constants } from '@shared/models/constants'; import { Router } from '@angular/router'; import { OAuth2ClientLoginInfo } from '@shared/models/oauth2.models'; -import { UtilsService } from "@core/services/utils.service"; +import { validateEmail } from '@app/core/utils'; @Component({ selector: 'tb-login', @@ -36,7 +36,7 @@ export class LoginComponent extends PageComponent implements OnInit { passwordViolation = false; loginFormGroup = this.fb.group({ - username: ['', [Validators.required, this.utils.validateEmail]], + username: ['', [Validators.required, validateEmail]], password: [''] }); oauth2Clients: Array = null; @@ -44,7 +44,6 @@ export class LoginComponent extends PageComponent implements OnInit { constructor(protected store: Store, private authService: AuthService, public fb: UntypedFormBuilder, - private utils: UtilsService, private router: Router) { super(store); } diff --git a/ui-ngx/src/app/modules/login/pages/login/reset-password-request.component.ts b/ui-ngx/src/app/modules/login/pages/login/reset-password-request.component.ts index eaa9622eb7..3daeaa693d 100644 --- a/ui-ngx/src/app/modules/login/pages/login/reset-password-request.component.ts +++ b/ui-ngx/src/app/modules/login/pages/login/reset-password-request.component.ts @@ -22,7 +22,7 @@ import { PageComponent } from '@shared/components/page.component'; import { UntypedFormBuilder, Validators } from '@angular/forms'; import { ActionNotificationShow } from '@core/notification/notification.actions'; import { TranslateService } from '@ngx-translate/core'; -import { UtilsService } from "@core/services/utils.service"; +import { validateEmail } from '@app/core/utils'; @Component({ selector: 'tb-reset-password-request', @@ -34,13 +34,12 @@ export class ResetPasswordRequestComponent extends PageComponent implements OnIn clicked: boolean = false; requestPasswordRequest = this.fb.group({ - email: ['', [Validators.required, this.utils.validateEmail]], + email: ['', [Validators.required, validateEmail]], }, {updateOn: 'submit'}); constructor(protected store: Store, private authService: AuthService, private translate: TranslateService, - private utils: UtilsService, public fb: UntypedFormBuilder) { super(store); } diff --git a/ui-ngx/src/app/shared/components/contact.component.ts b/ui-ngx/src/app/shared/components/contact.component.ts index e085a5e99a..330a3a100e 100644 --- a/ui-ngx/src/app/shared/components/contact.component.ts +++ b/ui-ngx/src/app/shared/components/contact.component.ts @@ -14,15 +14,14 @@ /// limitations under the License. /// -import { Component, Input, OnInit } from '@angular/core'; +import { Component, Input } from '@angular/core'; import { UntypedFormGroup } from '@angular/forms'; -import { UtilsService } from "@core/services/utils.service"; @Component({ selector: 'tb-contact', templateUrl: './contact.component.html' }) -export class ContactComponent implements OnInit { +export class ContactComponent { @Input() parentForm: UntypedFormGroup; @@ -31,13 +30,6 @@ export class ContactComponent implements OnInit { phoneInputDefaultCountry = 'US'; - constructor(private utils: UtilsService) { - } - - ngOnInit() { - this.parentForm.controls['email'].addValidators(this.utils.validateEmail) - } - changeCountry(countryCode: string) { this.phoneInputDefaultCountry = countryCode ?? 'US'; setTimeout(() => {