urn_perun_user_facility_attribute_def_def_defaultUnixGID extends UserFacilityAttributesModuleAbstract implements UserFacilityAttributesModuleImplApi { @Override public void checkAttributeSemantics(PerunSessionImpl sess, User user, Facility facility, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException { Integer gid = attribute.valueAsInteger(); if(gid == null) return; Attribute namespaceAttribute; try { namespaceAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":unixGID-namespace"); } catch(AttributeNotExistsException ex) { throw new ConsistencyErrorException(ex); } if(namespaceAttribute.getValue() == null) throw new WrongReferenceAttributeValueException(attribute, namespaceAttribute, user, facility, facility, null, "Reference attribute is null"); String namespaceName = namespaceAttribute.valueAsString(); Attribute unixGroupNameNamespace; try { unixGroupNameNamespace = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":unixGroupName-namespace"); } catch(AttributeNotExistsException ex) { throw new ConsistencyErrorException(ex); } if(unixGroupNameNamespace.getValue() == null) throw new WrongReferenceAttributeValueException(attribute, unixGroupNameNamespace, user, facility, facility, null, "Reference attribute is null"); String unixGroupNameNamespaceName = unixGroupNameNamespace.valueAsString(); Attribute resourceGidAttribute; try { resourceGidAttribute = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_RESOURCE_ATTR_DEF + ":unixGID-namespace:" + namespaceName)); } catch(AttributeNotExistsException ex) { throw new ConsistencyErrorException("Namespace from value of " + namespaceAttribute + " doesn't exists. (Resource attribute " + AttributesManager.NS_RESOURCE_ATTR_DEF + ":unixGID-namespace:" + namespaceName + " doesn't exists", ex); } resourceGidAttribute.setValue(attribute.getValue()); List<Resource> allowedResources = sess.getPerunBl().getUsersManagerBl().getAllowedResources(sess, facility, user); List<Resource> allowedResourcesWithSameGid = sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, resourceGidAttribute); allowedResourcesWithSameGid.retainAll(allowedResources); if(!allowedResourcesWithSameGid.isEmpty()) return; Attribute groupGidAttribute; try { groupGidAttribute = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_GROUP_ATTR_DEF + ":unixGID-namespace:" + namespaceName)); groupGidAttribute.setValue(attribute.getValue()); } catch(AttributeNotExistsException ex) { throw new ConsistencyErrorException("Namespace from value of " + namespaceAttribute + " doesn't exists. (Group-resource attribute " + AttributesManager.NS_GROUP_ATTR_DEF + ":unixGID-namespace:" + namespaceName + " doesn't exists", ex); } List<Group> groupWithSameGid = sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, groupGidAttribute); groupWithSameGid.retainAll(sess.getPerunBl().getFacilitiesManagerBl().getAllowedGroups(sess, facility, null, null)); for(Group group : groupWithSameGid) { try { Attribute unixGroupName = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, group, AttributesManager.NS_GROUP_ATTR_DEF + ":unixGroupName-namespace:" + unixGroupNameNamespaceName); if(unixGroupName.getValue() == null || (unixGroupName.valueAsString()).isEmpty()) { continue; } } catch(AttributeNotExistsException ex) { throw new InternalErrorException(ex); } if(sess.getPerunBl().getGroupsManagerBl().isUserMemberOfGroup(sess, user, group)) { return; } } throw new WrongReferenceAttributeValueException(attribute, null, user, facility, "User isn't allowed to have the default unix group which have this gid (" + gid + ") or such group doesn't exist. " + user); }  @Override void checkAttributeSemantics(PerunSessionImpl sess, User user, Facility facility, Attribute attribute); @Override List<String> getDependencies(); AttributeDefinition getAttributeDefinition();  }
@Test(expected = WrongReferenceAttributeValueException.class) public void testSemanticsWithNullValueOfNamespaceAttribute() throws Exception { System.out.println("testSemanticsWithNullValueOfNamespaceAttribute()"); namespace.setValue(null); classInstance.checkAttributeSemantics(sess, user, facility, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testSemanticsWithNullValueOfUnixGroupNameNamespace() throws Exception { System.out.println("testSemanticsWithNullValueOfUnixGroupNameNamespace()"); unixGroupNamespace.setValue(null); classInstance.checkAttributeSemantics(sess, user, facility, attributeToCheck); }
@Test(expected = WrongReferenceAttributeValueException.class) public void testSemanticsWithNoGroupsForGivenGID() throws Exception { System.out.println("testSemanticsWithNoGroupsForGivenGID()"); classInstance.checkAttributeSemantics(sess, user, facility, attributeToCheck); }
@Test public void testSemanticsCorrect() throws Exception { System.out.println("testSemanticsCorrect()"); Resource resource = new Resource(); List<Resource> list = new ArrayList<>(); list.add(resource); when(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(any(PerunSessionImpl.class), any(Attribute.class))).thenReturn(list); when(sess.getPerunBl().getUsersManagerBl().getAllowedResources(sess, facility, user)).thenReturn(list); classInstance.checkAttributeSemantics(sess, user, facility, attributeToCheck); }