AccessManager { public boolean isIntranet(String ip) { if (ip.startsWith("0:0:0:0:0:0:0:1") || ip.equals("127.0.0.1")) return true; String ipv6LinkLocalPrefix = "fe80:"; if (ip.toLowerCase().startsWith(ipv6LinkLocalPrefix)) { return true; } else if (ip.indexOf(':') >= 0) { return false; } Setting network = settingRepository.findOne(Settings.SYSTEM_INTRANET_NETWORK); Setting netmask = settingRepository.findOne(Settings.SYSTEM_INTRANET_NETMASK); try { if (network != null && netmask != null && StringUtils.isNotEmpty(network.getValue()) && StringUtils.isNotEmpty(netmask.getValue())) { long lIntranetNet = getAddress(network.getValue()); long lIntranetMask = getAddress(netmask.getValue()); long lAddress = getAddress(ip.split(",")[0]); return (lAddress & lIntranetMask) == (lIntranetNet & lIntranetMask); } } catch (Exception nfe) { Log.error(Geonet.ACCESS_MANAGER,"isIntranet error: " + nfe.getMessage(), nfe); } return false; }  Set<Operation> getOperations(ServiceContext context, String mdId, String ip); Set<Operation> getOperations(ServiceContext context, String mdId, String ip, Collection<Operation> operations); Set<String> getOperationNames(ServiceContext context, String mdId, String ip, Collection<Operation> operations); Set<Operation> getAllOperations(ServiceContext context, String mdId, String ip); Set<Integer> getUserGroups(UserSession usrSess, String ip, boolean editingGroupsOnly); static List<Integer> getGroups(UserSession session, Profile profile); Set<Integer> getReviewerGroups(UserSession usrSess); Set<Integer> getVisibleGroups(final int userId); boolean canEdit(final ServiceContext context, final String id); boolean canReview(final ServiceContext context, final String id); boolean canChangeStatus(final ServiceContext context, final String id); boolean isOwner(final ServiceContext context, final String id); boolean isOwner(ServiceContext context, MetadataSourceInfo sourceInfo); Element getContentReviewers(ServiceContext context, final Set<Integer> metadataIds); boolean isVisibleToAll(final String metadataId); boolean canDownload(final ServiceContext context, final String id); boolean canDynamic(final ServiceContext context, final String id); boolean hasPermission(final AbstractMetadata metadata, final Group group, final int opId); boolean hasEditPermission(final ServiceContext context, final String id); boolean hasReviewPermission(final ServiceContext context, final String id); boolean hasReviewPermission(final ServiceContext context, final AbstractMetadata metadata); boolean hasOnwershipReviewPermission(final ServiceContext context, final String id); int getPrivilegeId(final String name); String getPrivilegeName(int id); boolean isIntranet(String ip);  }
@Test public void testIsIntranetBackwardCompatibility() { settingManager.setValue(Settings.SYSTEM_INTRANET_NETMASK, "255.255.255.0"); settingManager.setValue(Settings.SYSTEM_INTRANET_NETWORK, "192.168.1.0"); String ipPrefix = "192.168.1."; for (int i = 1; i < 255; i++) { String ipToTest = ipPrefix + i; assertTrue("IP " + ipToTest +" must be in the intranet", accessManager.isIntranet(ipToTest)); } assertFalse("IP 192.168.2.1 is not in the intranet", accessManager.isIntranet("192.168.2.1")); }
@Test public void testIsIntranet() { String ipPrefix = "192.168.1."; settingManager.setValue(Settings.SYSTEM_INTRANET_NETMASK, "255.255.255.255"); settingManager.setValue(Settings.SYSTEM_INTRANET_NETWORK, "192.168.1.1"); assertTrue(accessManager.isIntranet("192.168.1.1")); assertFalse(accessManager.isIntranet("192.168.1.2")); assertFalse(accessManager.isIntranet("192.168.1.0")); settingManager.setValue(Settings.SYSTEM_INTRANET_NETMASK, "255.255.255.254"); assertTrue(accessManager.isIntranet("192.168.1.0")); assertTrue(accessManager.isIntranet("192.168.1.1")); assertFalse(accessManager.isIntranet("192.168.1.2")); settingManager.setValue(Settings.SYSTEM_INTRANET_NETMASK, "255.255.255.252"); assertTrue(accessManager.isIntranet("192.168.1.0")); assertTrue(accessManager.isIntranet("192.168.1.1")); assertTrue(accessManager.isIntranet("192.168.1.2")); assertTrue(accessManager.isIntranet("192.168.1.3")); assertFalse(accessManager.isIntranet("192.168.1.4")); settingManager.setValue(Settings.SYSTEM_INTRANET_NETMASK, "255.255.255.248"); for (int i = 1; i < 8; i++) { String ipToTest = ipPrefix + i; assertTrue("IP " + ipToTest +" is in the intranet", accessManager.isIntranet(ipToTest)); } assertFalse("IP " + "192.168.1.8" +" is not in the intranet", accessManager.isIntranet("192.168.1.8")); settingManager.setValue(Settings.SYSTEM_INTRANET_NETMASK, "255.255.255.0"); for (int i = 1; i < 256; i++) { String ipToTest = ipPrefix + i; assertTrue("IP " + ipToTest +" is in the intranet", accessManager.isIntranet(ipToTest)); } assertFalse("IP " + "192.168.2.1" +" is not in the intranet", accessManager.isIntranet("192.168.2.1")); }