user-registration-hook/src/main/java/org/gcube/portal/usersaccount/MyCreateUserAccountListener...

89 lines
3.7 KiB
Java
Raw Normal View History

package org.gcube.portal.usersaccount;
import java.util.List;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.portal.mailing.EmailNotification;
import org.gcube.vomanagement.usermanagement.GroupManager;
import org.gcube.vomanagement.usermanagement.RoleManager;
import org.gcube.vomanagement.usermanagement.UserManager;
import org.gcube.vomanagement.usermanagement.exception.RoleRetrievalFault;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayRoleManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.gcube.vomanagement.usermanagement.model.GatewayRolesNames;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.liferay.portal.ModelListenerException;
import com.liferay.portal.model.BaseModelListener;
import com.liferay.portal.model.User;
/**
*
* @author Massimiliano Assante, CNR-ISTI
*
* Model Listeners are used to listen for events on models and do something in response.
* Theyre similar in concept to custom action hooks, which perform actions in response to portal events (user login, for example).
* Model listeners implement the ModelListener interface.
*
* @see https://dev.liferay.com/develop/tutorials/-/knowledge_base/6-2/creating-model-listeners
*/
public class MyCreateUserAccountListener extends BaseModelListener<User> {
private static final Logger _log = LoggerFactory.getLogger(MyCreateUserAccountListener.class);
final String SUBJECT = "New user account notification";
@Override
public void onAfterCreate(User user) throws ModelListenerException {
System.out.println("onAfterCreate User INTERCEPTED " + user.getFullName());
handleUserRegistration(user);
}
private void handleUserRegistration(User user) {
UserManager um = new LiferayUserManager();
GroupManager gm = new LiferayGroupManager();
RoleManager rm = new LiferayRoleManager();
try {
System.out.println("addUser hook ON");
String rootVoName = PortalContext.getConfiguration().getInfrastructureName();
long groupId = gm.getGroupIdFromInfrastructureScope("/"+rootVoName);
long infraManagerRoleId = -1;
try {
infraManagerRoleId = rm.getRoleIdByName(GatewayRolesNames.INFRASTRUCTURE_MANAGER.getRoleName());
}
catch (RoleRetrievalFault e) {
_log.warn("There is no (Site) Role " + infraManagerRoleId + " in this portal. Will not notify about newly user accounts.");
return;
}
_log.trace("Root is: " + rootVoName + " Scanning roles ....");
List<GCubeUser> managers = um.listUsersByGroupAndRole(groupId, infraManagerRoleId);
if (managers == null || managers.isEmpty()) {
_log.warn("There are no users with (Site) Role " + infraManagerRoleId + " on " + rootVoName + " in this portal. Will not notify about newly user accounts.");
}
else {
for (GCubeUser manager : managers) {
sendNotification(manager, user);
_log.info("sent email to manager: " + manager.getEmail());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendNotification(GCubeUser manager, User user) {
StringBuilder body = new StringBuilder("Dear ").append(manager.getFirstName())
.append("<p>").append("<b>")
.append(user.getFullName()).append(" (").append(user.getScreenName()).append(")").append("</b>").append(" registered to the infrastructure gateway with the following email: ").append(user.getEmailAddress())
.append("</p>")
.append("<p>")
.append("You received this email because you are an Infrastructure Manager in this portal.")
.append("</p>");
EmailNotification toSend = new EmailNotification(manager.getEmail(), SUBJECT, body.toString(), null);
toSend.sendEmail();
}
}