Search in sources :

Example 1 with User

use of org.gbif.ipt.model.User in project ipt by gbif.

the class UserAccountManagerImpl method delete.

@Override
public User delete(String email) throws DeletionNotAllowedException, IOException {
    if (email != null) {
        User remUser = get(email);
        if (remUser != null) {
            // when deleting an admin, ensure another admin still exists
            if (remUser.getRole() == Role.Admin) {
                boolean lastAdmin = true;
                for (User u : users.values()) {
                    if (u.getRole() == Role.Admin && !u.equals(remUser)) {
                        lastAdmin = false;
                        break;
                    }
                }
                if (lastAdmin) {
                    LOG.warn("Last admin cannot be deleted");
                    throw new DeletionNotAllowedException(Reason.LAST_ADMIN);
                }
            }
            Set<String> resourcesCreatedByUser = new HashSet<>();
            for (Resource r : resourceManager.list()) {
                User creator = get(r.getCreator().getEmail());
                if (creator != null && creator.equals(remUser)) {
                    resourcesCreatedByUser.add(r.getShortname());
                }
            }
            Set<String> resourcesManagedOnlyByUser = new HashSet<>();
            for (Resource r : resourceManager.list(remUser)) {
                Set<User> managers = new HashSet<>();
                // add creator to list of managers, but only if creator has manager rights!
                User creator = get(r.getCreator().getEmail());
                if (creator != null && creator.hasManagerRights()) {
                    managers.add(creator);
                }
                for (User m : r.getManagers()) {
                    User manager = get(m.getEmail());
                    if (manager != null && !managers.contains(manager)) {
                        managers.add(manager);
                    }
                }
                // lastly, exclude user to be deleted, then check if at least one user with manager rights remains for resource
                managers.remove(remUser);
                if (managers.isEmpty()) {
                    resourcesManagedOnlyByUser.add(r.getShortname());
                }
            }
            if (!resourcesManagedOnlyByUser.isEmpty()) {
                // Check #1, is user the only manager that exists for or more resources? If yes, prevent deletion!
                throw new DeletionNotAllowedException(Reason.LAST_RESOURCE_MANAGER, resourcesManagedOnlyByUser.toString());
            } else if (!resourcesCreatedByUser.isEmpty()) {
                // Check #2, is user the creator of one or more resources? If yes, prevent deletion!
                throw new DeletionNotAllowedException(Reason.IS_RESOURCE_CREATOR, resourcesCreatedByUser.toString());
            } else if (remove(email)) {
                // and remove user from each resource's list of managers
                for (Resource r : resourceManager.list(remUser)) {
                    r.getManagers().remove(remUser);
                    resourceManager.save(r);
                }
                // persist changes to users.xml
                save();
                return remUser;
            }
        }
    }
    return null;
}
Also used : User(org.gbif.ipt.model.User) Resource(org.gbif.ipt.model.Resource) DeletionNotAllowedException(org.gbif.ipt.service.DeletionNotAllowedException) HashSet(java.util.HashSet)

Example 2 with User

use of org.gbif.ipt.model.User in project ipt by gbif.

the class ResourceManagerImpl method addOrUpdateVersionHistory.

/**
 * Construct or update the VersionHistory for version v of resource, and make sure that it is added to the resource's
 * VersionHistory List.
 *
 * @param resource resource published
 * @param version  version of resource published
 * @param published true if this version has been published successfully, false otherwise
 * @param action   action
 */
protected synchronized void addOrUpdateVersionHistory(Resource resource, BigDecimal version, boolean published, BaseAction action) {
    LOG.info("Adding or updating version: " + version.toPlainString());
    VersionHistory versionHistory;
    // Construct new VersionHistory, or update existing one if it exists
    VersionHistory existingVersionHistory = resource.findVersionHistory(version);
    if (existingVersionHistory == null) {
        versionHistory = new VersionHistory(version, resource.getStatus());
        resource.addVersionHistory(versionHistory);
        LOG.info("Adding VersionHistory for version " + version.toPlainString());
    } else {
        versionHistory = existingVersionHistory;
        LOG.info("Updating VersionHistory for version " + version.toPlainString());
    }
    // DOI
    versionHistory.setDoi(resource.getDoi());
    // DOI status
    versionHistory.setStatus(resource.getIdentifierStatus());
    // change summary
    versionHistory.setChangeSummary(resource.getChangeSummary());
    // core records published
    versionHistory.setRecordsPublished(resource.getRecordsPublished());
    // record published by extension
    versionHistory.setRecordsByExtension(resource.getRecordsByExtension());
    // modifiedBy
    User modifiedBy = action.getCurrentUser();
    if (modifiedBy != null) {
        versionHistory.setModifiedBy(modifiedBy);
    }
    // released - only set when version was published successfully
    if (published) {
        versionHistory.setReleased(new Date());
    }
}
Also used : User(org.gbif.ipt.model.User) VersionHistory(org.gbif.ipt.model.VersionHistory) Date(java.util.Date)

