Skip to content

Commit f4f87cf

Browse files
vNovskiatarix83
authored andcommitted
Merged in DSC-1379-security-for-nested-metadata-shoul-take-security-level (pull request DSpace#1060)
DSC-1379 security for nested metadata shoul take security level Approved-by: Giuseppe Digilio
2 parents 8a3781e + 1112b40 commit f4f87cf

2 files changed

Lines changed: 146 additions & 39 deletions

File tree

src/app/shared/form/builder/ds-dynamic-form-ui/models/relation-group/modal/dynamic-relation-group-modal.components.ts

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,30 @@ export class DsDynamicRelationGroupModalComponent extends DynamicFormControlComp
6565
@Output() edit: EventEmitter<any> = new EventEmitter<any>();
6666
@Output() add: EventEmitter<any> = new EventEmitter<any>();
6767

68-
@ViewChild('formRef', {static: false}) private formRef: FormComponent;
68+
@ViewChild('formRef', { static: false }) private formRef: FormComponent;
6969

7070
public formModel: DynamicFormControlModel[];
7171
public vocabulary$: Observable<Vocabulary>;
72+
public securityLevelParent: number;
7273

7374
private subs: Subscription[] = [];
7475

7576

7677
constructor(private vocabularyService: VocabularyService,
77-
private formBuilderService: FormBuilderService,
78-
private formService: FormService,
79-
private cdr: ChangeDetectorRef,
80-
protected layoutService: DynamicFormLayoutService,
81-
protected validationService: DynamicFormValidationService,
82-
protected modalService: NgbModal,
83-
protected submissionService: SubmissionService,
84-
private activeModal: NgbActiveModal
78+
private formBuilderService: FormBuilderService,
79+
private formService: FormService,
80+
private cdr: ChangeDetectorRef,
81+
protected layoutService: DynamicFormLayoutService,
82+
protected validationService: DynamicFormValidationService,
83+
protected modalService: NgbModal,
84+
protected submissionService: SubmissionService,
85+
private activeModal: NgbActiveModal
8586
) {
8687
super(layoutService, validationService);
8788
}
8889

8990
ngOnInit() {
90-
const config = {rows: this.model.formConfiguration} as SubmissionFormsModel;
91+
const config = { rows: this.model.formConfiguration } as SubmissionFormsModel;
9192
this.formId = this.formService.getUniqueId(this.model.id);
9293
this.formModel = this.formBuilderService.modelFromConfiguration(
9394
this.model.submissionId,
@@ -113,10 +114,8 @@ export class DsDynamicRelationGroupModalComponent extends DynamicFormControlComp
113114
if (isNotEmpty(nextValue)) {
114115
model.value = nextValue;
115116
}
116-
// as the value doesn't support the security level, add into the big model
117-
if (value && typeof value !== 'string') {
118-
(model as any).securityLevel = value.securityLevel;
119-
}
117+
118+
this.initSecurityLevelConfig(model, modelRow);
120119
});
121120
});
122121
}
@@ -262,6 +261,7 @@ export class DsDynamicRelationGroupModalComponent extends DynamicFormControlComp
262261
modelRow.group.forEach((model: DynamicInputModel) => {
263262
if (model.name === this.model.mandatoryField) {
264263
mandatoryFieldModel = model;
264+
this.initSecurityLevelConfig(model, modelRow);
265265
return;
266266
}
267267
});
@@ -293,12 +293,16 @@ export class DsDynamicRelationGroupModalComponent extends DynamicFormControlComp
293293
const item = Object.create({});
294294
this.formModel.forEach((row) => {
295295
const modelRow = row as DynamicFormGroupModel;
296+
const mainRow: any = modelRow.group.find(model => model.name === this.model.name);
296297
modelRow.group.forEach((control: DynamicInputModel) => {
297298
const controlValue: any = (control?.value as any)?.value || control?.value || PLACEHOLDER_PARENT_METADATA;
298299
const controlAuthority: any = (control?.value as any)?.authority || null;
300+
299301
item[control.name] =
300302
new FormFieldMetadataValueObject(
301-
controlValue, (control as any)?.language, (control as any)?.securityLevel, controlAuthority,
303+
controlValue, (control as any)?.language,
304+
controlValue === PLACEHOLDER_PARENT_METADATA ? null : mainRow.securityLevel,
305+
controlAuthority,
302306
null, 0, null,
303307
(control?.value as any)?.otherInformation || null
304308
);
@@ -307,6 +311,33 @@ export class DsDynamicRelationGroupModalComponent extends DynamicFormControlComp
307311
return item;
308312
}
309313

314+
private initSecurityLevelConfig(chipModel: DynamicInputModel, modelGroup: DynamicFormGroupModel) {
315+
if (this.model.name === chipModel.name && this.model.securityConfigLevel.length > 1) {
316+
(chipModel as any).securityConfigLevel = this.model.securityConfigLevel;
317+
(chipModel as any).toggleSecurityVisibility = true;
318+
319+
const mainRow = modelGroup.group.find(itemModel => itemModel.name === this.model.name);
320+
321+
(chipModel as any).securityLevel = (mainRow as any).securityLevel || 0;
322+
this.securityLevelParent = (mainRow as any).securityLevel;
323+
324+
modelGroup.group.forEach((item: any) => {
325+
if (item.name !== this.model.name) {
326+
item.securityConfigLevel = this.model.securityConfigLevel;
327+
item.toggleSecurityVisibility = false;
328+
item.securityLevel = this.securityLevelParent;
329+
}
330+
});
331+
}
332+
if (this.model.securityConfigLevel.length === 1) {
333+
modelGroup.group.forEach((item: any) => {
334+
item.securityConfigLevel = this.model.securityConfigLevel;
335+
item.toggleSecurityVisibility = false;
336+
item.securityLevel = this.model.securityLevel;
337+
});
338+
}
339+
}
340+
310341
private retrieveVocabulary(vocabularyOptions: VocabularyOptions): void {
311342
this.vocabulary$ = this.vocabularyService.findVocabularyById(vocabularyOptions.name).pipe(
312343
getFirstSucceededRemoteDataPayload(),

src/app/shared/form/builder/ds-dynamic-form-ui/models/relation-inline-group/dynamic-relation-inline-group.components.ts

Lines changed: 100 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
DynamicFormControlModel,
1111
DynamicFormGroupModel,
1212
DynamicFormLayoutService,
13-
DynamicFormValidationService
13+
DynamicFormValidationService,
14+
DynamicInputModel
1415
} from '@ng-dynamic-forms/core';
1516

1617
import { DynamicRelationGroupModel } from '../relation-group/dynamic-relation-group.model';
@@ -49,24 +50,24 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
4950
public formGroup: FormGroup;
5051
public formModel: DynamicFormControlModel[];
5152

52-
@ViewChild('formRef', {static: false}) private formRef: FormComponent;
53+
@ViewChild('formRef', { static: false }) private formRef: FormComponent;
5354
protected metadataSecurityConfiguration: MetadataSecurityConfiguration;
5455

5556
constructor(private formBuilderService: FormBuilderService,
56-
private formService: FormService,
57-
protected layoutService: DynamicFormLayoutService,
58-
protected submissionService: SubmissionService,
59-
protected validationService: DynamicFormValidationService
57+
private formService: FormService,
58+
protected layoutService: DynamicFormLayoutService,
59+
protected submissionService: SubmissionService,
60+
protected validationService: DynamicFormValidationService
6061
) {
6162
super(layoutService, validationService);
6263
}
6364

6465
ngOnInit() {
6566
this.submissionService.getSubmissionSecurityConfiguration(this.model.submissionId).pipe(
6667
take(1)).subscribe(security => {
67-
this.metadataSecurityConfiguration = security;
68-
});
69-
const config = {rows: this.model.formConfiguration} as SubmissionFormsModel;
68+
this.metadataSecurityConfiguration = security;
69+
});
70+
const config = { rows: this.model.formConfiguration } as SubmissionFormsModel;
7071

7172
this.formId = this.formService.getUniqueId(this.model.id);
7273
this.formModel = this.initArrayModel(config);
@@ -76,7 +77,6 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
7677

7778
initArrayModel(formConfig): DynamicRowArrayModel[] {
7879
let arrayCounter = 0;
79-
8080
const config = {
8181
id: this.model.id + '_array',
8282
initialCount: isNotEmpty(this.model.value) ? (this.model.value as any[]).length : 1,
@@ -114,8 +114,12 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
114114
this.model.readOnly,
115115
this.formBuilderService.getTypeBindModel(),
116116
true,
117-
this.metadataSecurityConfiguration);
118-
return formModel[0];
117+
this.metadataSecurityConfiguration)[0];
118+
119+
(formModel as any).group?.forEach((modelItem: DynamicInputModel) => {
120+
this.initSecurityLevelConfig(modelItem, (formModel as any));
121+
});
122+
return formModel;
119123
}
120124

121125
onBlur(event: DynamicFormControlEvent) {
@@ -124,7 +128,11 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
124128

125129
onChange(event: DynamicFormControlEvent) {
126130
const index = event.model.parent.parent.index;
127-
const groupValue = this.getRowValue(event.model.parent as DynamicFormGroupModel);
131+
let parentSecurityLevel;
132+
if (event.type === 'change') {
133+
parentSecurityLevel = this.model.securityLevel;
134+
}
135+
const groupValue = this.getRowValue(event.model.parent as DynamicFormGroupModel, parentSecurityLevel);
128136

129137
if (this.hasEmptyGroupValue(groupValue)) {
130138
this.removeItemFromArray(event);
@@ -155,24 +163,76 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
155163
}
156164
}
157165

158-
private getRowValue(formGroup: DynamicFormGroupModel) {
166+
private findModelGroups() {
167+
this.formModel.forEach((row: any) => {
168+
row.groups.forEach((groupArray) => {
169+
groupArray.group.forEach((groupRow) => {
170+
const modelRow = groupRow as DynamicFormGroupModel;
171+
modelRow.group.forEach((model: DynamicInputModel) => {
172+
this.initSecurityLevelConfig(model, modelRow);
173+
});
174+
});
175+
});
176+
});
177+
}
178+
179+
private initSecurityLevelConfig(model: DynamicInputModel | any, modelGroup: DynamicFormGroupModel) {
180+
if (this.model.name === model.name && this.model.securityConfigLevel?.length > 1) {
181+
model.securityConfigLevel = this.model.securityConfigLevel;
182+
model.toggleSecurityVisibility = true;
183+
184+
let mainSecurityLevel;
185+
const mainRow = modelGroup.group.find(itemModel => itemModel.name === this.model.name);
186+
if (isNotEmpty(this.model.securityLevel)) {
187+
mainSecurityLevel = this.model.securityLevel;
188+
} else {
189+
mainSecurityLevel = (mainRow as any).securityLevel;
190+
}
191+
192+
model.securityLevel = mainSecurityLevel;
193+
194+
modelGroup.group.forEach((item: any) => {
195+
if (item.name !== this.model.name) {
196+
item.securityConfigLevel = this.model.securityConfigLevel;
197+
item.toggleSecurityVisibility = false;
198+
item.securityLevel = mainSecurityLevel;
199+
}
200+
});
201+
}
202+
if (this.model.securityConfigLevel?.length === 1) {
203+
modelGroup.group.forEach((item: any) => {
204+
item.securityConfigLevel = this.model.securityConfigLevel;
205+
item.toggleSecurityVisibility = false;
206+
item.securityLevel = this.model.securityLevel;
207+
});
208+
}
209+
}
210+
211+
private getRowValue(formGroup: DynamicFormGroupModel, securityLevel?: number) {
212+
let mainSecurityLevel;
213+
if (isNotEmpty(securityLevel)) {
214+
mainSecurityLevel = securityLevel;
215+
} else {
216+
const mainRow = formGroup.group.find(itemModel => itemModel.name === this.model.name);
217+
mainSecurityLevel = (mainRow as any).securityLevel;
218+
}
159219
const groupValue = Object.create({});
160220
formGroup.group.forEach((model: any) => {
161221
if (model.name !== this.model.mandatoryField) {
162222
if (isEmpty(model.value)) {
163223
groupValue[model.name] = PLACEHOLDER_PARENT_METADATA;
164224
} else {
165225
if (typeof model.value === 'string') {
166-
groupValue[model.name] = new FormFieldMetadataValueObject(model.value, null, model.securityLevel);
226+
groupValue[model.name] = new FormFieldMetadataValueObject(model.value, null, mainSecurityLevel);
167227
} else {
168-
groupValue[model.name] = model.value;
228+
groupValue[model.name] = Object.assign(new FormFieldMetadataValueObject(), model.value, { securityLevel: mainSecurityLevel || null });
169229
}
170230
}
171231
} else {
172232
if (typeof model.value === 'string') {
173-
groupValue[model.name] = new FormFieldMetadataValueObject(model.value, null, model.securityLevel);
233+
groupValue[model.name] = new FormFieldMetadataValueObject(model.value, null, mainSecurityLevel);
174234
} else {
175-
groupValue[model.name] = model.value;
235+
groupValue[model.name] = Object.assign(new FormFieldMetadataValueObject(), model.value, { securityLevel: mainSecurityLevel || null });
176236
}
177237
}
178238
});
@@ -192,7 +252,7 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
192252
return normValue;
193253
}
194254

195-
private hasPlaceholder(value: string|FormFieldMetadataValueObject): boolean {
255+
private hasPlaceholder(value: string | FormFieldMetadataValueObject): boolean {
196256
return (value instanceof FormFieldMetadataValueObject) ? value.hasPlaceholder() : (isNotEmpty(value) && value === PLACEHOLDER_PARENT_METADATA);
197257
}
198258

@@ -216,15 +276,29 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
216276
}
217277

218278
private updateArrayModelValue(groupValue, index) {
219-
let modelValue = this.model.value;
279+
let parentSecurityLevel = this.model.securityLevel || this.model.securityConfigLevel?.length > 0 ? this.model.securityConfigLevel[0] : null;
280+
for (const name of Object.keys(groupValue)) {
281+
if (name === this.model.name && isNotEmpty(groupValue[name].securityLevel)) {
282+
parentSecurityLevel = groupValue[name].securityLevel;
283+
break;
284+
}
285+
}
286+
if (isNotEmpty(parentSecurityLevel)) {
287+
Object.keys(groupValue).forEach(model => {
288+
if (groupValue[model] instanceof Object) {
289+
groupValue[model].securityLevel = parentSecurityLevel;
290+
}
291+
});
292+
this.model.securityLevel = parentSecurityLevel;
293+
}
220294

295+
let modelValue = this.model.value;
221296
if (isEmpty(modelValue)) {
222297
modelValue = [groupValue];
223298
} else {
224299
modelValue[index] = groupValue;
225300
}
226301
this.model.value = modelValue;
227-
this.change.emit();
228302
}
229303

230304
onCustomEvent(event) {
@@ -246,7 +320,7 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
246320

247321
if (arrayOfValue[index] === undefined || arrayOfValue[previousIndex] === undefined) {
248322
return;
249-
} else if ( arrayOfValue.length > 0 ) {
323+
} else if (arrayOfValue.length > 0) {
250324
arrayOfValue = arrayOfValue.filter((el) => el !== undefined);
251325
} else {
252326
return;
@@ -260,9 +334,11 @@ export class DsDynamicRelationInlineGroupComponent extends DynamicFormControlCom
260334
}
261335

262336
private copyArrayItem(event) {
263-
const index = event.model.parent.index;
264-
const groupValue = this.getRowValue(event.model as DynamicFormGroupModel);
337+
const index = Array.isArray(this.model.value) ? this.model.value.length : event.model.parent.index;
338+
const mainRow = event.model.group.find(itemModel => itemModel.name === this.model.name);
339+
const groupValue = this.getRowValue(event.model as DynamicFormGroupModel, mainRow.securityLevel);
265340
this.updateArrayModelValue(groupValue, index);
341+
this.findModelGroups();
266342
this.change.emit();
267343
}
268344
}

0 commit comments

Comments
 (0)