Skip to content

Commit

Permalink
Merge pull request #2197 from stavamichal/changesOfModulesForO365
Browse files Browse the repository at this point in the history
Changes for o365 email modules
  • Loading branch information
stavamichal authored and zlamalp committed Mar 24, 2021
1 parent 2dfcb62 commit 081540d
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class urn_perun_group_attribute_def_def_o365EmailAddresses_o365mu extends

private final static Logger log = LoggerFactory.getLogger(urn_perun_group_attribute_def_def_o365EmailAddresses_o365mu.class);
static final String ADNAME_ATTRIBUTE = AttributesManager.NS_GROUP_ATTR_DEF + ":adName:o365mu";
static final String MEMBER_O365EMAIL_ADDRESSES_MU_ATTRIBUTE = AttributesManager.NS_MEMBER_ATTR_DEF + ":o365EmailAddresses:mu";
static final String USER_O365EMAIL_ADDRESSES_MU_ATTRIBUTE = AttributesManager.NS_USER_ATTR_DEF + ":o365UserEmailAddresses:mu";

@Override
public void checkAttributeSyntax(PerunSessionImpl sess, Group group, Attribute attribute) throws WrongAttributeValueException {
Expand Down Expand Up @@ -97,18 +97,18 @@ public void checkAttributeSemantics(PerunSessionImpl sess, Group group, Attribut
}

//No need to check duplicities among other groups (attribute is unique)
//check for duplicities among members attributes and this one
//check for duplicities among users attributes and this one
try {
AttributesManagerBl attributesManagerBl = sess.getPerunBl().getAttributesManagerBl();
Attribute memberO365EmailAddresses = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, MEMBER_O365EMAIL_ADDRESSES_MU_ATTRIBUTE));
memberO365EmailAddresses.setValue(emails);
Set<Pair<Integer, Integer>> memberPairs = attributesManagerBl.getPerunBeanIdsForUniqueAttributeValue(sess, memberO365EmailAddresses);
if (!memberPairs.isEmpty()) {
throw new WrongReferenceAttributeValueException(attribute, memberO365EmailAddresses, group, null, "member " + BeansUtils.getSingleId(memberPairs) + " ");
Attribute userO365EmailAddresses = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, USER_O365EMAIL_ADDRESSES_MU_ATTRIBUTE));
userO365EmailAddresses.setValue(emails);
Set<Pair<Integer, Integer>> usersPairs = attributesManagerBl.getPerunBeanIdsForUniqueAttributeValue(sess, userO365EmailAddresses);
if (!usersPairs.isEmpty()) {
throw new WrongReferenceAttributeValueException(attribute, userO365EmailAddresses, group, null, "member " + BeansUtils.getSingleId(usersPairs) + " ");
}
} catch(AttributeNotExistsException ex) {
//If attribute not exists, we can log it and skip it, because there are no duplicates in not existing attributes
log.debug("Attribute {} not exists to check duplicities in it while checkAttributeSemantics for {}.", MEMBER_O365EMAIL_ADDRESSES_MU_ATTRIBUTE, attribute);
log.debug("Attribute {} not exists to check duplicities in it while checkAttributeSemantics for {}.", USER_O365EMAIL_ADDRESSES_MU_ATTRIBUTE, attribute);
}
}
/**
Expand All @@ -131,7 +131,10 @@ public Attribute fillAttribute(PerunSessionImpl sess, Group group, AttributeDefi

@Override
public List<String> getDependencies() {
return Collections.singletonList(ADNAME_ATTRIBUTE);
List<String> dependencies = new ArrayList<>();
dependencies.add(ADNAME_ATTRIBUTE);
dependencies.add(USER_O365EMAIL_ADDRESSES_MU_ATTRIBUTE);
return dependencies;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ public void checkAttributeSemantics(PerunSessionImpl sess, Group group, Resource
if (!groupResourcePairs.isEmpty()) {
throw new WrongReferenceAttributeValueException(attribute, attribute, group, resource, "some of the email addresses are already assigned to the following group_resource pairs: " + groupResourcePairs);
}
Attribute memberO365EmailAddresses = new Attribute(new urn_perun_member_attribute_def_def_o365EmailAddresses_mu().getAttributeDefinition());
memberO365EmailAddresses.setValue(emails);
Set<Pair<Integer, Integer>> memberPairs = attributesManagerBl.getPerunBeanIdsForUniqueAttributeValue(sess, memberO365EmailAddresses);
if (!memberPairs.isEmpty()) {
throw new WrongReferenceAttributeValueException(attribute, memberO365EmailAddresses, group, resource, "member " + BeansUtils.getSingleId(memberPairs) + " ");
Attribute userO365EmailAddresses = new Attribute(new urn_perun_user_attribute_def_def_o365UserEmailAddresses_mu().getAttributeDefinition());
userO365EmailAddresses.setValue(emails);
Set<Pair<Integer, Integer>> userPairs = attributesManagerBl.getPerunBeanIdsForUniqueAttributeValue(sess, userO365EmailAddresses);
if (!userPairs.isEmpty()) {
throw new WrongReferenceAttributeValueException(attribute, userO365EmailAddresses, group, resource, "user " + BeansUtils.getSingleId(userPairs) + " ");
}
}

Expand All @@ -134,7 +134,10 @@ public Attribute fillAttribute(PerunSessionImpl sess, Group group, Resource reso

@Override
public List<String> getDependencies() {
return Collections.singletonList(ADNAME_ATTRIBUTE);
List<String> dependencies = new ArrayList<>();
dependencies.add(ADNAME_ATTRIBUTE);
dependencies.add(new urn_perun_user_attribute_def_def_o365UserEmailAddresses_mu().getAttributeDefinition().getName());
return dependencies;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@
* Requirements:
* <ul>
* <li>type is list</li>
* <li>all values are email addresses</li>
* <li>must contain value uco@muni.cz</li>
* <li>value can be null</li>
* <li>if not null, than all values are email addresses</li>
* <li>no duplicates among the list values</li>
* <li>no duplicates among all values of this attribute for all members and all values of
* attribute urn:perun:group_resource:attribute-def:def:o365EmailAddresses:mu</li>
* <li>no duplicates among all values of this attribute for all members</li>
* </ul>
*
* @author Martin Kuba &lt;makub@ics.muni.cz>
Expand All @@ -53,8 +52,6 @@ public class urn_perun_member_attribute_def_def_o365EmailAddresses_mu extends Me
private final static Logger log = LoggerFactory.getLogger(urn_perun_member_attribute_def_def_o365EmailAddresses_mu.class);

private static final String NAMESPACE = AttributesManager.NS_MEMBER_ATTR_DEF;
static final String UCO_ATTRIBUTE = AttributesManager.NS_USER_ATTR_DEF + ":login-namespace:mu";
static final String G_D_O365_EMAIL_ADDRESSES_O365MU_ATTR = AttributesManager.NS_GROUP_ATTR_DEF + ":o365EmailAddresses:o365mu";

public void checkAttributeSyntax(PerunSessionImpl perunSession, Member member, Attribute attribute) throws WrongAttributeValueException {
Object value = attribute.getValue();
Expand Down Expand Up @@ -82,84 +79,13 @@ else if (!(value instanceof ArrayList)) {
@Override
public void checkAttributeSemantics(PerunSessionImpl sess, Member member, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
log.trace("checkAttributeSemantics(member={},attribute={})", member, attribute);
List<String> emails;

//get values
if (attribute.getValue() == null) {
throw new WrongReferenceAttributeValueException(attribute, "can't be null.");
} else {
emails = attribute.valueAsList();
}

//check for presence of uco@muni.cz
Attribute attrUCO = getUserUco(sess, member);
String UCO = attrUCO.valueAsString();
//Throw an exception if UCO is null (we need to have this value not-null to correctly check value of this attribute)
if(UCO == null) {
throw new WrongReferenceAttributeValueException(attribute, attrUCO, member, null, UCO_ATTRIBUTE + " has null value!");
}
String ucoEmail = UCO + "@muni.cz";
if (!emails.contains(ucoEmail)) {
throw new WrongReferenceAttributeValueException(attribute, attrUCO, member, null, member, null, "does not contain " + ucoEmail);
}

//No need to check duplicities between other members, cause attribute is unique
//Check uniqueness between this attribute and groups
try {
Attribute groupO365EmailAddresses = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, G_D_O365_EMAIL_ADDRESSES_O365MU_ATTR));
groupO365EmailAddresses.setValue(emails);
Set<Integer> groupsWithSameAttributeValueSet = sess.getPerunBl().getAttributesManagerBl().getPerunBeanIdsForUniqueAttributeValue(sess, groupO365EmailAddresses).stream()
.map(Pair::getLeft)
.collect(Collectors.toSet());
if (!groupsWithSameAttributeValueSet.isEmpty()) {
try {
Group groupWithDuplicateEmail = sess.getPerunBl().getGroupsManagerBl().getGroupById(sess, groupsWithSameAttributeValueSet.iterator().next());
throw new WrongReferenceAttributeValueException(attribute, groupO365EmailAddresses, member, null, groupWithDuplicateEmail, null, "some of the email addresses are already assigned.");
} catch (GroupNotExistsException e) {
throw new ConsistencyErrorException(e);
}
}
} catch (AttributeNotExistsException ex) {
//If attribute not exists, we can log it and skip it, because there are no duplicates in not existing attributes
log.debug("Attribute {} not exists to check duplicities in it while checkAttributeSemantics for {}.", G_D_O365_EMAIL_ADDRESSES_O365MU_ATTR, attribute);
}
}

@Override
public List<String> getDependencies() {
return Arrays.asList(UCO_ATTRIBUTE, G_D_O365_EMAIL_ADDRESSES_O365MU_ATTR);
}

/**
* Prefills values uco@mail.muni.cz and uco@muni.cz
*/
@Override
public Attribute fillAttribute(PerunSessionImpl sess, Member member, AttributeDefinition attrDef) throws WrongAttributeAssignmentException {
return new Attribute(attrDef, getUserUcoEmails(sess, member));
}

/**
* Gets user uco attribute urn:perun:user:attribute-def:def:login-namespace:mu.
*
* @return Attribute with STRING UCO value if exists, with null value if not exists
*/
private Attribute getUserUco(PerunSessionImpl sess, Member member) throws WrongAttributeAssignmentException {
try {
User user = sess.getPerunBl().getUsersManagerBl().getUserById(sess, member.getUserId());
return sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, UCO_ATTRIBUTE);
} catch (UserNotExistsException | AttributeNotExistsException e) {
throw new InternalErrorException(e.getMessage(), e);
}
}

/**
* @returns uco@muni.cz in list if UCO exists, null if not exists
*/
private ArrayList<String> getUserUcoEmails(PerunSessionImpl sess, Member member) throws WrongAttributeAssignmentException {
Attribute attributeUCO = getUserUco(sess, member);
String uco = attributeUCO.valueAsString();
if(uco == null) return null;
else return Lists.newArrayList(uco + "@muni.cz");
//No need to check duplicities, attribute is unique
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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.Pair;
import cz.metacentrum.perun.core.api.User;
import cz.metacentrum.perun.core.api.exceptions.InternalErrorException;
import cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException;
import cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException;
import cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException;
import cz.metacentrum.perun.core.bl.AttributesManagerBl;
import cz.metacentrum.perun.core.impl.PerunSessionImpl;
import cz.metacentrum.perun.core.implApi.modules.attributes.UserAttributesModuleAbstract;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;

import static cz.metacentrum.perun.core.impl.Utils.emailPattern;
import static cz.metacentrum.perun.core.impl.Utils.hasDuplicate;

public class urn_perun_user_attribute_def_def_o365SystemEmailAddresses_mu extends UserAttributesModuleAbstract {

private final static Logger log = LoggerFactory.getLogger(urn_perun_user_attribute_def_def_o365SystemEmailAddresses_mu.class);

@Override
public void checkAttributeSyntax(PerunSessionImpl perunSession, User user, Attribute attribute) throws InternalErrorException, WrongAttributeValueException {
//empty value is valid
if(attribute.getValue() == null) return;
ArrayList<String> emails = attribute.valueAsList();

//check syntax of all values
for (String email : emails) {
Matcher emailMatcher = emailPattern.matcher(email);
if (!emailMatcher.matches())
throw new WrongAttributeValueException(attribute, user, "Email " + email + " is not in correct form.");
}

//check for duplicities
if (hasDuplicate(emails)) {
throw new WrongAttributeValueException(attribute, user, "duplicate values");
}
}

@Override
public void checkAttributeSemantics(PerunSessionImpl perunSession, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
log.trace("checkAttributeSemantics(user={},attribute={})", user, attribute);

//empty value is valid
if(attribute.getValue() == null) return;
ArrayList<String> emails = attribute.valueAsList();

//No need to check duplicities, attribute is unique
}

@Override
public AttributeDefinition getAttributeDefinition() {
AttributeDefinition attr = new AttributeDefinition();
attr.setNamespace(AttributesManager.NS_USER_ATTR_DEF);
attr.setFriendlyName("o365SystemEmailAddresses:mu");
attr.setDisplayName("System managed email addresses for MU o365");
attr.setType(ArrayList.class.getName());
attr.setUnique(true);
attr.setDescription("System managed email addresses for MU o365");
return attr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@
import cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException;
import cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException;
import cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException;
import cz.metacentrum.perun.core.bl.AttributesManagerBl;
import cz.metacentrum.perun.core.impl.PerunSessionImpl;
import cz.metacentrum.perun.core.implApi.modules.attributes.UserAttributesModuleAbstract;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;

import static cz.metacentrum.perun.core.impl.Utils.emailPattern;
import static cz.metacentrum.perun.core.impl.Utils.hasDuplicate;
import static cz.metacentrum.perun.core.impl.Utils.ucoEmailPattern;
import static cz.metacentrum.perun.core.impl.Utils.*;

public class urn_perun_user_attribute_def_def_o365UserEmailAddresses_mu extends UserAttributesModuleAbstract {

private final static Logger log = LoggerFactory.getLogger(urn_perun_user_attribute_def_def_o365UserEmailAddresses_mu.class);
final static String G_D_O365_EMAIL_ADDRESSES_O365_MU_ATTR = AttributesManager.NS_GROUP_ATTR_DEF + ":o365EmailAddresses:o365mu";

@Override
public void checkAttributeSyntax(PerunSessionImpl perunSession, User user, Attribute attribute) throws InternalErrorException, WrongAttributeValueException {
//empty value is valid
Expand All @@ -51,6 +50,33 @@ public void checkAttributeSyntax(PerunSessionImpl perunSession, User user, Attri
}
}

@Override
public void checkAttributeSemantics(PerunSessionImpl perunSession, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
log.trace("checkAttributeSemantics(member={},attribute={})", user, attribute);

//empty value is valid
if(attribute.getValue() == null) return;
ArrayList<String> emails = attribute.valueAsList();

//check for duplicities among this attribute and all groups (urn_perun_group_attribute_def_def_o365EmailAddresses_o365mu)
try {
Attribute groupO365EmailAddresses = new Attribute(perunSession.getPerunBl().getAttributesManagerBl().getAttributeDefinition(perunSession, G_D_O365_EMAIL_ADDRESSES_O365_MU_ATTR));
groupO365EmailAddresses.setValue(emails);
Set<Pair<Integer, Integer>> groupResourcePairs = perunSession.getPerunBl().getAttributesManagerBl().getPerunBeanIdsForUniqueAttributeValue(perunSession, groupO365EmailAddresses);
if (!groupResourcePairs.isEmpty()) {
throw new WrongReferenceAttributeValueException(attribute, groupO365EmailAddresses, user, null, "some of the email addresses are already assigned to the following group_resource pairs: " + groupResourcePairs);
}
} catch (AttributeNotExistsException ex) {
//If attribute not exists, we can log it and skip it, because there are no duplicates in not existing attributes
log.debug("Attribute {} not exists to check duplicities in it while checkAttributeSemantics for {}.", G_D_O365_EMAIL_ADDRESSES_O365_MU_ATTR, attribute);
}
}

@Override
public List<String> getDependencies() {
return Collections.singletonList(G_D_O365_EMAIL_ADDRESSES_O365_MU_ATTR);
}

@Override
public AttributeDefinition getAttributeDefinition() {
AttributeDefinition attr = new AttributeDefinition();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public void setUp() throws Exception {
.thenReturn(adName);
when(adNameAttr.getValue())
.thenReturn(adName);
when(am.getAttributeDefinition(session, urn_perun_group_attribute_def_def_o365EmailAddresses_o365mu.MEMBER_O365EMAIL_ADDRESSES_MU_ATTRIBUTE))
.thenReturn(new urn_perun_member_attribute_def_def_o365EmailAddresses_mu().getAttributeDefinition());
when(am.getAttributeDefinition(session, urn_perun_group_attribute_def_def_o365EmailAddresses_o365mu.USER_O365EMAIL_ADDRESSES_MU_ATTRIBUTE))
.thenReturn(new urn_perun_user_attribute_def_def_o365UserEmailAddresses_mu().getAttributeDefinition());

attributeToCheck = new Attribute(classInstance.getAttributeDefinition());
attributeToCheck.setId(100);
Expand All @@ -79,7 +79,7 @@ public void fillAttribute() throws Exception {
assertThat(attributeValue, equalTo(expectedValue));

//check that value generated by fillAttribute() is acceptable for checkAttributeSemantics()
when(am.getPerunBeanIdsForUniqueAttributeValue(eq(session), argThat(new BeanAttributeMatcher("member"))))
when(am.getPerunBeanIdsForUniqueAttributeValue(eq(session), argThat(new BeanAttributeMatcher("user"))))
.thenReturn(Sets.newHashSet());
classInstance.checkAttributeSemantics(session, group, attribute);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ public void testCorrect() throws Exception {
public void testUniqItself() throws Exception {
System.out.println("testUniqItself");
attributeToCheck.setValue(new ArrayList<>(Arrays.asList("my@example.com", "my2@google.com")));
when(am.getPerunBeanIdsForUniqueAttributeValue(eq(session), argThat(new BeanAttributeMatcher("member"))))
when(am.getPerunBeanIdsForUniqueAttributeValue(eq(session), argThat(new BeanAttributeMatcher("user"))))
.thenReturn(Sets.newHashSet());
classInstance.checkAttributeSemantics(session, group, attributeToCheck);
}
Expand All @@ -155,7 +155,7 @@ public void testUniqItself() throws Exception {
public void testUniqClash() throws Exception {
System.out.println("testUniqClash");
attributeToCheck.setValue(new ArrayList<>(Arrays.asList("my@example.com", "my2@google.com")));
when(am.getPerunBeanIdsForUniqueAttributeValue(eq(session), argThat(new BeanAttributeMatcher("member"))))
when(am.getPerunBeanIdsForUniqueAttributeValue(eq(session), argThat(new BeanAttributeMatcher("user"))))
.thenReturn(Sets.newHashSet(new Pair<>(10, 0)));
classInstance.checkAttributeSemantics(session, group, attributeToCheck);
}
Expand Down
Loading

0 comments on commit 081540d

Please sign in to comment.