Skip to content

Commit

Permalink
add delegation fix (moved old functionality to new user details page)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterynaHonchar committed Sep 13, 2021
1 parent 8ec6e13 commit b00c9d4
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public abstract class AbstractPageObjectDetails<O extends ObjectType, ODM extend

private ODM objectDetailsModels;

private ProgressPanel progressPanel;
protected ProgressPanel progressPanel;
protected boolean previewRequested;

public AbstractPageObjectDetails() {
Expand Down Expand Up @@ -313,23 +313,12 @@ private void executeAddDelta(Collection<ObjectDelta<? extends ObjectType>> delta
}
}

private void executeModifyDelta(Collection<ObjectDelta<? extends ObjectType>> deltas, boolean previewOnly, ExecuteChangeOptionsDto executeChangeOptionsDto, Task task, OperationResult result, AjaxRequestTarget target) {
//TODO only in UserDetialsPanel
//boolean delegationChangesExist = processDeputyAssignments(previewOnly);
protected void executeModifyDelta(Collection<ObjectDelta<? extends ObjectType>> deltas, boolean previewOnly, ExecuteChangeOptionsDto executeChangeOptionsDto, Task task, OperationResult result, AjaxRequestTarget target) {
try {
if (deltas.isEmpty() && !executeChangeOptionsDto.isReconcile()) {
progressPanel.clearProgressPanel(); // from previous attempts (useful only if we would call finishProcessing at the end, but that's not the case now)
if (!previewOnly) {
// if (!delegationChangesExist) {
// result.recordWarning(getString("PageAdminObjectDetails.noChangesSave"));
// showResult(result);
// } //TODO user page
redirectBack();
} else {
// if (!delegationChangesExist) {
// warn(getString("PageAdminObjectDetails.noChangesPreview"));
// target.add(getFeedbackPanel());
// } //TODO user page
}
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,39 @@
*/
package com.evolveum.midpoint.gui.impl.page.admin.user;

import com.evolveum.midpoint.prism.PrismObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.mapper.parameter.PageParameters;

import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.gui.impl.page.admin.focus.PageFocusDetails;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.delta.ContainerDelta;
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.equivalence.EquivalenceStrategy;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.task.api.Task;
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.application.AuthorizationAction;
import com.evolveum.midpoint.web.application.PageDescriptor;
import com.evolveum.midpoint.web.application.Url;
import com.evolveum.midpoint.web.component.assignment.AssignmentEditorDto;
import com.evolveum.midpoint.web.page.admin.users.component.ExecuteChangeOptionsDto;
import com.evolveum.midpoint.web.page.admin.users.component.UserSummaryPanel;
import com.evolveum.midpoint.web.page.admin.users.dto.UserDtoStatus;
import com.evolveum.midpoint.web.util.OnePageParameterEncoder;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;

@PageDescriptor(
Expand Down Expand Up @@ -68,5 +86,130 @@ protected UserDetailsModel createObjectDetailsModels(PrismObject<UserType> objec
return new UserDetailsModel(createPrismObejctModel(object), this);
}

@Override
protected void executeModifyDelta(Collection<ObjectDelta<? extends ObjectType>> deltas, boolean previewOnly,
ExecuteChangeOptionsDto executeChangeOptionsDto, Task task, OperationResult result, AjaxRequestTarget target) {
boolean delegationChangesExist = processDeputyAssignments(previewOnly);
if (deltas.isEmpty() && !executeChangeOptionsDto.isReconcile()) {
progressPanel.clearProgressPanel();
if (!previewOnly) {
if (!delegationChangesExist) {
result.recordWarning(getString("PageAdminObjectDetails.noChangesSave"));
showResult(result);
}
redirectBack();
} else {
if (!delegationChangesExist) {
warn(getString("PageAdminObjectDetails.noChangesPreview"));
target.add(getFeedbackPanel());
}
}
return;
}
super.executeModifyDelta(deltas, previewOnly, executeChangeOptionsDto, task, result, target);
}

private boolean processDeputyAssignments(boolean previewOnly) {
boolean isAnythingChanged = false;
for (AssignmentEditorDto dto : ((UserDetailsModel)getObjectDetailsModels()).getDelegationsModel().getObject()) {
if (!UserDtoStatus.MODIFY.equals(dto.getStatus())) {
if (!previewOnly) {
UserType user = dto.getDelegationOwner();
saveDelegationToUser(user.asPrismObject(), dto);
}
isAnythingChanged = true;
}
}
return isAnythingChanged;
}

private void saveDelegationToUser(PrismObject<UserType> user, AssignmentEditorDto assignmentDto) {
OperationResult result = new OperationResult(OPERATION_SAVE);
try {
getPrismContext().adopt(user);
Collection<ObjectDelta<? extends ObjectType>> deltas = prepareDelegationDelta(user, assignmentDto);
getModelService().executeChanges(deltas, getExecuteChangesOptionsDto().createOptions(PrismContext.get()), createSimpleTask(OPERATION_SAVE), result);

result.recordSuccess();
} catch (Exception e) {
LoggingUtils.logUnexpectedException(LOGGER, "Could not save assignments ", e);
error("Could not save assignments. Reason: " + e);
} finally {
result.recomputeStatus();
}

showResult(result);
}

private Collection<ObjectDelta<? extends ObjectType>> prepareDelegationDelta(PrismObject<UserType> user, AssignmentEditorDto dto)
throws SchemaException {
Collection<ObjectDelta<? extends ObjectType>> deltas = new ArrayList<>();
ObjectDelta<UserType> delta = user.createModifyDelta();
List<AssignmentEditorDto> userAssignmentsDtos = new ArrayList<>();
userAssignmentsDtos.add(dto);

deltas.add(delta);
PrismContainerDefinition<?> def = user.getDefinition().findContainerDefinition(UserType.F_ASSIGNMENT);
handleDelegationAssignmentDeltas(delta, userAssignmentsDtos, def);
return deltas;
}

protected void handleDelegationAssignmentDeltas(ObjectDelta<UserType> focusDelta,
List<AssignmentEditorDto> assignments, PrismContainerDefinition def) 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 (true) {
oldValue.applyDefinition(def, false);
} else {
oldValue.applyDefinition(def);
}
assDelta.addValueToDelete(oldValue.clone());
break;
case MODIFY:
if (!assDto.isModified(getPrismContext())) {
LOGGER.trace("Assignment '{}' not modified.", assDto.getName());
continue;
}

handleModifyAssignmentDelta(assDto, def, newValue, focusDelta);
break;
default:
warn(getString("pageAdminUser.message.illegalAssignmentState", assDto.getStatus()));
}
}

if (!assDelta.isEmpty()) {
assDelta = focusDelta.addModification(assDelta);
}
}

private void handleModifyAssignmentDelta(AssignmentEditorDto assDto,
PrismContainerDefinition assignmentDef, PrismContainerValue newValue, ObjectDelta<UserType> focusDelta)
throws SchemaException {
LOGGER.debug("Handling modified assignment '{}', computing delta.", assDto.getName());

PrismValue oldValue = assDto.getOldValue();
Collection<? extends ItemDelta> deltas = oldValue.diff(newValue, EquivalenceStrategy.IGNORE_METADATA);

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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private <AR extends AbstractRoleType> boolean isDelegableTarget(PrismObject<AR>
if (targetObject != null) {
isDelegable = targetObject.asObjectable().isDelegable();
}
return isDelegable;
return isDelegable != null && isDelegable;
}

private AssignmentInfoDto createDelegableAssignmentsPreviewDto(PrismObject<? extends AssignmentHolderType> targetObject, AssignmentType assignment) {
Expand Down

0 comments on commit b00c9d4

Please sign in to comment.