Removed asl core, minor fixes to pom.xml. Version moved to 2.2.0

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/admin/create-users@134397 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-11-19 17:08:52 +00:00
parent 6a421bc21d
commit 42b1779909
4 changed files with 74 additions and 176 deletions

View File

@ -1,4 +1,9 @@
<ReleaseNotes> <ReleaseNotes>
<Changeset component="org.gcube.portlets.admin.create-users.2-2-0"
date="2016-12-02">
<Change>Removed ASLSession</Change>
<Change>Minor pom.xml changes</Change>
</Changeset>
<Changeset component="org.gcube.portlets.admin.create-users.2-1-0" <Changeset component="org.gcube.portlets.admin.create-users.2-1-0"
date="2016-10-31"> date="2016-10-31">
<Change>Fixed unknown question in case of password recovery</Change> <Change>Fixed unknown question in case of password recovery</Change>

25
pom.xml
View File

@ -13,7 +13,7 @@
<groupId>org.gcube.portlets.admin</groupId> <groupId>org.gcube.portlets.admin</groupId>
<artifactId>create-users</artifactId> <artifactId>create-users</artifactId>
<packaging>war</packaging> <packaging>war</packaging>
<version>2.1.0-SNAPSHOT</version> <version>2.2.0-SNAPSHOT</version>
<name>Create users portlet</name> <name>Create users portlet</name>
<description> <description>
Create users portlet allows a VRE-Manager to enter user's personal data and automatically register he/she in the portal. Create users portlet allows a VRE-Manager to enter user's personal data and automatically register he/she in the portal.
@ -90,10 +90,12 @@
<dependency> <dependency>
<groupId>org.gcube.portal</groupId> <groupId>org.gcube.portal</groupId>
<artifactId>custom-portal-handler</artifactId> <artifactId>custom-portal-handler</artifactId>
<scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.gwtbootstrap</groupId> <groupId>com.github.gwtbootstrap</groupId>
<artifactId>gwt-bootstrap</artifactId> <artifactId>gwt-bootstrap</artifactId>
<scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.dvos</groupId> <groupId>org.gcube.dvos</groupId>
@ -105,19 +107,10 @@
<artifactId>portal-service</artifactId> <artifactId>portal-service</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.gcube.applicationsupportlayer</groupId>
<artifactId>aslcore</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>org.gcube.core</groupId> <groupId>org.gcube.core</groupId>
<artifactId>common-scope-maps</artifactId> <artifactId>common-scope-maps</artifactId>
<scope>compile</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.portlet</groupId> <groupId>javax.portlet</groupId>
@ -129,6 +122,16 @@
<artifactId>social-networking-library</artifactId> <artifactId>social-networking-library</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>

View File

