Skip to content

Commit

Permalink
Community request: fake EPerson from configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwoodiupui committed Aug 2, 2023
1 parent a76af35 commit bb9e88d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,15 @@ public boolean doCuration(Context c, XmlWorkflowItem wfi)
Curator curator = new Curator();
curator.setReporter(reporter);
c.turnOffAuthorisationSystem();
boolean wasAnonymous = false;
if (null == c.getCurrentUser()) { // We need someone to email
c.setCurrentUser(ePersonService.findAnAdministrator(c));
wasAnonymous = true;
c.setCurrentUser(ePersonService.getSystemEPerson(c));
}
boolean failedP = curate(curator, c, wfi);
if (wasAnonymous) {
c.setCurrentUser(null);
}
c.restoreAuthSystemState();
return failedP;
}
Expand Down
29 changes: 19 additions & 10 deletions dspace-api/src/main/java/org/dspace/eperson/EPersonServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,28 @@ public EPerson find(Context context, UUID id) throws SQLException {
return ePersonDAO.findByID(context, EPerson.class, id);
}

/**
* Create a fake EPerson which can receive email. Its address will be the
* value of "mail.admin", or "postmaster" if all else fails.
* @param c
* @return
* @throws SQLException
*/
@Override
public EPerson findAnAdministrator(Context c)
public EPerson getSystemEPerson(Context c)
throws SQLException {
List<EPerson> contacts = groupService.findByName(c, Group.ADMIN).getMembers();
EPerson currentUser;
if (contacts.isEmpty()) {
log.warn("Administrators group is empty");
currentUser = findByEmail(c, configurationService.getProperty("mail.admin"));
// Null if no such EPerson
} else {
currentUser = contacts.get(0);
String adminEmail = configurationService.getProperty("mail.admin");
if (null == adminEmail) {
adminEmail = "postmaster"; // Last-ditch attempt to send *somewhere*
}
return currentUser;
EPerson systemEPerson = findByEmail(c, adminEmail);

if (null == systemEPerson) {
systemEPerson = new EPerson();
systemEPerson.setEmail(adminEmail);
}

return systemEPerson;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.validation.constraints.NotNull;

import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Item;
Expand Down Expand Up @@ -158,15 +159,16 @@ public List<EPerson> findAll(Context context, int sortField, int pageSize, int o
throws SQLException;

/**
* Try very hard to find an administrator's account. Might return a member
* of the Administrators group, or an account with a configured email
* address.
* The "System EPerson" is a fake account that exists only to receive email.
* It has an email address that should be presumed usable. It does not
* exist in the database and is not complete.
*
* @param context current DSpace session.
* @return a presumed administrator account, or null if none could be found.
* @return an EPerson that can presumably receive email.
* @throws SQLException
*/
public EPerson findAnAdministrator(Context context)
@NotNull
public EPerson getSystemEPerson(Context context)
throws SQLException;

/**
Expand Down

0 comments on commit bb9e88d

Please sign in to comment.