-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): virtual attribute for eligibilities
* new user virtual attributed created for aggregating eligibilities attributes from obtained user ext sources (only the highest value is used for each key)
- Loading branch information
Showing
7 changed files
with
279 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...run/core/impl/modules/attributes/urn_perun_user_attribute_def_virt_userEligibilities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package cz.metacentrum.perun.core.impl.modules.attributes; | ||
|
||
import cz.metacentrum.perun.core.api.Attribute; | ||
import cz.metacentrum.perun.core.api.AttributeDefinition; | ||
import cz.metacentrum.perun.core.api.AttributesManager; | ||
import cz.metacentrum.perun.core.api.User; | ||
import cz.metacentrum.perun.core.api.UserExtSource; | ||
import cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException; | ||
import cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException; | ||
import cz.metacentrum.perun.core.impl.PerunSessionImpl; | ||
import cz.metacentrum.perun.core.implApi.modules.attributes.SkipValueCheckDuringDependencyCheck; | ||
import cz.metacentrum.perun.core.implApi.modules.attributes.UserVirtualAttributeCollectedFromUserExtSource; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Virtual attribute for user's eligibilities. | ||
* Attribute is calculated using all user's ext sources and their corresponding eligibilities | ||
* attribute so that for each unique key (name) only the highest obtained value is selected. | ||
* | ||
* @author Luboslav Halama | ||
*/ | ||
@SuppressWarnings("unused") | ||
@SkipValueCheckDuringDependencyCheck | ||
public class urn_perun_user_attribute_def_virt_userEligibilities extends UserVirtualAttributeCollectedFromUserExtSource { | ||
|
||
private final static String USER_ELIGIBILITIES_FRIENDLY_NAME = "userEligibilities"; | ||
private final static String A_U_V_USER_ELIGIBILITIES = AttributesManager.NS_USER_ATTR_VIRT + ":" + USER_ELIGIBILITIES_FRIENDLY_NAME; | ||
|
||
@Override | ||
public String getSourceAttributeFriendlyName() { | ||
return "eligibilities"; | ||
} | ||
|
||
@Override | ||
public String getDestinationAttributeFriendlyName() { | ||
return USER_ELIGIBILITIES_FRIENDLY_NAME; | ||
} | ||
|
||
@Override | ||
public Attribute getAttributeValue(PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition) { | ||
List<UserExtSource> userExtSources = sess.getPerunBl().getUsersManagerBl().getUserExtSources(sess, user); | ||
|
||
Map<String, String> collectedEligibilities = new LinkedHashMap<>(); | ||
|
||
for (UserExtSource extSource : userExtSources) { | ||
Attribute attribute; | ||
try { | ||
attribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, extSource, | ||
AttributesManager.NS_UES_ATTR_DEF + ":" + getSourceAttributeFriendlyName()); | ||
} catch (AttributeNotExistsException | WrongAttributeAssignmentException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
Map<String, String> eligibilities = attribute.valueAsMap(); | ||
|
||
// if no eligibilities were obtained, skip this ext source | ||
if (eligibilities == null) { | ||
continue; | ||
} | ||
|
||
for (String key : eligibilities.keySet()) { | ||
|
||
// if given key is not present yet, store it | ||
if (!collectedEligibilities.containsKey(key)) { | ||
collectedEligibilities.put(key, eligibilities.get(key)); | ||
continue; | ||
} | ||
|
||
// if given is present, check if value should be updated | ||
int timestamp = Integer.parseInt(eligibilities.get(key)); | ||
if (Integer.parseInt(collectedEligibilities.get(key)) < timestamp) { | ||
collectedEligibilities.replace(key, eligibilities.get(key)); | ||
} | ||
} | ||
} | ||
|
||
Attribute attribute = new Attribute(attributeDefinition); | ||
attribute.setValue(collectedEligibilities); | ||
return attribute; | ||
} | ||
|
||
// @Override | ||
// public List<AttributeHandleIdentifier> getHandleIdentifiers() { | ||
// List<AttributeHandleIdentifier> handleIdentifiers = super.getHandleIdentifiers(); | ||
// handleIdentifiers.add(auditEvent -> { | ||
// if (auditEvent instanceof AttributeChangedForUser && | ||
// ((AttributeChangedForUser) auditEvent).getAttribute().getFriendlyName().equals(getDestinationAttributeFriendlyName())) { | ||
// return ((AttributeChangedForUser) auditEvent).getUser().getId(); | ||
// } else { | ||
// return null; | ||
// } | ||
// }); | ||
// return handleIdentifiers; | ||
// } | ||
// | ||
// @Override | ||
// public List<AuditEvent> resolveVirtualAttributeValueChange(PerunSessionImpl perunSession, AuditEvent message) throws AttributeNotExistsException, WrongAttributeAssignmentException, WrongReferenceAttributeValueException { | ||
// List<AuditEvent> resolvingMessages = super.resolveVirtualAttributeValueChange(perunSession, message); | ||
// if (message == null) return resolvingMessages; | ||
// | ||
// if (message instanceof AttributeSetForUser && | ||
// ((AttributeSetForUser) message).getAttribute().getFriendlyName().equals(getSecondarySourceAttributeFriendlyName())) { | ||
// AttributeDefinition attrVirtUserEligibilitiesDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, A_U_V_USER_ELIGIBILITIES); | ||
// resolvingMessages.add(new AttributeSetForUser(new Attribute(attrVirtUserEligibilitiesDefinition), ((AttributeSetForUser) message).getUser())); | ||
// } else if (message instanceof AttributeChangedForUser) { | ||
// AttributeDefinition attrVirtUserEligibilitiesDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, A_U_V_USER_ELIGIBILITIES); | ||
// resolvingMessages.add(new AttributeChangedForUser(new Attribute(attrVirtUserEligibilitiesDefinition), ((AttributeChangedForUser) message).getUser())); | ||
// } else if (message instanceof AttributeRemovedForUser) { | ||
// AttributeDefinition attrVirtUserEligibilitiesDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, A_U_V_USER_ELIGIBILITIES); | ||
// resolvingMessages.add(new AttributeRemovedForUser(new Attribute(attrVirtUserEligibilitiesDefinition), ((AttributeRemovedForUser) message).getUser())); | ||
// } | ||
// | ||
// return resolvingMessages; | ||
// } | ||
|
||
// private AuditEvent resolveEvent(PerunSessionImpl perunSession, User user) throws AttributeNotExistsException { | ||
// AttributeDefinition attrVirtUserEligibilitiesDefinition = perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, A_U_V_USER_ELIGIBILITIES); | ||
// return new AttributeChangedForUser(new Attribute(attrVirtUserEligibilitiesDefinition), user); | ||
// } | ||
|
||
@Override | ||
public AttributeDefinition getAttributeDefinition() { | ||
AttributeDefinition attr = new AttributeDefinition(); | ||
attr.setNamespace(AttributesManager.NS_USER_ATTR_VIRT); | ||
attr.setFriendlyName(USER_ELIGIBILITIES_FRIENDLY_NAME); | ||
attr.setDisplayName("user eligibilities"); | ||
attr.setType(LinkedHashMap.class.getName()); | ||
attr.setDescription("Virtual attribute, which collects all eligibilities user ext source attributes " + | ||
"with keys and values (map). Only the highest value is selected for each key."); | ||
return attr; | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...core/impl/modules/attributes/urn_perun_user_attribute_def_virt_userEligibilitiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package cz.metacentrum.perun.core.impl.modules.attributes; | ||
|
||
import cz.metacentrum.perun.core.api.Attribute; | ||
import cz.metacentrum.perun.core.api.AttributeDefinition; | ||
import cz.metacentrum.perun.core.api.AttributesManager; | ||
import cz.metacentrum.perun.core.api.ExtSource; | ||
import cz.metacentrum.perun.core.api.ExtSourcesManager; | ||
import cz.metacentrum.perun.core.api.User; | ||
import cz.metacentrum.perun.core.api.UserExtSource; | ||
import cz.metacentrum.perun.core.bl.AttributesManagerBl; | ||
import cz.metacentrum.perun.core.bl.PerunBl; | ||
import cz.metacentrum.perun.core.bl.UsersManagerBl; | ||
import cz.metacentrum.perun.core.impl.PerunSessionImpl; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class urn_perun_user_attribute_def_virt_userEligibilitiesTest { | ||
|
||
private static urn_perun_user_attribute_def_virt_userEligibilities classInstance; | ||
private static PerunSessionImpl session; | ||
private static User user; | ||
private static Attribute uesAttribute1; | ||
private static Attribute uesAttribute2; | ||
private static Attribute userEligibilitiesAttribute; | ||
private static AttributeDefinition userEligibilitiesAttributeDefinition; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
classInstance = new urn_perun_user_attribute_def_virt_userEligibilities(); | ||
session = mock(PerunSessionImpl.class); | ||
user = new User(); | ||
UserExtSource extSource = new UserExtSource(new ExtSource("test ext source", ExtSourcesManager.EXTSOURCE_LDAP), "test-login"); | ||
UserExtSource extSource2 = new UserExtSource(new ExtSource("another test ext source", ExtSourcesManager.EXTSOURCE_LDAP), "test-login2"); | ||
|
||
userEligibilitiesAttributeDefinition = new AttributeDefinition(); | ||
userEligibilitiesAttributeDefinition.setNamespace(AttributesManager.NS_USER_ATTR_VIRT); | ||
userEligibilitiesAttributeDefinition.setFriendlyName("userEligibilities"); | ||
userEligibilitiesAttributeDefinition.setDisplayName("user eligibilities"); | ||
userEligibilitiesAttributeDefinition.setType(HashMap.class.getName()); | ||
userEligibilitiesAttributeDefinition.setDescription("Virtual attribute, which collects all eligibilities user ext source attributes " + | ||
"with keys and values (map). Only the highest value is selected for each key."); | ||
userEligibilitiesAttribute = new Attribute(userEligibilitiesAttributeDefinition); | ||
|
||
AttributeDefinition attrDef = new AttributeDefinition(); | ||
attrDef.setFriendlyName("eligibilities"); | ||
attrDef.setType(AttributesManager.NS_UES_ATTR_DEF); | ||
attrDef.setDescription("eligibilities test attribute"); | ||
uesAttribute1 = new Attribute(attrDef); | ||
uesAttribute2 = new Attribute(attrDef); | ||
|
||
Map<String, String> attrValue = new LinkedHashMap<>(); | ||
attrValue.put("cesnet", "8150000"); | ||
uesAttribute1.setValue(attrValue); | ||
|
||
attrValue = new LinkedHashMap<>(); | ||
attrValue.put("cesnet", "9999999"); | ||
attrValue.put("mu", "1000000"); | ||
uesAttribute2.setValue(attrValue); | ||
|
||
when(session.getPerunBl()).thenReturn(mock(PerunBl.class)); | ||
when(session.getPerunBl().getUsersManagerBl()).thenReturn(mock(UsersManagerBl.class)); | ||
when(session.getPerunBl().getAttributesManagerBl()).thenReturn(mock(AttributesManagerBl.class)); | ||
|
||
when(session.getPerunBl().getUsersManagerBl().getUserExtSources(session, user)).thenReturn(List.of(extSource, extSource2)); | ||
when(session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, AttributesManager.NS_USER_ATTR_VIRT + ":userEligibilities")) | ||
.thenReturn(userEligibilitiesAttribute); | ||
when(session.getPerunBl().getAttributesManagerBl().getAttribute(session, extSource, AttributesManager.NS_UES_ATTR_DEF + ":eligibilities")) | ||
.thenReturn(uesAttribute1); | ||
when(session.getPerunBl().getAttributesManagerBl().getAttribute(session, extSource2, AttributesManager.NS_UES_ATTR_DEF + ":eligibilities")) | ||
.thenReturn(uesAttribute2); | ||
} | ||
|
||
@Test | ||
public void getEligibilitiesAttributeValue() throws Exception { | ||
System.out.println("getEligibilitiesAttributeValue()"); | ||
|
||
Attribute attr = classInstance.getAttributeValue(session, user, userEligibilitiesAttributeDefinition); | ||
Map<String, String> values = (LinkedHashMap<String, String>) attr.getValue(); | ||
|
||
assertEquals(2, values.size()); | ||
assertTrue(values.keySet().containsAll(List.of("cesnet", "mu"))); | ||
|
||
for (String key : values.keySet()) { | ||
String value = values.get(key); | ||
if (key.equals("cesnet")) { | ||
assertEquals("9999999", value); | ||
} else { | ||
assertEquals("1000000", value); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters