urn_perun_group_attribute_def_def_unixGroupName_namespace extends GroupAttributesModuleAbstract implements GroupAttributesModuleImplApi { @Override public void checkAttributeSemantics(PerunSessionImpl sess, Group group, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException{ String groupName = null; if(attribute.getValue() != null) groupName = attribute.valueAsString(); String groupNameNamespace = attribute.getFriendlyNameParameter(); if(groupName == null) { return; } try { Attribute groupUnixGroupName = attribute; Attribute resourceUnixGroupName = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_R_unixGroupName_namespace + ":" + groupNameNamespace)); resourceUnixGroupName.setValue(attribute.getValue()); List<Group> groupsWithSameGroupNameInTheSameNamespace = new ArrayList<>(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, groupUnixGroupName)); List<Resource> resourcesWithSameGroupNameInTheSameNamespace = new ArrayList<>(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, resourceUnixGroupName)); groupsWithSameGroupNameInTheSameNamespace.remove(group); if(groupsWithSameGroupNameInTheSameNamespace.isEmpty() && resourcesWithSameGroupNameInTheSameNamespace.isEmpty()) return; boolean haveRights = sess.getPerunBl().getModulesUtilsBl().haveRightToWriteAttributeInAnyGroupOrResource(sess, groupsWithSameGroupNameInTheSameNamespace, resourcesWithSameGroupNameInTheSameNamespace, groupUnixGroupName, resourceUnixGroupName); if(!haveRights) throw new WrongReferenceAttributeValueException(attribute, null, group, null, "This groupName is already used for other group or resource and user has no rights to use it."); List<Attribute> groupUnixGIDs = sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, group, A_G_unixGID_namespace + ":"); List<Attribute> resourceVersionOfUnixGIDs = sess.getPerunBl().getModulesUtilsBl().getListOfResourceGIDsFromListOfGroupGIDs(sess, groupUnixGIDs); if(!groupsWithSameGroupNameInTheSameNamespace.isEmpty()) { for(Group g: groupsWithSameGroupNameInTheSameNamespace) { for(Attribute a: groupUnixGIDs) { int compare; compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, g, a); if(compare > 0) { throw new WrongReferenceAttributeValueException(attribute, a, group, null, g, null, "One of the group GIDs is from the same namespace like other group GID but with different values."); } } } } if(!resourcesWithSameGroupNameInTheSameNamespace.isEmpty()) { for(Resource r: resourcesWithSameGroupNameInTheSameNamespace) { for(Attribute a: resourceVersionOfUnixGIDs) { int compare; compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, r, a); if(compare > 0) { throw new WrongReferenceAttributeValueException(attribute, a, group, null, r, null, "One of the group GIDs is from the same namespace like other resource GIDs but with different values."); } } } } } catch(AttributeNotExistsException ex) { throw new ConsistencyErrorException(ex); } }  @Override void checkAttributeSyntax(PerunSessionImpl sess, Group group, Attribute attribute); @Override void checkAttributeSemantics(PerunSessionImpl sess, Group group, Attribute attribute); @Override void changedAttributeHook(PerunSessionImpl session, Group group, Attribute attribute); @Override List<String> getDependencies();  }
@Test(expected = WrongReferenceAttributeValueException.class) public void testGroupWithSameGID() throws Exception { System.out.println("testGroupWithSameGID()"); attributeToCheck.setValue("my name"); Group group2 = new Group(2,"group2","Group 2",null,null,null,null,0,0); List<Group> listOfGroups = new ArrayList<>(); listOfGroups.add(group2); when(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, attributeToCheck)).thenReturn(listOfGroups); when(sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, group, AttributesManager.NS_GROUP_ATTR_DEF + ":unixGID-namespace:")).thenReturn(Collections.singletonList(attributeToCheck)); when(sess.getPerunBl().getModulesUtilsBl().haveRightToWriteAttributeInAnyGroupOrResource(sess, listOfGroups, new ArrayList<>(), attributeToCheck, attributeToCheck)).thenReturn(true); when(sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, group2, attributeToCheck)).thenReturn(2); classInstance.checkAttributeSemantics(sess, group, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testResourceWithSameGID() throws Exception { System.out.println("testResourceWithSameGID()"); attributeToCheck.setValue("my name"); Resource resource = mock(Resource.class); when(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, attributeToCheck)).thenReturn(Collections.singletonList(resource)); when(sess.getPerunBl().getModulesUtilsBl().getListOfResourceGIDsFromListOfGroupGIDs(sess, new ArrayList<>())).thenReturn(Collections.singletonList(attributeToCheck)); when(sess.getPerunBl().getModulesUtilsBl().haveRightToWriteAttributeInAnyGroupOrResource(sess, new ArrayList<>(), Collections.singletonList(resource), attributeToCheck, attributeToCheck)).thenReturn(true); when(sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, resource, attributeToCheck)).thenReturn(2); classInstance.checkAttributeSemantics(sess, group, attributeToCheck); }
@Test public void testCorrectSemantics() throws Exception { System.out.println("testCorrectSemantics()"); attributeToCheck.setValue("my name"); classInstance.checkAttributeSemantics(sess, group, attributeToCheck); }