Search in sources :

Example 1 with IpGroup

use of edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup in project dataverse by IQSS.

the class Groups method putIpGroups.

/**
 * Creates or updates the {@link IpGroup} named {@code groupName}.
 * @param groupName Name of the group.
 * @param dto data of the group.
 * @return Response describing the created group or the error that prevented
 *         that group from being created.
 */
@PUT
@Path("ip/{groupName}")
public Response putIpGroups(@PathParam("groupName") String groupName, JsonObject dto) {
    try {
        if (groupName == null || groupName.trim().isEmpty()) {
            return badRequest("Group name cannot be empty");
        }
        if (!legalGroupName.matcher(groupName).matches()) {
            return badRequest("Group name can contain only letters, digits, and the chars '-' and '_'");
        }
        IpGroup grp = new JsonParser().parseIpGroup(dto);
        grp.setGroupProvider(ipGroupPrv);
        grp.setPersistedGroupAlias(groupName);
        grp = ipGroupPrv.store(grp);
        return created("/groups/ip/" + grp.getPersistedGroupAlias(), json(grp));
    } catch (Exception e) {
        logger.log(Level.WARNING, "Error while storing a new IP group: " + e.getMessage(), e);
        return error(Response.Status.INTERNAL_SERVER_ERROR, "Error: " + e.getMessage());
    }
}
Also used : IpGroup(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup) JsonParser(edu.harvard.iq.dataverse.util.json.JsonParser) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 2 with IpGroup

use of edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup in project dataverse by IQSS.

the class JsonParserTest method testIpGroupRoundTrip.

@Test
public void testIpGroupRoundTrip() {
    IpGroup original = new IpGroup();
    original.setDescription("Ip group description");
    original.setDisplayName("Test-ip-group");
    original.setId(42l);
    original.setPersistedGroupAlias("test-ip-group");
    original.setGroupProvider(new IpGroupProvider(null));
    original.add(IpAddressRange.make(IpAddress.valueOf("1.2.1.1"), IpAddress.valueOf("1.2.1.10")));
    original.add(IpAddressRange.make(IpAddress.valueOf("1.1.1.1"), IpAddress.valueOf("1.1.1.1")));
    original.add(IpAddressRange.make(IpAddress.valueOf("1:2:3::4:5"), IpAddress.valueOf("1:2:3::4:5")));
    original.add(IpAddressRange.make(IpAddress.valueOf("1:2:3::3:ff"), IpAddress.valueOf("1:2:3::3:5")));
    JsonObject serialized = JsonPrinter.json(original).build();
    System.out.println(serialized.toString());
    IpGroup parsed = new JsonParser().parseIpGroup(serialized);
    assertEquals(original, parsed);
}
Also used : JsonObject(javax.json.JsonObject) IpGroup(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup) IpGroupProvider(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroupProvider) Test(org.junit.Test)

Example 3 with IpGroup

use of edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup in project dataverse by IQSS.

the class JsonParserTest method testIpGroupRoundTrip_singleIpv6Address.

@Test
public void testIpGroupRoundTrip_singleIpv6Address() {
    IpGroup original = new IpGroup();
    original.setDescription("Ip group description");
    original.setDisplayName("Test-ip-group");
    original.setId(42l);
    original.setPersistedGroupAlias("test-ip-group");
    original.setGroupProvider(new IpGroupProvider(null));
    original.add(IpAddressRange.make(IpAddress.valueOf("fe80::22c9:d0ff:fe48:ce61"), IpAddress.valueOf("fe80::22c9:d0ff:fe48:ce61")));
    JsonObject serialized = JsonPrinter.json(original).build();
    System.out.println(serialized.toString());
    IpGroup parsed = new JsonParser().parseIpGroup(serialized);
    assertEquals(original, parsed);
    assertTrue(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe80::22c9:d0ff:fe48:ce61"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe80::22c9:d0ff:fe48:ce60"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe80::22c9:d0ff:fe48:ce62"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe80::22c9:d0ff:fe47:ce61"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe80::22c9:d0af:fe48:ce61"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe79::22c9:d0ff:fe48:ce61"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("2.1.1.1"))));
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) JsonObject(javax.json.JsonObject) IpGroup(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup) IpGroupProvider(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroupProvider) Test(org.junit.Test)