Example 3 with User

use of org.gbif.ipt.model.User in project ipt by gbif.

the class OverviewActionIT method testDeleteReservedDoiWhenPreviousDoiExists.

/**
 * Test deleting reserved DOI, when the resource was previously assigned a DOI.
 */
@ParameterizedTest
@MethodSource("data")
public void testDeleteReservedDoiWhenPreviousDoiExists(OverviewAction action, DOIRegistrationAgency type) throws Exception {
    before(action, type);
    LOG.info("Testing " + type + "...");
    action.setDeleteDoi("true");
    action.setReserveDoi("true");
    // mock resource being assigned DOI
    DOI assignedDoi = new DOI("10.5072/bclona1");
    r.setDoi(assignedDoi);
    r.setDoiOrganisationKey(ORGANISATION_KEY);
    r.setIdentifierStatus(IdentifierStatus.PUBLIC);
    User user = new User();
    user.setEmail("jsmith@gbif.org");
    VersionHistory history = new VersionHistory(new BigDecimal("1.0"), new Date(), PublicationStatus.PUBLIC);
    history.setModifiedBy(user);
    history.setDoi(assignedDoi);
    history.setStatus(IdentifierStatus.PUBLIC);
    r.addVersionHistory(history);
    assertNotNull(r.getDoi());
    r.getEml().getCitation().setIdentifier(r.getDoi().getUrl().toString());
    assertTrue(r.isAlreadyAssignedDoi());
    // reserve new DOI for resource
    action.reserveDoi();
    DOI reserved = r.getDoi();
    assertNotNull(reserved);
    assertEquals(IdentifierStatus.PUBLIC_PENDING_PUBLICATION, r.getIdentifierStatus());
    assertEquals(ORGANISATION_KEY, r.getDoiOrganisationKey());
    // alternate ids updated
    assertEquals(1, r.getEml().getAlternateIdentifiers().size());
    assertEquals(reserved.toString(), r.getEml().getAlternateIdentifiers().get(0));
    // new DOI set as citation id
    assertEquals(reserved.getUrl().toString(), r.getEml().getCitation().getIdentifier());
    LOG.info("DOI was reserved successfully, DOI=" + reserved);
    action.deleteDoi();
    // make sure the reserved DOI was deleted, and previous DOI reassigned
    assertEquals(assignedDoi.getDoiName(), r.getDoi().getDoiName());
    assertEquals(ORGANISATION_KEY, r.getDoiOrganisationKey());
    assertEquals(IdentifierStatus.PUBLIC, r.getIdentifierStatus());
    // alternate ids updated
    assertEquals(1, r.getEml().getAlternateIdentifiers().size());
    assertEquals("10.5072/bclona1", r.getEml().getAlternateIdentifiers().get(0));
    // citation id reset to previous DOI
    assertEquals("https://doi.org/10.5072/bclona1", r.getEml().getCitation().getIdentifier());
    assertTrue(r.isAlreadyAssignedDoi());
    LOG.info("Existing DOI was deleted successfully");
    // for fun, try to publish resource having this deleted DOI - should not be possible!!
    r.setDoi(reserved);
    assertTrue(r.getDoi() != null && r.isPubliclyAvailable());
    // reset action errors, .clear() doesn't work
    List<String> collection = new ArrayList<>();
    action.setActionErrors(collection);
    action.setPublish("true");
    assertEquals("input", action.publish());
    assertEquals(1, action.getActionErrors().size());
    LOG.info("Publishing resource with deleted DOI failed as expected");
}
Also used : User(org.gbif.ipt.model.User) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) VersionHistory(org.gbif.ipt.model.VersionHistory) BigDecimal(java.math.BigDecimal) Date(java.util.Date) DOI(org.gbif.api.model.common.DOI) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with User

