-
Notifications
You must be signed in to change notification settings - Fork 188
/
PageAdminFocus.java
864 lines (747 loc) · 38.1 KB
/
PageAdminFocus.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
/*
* Copyright (c) 2010-2019 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.web.page.admin;
import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.gui.api.prism.ItemStatus;
import com.evolveum.midpoint.gui.api.prism.PrismObjectWrapper;
import com.evolveum.midpoint.gui.api.prism.ShadowWrapper;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.gui.impl.factory.PrismObjectWrapperFactory;
import com.evolveum.midpoint.gui.impl.factory.WrapperContext;
import com.evolveum.midpoint.gui.impl.prism.PrismObjectValueWrapper;
import com.evolveum.midpoint.model.api.ModelExecuteOptions;
import com.evolveum.midpoint.model.api.context.*;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.delta.*;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.prism.schema.SchemaRegistry;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.NoFocusNameSchemaException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.assignment.AssignmentEditorDto;
import com.evolveum.midpoint.web.component.progress.ProgressReportingAwarePage;
import com.evolveum.midpoint.web.page.admin.users.component.AssignmentInfoDto;
import com.evolveum.midpoint.web.page.admin.users.dto.UserDtoStatus;
import com.evolveum.midpoint.web.util.validation.MidpointFormValidator;
import com.evolveum.midpoint.web.util.validation.SimpleValidationError;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
public abstract class PageAdminFocus<F extends FocusType> extends PageAdminObjectDetails<F>
implements ProgressReportingAwarePage {
private static final long serialVersionUID = 1L;
private LoadableModel<List<ShadowWrapper>> projectionModel;
private LoadableModel<List<AssignmentEditorDto>> delegatedToMeModel;
private static final String DOT_CLASS = PageAdminFocus.class.getName() + ".";
private static final String OPERATION_RECOMPUTE_ASSIGNMENTS = DOT_CLASS + "recomputeAssignments";
private static final String OPERATION_LOAD_SHADOW = DOT_CLASS + "loadShadow";
private static final Trace LOGGER = TraceManager.getTrace(PageAdminFocus.class);
public PageAdminFocus() {
initialize(null);
}
public PageAdminFocus(PageParameters parameters) {
getPageParameters().overwriteWith(parameters);
initialize(null);
}
public PageAdminFocus(final PrismObject<F> userToEdit) {
initialize(userToEdit);
}
public PageAdminFocus(final PrismObject<F> unitToEdit, boolean isNewObject) {
initialize(unitToEdit, isNewObject);
}
public PageAdminFocus(final PrismObject<F> unitToEdit, boolean isNewObject, boolean isReadonly) {
initialize(unitToEdit, isNewObject, isReadonly);
}
@Override
protected void initializeModel(final PrismObject<F> objectToEdit, boolean isNewObject, boolean isReadonly) {
super.initializeModel(objectToEdit, isNewObject, isReadonly);
projectionModel = new LoadableModel<List<ShadowWrapper>>(false) {
private static final long serialVersionUID = 1L;
@Override
protected List<ShadowWrapper> load() {
return loadShadowWrappers(true);
}
};
delegatedToMeModel= new LoadableModel<List<AssignmentEditorDto>>(false) {
private static final long serialVersionUID = 1L;
@Override
protected List<AssignmentEditorDto> load() {
return loadDelegatedToMe();
}
};
}
public LoadableModel<List<ShadowWrapper>> getProjectionModel() {
return projectionModel;
}
public LoadableModel<List<AssignmentEditorDto>> getDelegatedToMeModel() {
return delegatedToMeModel;
}
public List<ShadowWrapper> getFocusShadows() {
return projectionModel.getObject();
}
protected void reviveModels() throws SchemaException {
super.reviveModels();
WebComponentUtil.revive(projectionModel, getPrismContext());
}
@Override
public void continueEditing(AjaxRequestTarget target) {
getMainPanel().setVisible(true);
getProgressPanel().hide();
getProgressPanel().hideAbortButton(target);
getProgressPanel().hideBackButton(target);
getProgressPanel().hideContinueEditingButton(target);
target.add(this);
}
private List<ShadowWrapper> loadShadowWrappers(boolean noFetch) {
List<ShadowWrapper> list = new ArrayList<>();
PrismObjectWrapper<F> focusWrapper = getObjectModel().getObject();
PrismObject<F> focus = focusWrapper.getObject();
PrismReference prismReference = focus.findReference(UserType.F_LINK_REF);
if (prismReference == null || prismReference.isEmpty()) {
return new ArrayList<>();
}
List<PrismReferenceValue> references = prismReference.getValues();
Task task = createSimpleTask(OPERATION_LOAD_SHADOW);
for (PrismReferenceValue reference : references) {
if(reference == null || (reference.getOid() == null && reference.getTargetType() == null)) {
LOGGER.trace("Skiping reference for shadow with null oid");
continue; // default value
}
OperationResult subResult = task.getResult().createMinorSubresult(OPERATION_LOAD_SHADOW);
PrismObject<ShadowType> projection = getPrismObjectForWrapper(ShadowType.class, reference.getOid(),
noFetch, task, subResult, createLoadOptionForShadowWrapper());
if(projection == null) {
// showResult(subResult, "pageAdminFocus.message.couldntLoadShadowProjection");
LOGGER.error("Couldn't load shadow projection");
continue;
}
try {
ShadowWrapper wrapper = loadShadowWrapper(projection, task, subResult);
wrapper.setLoadWithNoFetch(noFetch);
if (wrapper != null) {
list.add((ShadowWrapper)wrapper);
} else {
showResult(subResult, "pageAdminFocus.message.shadowWrapperIsNull");
LOGGER.error("ShadowWrapper is null");
}
//TODO catch Exception/Runtim,eException, Throwable
} catch (SchemaException e) {
showResult(subResult, "pageAdminFocus.message.couldntCreateShadowWrapper");
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't create shadow wrapper", e);
}
}
return list;
}
private Collection<SelectorOptions<GetOperationOptions>> createLoadOptionForShadowWrapper(){
return getSchemaHelper().getOperationOptionsBuilder()
.item(ShadowType.F_RESOURCE_REF).resolve().readOnly()
.build();
}
public ShadowWrapper loadShadowWrapper(PrismObject<ShadowType> projection, Task task, OperationResult result) throws SchemaException{
PrismObjectWrapperFactory<ShadowType> factory = getRegistry().getObjectWrapperFactory(projection.getDefinition());
WrapperContext context = new WrapperContext(task, result);
context.setCreateIfEmpty(false);
ShadowWrapper wrapper = (ShadowWrapper) factory.createObjectWrapper(projection, ItemStatus.NOT_CHANGED, context);
wrapper.setProjectionStatus(UserDtoStatus.MODIFY);
return wrapper;
}
public void loadFullShadow(PrismObjectValueWrapper<ShadowType> shadowWrapperValue, AjaxRequestTarget target) {
if(shadowWrapperValue.getRealValue() == null) {
error(getString("pageAdminFocus.message.couldntCreateShadowWrapper"));
LOGGER.error("Couldn't create shadow wrapper, because RealValue is null in " + shadowWrapperValue);
return;
}
String oid = shadowWrapperValue.getRealValue().getOid();
Task task = createSimpleTask(OPERATION_LOAD_SHADOW);
OperationResult result = task.getResult();
PrismObject<ShadowType> projection = getPrismObjectForWrapper(ShadowType.class, oid, false, task,
result, createLoadOptionForShadowWrapper());
if (projection == null) {
result.recordFatalError(getString("PageAdminFocus.message.loadFullShadow.fatalError", shadowWrapperValue.getRealValue()));
showResult(result);
target.add(getFeedbackPanel());
return;
}
ShadowWrapper shadowWrapperNew;
try {
shadowWrapperNew = loadShadowWrapper(projection, task, result);
if (shadowWrapperNew == null) {
error(getString("pageAdminFocus.message.shadowWrapperIsNull"));
LOGGER.error("ShadowWrapper is null");
return;
}
shadowWrapperValue.getItems().clear();
shadowWrapperValue.getItems().addAll((Collection) shadowWrapperNew.getValue().getItems());
((ShadowWrapper)shadowWrapperValue.getParent()).setLoadWithNoFetch(false);
} catch (SchemaException e) {
error(getString("pageAdminFocus.message.couldntCreateShadowWrapper"));
LOGGER.error("Couldn't create shadow wrapper", e);
}
}
// @Override
// protected List<FocusSubwrapperDto<OrgType>> loadOrgWrappers() {
// return loadSubwrappers(OrgType.class, UserType.F_PARENT_ORG_REF, false);
// }
// private <S extends ObjectType> List<FocusSubwrapperDto<S>> loadSubwrappers(Class<S> type,
// ItemName propertyToLoad, boolean noFetch) {
// List<FocusSubwrapperDto<S>> list = new ArrayList<>();
//
// PrismObjectWrapper<F> focusWrapper = getObjectModel().getObject();
// PrismObject<F> focus = focusWrapper.getObject();
// PrismReference prismReference = focus.findReference(propertyToLoad);
// if (prismReference == null) {
// return new ArrayList<>();
// }
// List<PrismReferenceValue> references = prismReference.getValues();
//
// Task task = createSimpleTask(OPERATION_LOAD_SHADOW);
// for (PrismReferenceValue reference : references) {
// FocusSubwrapperDto<S> subWrapper = loadSubWrapperDto(type, reference.getOid(), noFetch, task);
// if (subWrapper != null) {
// list.add(subWrapper);
// }
// }
//
// return list;
// }
private <S extends ObjectType> PrismObject<S> getPrismObjectForWrapper(Class<S> type, String oid, boolean noFetch,
Task task, OperationResult subResult, Collection<SelectorOptions<GetOperationOptions>> loadOptions){
if (oid == null) {
return null;
}
if (noFetch) {
GetOperationOptions rootOptions = SelectorOptions.findRootOptions(loadOptions);
if (rootOptions == null) {
loadOptions.add(new SelectorOptions<>(GetOperationOptions.createNoFetch()));
} else {
rootOptions.setNoFetch(true);
}
}
PrismObject<S> projection = WebModelServiceUtils.loadObject(type, oid, loadOptions, this, task, subResult);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Loaded projection {} ({}):\n{}", oid, loadOptions, projection==null?null:projection.debugDump());
}
return projection;
}
private List<AssignmentEditorDto> loadDelegatedToMe() {
List<AssignmentEditorDto> list = new ArrayList<>();
PrismObjectWrapper<F> focusWrapper = getObjectModel().getObject();
PrismObject<F> focus = focusWrapper.getObject();
List<AssignmentType> assignments = focus.asObjectable().getAssignment();
for (AssignmentType assignment : assignments) {
if (assignment.getTargetRef() != null &&
UserType.COMPLEX_TYPE.equals(assignment.getTargetRef().getType())) {
AssignmentEditorDto dto = new AssignmentEditorDto(UserDtoStatus.MODIFY, assignment, this);
dto.setSimpleView(true);
dto.setEditable(false);
list.add(dto);
}
}
Collections.sort(list);
return list;
}
@Override
protected void prepareObjectForAdd(PrismObject<F> focus) throws SchemaException {
super.prepareObjectForAdd(focus);
F focusType = focus.asObjectable();
// handle added accounts
List<ShadowType> shadowsToAdd = prepareShadowObject(getFocusShadows());
for (ShadowType shadowToAdd : shadowsToAdd) {
addDefaultKindAndIntent(shadowToAdd.asPrismObject());
ObjectReferenceType linkRef = new ObjectReferenceType();
linkRef.asReferenceValue().setObject(shadowToAdd.asPrismObject());
focusType.getLinkRef().add(linkRef);
}
// List<OrgType> orgsToAdd = prepareSubobject(getParentOrgs());
// if (!orgsToAdd.isEmpty()){
// focusType.getParentOrg().addAll(orgsToAdd);
// }
}
@Override
protected void prepareObjectDeltaForModify(ObjectDelta<F> focusDelta) throws SchemaException {
super.prepareObjectDeltaForModify(focusDelta);
// handle accounts
PrismObjectDefinition<F> objectDefinition = getObjectDefinition();
PrismReferenceDefinition refDef = objectDefinition.findReferenceDefinition(FocusType.F_LINK_REF);
ReferenceDelta refDelta = prepareUserAccountsDeltaForModify(refDef);
if (!refDelta.isEmpty()) {
focusDelta.addModification(refDelta);
}
// refDef = objectDefinition.findReferenceDefinition(FocusType.F_PARENT_ORG_REF);
// refDelta = prepareUserOrgsDeltaForModify(refDef);
// if (!refDelta.isEmpty()) {
// focusDelta.addModification(refDelta);
// }
}
protected PrismObjectDefinition<F> getObjectDefinition() {
SchemaRegistry registry = getPrismContext().getSchemaRegistry();
return registry
.findObjectDefinitionByCompileTimeClass(getCompileTimeClass());
}
protected ContainerDelta handleAssignmentDeltas(ObjectDelta<F> focusDelta,
List<AssignmentEditorDto> assignments, PrismContainerDefinition def,
boolean isDelegation) throws SchemaException {
ContainerDelta assDelta = getPrismContext().deltaFactory().container().create(ItemPath.EMPTY_PATH, def.getItemName(), def);
for (AssignmentEditorDto assDto : assignments) {
PrismContainerValue newValue = assDto.getNewValue(getPrismContext());
switch (assDto.getStatus()) {
case ADD:
newValue.applyDefinition(def, false);
assDelta.addValueToAdd(newValue.clone());
break;
case DELETE:
PrismContainerValue oldValue = assDto.getOldValue();
if (isDelegation){
oldValue.applyDefinition(def, false);
} else {
oldValue.applyDefinition(def);
}
assDelta.addValueToDelete(oldValue.clone());
break;
case MODIFY:
if (!assDto.isModified(getPrismContext())) {
LOGGER.trace("Assignment '{}' not modified.", new Object[] { assDto.getName() });
continue;
}
handleModifyAssignmentDelta(assDto, def, newValue, focusDelta);
break;
default:
warn(getString("pageAdminUser.message.illegalAssignmentState", assDto.getStatus()));
}
}
if (!assDelta.isEmpty()) {
assDelta = focusDelta.addModification(assDelta);
}
return assDelta;
}
private void handleModifyAssignmentDelta(AssignmentEditorDto assDto,
PrismContainerDefinition assignmentDef, PrismContainerValue newValue, ObjectDelta<F> focusDelta)
throws SchemaException {
LOGGER.debug("Handling modified assignment '{}', computing delta.",
new Object[] { assDto.getName() });
PrismValue oldValue = assDto.getOldValue();
Collection<? extends ItemDelta> deltas = oldValue.diff(newValue);
for (ItemDelta delta : deltas) {
ItemPath deltaPath = delta.getPath().rest();
ItemDefinition deltaDef = assignmentDef.findItemDefinition(deltaPath);
delta.setParentPath(WebComponentUtil.joinPath(oldValue.getPath(), delta.getPath().allExceptLast()));
delta.applyDefinition(deltaDef);
focusDelta.addModification(delta);
}
}
@Override
protected boolean executeForceDelete(PrismObjectWrapper<F> userWrapper, Task task, ModelExecuteOptions options,
OperationResult parentResult) {
if (isForce()) {
OperationResult result = parentResult.createSubresult("Force delete operation");
try {
ObjectDelta<F> forceDeleteDelta = getForceDeleteDelta(userWrapper);
forceDeleteDelta.revive(getPrismContext());
if (forceDeleteDelta != null && !forceDeleteDelta.isEmpty()) {
getModelService().executeChanges(WebComponentUtil.createDeltaCollection(forceDeleteDelta),
options, task, result);
}
} catch (Exception ex) {
result.recordFatalError(getString("PageAdminFocus.message.executeForceDelete.fatalError"));
LoggingUtils.logUnexpectedException(LOGGER, "Failed to execute delete operation with force", ex);
return false;
}
result.recomputeStatus();
result.recordSuccessIfUnknown();
return true;
}
return false;
}
private ObjectDelta<F> getForceDeleteDelta(PrismObjectWrapper<F> focusWrapper) throws SchemaException {
List<ShadowWrapper> accounts = getFocusShadows();
List<ReferenceDelta> refDeltas = new ArrayList<>();
ObjectDelta<F> forceDeleteDelta = null;
for (ShadowWrapper account : accounts) {
if (account.getProjectionStatus() == UserDtoStatus.DELETE) {
ReferenceDelta refDelta = getPrismContext().deltaFactory().reference().createModificationDelete(UserType.F_LINK_REF,
focusWrapper.getObject().getDefinition(), account.getObject());
refDeltas.add(refDelta);
} else if (account.getProjectionStatus() == UserDtoStatus.UNLINK) {
ReferenceDelta refDelta = getPrismContext().deltaFactory().reference().createModificationDelete(UserType.F_LINK_REF,
focusWrapper.getObject().getDefinition(), account.getObject().getOid());
refDeltas.add(refDelta);
}
}
if (!refDeltas.isEmpty()) {
forceDeleteDelta = getPrismContext().deltaFactory().object()
.createModifyDelta(focusWrapper.getObject().getOid(), refDeltas,
getCompileTimeClass());
}
if (forceDeleteDelta == null) {
forceDeleteDelta = getPrismContext().deltaFactory().object().createEmptyModifyDelta(getCompileTimeClass(),
focusWrapper.getObject().getOid());
}
return forceDeleteDelta;
}
// private <P extends ObjectType> List<P> prepareSubobject(List<FocusSubwrapperDto<P>> projections) throws SchemaException{
// List<P> projectionsToAdd = new ArrayList<>();
// for (FocusSubwrapperDto<P> projection : projections) {
// if (!projection.isLoadedOK()) {
// continue;
// }
//
// if (UserDtoStatus.MODIFY.equals(projection.getStatus())) {
// // this is legal e.g. when child org is being create (one assignment comes pre-created)
// // TODO do we need more specific checks here?
// continue;
// }
//
// if (!UserDtoStatus.ADD.equals(projection.getStatus())) {
// warn(getString("pageAdminFocus.message.illegalAccountState", projection.getStatus()));
// continue;
// }
//
// ObjectWrapperOld<P> projectionWrapper = projection.getObjectOld();
// ObjectDelta<P> delta = projectionWrapper.getObjectDelta();
// PrismObject<P> proj = delta.getObjectToAdd();
// WebComponentUtil.encryptCredentials(proj, true, getMidpointApplication());
//
// projectionsToAdd.add(proj.asObjectable());
// }
// return projectionsToAdd;
// }
private List<ShadowType> prepareShadowObject(List<ShadowWrapper> projections) throws SchemaException{
List<ShadowType> projectionsToAdd = new ArrayList<>();
for (ShadowWrapper projection : projections) {
if (UserDtoStatus.MODIFY.equals(projection.getProjectionStatus())) {
// this is legal e.g. when child org is being create (one assignment comes pre-created)
// TODO do we need more specific checks here?
continue;
}
if (!UserDtoStatus.ADD.equals(projection.getProjectionStatus())) {
warn(getString("pageAdminFocus.message.illegalAccountState", projection.getStatus()));
continue;
}
ObjectDelta<ShadowType> delta = projection.getObjectDelta();
PrismObject<ShadowType> proj = delta.getObjectToAdd();
WebComponentUtil.encryptCredentials(proj, true, getMidpointApplication());
projectionsToAdd.add(proj.asObjectable());
}
return projectionsToAdd;
}
@Override
protected List<ObjectDelta<? extends ObjectType>> getAdditionalModifyDeltas(OperationResult result) {
return getShadowModifyDeltas(result);
}
private List<ObjectDelta<? extends ObjectType>> getShadowModifyDeltas(OperationResult result) {
List<ObjectDelta<? extends ObjectType>> deltas = new ArrayList<>();
List<ShadowWrapper> accounts = getFocusShadows();
for (ShadowWrapper account : accounts) {
try {
ObjectDelta<ShadowType> delta = account.getObjectDelta();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Account delta computed from {} as:\n{}",
new Object[] { account, delta.debugDump(3) });
}
if (!UserDtoStatus.MODIFY.equals(account.getProjectionStatus())) {
continue;
}
if (delta == null || delta.isEmpty()) {
continue;
}
WebComponentUtil.encryptCredentials(delta, true, getMidpointApplication());
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Modifying account:\n{}", new Object[] { delta.debugDump(3) });
}
deltas.add(delta);
} catch (Exception ex) {
result.recordFatalError(getString("PageAdminFocus.message.getShadowModifyDeltas.fatalError"), ex);
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't compute account delta", ex);
}
}
return deltas;
}
private ReferenceDelta prepareUserAccountsDeltaForModify(PrismReferenceDefinition refDef)
throws SchemaException {
ReferenceDelta refDelta = getPrismContext().deltaFactory().reference().create(refDef);
List<ShadowWrapper> accounts = getFocusShadows();
for (ShadowWrapper accountWrapper : accounts) {
accountWrapper.revive(getPrismContext());
ObjectDelta delta = accountWrapper.getObjectDelta();
PrismReferenceValue refValue = getPrismContext().itemFactory().createReferenceValue(null, OriginType.USER_ACTION, null);
PrismObject<ShadowType> account;
switch (accountWrapper.getProjectionStatus()) {
case ADD:
account = delta.getObjectToAdd();
if (skipAddShadow(account.asObjectable().getResourceRef(), accounts)) {
break;
}
addDefaultKindAndIntent(account);
WebComponentUtil.encryptCredentials(account, true, getMidpointApplication());
refValue.setObject(account);
refDelta.addValueToAdd(refValue);
break;
case DELETE:
account = accountWrapper.getObject();
if (skipDeleteShadow(account.asObjectable().getResourceRef(), accounts)) {
break;
}
refValue.setObject(account);
refDelta.addValueToDelete(refValue);
break;
case MODIFY:
// nothing to do, account modifications were applied
// before
continue;
case UNLINK:
refValue.setOid(delta.getOid());
refValue.setTargetType(ShadowType.COMPLEX_TYPE);
refDelta.addValueToDelete(refValue);
break;
default:
warn(getString("pageAdminFocus.message.illegalAccountState", accountWrapper.getProjectionStatus()));
}
// }
}
return refDelta;
}
private boolean skipAddShadow(ObjectReferenceType resourceRef, List<ShadowWrapper> accounts) {
if (resourceRef == null) {
return false;
}
String actualresourceOid = resourceRef.getOid();
if (actualresourceOid == null) {
return false;
}
for (ShadowWrapper account : accounts) {
if (account.getProjectionStatus().equals(UserDtoStatus.DELETE)
&& account.getObject().asObjectable().getResourceRef() != null
&& account.getObject().asObjectable().getResourceRef().getOid() != null
&& account.getObject().asObjectable().getResourceRef().getOid().equals(actualresourceOid)) {
return true;
}
}
return false;
}
private boolean skipDeleteShadow(ObjectReferenceType resourceRef, List<ShadowWrapper> accounts) throws SchemaException {
if (resourceRef == null) {
return false;
}
String actualresourceOid = resourceRef.getOid();
if (actualresourceOid == null) {
return false;
}
for (ShadowWrapper account : accounts) {
if (account.getProjectionStatus().equals(UserDtoStatus.ADD)
&& account.getObjectDelta().getObjectToAdd().asObjectable().getResourceRef() != null
&& account.getObjectDelta().getObjectToAdd().asObjectable().getResourceRef().getOid() != null
&& account.getObjectDelta().getObjectToAdd().asObjectable().getResourceRef().getOid().equals(actualresourceOid)) {
return true;
}
}
return false;
}
private void addDefaultKindAndIntent(PrismObject<ShadowType> account) {
if (account.asObjectable().getKind() == null) {
account.asObjectable().setKind(ShadowKindType.ACCOUNT);
}
if (account.asObjectable().getIntent() == null) {
account.asObjectable().setIntent(SchemaConstants.INTENT_DEFAULT);
}
}
public List<AssignmentInfoDto> showAllAssignmentsPerformed(AjaxRequestTarget ajaxRequestTarget) {
LOGGER.debug("Recompute user assignments");
Task task = createSimpleTask(OPERATION_RECOMPUTE_ASSIGNMENTS);
OperationResult result = new OperationResult(OPERATION_RECOMPUTE_ASSIGNMENTS);
ObjectDelta<F> delta;
Set<AssignmentInfoDto> assignmentInfoDtoSet = new TreeSet<>();
try {
reviveModels();
PrismObjectWrapper<F> focusWrapper = getObjectWrapper();
delta = focusWrapper.getObjectDelta();
// if (focusWrapper.getOldDelta() != null) {
// delta = ObjectDeltaCollectionsUtil.summarize(focusWrapper.getOldDelta(), delta);
// }
switch (focusWrapper.getStatus()) {
case ADDED:
PrismObject<F> focus = delta.getObjectToAdd();
prepareObjectForAdd(focus);
getPrismContext().adopt(focus, getCompileTimeClass());
LOGGER.trace("Delta before add focus:\n{}", delta.debugDumpLazily(3));
if (!delta.isEmpty()) {
delta.revive(getPrismContext());
} else {
result.recordSuccess();
}
break;
case NOT_CHANGED:
prepareObjectDeltaForModify(delta);
LOGGER.trace("Delta before modify user:\n{}", delta.debugDumpLazily(3));
List<ObjectDelta<? extends ObjectType>> accountDeltas = getShadowModifyDeltas(result);
if (!delta.isEmpty()) {
delta.revive(getPrismContext());
}
for (ObjectDelta accDelta : accountDeltas) {
if (!accDelta.isEmpty()) {
accDelta.revive(getPrismContext());
}
}
break;
default:
error(getString("pageAdminFocus.message.unsupportedState", focusWrapper.getStatus()));
}
ModelContext<UserType> modelContext;
try {
ModelExecuteOptions options = ModelExecuteOptions.createEvaluateAllAssignmentRelationsOnRecompute();
modelContext = getModelInteractionService().previewChanges(Collections.singleton(delta), options, task, result);
} catch (NoFocusNameSchemaException e) {
info(getString("pageAdminFocus.message.noUserName"));
ajaxRequestTarget.add(getFeedbackPanel());
return null;
}
DeltaSetTriple<? extends EvaluatedAssignment<?>> evaluatedAssignmentTriple = modelContext.getEvaluatedAssignmentTriple();
Collection<? extends EvaluatedAssignment<?>> evaluatedAssignments = null;
if (evaluatedAssignmentTriple != null) {
evaluatedAssignments = evaluatedAssignmentTriple.getNonNegativeValues();
}
if (evaluatedAssignments == null || evaluatedAssignments.isEmpty()) {
info(getString("pageAdminFocus.message.noAssignmentsAvailable"));
ajaxRequestTarget.add(getFeedbackPanel());
return null;
}
for (EvaluatedAssignment<?> evaluatedAssignment : evaluatedAssignments) {
if (!evaluatedAssignment.isValid()) {
continue;
}
// roles and orgs
DeltaSetTriple<? extends EvaluatedAssignmentTarget> targetsTriple = evaluatedAssignment.getRoles();
Collection<? extends EvaluatedAssignmentTarget> targets = targetsTriple.getNonNegativeValues();
for (EvaluatedAssignmentTarget target : targets) {
if (target.getTarget() != null && ArchetypeType.class.equals(target.getTarget().getCompileTimeClass())){
continue;
}
if (target.appliesToFocusWithAnyRelation(getRelationRegistry())) {
assignmentInfoDtoSet.add(createAssignmentsPreviewDto(target, task, result));
}
}
// all resources
DeltaSetTriple<EvaluatedConstruction> evaluatedConstructionsTriple = evaluatedAssignment
.getEvaluatedConstructions(task, result);
Collection<EvaluatedConstruction> evaluatedConstructions = evaluatedConstructionsTriple
.getNonNegativeValues();
for (EvaluatedConstruction construction : evaluatedConstructions) {
if (!construction.isWeak()) {
assignmentInfoDtoSet.add(createAssignmentsPreviewDto(construction));
}
}
}
return new ArrayList<>(assignmentInfoDtoSet);
} catch (Exception e) {
LoggingUtils.logUnexpectedException(LOGGER, "Could not create assignments preview.", e);
error("Could not create assignments preview. Reason: " + e);
ajaxRequestTarget.add(getFeedbackPanel());
}
return null;
}
private AssignmentInfoDto createAssignmentsPreviewDto(EvaluatedAssignmentTarget evaluatedAbstractRole,
Task task, OperationResult result) {
return createAssignmentsPreviewDto(evaluatedAbstractRole.getTarget(), evaluatedAbstractRole.isDirectlyAssigned(),
evaluatedAbstractRole.getAssignmentPath(), evaluatedAbstractRole.getAssignment());
}
protected AssignmentInfoDto createDelegableAssignmentsPreviewDto(AssignmentType assignment, Task task, OperationResult result) {
if (assignment.getTargetRef() != null) {
if (RoleType.COMPLEX_TYPE.equals(assignment.getTargetRef().getType())
|| OrgType.COMPLEX_TYPE.equals(assignment.getTargetRef().getType())
|| ServiceType.COMPLEX_TYPE.equals(assignment.getTargetRef().getType())) {
PrismObject<AbstractRoleType> targetObject = WebModelServiceUtils.resolveReferenceNoFetch(assignment.getTargetRef(),
PageAdminFocus.this, task, result);
Boolean isDelegable = false;
if (targetObject != null) {
isDelegable = targetObject.asObjectable().isDelegable();
}
if (Boolean.TRUE.equals(isDelegable)) {
return createAssignmentsPreviewDto(targetObject, true, null, assignment);
}
}
}
return null;
}
private AssignmentInfoDto createAssignmentsPreviewDto(PrismObject<? extends AssignmentHolderType> targetObject,
boolean isDirectlyAssigned, AssignmentPath assignmentPath, AssignmentType assignment) {
AssignmentInfoDto dto = new AssignmentInfoDto();
dto.setTargetOid(targetObject.getOid());
dto.setTargetName(getNameToDisplay(targetObject));
dto.setTargetDescription(targetObject.asObjectable().getDescription());
dto.setTargetClass(targetObject.getCompileTimeClass());
dto.setTargetType(WebComponentUtil.classToQName(getPrismContext(), targetObject.getCompileTimeClass()));
dto.setDirect(isDirectlyAssigned);
dto.setAssignmentParent(assignmentPath);
if (assignment != null) {
if (assignment.getTenantRef() != null) {
dto.setTenantName(WebModelServiceUtils.resolveReferenceName(assignment.getTenantRef(), PageAdminFocus.this));
dto.setTenantRef(assignment.getTenantRef());
}
if (assignment.getOrgRef() != null) {
dto.setOrgRefName(WebModelServiceUtils.resolveReferenceName(assignment.getOrgRef(), PageAdminFocus.this));
dto.setOrgRef(assignment.getOrgRef());
}
if (assignment.getTargetRef() != null){
dto.setRelation(assignment.getTargetRef().getRelation());
}
}
return dto;
}
private String getNameToDisplay(PrismObject<? extends AssignmentHolderType> target) {
if (target.canRepresent(AbstractRoleType.class)) {
String n = PolyString.getOrig(((AbstractRoleType)target.asObjectable()).getDisplayName());
if (StringUtils.isNotBlank(n)) {
return n;
}
}
return PolyString.getOrig(target.asObjectable().getName());
}
private AssignmentInfoDto createAssignmentsPreviewDto(EvaluatedConstruction evaluatedConstruction) {
AssignmentInfoDto dto = new AssignmentInfoDto();
PrismObject<ResourceType> resource = evaluatedConstruction.getResource();
dto.setTargetOid(resource.getOid());
dto.setTargetName(PolyString.getOrig(resource.asObjectable().getName()));
dto.setTargetDescription(resource.asObjectable().getDescription());
dto.setTargetClass(resource.getCompileTimeClass());
dto.setDirect(evaluatedConstruction.isDirectlyAssigned());
dto.setAssignmentParent(evaluatedConstruction.getAssignmentPath());
dto.setKind(evaluatedConstruction.getKind());
dto.setIntent(evaluatedConstruction.getIntent());
return dto;
}
@Override
protected void performAdditionalValidation(PrismObject<F> object,
Collection<ObjectDelta<? extends ObjectType>> deltas, Collection<SimpleValidationError> errors) throws SchemaException {
if (object != null && object.asObjectable() != null) {
for (AssignmentType assignment : object.asObjectable().getAssignment()) {
for (MidpointFormValidator validator : getFormValidatorRegistry().getValidators()) {
if (errors == null) {
errors = validator.validateAssignment(assignment);
} else {
errors.addAll(validator.validateAssignment(assignment));
}
}
}
}
}
protected boolean isFocusHistoryPage(){
return false;
}
protected boolean isSelfProfile(){
return false;
}
}