[UXP-10] Notify unaccepted cookies - Fixes

This commit is contained in:
Davide Negretti
2022-09-08 20:06:36 +02:00
parent 05784bfec6
commit 97a12cae47
3 changed files with 57 additions and 23 deletions

View File

@@ -2,7 +2,7 @@
<h2>{{MESSAGE_PREFIX + '.header'|translate}}</h2> <h2>{{MESSAGE_PREFIX + '.header'|translate}}</h2>
<p>{{MESSAGE_PREFIX + '.info' | translate}}</p> <p>{{MESSAGE_PREFIX + '.info' | translate}}</p>
<form [class]="'ng-invalid'" [formGroup]="form" (ngSubmit)="register()"> <form [class]="'ng-invalid'" [formGroup]="form">
<div class="form-group"> <div class="form-group">
<div class="row"> <div class="row">
@@ -24,23 +24,31 @@
<div class="col-12"> <div class="col-12">
{{MESSAGE_PREFIX + '.email.hint' |translate}} {{MESSAGE_PREFIX + '.email.hint' |translate}}
</div> </div>
<div class="col-12" *ngIf="(googleRecaptchaService.captchaVersion() | async) === 'v2'">
<ds-google-recaptcha [captchaMode]="(googleRecaptchaService.captchaMode() | async)" (executeRecaptcha)="register($event)"></ds-google-recaptcha>
</div>
</div> </div>
</div> </div>
</form> </form>
<button *ngIf="(googleRecaptchaService.captchaVersion() | async) === 'v2' && (googleRecaptchaService.captchaMode() | async) === 'invisible'" <ds-alert [type]="AlertTypeEnum.Warning" *ngIf="!isRecaptchaCookieAccepted()">
class="btn btn-primary" <p class="m-0">{{ MESSAGE_PREFIX + '.google-recaptcha.must-accept-cookies' | translate }}</p>
[disabled]="form.invalid" <p class="m-0"><a href="javascript:void(0);" (click)="this.klaroService.showSettings()">{{ MESSAGE_PREFIX + '.google-recaptcha.open-cookie-settings' | translate }}</a></p>
(click)="executeRecaptcha()"> </ds-alert>
{{ MESSAGE_PREFIX + '.submit' | translate }}</button>
<button *ngIf="!((googleRecaptchaService.captchaVersion() | async) === 'v2' && (googleRecaptchaService.captchaMode() | async) === 'invisible')" <div class="my-3" *ngIf="(googleRecaptchaService.captchaVersion() | async) === 'v2'">
class="btn btn-primary" <ds-google-recaptcha [captchaMode]="(googleRecaptchaService.captchaMode() | async)" (executeRecaptcha)="register($event)"></ds-google-recaptcha>
[disabled]="form.invalid" </div>
(click)="register()">
{{ MESSAGE_PREFIX + '.submit' | translate }}</button> <ng-container *ngIf="!((googleRecaptchaService.captchaVersion() | async) === 'v2' && (googleRecaptchaService.captchaMode() | async) === 'invisible'); else v2Invisible">
<button class="btn btn-primary" [disabled]="form.invalid || (disableRegisterButton() | async)" (click)="register()">
{{ MESSAGE_PREFIX + '.submit' | translate }}
</button>
</ng-container>
<ng-template #v2Invisible>
<button class="btn btn-primary" [disabled]="form.invalid" (click)="executeRecaptcha()">
{{ MESSAGE_PREFIX + '.submit' | translate }}
</button>
</ng-template>
</div> </div>

View File

