From adb05fb69074dad637cf1f9a7551a483e47ef2ce Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Wed, 24 Mar 2021 17:52:43 -0400 Subject: [PATCH] LP1904036 Field patterns Signed-off-by: Bill Erickson Signed-off-by: Jane Sandberg Signed-off-by: Galen Charlton --- .../src/app/staff/circ/patron/edit.component.html | 8 +-- .../src/app/staff/circ/patron/edit.component.ts | 70 +++++++++++++++++++++- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.html b/Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.html index fe3d34f8c8..75d06e0105 100644 --- a/Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.html +++ b/Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.html @@ -51,7 +51,7 @@ (ngModelChange)="fieldValueChange(args.path, args.index, args.field, $event)" (change)="afterFieldChange(args.path, args.index, args.field)" [required]="fieldRequired(getClass(args.cls) + '.' + args.field)" - [pattern]="fieldPattern(getClass(args.cls) + '.' + args.field)" + [pattern]="fieldPattern(getClass(args.cls), args.field)" [disabled]="args.disabled" /> @@ -69,7 +69,7 @@ (ngModelChange)="fieldValueChange(args.path, args.index, args.field, $event)" (change)="afterFieldChange(args.path, args.index, args.field)" [required]="fieldRequired(getClass(args.cls) + '.' + args.field)" - [pattern]="fieldPattern(getClass(args.cls) + '.' + args.field)" + [pattern]="fieldPattern(getClass(args.cls), args.field)" [disabled]="disabled" /> @@ -248,7 +248,7 @@ (ngModelChange)="fieldValueChange(null, null, 'name_keywords', $event)" (change)="afterFieldChange(null, args.index, 'name_keywords')" [required]="fieldRequired('au.name_keywords')" - [pattern]="fieldPattern('au.name_keywords')"> + [pattern]="fieldPattern('au', 'name_keywords')"> @@ -434,7 +434,7 @@ (ngModelChange)="fieldValueChange(null, null, 'alert_message', $event)" (change)="afterFieldChange(null, null, 'alert_message')" [required]="fieldRequired('au.alert_message')" - [pattern]="fieldPattern('au.alert_message')"> + [pattern]="fieldPattern('au', 'alert_message')"> diff --git a/Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.ts b/Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.ts index 4bdffa9e7b..7358fe7017 100644 --- a/Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/circ/patron/edit.component.ts @@ -122,6 +122,12 @@ export class EditComponent implements OnInit { expireDate: Date; changesPending = false; + fieldPatterns: {[cls: string]: {[field: string]: RegExp}} = { + au: {}, + ac: {}, + aua: {} + }; + fieldVisibility: {[key: string]: FieldVisibility} = {}; // All locations we have the specified permissions @@ -169,6 +175,7 @@ export class EditComponent implements OnInit { .then(_ => this.setInetLevels()) .then(_ => this.setOptInSettings()) .then(_ => this.setSmsCarriers()) + .then(_ => this.setFieldPatterns()) .then(_ => this.loading = false); } @@ -546,9 +553,10 @@ export class EditComponent implements OnInit { } - fieldPattern(field: string): string { - // TODO - return null; + fieldPattern(idlClass: string, field: string): RegExp { + if (!this.fieldPatterns[idlClass][field]) + this.fieldPatterns[idlClass][field] = new RegExp('.*'); + return this.fieldPatterns[idlClass][field]; } generatePassword() { @@ -857,6 +865,62 @@ export class EditComponent implements OnInit { canSave(): boolean { return document.querySelector('.ng-invalid') === null; } + + setFieldPatterns() { + let regex; + + if (regex = this.context.settingsCache['opac.username_regex']) { + this.fieldPatterns.au.usrname = new RegExp(regex); + } + + if (regex = + this.context.settingsCache['ui.patron.edit.ac.barcode.regex']) { + this.fieldPatterns.ac.barcode = new RegExp(regex); + } + + if (regex = this.context.settingsCache['global.password_regex']) { + this.fieldPatterns.au.passwd = new RegExp(regex); + } + + if (regex = this.context.settingsCache['ui.patron.edit.phone.regex']) { + // apply generic phone regex first, replace below as needed. + this.fieldPatterns.au.day_phone = new RegExp(regex); + this.fieldPatterns.au.evening_phone = new RegExp(regex); + this.fieldPatterns.au.other_phone = new RegExp(regex); + } + + // the remaining this.fieldPatterns fit a well-known key name pattern + + Object.keys(this.context.settingsCache).forEach(key => { + const val = this.context.settingsCache[key]; + if (!val) { return; } + const parts = key.match(/ui.patron.edit\.(\w+)\.(\w+)\.regex/); + if (!parts) { return; } + const cls = parts[1]; + const name = parts[2]; + this.fieldPatterns[cls][name] = new RegExp(val); + }); + } + + // The username must match either the configured regex or the + // patron's barcode + updateUsernameRegex() { + const regex = this.context.settingsCache['opac.username_regex']; + if (regex) { + const barcode = this.patron.card().barcode(); + if (barcode) { + this.fieldPatterns.au.usrname = + new RegExp(`${regex}|^${barcode}$`); + } else { + // username must match the regex + this.fieldPatterns.au.usrname = new RegExp(regex); + } + } else { + // username can be any format. + this.fieldPatterns.au.usrname = new RegExp('.*'); + } + } + } -- 2.11.0