Skip to content

Commit

Permalink
adding unassign action for bulk action MID-4983
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Feb 28, 2019
1 parent 3d0e41d commit f61aef8
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 4 deletions.
Expand Up @@ -113,6 +113,21 @@ public static <F extends FocusType> void assertNotAssigned(PrismObject<F> user,
}
}
}

public static <F extends FocusType> void assertNotAssigned(PrismObject<F> user, String targetOid, QName refType, QName relation) {
F userType = user.asObjectable();
for (AssignmentType assignmentType: userType.getAssignment()) {
ObjectReferenceType targetRef = assignmentType.getTargetRef();
if (targetRef != null) {
if (QNameUtil.match(refType, targetRef.getType())) {
if (targetOid.equals(targetRef.getOid()) &&
getPrismContext().relationMatches(targetRef.getRelation(), relation)) {
AssertJUnit.fail(user + " does have assigned "+refType.getLocalPart()+" "+targetOid+", relation "+relation+"while not expecting it");
}
}
}
}
}

public static <F extends AssignmentHolderType> void assertAssignments(PrismObject<F> user, int expectedNumber) {
F userType = user.asObjectable();
Expand Down
Expand Up @@ -25,7 +25,11 @@
import com.evolveum.midpoint.prism.PrismObjectValue;
import com.evolveum.midpoint.prism.PrismValue;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.query.QueryFactory;
import com.evolveum.midpoint.schema.constants.RelationTypes;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.CommonException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
Expand All @@ -37,7 +41,11 @@
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.xml.namespace.QName;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
*
Expand Down Expand Up @@ -129,8 +137,55 @@ public PipelineData execute(ActionExpressionType expression, PipelineData input,
return input; // TODO updated objects?
}

private ObjectDelta<? extends ObjectType> createDelta(ObjectType object, Collection<ObjectReferenceType> resources, Collection<ObjectReferenceType> roles, Collection<String> relations) throws ScriptExecutionException {
// TODO implement delta creation
return null;
private ObjectDelta<? extends ObjectType> createDelta(AssignmentHolderType object, Collection<ObjectReferenceType> resources, Collection<ObjectReferenceType> roles, Collection<String> relations) throws ScriptExecutionException {
List<AssignmentType> assignmentsForDelete = new ArrayList<>();

List<AssignmentType> oldAssignments = object.getAssignment();
if(oldAssignments != null) {
for (AssignmentType oldAssignment : oldAssignments) {
if(oldAssignment.getTargetRef() != null) {
if (roles != null) {
outerloop:
for (ObjectReferenceType roleRef : roles) {
if(oldAssignment.getTargetRef().getOid().equals(roleRef.getOid())) {
if(relations != null && !relations.isEmpty()) {
for (String relationQuery : relations) {
if(prismContext.relationMatches(QNameUtil.uriToQName(relationQuery, true), oldAssignment.getTargetRef().getRelation())) {
assignmentsForDelete.add(oldAssignment.clone());
break outerloop;
}
}
} else {
if(prismContext.relationMatches(RelationTypes.MEMBER.getRelation(), oldAssignment.getTargetRef().getRelation())) {
assignmentsForDelete.add(oldAssignment.clone());
break outerloop;
}
}
}
}
}
} else if(oldAssignment.getConstruction() != null) {
if (resources != null) {
for (ObjectReferenceType resourceRef : resources) {
if(oldAssignment.getConstruction().getResourceRef() != null
&& oldAssignment.getConstruction().getResourceRef().getOid().equals(resourceRef.getOid())) {
assignmentsForDelete.add(oldAssignment.clone());
break;
}
}
}
}
}
}

ObjectDelta<? extends ObjectType> delta = null;

try {
delta = prismContext.deltaFor(object.getClass()).item(ItemPath.create(AssignmentHolderType.F_ASSIGNMENT))
.delete(assignmentsForDelete.toArray(new AssignmentType[0])).asObjectDelta(object.getOid());
} catch (SchemaException e) {
throw new ScriptExecutionException("Couldn't prepare modification to delete resource/role assignments", e);
}
return delta;
}
}
Expand Up @@ -322,6 +322,9 @@ public class AbstractConfiguredModelIntegrationTest extends AbstractModelIntegra
protected static final String USER_GUYBRUSH_GIVEN_NAME = "Guybrush";
protected static final String USER_GUYBRUSH_FAMILY_NAME = "Threepwood";
protected static final String USER_GUYBRUSH_LOCALITY = "Melee Island";

public static final File USER_WILL_FILE = new File(COMMON_DIR, "user-will.xml");
public static final String USER_WILL_OID = "c0c010c0-d34d-b33f-f00d-111111145118";

// Largo does not have a full name set, employeeType=PIRATE
protected static final File USER_LARGO_FILE = new File(COMMON_DIR, "user-largo.xml");
Expand Down
Expand Up @@ -82,6 +82,7 @@ public class AbstractInitializedModelIntegrationTest extends AbstractConfiguredM
protected UserType userTypeBarbossa;
protected UserType userTypeGuybrush;
protected UserType userTypeElaine;
protected UserType userTypeWill;

protected DummyResourceContoller dummyResourceCtl;

Expand Down Expand Up @@ -230,6 +231,7 @@ public void initSystem(Task initTask, OperationResult initResult) throws Excepti
userTypeBarbossa = repoAddObjectFromFile(USER_BARBOSSA_FILE, UserType.class, initResult).asObjectable();
userTypeGuybrush = repoAddObjectFromFile(USER_GUYBRUSH_FILE, UserType.class, initResult).asObjectable();
userTypeElaine = repoAddObjectFromFile(USER_ELAINE_FILE, UserType.class, initResult).asObjectable();
userTypeWill = repoAddObjectFromFile(USER_WILL_FILE, UserType.class, true, initResult).asObjectable();

// Roles
repoAddObjectFromFile(ROLE_PIRATE_FILE, initResult);
Expand Down
Expand Up @@ -29,13 +29,15 @@
import com.evolveum.midpoint.prism.delta.ItemDelta;
import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.prism.util.PrismAsserts;
import com.evolveum.midpoint.schema.constants.RelationTypes;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.internals.InternalMonitor;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import com.evolveum.midpoint.schema.util.ObjectQueryUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.test.util.LogfileTestTailer;
import com.evolveum.midpoint.test.util.MidPointAsserts;
import com.evolveum.midpoint.test.util.TestUtil;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
Expand Down Expand Up @@ -91,6 +93,9 @@ public class TestScriptingBasic extends AbstractInitializedModelIntegrationTest
private static final File RECOMPUTE_JACK_FILE = new File(TEST_DIR, "recompute-jack.xml");
private static final File ASSIGN_TO_JACK_FILE = new File(TEST_DIR, "assign-to-jack.xml");
private static final File ASSIGN_TO_JACK_2_FILE = new File(TEST_DIR, "assign-to-jack-2.xml");
private static final File UNASSIGN_FROM_WILL_FILE = new File(TEST_DIR, "unassign-from-will.xml");
private static final File UNASSIGN_FROM_WILL_2_FILE = new File(TEST_DIR, "unassign-from-will-2.xml");
private static final File UNASSIGN_FROM_WILL_3_FILE = new File(TEST_DIR, "unassign-from-will-3.xml");
private static final File PURGE_DUMMY_BLACK_SCHEMA_FILE = new File(TEST_DIR, "purge-dummy-black-schema.xml");
private static final File TEST_DUMMY_RESOURCE_FILE = new File(TEST_DIR, "test-dummy-resource.xml");
private static final File NOTIFICATION_ABOUT_JACK_FILE = new File(TEST_DIR, "notification-about-jack.xml");
Expand Down Expand Up @@ -550,7 +555,7 @@ public void test360AssignToJack() throws Exception {
assertAssignedAccount(jack, "10000000-0000-0000-0000-000000000104");
assertAssignedRole(jack, "12345678-d34d-b33f-f00d-55555555cccc");
}

@Test
public void test370AssignToJackInBackground() throws Exception {
final String TEST_NAME = "test370AssignToJackInBackground";
Expand Down Expand Up @@ -601,6 +606,81 @@ public void test380DisableJackInBackgroundSimple() throws Exception {
display("jack after disable script", jack);
assertAdministrativeStatusDisabled(jack);
}

@Test
public void test390UnassignFromWill() throws Exception {
final String TEST_NAME = "test390UnassignFromJack";
TestUtil.displayTestTitle(this, TEST_NAME);

// GIVEN
Task task = createTask(DOT_CLASS + TEST_NAME);
OperationResult result = task.getResult();
PrismProperty<ScriptingExpressionType> expression = parseAnyData(UNASSIGN_FROM_WILL_FILE);

// WHEN
ExecutionContext output = scriptingExpressionEvaluator.evaluateExpression(expression.getAnyValue().getValue(), task, result);

// THEN
dumpOutput(output, result);
assertOutputData(output, 1, OperationResultStatus.SUCCESS);
result.computeStatus();
TestUtil.assertSuccess(result);
PrismObject<UserType> will = getUser(USER_WILL_OID);
display("will after unassign assignment", will);
MidPointAsserts.assertNotAssigned(will, "12345678-d34d-b33f-f00d-555555556666", RoleType.COMPLEX_TYPE, RelationTypes.MEMBER.getRelation());
MidPointAsserts.assertAssigned(will, "12345678-d34d-b33f-f00d-555555556666", RoleType.COMPLEX_TYPE, RelationTypes.MANAGER.getRelation());
MidPointAsserts.assertAssignedResource(will, "10000000-0000-0000-0000-000000000004");
}

@Test
public void test391UnassignFromWill() throws Exception {
final String TEST_NAME = "test391UnassignFromJack";
TestUtil.displayTestTitle(this, TEST_NAME);

// GIVEN
Task task = createTask(DOT_CLASS + TEST_NAME);
OperationResult result = task.getResult();
PrismProperty<ScriptingExpressionType> expression = parseAnyData(UNASSIGN_FROM_WILL_2_FILE);

// WHEN
ExecutionContext output = scriptingExpressionEvaluator.evaluateExpression(expression.getAnyValue().getValue(), task, result);

// THEN
dumpOutput(output, result);
assertOutputData(output, 1, OperationResultStatus.SUCCESS);
result.computeStatus();
TestUtil.assertSuccess(result);
PrismObject<UserType> will = getUser(USER_WILL_OID);
display("will after unassign assignment", will);
MidPointAsserts.assertNotAssigned(will, "12345678-d34d-b33f-f00d-555555556666", RoleType.COMPLEX_TYPE, RelationTypes.MEMBER.getRelation());
MidPointAsserts.assertNotAssigned(will, "12345678-d34d-b33f-f00d-555555556666", RoleType.COMPLEX_TYPE, RelationTypes.MANAGER.getRelation());
MidPointAsserts.assertAssignedResource(will, "10000000-0000-0000-0000-000000000004");
}

@Test
public void test392UnassignFromWill() throws Exception {
final String TEST_NAME = "test392UnassignFromJack";
TestUtil.displayTestTitle(this, TEST_NAME);

// GIVEN
Task task = createTask(DOT_CLASS + TEST_NAME);
OperationResult result = task.getResult();
PrismProperty<ScriptingExpressionType> expression = parseAnyData(UNASSIGN_FROM_WILL_3_FILE);

// WHEN
ExecutionContext output = scriptingExpressionEvaluator.evaluateExpression(expression.getAnyValue().getValue(), task, result);

// THEN
dumpOutput(output, result);
assertOutputData(output, 1, OperationResultStatus.SUCCESS);
result.computeStatus();
TestUtil.assertSuccess(result);
PrismObject<UserType> will = getUser(USER_WILL_OID);
display("will after unassign assignment", will);
MidPointAsserts.assertNotAssigned(will, "12345678-d34d-b33f-f00d-555555556666", RoleType.COMPLEX_TYPE, RelationTypes.MEMBER.getRelation());
MidPointAsserts.assertNotAssigned(will, "12345678-d34d-b33f-f00d-555555556666", RoleType.COMPLEX_TYPE, RelationTypes.MANAGER.getRelation());
MidPointAsserts.assertNotAssignedResource(will, "10000000-0000-0000-0000-000000000004");
}

@Test
public void test400PurgeSchema() throws Exception {
Expand Down
53 changes: 53 additions & 0 deletions model/model-intest/src/test/resources/common/user-will.xml
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2010-2018 Evolveum
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<user oid="c0c010c0-d34d-b33f-f00d-111111145118"
xmlns='http://midpoint.evolveum.com/xml/ns/public/common/common-3'
xmlns:c='http://midpoint.evolveum.com/xml/ns/public/common/common-3'
xmlns:t='http://prism.evolveum.com/xml/ns/public/types-3'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:piracy='http://midpoint.evolveum.com/xml/ns/samples/piracy'
xmlns:org="http://midpoint.evolveum.com/xml/ns/public/common/org-3">
<name>will</name>
<!-- No full name -->
<subtype>PIRATE</subtype>
<givenName>Will</givenName>
<familyName>Turner</familyName>
<assignment>
<targetRef oid="12345678-d34d-b33f-f00d-555555556666" relation="default" type="c:RoleType"/>
</assignment>
<assignment>
<targetRef oid="12345678-d34d-b33f-f00d-555555556666" relation="manager" type="c:RoleType"/>
</assignment>
<assignment>
<construction>
<resourceRef oid="10000000-0000-0000-0000-000000000004"/>
</construction>
</assignment>
<activation>
<validFrom>1777-05-30T14:15:00Z</validFrom>
<validTo>2222-02-22T08:30:42Z</validTo>
</activation>
<credentials>
<password>
<value>
<clearValue>orlando123</clearValue>
</value>
</password>
</credentials>

</user>
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
~ Copyright (c) 2010-2017 Evolveum
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<s:search xmlns:s="http://midpoint.evolveum.com/xml/ns/public/model/scripting-3"
xmlns:c="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<s:type>c:UserType</s:type>
<s:searchFilter>
<equal xmlns="http://prism.evolveum.com/xml/ns/public/query-3">
<path>c:name</path>
<value>will</value>
</equal>
</s:searchFilter>
<s:action>
<s:type>unassign</s:type>
<s:parameter>
<s:name>role</s:name>
<c:value xsi:type="xsd:string">12345678-d34d-b33f-f00d-555555556666</c:value>
</s:parameter>
<s:parameter>
<s:name>relation</s:name>
<c:value xsi:type="xsd:anyURI">any</c:value>
</s:parameter>
</s:action>
</s:search>
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
~ Copyright (c) 2010-2017 Evolveum
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<s:search xmlns:s="http://midpoint.evolveum.com/xml/ns/public/model/scripting-3"
xmlns:c="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<s:type>c:UserType</s:type>
<s:searchFilter>
<equal xmlns="http://prism.evolveum.com/xml/ns/public/query-3">
<path>c:name</path>
<value>will</value>
</equal>
</s:searchFilter>
<s:action>
<s:type>unassign</s:type>
<s:parameter>
<s:name>resource</s:name>
<c:value xsi:type="xsd:string">10000000-0000-0000-0000-000000000004</c:value>
</s:parameter>
</s:action>
</s:search>

0 comments on commit f61aef8

Please sign in to comment.