@@ -1,4 +1,4 @@
import { Component, Input, OnInit } from '@angular/core'; import { Component, Input, OnInit, Optional } from '@angular/core';
import { EpersonRegistrationService } from '../core/data/eperson-registration.service'; import { EpersonRegistrationService } from '../core/data/eperson-registration.service';
import { NotificationsService } from '../shared/notifications/notifications.service'; import { NotificationsService } from '../shared/notifications/notifications.service';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
@@ -11,8 +11,11 @@ import { getFirstSucceededRemoteDataPayload } from '../core/shared/operators';
import { ConfigurationProperty } from '../core/shared/configuration-property.model'; import { ConfigurationProperty } from '../core/shared/configuration-property.model';
import { isNotEmpty } from '../shared/empty.util'; import { isNotEmpty } from '../shared/empty.util';
import { combineLatest, Observable, of, switchMap } from 'rxjs'; import { combineLatest, Observable, of, switchMap } from 'rxjs';
import { map } from 'rxjs/operators'; import { map, take, tap } from 'rxjs/operators';
import { GoogleRecaptchaService } from '../core/google-recaptcha/google-recaptcha.service'; import { CAPTCHA_NAME, GoogleRecaptchaService } from '../core/google-recaptcha/google-recaptcha.service';
import { AlertType } from '../shared/alert/aletr-type';
import { KlaroService } from '../shared/cookies/klaro.service';
import { CookieService } from '../core/services/cookie.service';
@Component({ @Component({
selector: 'ds-register-email-form', selector: 'ds-register-email-form',
@@ -34,6 +37,8 @@ export class RegisterEmailFormComponent implements OnInit {
@Input() @Input()
MESSAGE_PREFIX: string; MESSAGE_PREFIX: string;
public AlertTypeEnum = AlertType;
/** /**
* registration verification configuration * registration verification configuration
*/ */
@@ -47,7 +52,6 @@ export class RegisterEmailFormComponent implements OnInit {
return this.googleRecaptchaService.captchaMode(); return this.googleRecaptchaService.captchaMode();
} }
constructor( constructor(
private epersonRegistrationService: EpersonRegistrationService, private epersonRegistrationService: EpersonRegistrationService,
private notificationService: NotificationsService, private notificationService: NotificationsService,
@@ -55,7 +59,9 @@ constructor(
private router: Router, private router: Router,
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
private configService: ConfigurationDataService, private configService: ConfigurationDataService,
public googleRecaptchaService: GoogleRecaptchaService public googleRecaptchaService: GoogleRecaptchaService,
public cookieService: CookieService,
@Optional() public klaroService: KlaroService,
) { ) {
} }
@@ -80,14 +86,13 @@ constructor(
* execute the captcha function for v2 invisible * execute the captcha function for v2 invisible
*/ */
executeRecaptcha() { executeRecaptcha() {
console.log('executeRecaptcha');
this.googleRecaptchaService.executeRecaptcha(); this.googleRecaptchaService.executeRecaptcha();
} }
/** /**
* Register an email address * Register an email address
*/ */
register(tokenV2 = null) { register(tokenV2?) {
if (!this.form.invalid) { if (!this.form.invalid) {
if (this.registrationVerification) { if (this.registrationVerification) {
combineLatest([this.captchaVersion(), this.captchaMode()]).pipe( combineLatest([this.captchaVersion(), this.captchaMode()]).pipe(
@@ -100,6 +105,7 @@ constructor(
return of(tokenV2); return of(tokenV2);
} }
}), }),
take(1),
).subscribe((token) => { ).subscribe((token) => {
if (isNotEmpty(token)) { if (isNotEmpty(token)) {
this.registration(token); this.registration(token);
@@ -134,6 +140,26 @@ constructor(
}); });
} }
isRecaptchaCookieAccepted(): boolean {
const klaroAnonymousCookie = this.cookieService.get('klaro-anonymous');
return isNotEmpty(klaroAnonymousCookie) ? klaroAnonymousCookie[CAPTCHA_NAME] : false;
}
disableRegisterButton(): Observable<boolean> {
return combineLatest([this.captchaVersion(), this.captchaMode()]).pipe(
switchMap(([captchaVersion, captchaMode]) => {
if (captchaVersion === 'v2' && captchaMode === 'checkbox') {
// this.googleRecaptchaService.getRecaptchaTokenResponse()
return of(false);
// TODO disable if captcha unchecked
} else {
return of(false);
}
}),
tap(console.log)
);
}
get email() { get email() {
return this.form.get('email'); return this.form.get('email');
} }

View File

@@ -75,7 +75,7 @@ export class BrowserKlaroService extends KlaroService {
this.configService.findByPropertyName('registration.verification.enabled').pipe( this.configService.findByPropertyName('registration.verification.enabled').pipe(
getFirstCompletedRemoteData(), getFirstCompletedRemoteData(),
).subscribe((remoteData) => { ).subscribe((remoteData) => {
if (!remoteData.hasSucceeded || isEmpty(remoteData.payload?.values) || remoteData.payload.values[0].toLowerCase() !== 'true') { if (remoteData.statusCode === 404 || isEmpty(remoteData.payload?.values) || remoteData.payload.values[0].toLowerCase() !== 'true') {
this.klaroConfig.services = klaroConfiguration.services.filter(config => config.name !== CAPTCHA_NAME); this.klaroConfig.services = klaroConfiguration.services.filter(config => config.name !== CAPTCHA_NAME);
} }
}); });