Example 4 with IpGroup

use of edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup in project dataverse by IQSS.

the class JsonParserTest method testIpGroupRoundTrip_singleIpv4Address.

@Test
public void testIpGroupRoundTrip_singleIpv4Address() {
    IpGroup original = new IpGroup();
    original.setDescription("Ip group description");
    original.setDisplayName("Test-ip-group");
    original.setId(42l);
    original.setPersistedGroupAlias("test-ip-group");
    original.setGroupProvider(new IpGroupProvider(null));
    original.add(IpAddressRange.make(IpAddress.valueOf("1.1.1.1"), IpAddress.valueOf("1.1.1.1")));
    JsonObject serialized = JsonPrinter.json(original).build();
    System.out.println(serialized.toString());
    IpGroup parsed = new JsonParser().parseIpGroup(serialized);
    assertEquals(original, parsed);
    assertTrue(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.1.1.1"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.1.1.2"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.1.2.1"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.1.1.0"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.1.1.250"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("1.2.1.1"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("2.1.1.1"))));
    assertFalse(parsed.contains(new DataverseRequest(GuestUser.get(), IpAddress.valueOf("fe80::22c9:d0ff:fe48:ce61"))));
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) JsonObject(javax.json.JsonObject) IpGroup(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup) IpGroupProvider(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroupProvider) Test(org.junit.Test)

Example 5 with IpGroup

use of edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup in project dataverse by IQSS.

the class Groups method postIpGroup.

/**
 * Creates a new {@link IpGroup}. The name of the group is based on the
 * {@code alias:} field, but might be changed to ensure uniqueness.
 * @param dto
 * @return Response describing the created group or the error that prevented
 *         that group from being created.
 */
@POST
@Path("ip")
public Response postIpGroup(JsonObject dto) {
    try {
        IpGroup grp = new JsonParser().parseIpGroup(dto);
        grp.setGroupProvider(ipGroupPrv);
        grp.setPersistedGroupAlias(ipGroupPrv.findAvailableName(grp.getPersistedGroupAlias() == null ? "ipGroup" : grp.getPersistedGroupAlias()));
        grp = ipGroupPrv.store(grp);
        return created("/groups/ip/" + grp.getPersistedGroupAlias(), json(grp));
    } catch (Exception e) {
        logger.log(Level.WARNING, "Error while storing a new IP group: " + e.getMessage(), e);
        return error(Response.Status.INTERNAL_SERVER_ERROR, "Error: " + e.getMessage());
    }
}
Also used : IpGroup(edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup) JsonParser(edu.harvard.iq.dataverse.util.json.JsonParser) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

IpGroup (edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroup)10 IpGroupProvider (edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.IpGroupProvider)5 JsonObject (javax.json.JsonObject)5 Test (org.junit.Test)5 Dataverse (edu.harvard.iq.dataverse.Dataverse)4 IpAddress (edu.harvard.iq.dataverse.authorization.groups.impl.ipaddress.ip.IpAddress)3 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)3 ControlledVocabularyValue (edu.harvard.iq.dataverse.ControlledVocabularyValue)2 DataFile (edu.harvard.iq.dataverse.DataFile)2 Dataset (edu.harvard.iq.dataverse.Dataset)2 DatasetField (edu.harvard.iq.dataverse.DatasetField)2 DatasetFieldCompoundValue (edu.harvard.iq.dataverse.DatasetFieldCompoundValue)2 DatasetFieldType (edu.harvard.iq.dataverse.DatasetFieldType)2 DatasetFieldValue (edu.harvard.iq.dataverse.DatasetFieldValue)2 DatasetVersion (edu.harvard.iq.dataverse.DatasetVersion)2 DataverseContact (edu.harvard.iq.dataverse.DataverseContact)2 DataverseTheme (edu.harvard.iq.dataverse.DataverseTheme)2 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)2 TermsOfUseAndAccess (edu.harvard.iq.dataverse.TermsOfUseAndAccess)2 Util (edu.harvard.iq.dataverse.api.Util)2