@ -11,16 +11,11 @@ import java.util.List;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.application.framework.core.session.SessionManager;
import org.gcube.common.portal.PortalContext; import org.gcube.common.portal.PortalContext;
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
import org.gcube.portlets.admin.createusers.client.HandleUsersService; import org.gcube.portlets.admin.createusers.client.HandleUsersService;
import org.gcube.portlets.admin.createusers.shared.VreUserBean; import org.gcube.portlets.admin.createusers.shared.VreUserBean;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault;
import org.gcube.vomanagement.usermanagement.exception.UserManagementPortalException; import org.gcube.vomanagement.usermanagement.exception.UserManagementPortalException;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException; import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager; import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeUser; import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -48,12 +43,6 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
private static final long serialVersionUID = -3124676000683430170L; private static final long serialVersionUID = -3124676000683430170L;
private static final String REGISTERED_USERS_TABLE = "registered_users"; private static final String REGISTERED_USERS_TABLE = "registered_users";
//dev user
public static final String userid = "test.user";
//dev vre
private static final String vreID = "/gcube/devsec/devVRE";
// SQL TABLE FIELDS // SQL TABLE FIELDS
private static final String FIELD_EMAIL = "email"; private static final String FIELD_EMAIL = "email";
private static final String FIELD_NAME = "name"; private static final String FIELD_NAME = "name";
@ -61,31 +50,27 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
private static final String FIELD_INSTITUTION = "institution_organization"; private static final String FIELD_INSTITUTION = "institution_organization";
private static final String FIELD_REGISTRATION_DATE = "registration_date"; private static final String FIELD_REGISTRATION_DATE = "registration_date";
private static final String FIELD_VRE = "vre"; private static final String FIELD_VRE = "vre";
public static final String DEFAULT_COMPANY_WEB_ID = "liferay.com"; public static final String DEFAULT_COMPANY_WEB_ID = "liferay.com";
@Override @Override
public void init(){ public void init(){
logger.debug("Trying to get connect to liferay's DB from API"); if(isWithinPortal()){
try { logger.debug("Trying to get connect to liferay's DB from API");
try {
Connection con = DataAccess.getConnection(); Connection con = DataAccess.getConnection();
logger.debug("Connected!"); boolean exists = tableExists(con);
if(exists)
// check if the table already exists logger.debug("Table " + REGISTERED_USERS_TABLE + " already exists.");
boolean exists = tableExists(con); else
initializeTable(con);
if(exists){ } catch (Exception e) {
logger.debug("Table " + REGISTERED_USERS_TABLE + " already exists."); logger.error("Failed to connect to liferay's DB");
return;
} }
else{ }else
initializeTable(con); logger.debug("DEV MODE detected");
}
} catch (Exception e) {
logger.error("Failed to connect to liferay's DB");
return;
}
} }
/** /**
@ -122,7 +107,7 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
+ "vre" + " varchar(255) NOT NULL, " + "vre" + " varchar(255) NOT NULL, "
+ "registration_date" + " TIMESTAMP NOT NULL)"); + "registration_date" + " TIMESTAMP NOT NULL)");
logger.debug(REGISTERED_USERS_TABLE + " created"); logger.info(REGISTERED_USERS_TABLE + " created");
} catch (SQLException e) { } catch (SQLException e) {
logger.error("Error while creating table", e); logger.error("Error while creating table", e);
@ -134,27 +119,6 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
} }
} }
/**
* the current ASLSession
* @return the session
*/
private ASLSession getASLSession() {
String sessionID = this.getThreadLocalRequest().getSession().getId();
String user = (String) this.getThreadLocalRequest().getSession().getAttribute(ScopeHelper.USERNAME_ATTRIBUTE);
if (user == null) {
logger.warn("USER IS NULL setting " + userid + " and Running OUTSIDE PORTAL");
user = getDevelopmentUser();
SessionManager.getInstance().getASLSession(sessionID, user).setScope(vreID);
}
return SessionManager.getInstance().getASLSession(sessionID, user);
}
/** /**
* Online or in development mode? * Online or in development mode?
* @return true if you're running into the portal, false if in development * @return true if you're running into the portal, false if in development
@ -170,38 +134,22 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
} }
} }
/**
* when packaging test will fail if the user is not set to test.user
* @return .
*/
public String getDevelopmentUser() {
String user = userid;
// user = "costantino.perciante";
return user;
}
@Override @Override
public boolean deleteInvitedUser(String email) { public boolean deleteInvitedUser(String email) {
// if in dev mode return some samples // if in dev mode return some samples
if (!isWithinPortal()) { if (!isWithinPortal()) {
logger.debug("In dev mode."); logger.debug("In dev mode.");
return false; return false;
}else{ }else{
try{ try{
Connection con = DataAccess.getConnection(); Connection con = DataAccess.getConnection();
boolean deletedLiferay = deleteUserFromLiferay(email); boolean deletedLiferay = deleteUserFromLiferay(email);
boolean deletedTable = deleteUserFromTable(email, con); boolean deletedTable = deleteUserFromTable(email, con);
return deletedLiferay && deletedTable; return deletedLiferay && deletedTable;
}catch(SQLException e){ }catch(SQLException e){
logger.debug("Error while trying to delete user with email = " + email, e); logger.debug("Error while trying to delete user with email = " + email, e);
} }
return false; return false;
} }
} }
@ -209,66 +157,55 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
@Override @Override
public void sendEmailToUser(String email) { public void sendEmailToUser(String email) {
logger.debug("Sending welcome message to user with email " + email); if(isWithinPortal()){
try{ try{
logger.debug("Sending welcome message to user with email " + email);
PortalContext context = PortalContext.getConfiguration();
String gatewayName = context.getGatewayName(getThreadLocalRequest());
String emailSender = context.getSenderEmail(getThreadLocalRequest());
PortalContext context = PortalContext.getConfiguration(); InternetAddress to = new InternetAddress(email);
String gatewayName = context.getGatewayName(getThreadLocalRequest()); InternetAddress from = new InternetAddress(emailSender);
String emailSender = context.getSenderEmail(getThreadLocalRequest());
InternetAddress to = new InternetAddress(email); LiferayUserManager userManager = new LiferayUserManager();
InternetAddress from = new InternetAddress(emailSender); String portalUrl = context.getGatewayURL(getThreadLocalRequest());
String username = userManager.getFullNameFromEmail(email);
LiferayUserManager userManager = new LiferayUserManager(); MailMessage mailMessage = new MailMessage();
String portalUrl = context.getGatewayURL(getThreadLocalRequest()); mailMessage.setFrom(from);
String username = userManager.getFullNameFromEmail(email); mailMessage.setTo(to);
MailMessage mailMessage = new MailMessage(); // TODO it would be nice to let the creator specify the message to be sent
mailMessage.setFrom(from); String body = "<p>Dear " + username + ",<br />" + "<br />" +
mailMessage.setTo(to); "Welcome! Your new account at " + portalUrl + " is ready to be used. Your temporary password is training1, you will be asked to reset it at your first login.<br />" +
"<br />" + "Sincerely,<br />" +
// TODO it would be nice to let the creator specify the message to be sent gatewayName + "<br />" +
String body = "<p>Dear " + username + ",<br />" + "<br />" + emailSender + "<br />" + portalUrl;
"Welcome! Your new account at " + portalUrl + " is ready to be used. Your temporary password is training1, you will be asked to reset it at your first login.<br />" + String subject = gatewayName + ": Your New Account was created successfully!";
"<br />" + "Sincerely,<br />" +
gatewayName + "<br />" +
emailSender + "<br />" + portalUrl;
String subject = gatewayName + ": Your New Account was created successfully!";
mailMessage.setBody(body);
mailMessage.setSubject(subject);
mailMessage.setHTMLFormat(true);
MailServiceUtil.sendEmail(mailMessage);
}catch(Exception e){
logger.error("Error while sending email to user " + email, e);
}
mailMessage.setBody(body);
mailMessage.setSubject(subject);
mailMessage.setHTMLFormat(true);
MailServiceUtil.sendEmail(mailMessage);
}catch(Exception e){
logger.error("Error while sending email to user " + email, e);
}
}else
logger.debug("DEV MODE detected, mail sent...");
} }
@Override @Override
public VreUserBean register(String name, String surname, String institution, public VreUserBean register(String name, String surname, String institution,
String email, boolean sendEmail, boolean isMale) { String email, boolean sendEmail, boolean isMale) {
// if in dev mode return ok // if in dev mode return ok
if (!isWithinPortal()) { if (!isWithinPortal()) {
logger.debug("In dev mode."); logger.debug("In dev mode.");
return new VreUserBean(name, surname, institution, email, false, System.currentTimeMillis(), isMale); return new VreUserBean(name, surname, institution, email, false, System.currentTimeMillis(), isMale);
}else{ }else{
ASLSession session = getASLSession(); PortalContext pContext = PortalContext.getConfiguration();
String userName = session.getUsername(); String vre = pContext.getCurrentScope(getThreadLocalRequest());
//in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info)
//this check just return nothing if that happens
if (userName.compareTo("test.user") == 0) {
logger.debug("Found " + userName + " returning nothing");
return null;
}
String vre = session.getScopeName();
long timestamp = System.currentTimeMillis(); long timestamp = System.currentTimeMillis();
//checking if the user has been already registered or is already in the portal //checking if the user has been already registered or is already in the portal
@ -276,17 +213,14 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
boolean exists = userManager.userExistsByEmail(email); boolean exists = userManager.userExistsByEmail(email);
if(exists){ if(exists){
logger.debug("User with this email already present in the portal."); logger.debug("User with this email already present in the portal.");
return null; return null;
} }
else{ else{
logger.debug("Trying to add user: " + name + ", " + surname + ", " + email + ", " + institution); logger.debug("Trying to add user: " + name + ", " + surname + ", " + email + ", " + institution);
Connection con = null; Connection con = null;
try{ try{
con = DataAccess.getConnection(); con = DataAccess.getConnection();
String insert = "INSERT into " + REGISTERED_USERS_TABLE + String insert = "INSERT into " + REGISTERED_USERS_TABLE +
"("+ FIELD_EMAIL + "," "("+ FIELD_EMAIL + ","
@ -298,7 +232,6 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
+ ") values(?, ?, ?, ?, ?, ?)"; + ") values(?, ?, ?, ?, ?, ?)";
PreparedStatement statement = con.prepareStatement(insert); PreparedStatement statement = con.prepareStatement(insert);
statement.setString(1, email); statement.setString(1, email);
statement.setString(2, name); statement.setString(2, name);
statement.setString(3, surname); statement.setString(3, surname);
@ -306,14 +239,10 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
statement.setTimestamp(5, new Timestamp(timestamp)); statement.setTimestamp(5, new Timestamp(timestamp));
statement.setString(6, vre); statement.setString(6, vre);
int res = statement.executeUpdate(); int res = statement.executeUpdate();
if(res != 0){ if(res != 0){
logger.debug("User added in " + REGISTERED_USERS_TABLE); logger.debug("User added in " + REGISTERED_USERS_TABLE);
// add in liferay too // add in liferay too
try{ try{
GCubeUser user = userManager.createUser( GCubeUser user = userManager.createUser(
true, true,
"", "",
@ -334,7 +263,7 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
userManager.assignUserToGroup(getCurrentGroupID(), userManager.getUserId(user.getUsername())); userManager.assignUserToGroup(getCurrentGroupID(), userManager.getUserId(user.getUsername()));
if (sendEmail) if (sendEmail)
sendEmailToUser(email); sendEmailToUser(email);
}catch(Exception e){ }catch(Exception e){
// unable to create.. we need to delete it from the list of users // unable to create.. we need to delete it from the list of users
@ -361,7 +290,6 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
logger.error("Unable to close connection to the DB"); logger.error("Unable to close connection to the DB");
} }
} }
return new VreUserBean(name, surname, institution, email, false, timestamp, isMale); return new VreUserBean(name, surname, institution, email, false, timestamp, isMale);
} }
} }
@ -383,18 +311,10 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
}else{ }else{
LiferayUserManager userManager = new LiferayUserManager(); LiferayUserManager userManager = new LiferayUserManager();
ASLSession session = getASLSession();
String userName = session.getUsername();
//in case the portal is restarted and you have the social home open it will get test.user (no callback to set session info)
//this check just return nothing if that happens
if (userName.compareTo("test.user") == 0) {
logger.debug("Found " + userName + " returning nothing");
return null;
}
// evaluate current vre // evaluate current vre
String vre = session.getScopeName(); PortalContext pContext = PortalContext.getConfiguration();
String vre = pContext.getCurrentScope(getThreadLocalRequest());
Connection con = null; Connection con = null;
try{ try{
@ -415,10 +335,7 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
// check if the password has been changed or not wrt the default one // check if the password has been changed or not wrt the default one
boolean passwordChanged = userManager.isPasswordChanged(email); boolean passwordChanged = userManager.isPasswordChanged(email);
toReturn.add(new VreUserBean(name, surname, institution, email, passwordChanged, registrationDate, false)); toReturn.add(new VreUserBean(name, surname, institution, email, passwordChanged, registrationDate, false));
} }
// now, make sure these users are still on portal // now, make sure these users are still on portal
@ -500,25 +417,17 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
/** /**
* Get the current group ID * Get the current group ID
*
* @return the current group ID or null if an exception is thrown
* @throws Exception
*/ */
private Long getCurrentGroupID() { private Long getCurrentGroupID() {
ASLSession session = getASLSession();
logger.debug("The current group NAME is --> " + session.getGroupName()); PortalContext pContext = PortalContext.getConfiguration();
Long toReturn = null; return pContext.getCurrentGroupId(getThreadLocalRequest());
try {
toReturn = new LiferayGroupManager().getGroupId(session.getGroupName());
} catch (UserManagementSystemException | GroupRetrievalFault e) {
logger.error("Unable to retrieve id for group " + session.getGroupName());
}
return toReturn;
} }
public static Company getCompany() throws PortalException, SystemException { public static Company getCompany() throws PortalException, SystemException {
return CompanyLocalServiceUtil.getCompanyByWebId(getDefaultCompanyWebId()); return CompanyLocalServiceUtil.getCompanyByWebId(getDefaultCompanyWebId());
} }
/** /**
* *
* @return the default company web-id (e.g. iMarine.eu) * @return the default company web-id (e.g. iMarine.eu)

View File

@ -1,19 +0,0 @@
package org.gcube.portlets.admin.createusers.client;
import static org.junit.Assert.assertTrue;
import org.gcube.portlets.admin.createusers.server.CreateUsersImpl;
import org.junit.Test;
public class TestForDeploy {
@Test
public void testUserIsTestUser() {
CreateUsersImpl serviceImpl = new CreateUsersImpl();
String username = serviceImpl.getDevelopmentUser();
System.out.println("username for deploy is: " + username);
assertTrue(username.compareTo("test.user") == 0);
System.out.println("Test OK!");
}
}