use of org.gbif.ipt.model.User in project ipt by gbif.

the class OverviewActionTest method testRegisterResourceNotGBIFSupportedLicense.

/**
 * ODbl is not a GBIF-supported license - test registration fails when last published version of resource is assigned
 * ODbl.
 */
@Test
public void testRegisterResourceNotGBIFSupportedLicense() throws Exception {
    Resource r = new Resource();
    // ODbl
    r.getEml().setIntellectualRights("This work is licensed under a <a href=\"http://opendatacommons.org/licenses/odbl/1.0\">Open Data Commons Open Database License (ODbL) 1.0</a>");
    assertEquals("http://opendatacommons.org/licenses/odbl/1.0", r.getEml().parseLicenseUrl());
    assertFalse(r.isAssignedGBIFSupportedLicense());
    EmlWriter.writeEmlFile(emlFile, r.getEml());
    User user = new User();
    user.setEmail("jsmith@gbif.org");
    VersionHistory vh = new VersionHistory(new BigDecimal("1.0"), new Date(), PublicationStatus.PRIVATE);
    r.addVersionHistory(vh);
    action.setResource(r);
    assertEquals("input", action.registerResource());
    assertEquals(1, action.getActionErrors().size());
}
Also used : User(org.gbif.ipt.model.User) Resource(org.gbif.ipt.model.Resource) VersionHistory(org.gbif.ipt.model.VersionHistory) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 5 with User

use of org.gbif.ipt.model.User in project ipt by gbif.

the class OverviewActionTest method testIsLastPublishedVersionAssignedGBIFSupportedLicense.

/**
 * CC0 is a GBIF-supported license.
 */
@Test
public void testIsLastPublishedVersionAssignedGBIFSupportedLicense() throws IOException, TemplateException {
    Resource r = new Resource();
    // CCO
    r.getEml().setIntellectualRights("This work is licensed under <a href=\"http://creativecommons.org/publicdomain/zero/1.0/legalcode\">Creative Commons CCZero (CC0) 1.0 License</a>.");
    assertEquals("http://creativecommons.org/publicdomain/zero/1.0/legalcode", r.getEml().parseLicenseUrl());
    assertTrue(r.isAssignedGBIFSupportedLicense());
    EmlWriter.writeEmlFile(emlFile, r.getEml());
    User user = new User();
    user.setEmail("jsmith@gbif.org");
    VersionHistory vh = new VersionHistory(new BigDecimal("1.0"), new Date(), PublicationStatus.PRIVATE);
    r.addVersionHistory(vh);
    assertTrue(action.isLastPublishedVersionAssignedGBIFSupportedLicense(r));
}
Also used : User(org.gbif.ipt.model.User) Resource(org.gbif.ipt.model.Resource) VersionHistory(org.gbif.ipt.model.VersionHistory) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

User (org.gbif.ipt.model.User)32 Resource (org.gbif.ipt.model.Resource)17 BigDecimal (java.math.BigDecimal)12 VersionHistory (org.gbif.ipt.model.VersionHistory)12 Date (java.util.Date)10 File (java.io.File)8 Test (org.junit.jupiter.api.Test)8 ArrayList (java.util.ArrayList)6 DOI (org.gbif.api.model.common.DOI)4 Extension (org.gbif.ipt.model.Extension)4 UserAccountManager (org.gbif.ipt.service.admin.UserAccountManager)4 ResourceManagerImpl (org.gbif.ipt.service.manage.impl.ResourceManagerImpl)4 Injector (com.google.inject.Injector)3 ServletModule (com.google.inject.servlet.ServletModule)3 Struts2GuicePluginModule (com.google.inject.struts2.Struts2GuicePluginModule)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 SAXParserFactory (javax.xml.parsers.SAXParserFactory)3 BaseAction (org.gbif.ipt.action.BaseAction)3 IPTModule (org.gbif.ipt.config.IPTModule)3