@ -1,18 +1,6 @@
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.gcube.portal.notifications.thread.NewUserAccountNotificationThread ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
@ -35,55 +23,10 @@ public class MyCreateUserAccountListener extends BaseModelListener<User> {
@Override
public void onAfterCreate ( User user ) throws ModelListenerException {
System . out . println ( "onAfterCreate User INTERCEPTED " + user . getFullName ( ) ) ;
handleUserRegistration ( user ) ;
_log . info ( "onAfterCreate NewUserAccount listener for: " + user . getScreenName ( ) + " / " + user . getFullName ( ) ) ;
Thread emailManagersThread = new Thread ( new NewUserAccountNotificationThread ( user . getScreenName ( ) , user . getFullName ( ) , user . getEmailAddress ( ) ) ) ;
emailManagersThread . start ( ) ;
}
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 ( ) ;
}
}