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>
<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"
date="2016-10-31">
<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>
<artifactId>create-users</artifactId>
<packaging>war</packaging>
<version>2.1.0-SNAPSHOT</version>
<version>2.2.0-SNAPSHOT</version>
<name>Create users portlet</name>
<description>
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>
<groupId>org.gcube.portal</groupId>
<artifactId>custom-portal-handler</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.gwtbootstrap</groupId>
<artifactId>gwt-bootstrap</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.gcube.dvos</groupId>
@ -105,19 +107,10 @@
<artifactId>portal-service</artifactId>
<scope>provided</scope>
</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>
<groupId>org.gcube.core</groupId>
<artifactId>common-scope-maps</artifactId>
<scope>compile</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.portlet</groupId>
@ -129,6 +122,16 @@
<artifactId>social-networking-library</artifactId>
<scope>provided</scope>
</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>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -11,16 +11,11 @@ import java.util.List;
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.portal.custom.scopemanager.scopehelper.ScopeHelper;
import org.gcube.portlets.admin.createusers.client.HandleUsersService;
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.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager;
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
import org.slf4j.Logger;
@ -48,12 +43,6 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
private static final long serialVersionUID = -3124676000683430170L;
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
private static final String FIELD_EMAIL = "email";
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_REGISTRATION_DATE = "registration_date";
private static final String FIELD_VRE = "vre";
public static final String DEFAULT_COMPANY_WEB_ID = "liferay.com";
@Override
public void init(){
logger.debug("Trying to get connect to liferay's DB from API");
try {
Connection con = DataAccess.getConnection();
logger.debug("Connected!");
// check if the table already exists
boolean exists = tableExists(con);
if(exists){
logger.debug("Table " + REGISTERED_USERS_TABLE + " already exists.");
if(isWithinPortal()){
logger.debug("Trying to get connect to liferay's DB from API");
try {
Connection con = DataAccess.getConnection();
boolean exists = tableExists(con);
if(exists)
logger.debug("Table " + REGISTERED_USERS_TABLE + " already exists.");
else
initializeTable(con);
} catch (Exception e) {
logger.error("Failed to connect to liferay's DB");
return;
}
else{
initializeTable(con);
}
} catch (Exception e) {
logger.error("Failed to connect to liferay's DB");
return;
}
}else
logger.debug("DEV MODE detected");
}
/**
@ -122,7 +107,7 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
+ "vre" + " varchar(255) NOT NULL, "
+ "registration_date" + " TIMESTAMP NOT NULL)");
logger.debug(REGISTERED_USERS_TABLE + " created");
logger.info(REGISTERED_USERS_TABLE + " created");
} catch (SQLException 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?
* @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
public boolean deleteInvitedUser(String email) {
// if in dev mode return some samples
if (!isWithinPortal()) {
logger.debug("In dev mode.");
return false;
}else{
try{
Connection con = DataAccess.getConnection();
boolean deletedLiferay = deleteUserFromLiferay(email);
boolean deletedTable = deleteUserFromTable(email, con);
return deletedLiferay && deletedTable;
}catch(SQLException e){
logger.debug("Error while trying to delete user with email = " + email, e);
}
return false;
}
}
@ -209,66 +157,55 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
@Override
public void sendEmailToUser(String email) {
logger.debug("Sending welcome message to user with email " + email);
try{
if(isWithinPortal()){
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();
String gatewayName = context.getGatewayName(getThreadLocalRequest());
String emailSender = context.getSenderEmail(getThreadLocalRequest());
InternetAddress to = new InternetAddress(email);
InternetAddress from = new InternetAddress(emailSender);
InternetAddress to = new InternetAddress(email);
InternetAddress from = new InternetAddress(emailSender);
LiferayUserManager userManager = new LiferayUserManager();
String portalUrl = context.getGatewayURL(getThreadLocalRequest());
String username = userManager.getFullNameFromEmail(email);
LiferayUserManager userManager = new LiferayUserManager();
String portalUrl = context.getGatewayURL(getThreadLocalRequest());
String username = userManager.getFullNameFromEmail(email);
MailMessage mailMessage = new MailMessage();
mailMessage.setFrom(from);
mailMessage.setTo(to);
MailMessage mailMessage = new MailMessage();
mailMessage.setFrom(from);
mailMessage.setTo(to);
// TODO it would be nice to let the creator specify the message to be sent
String body = "<p>Dear " + username + ",<br />" + "<br />" +
"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 />" +
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);
}
// TODO it would be nice to let the creator specify the message to be sent
String body = "<p>Dear " + username + ",<br />" + "<br />" +
"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 />" +
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);
}
}else
logger.debug("DEV MODE detected, mail sent...");
}
@Override
public VreUserBean register(String name, String surname, String institution,
String email, boolean sendEmail, boolean isMale) {
// if in dev mode return ok
if (!isWithinPortal()) {
logger.debug("In dev mode.");
return new VreUserBean(name, surname, institution, email, false, System.currentTimeMillis(), isMale);
}else{
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;
}
String vre = session.getScopeName();
PortalContext pContext = PortalContext.getConfiguration();
String vre = pContext.getCurrentScope(getThreadLocalRequest());
long timestamp = System.currentTimeMillis();
//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);
if(exists){
logger.debug("User with this email already present in the portal.");
return null;
}
else{
logger.debug("Trying to add user: " + name + ", " + surname + ", " + email + ", " + institution);
Connection con = null;
try{
con = DataAccess.getConnection();
String insert = "INSERT into " + REGISTERED_USERS_TABLE +
"("+ FIELD_EMAIL + ","
@ -298,7 +232,6 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
+ ") values(?, ?, ?, ?, ?, ?)";
PreparedStatement statement = con.prepareStatement(insert);
statement.setString(1, email);
statement.setString(2, name);
statement.setString(3, surname);
@ -306,14 +239,10 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
statement.setTimestamp(5, new Timestamp(timestamp));
statement.setString(6, vre);
int res = statement.executeUpdate();
if(res != 0){
logger.debug("User added in " + REGISTERED_USERS_TABLE);
// add in liferay too
try{
GCubeUser user = userManager.createUser(
true,
"",
@ -334,7 +263,7 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
userManager.assignUserToGroup(getCurrentGroupID(), userManager.getUserId(user.getUsername()));
if (sendEmail)
sendEmailToUser(email);
}catch(Exception e){
// 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");
}
}
return new VreUserBean(name, surname, institution, email, false, timestamp, isMale);
}
}
@ -383,18 +311,10 @@ public class CreateUsersImpl extends RemoteServiceServlet implements HandleUsers
}else{
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
String vre = session.getScopeName();
PortalContext pContext = PortalContext.getConfiguration();
String vre = pContext.getCurrentScope(getThreadLocalRequest());
Connection con = null;
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
boolean passwordChanged = userManager.isPasswordChanged(email);
toReturn.add(new VreUserBean(name, surname, institution, email, passwordChanged, registrationDate, false));
}
// 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
*
* @return the current group ID or null if an exception is thrown
* @throws Exception
*/
private Long getCurrentGroupID() {
ASLSession session = getASLSession();
logger.debug("The current group NAME is --> " + session.getGroupName());
Long toReturn = null;
try {
toReturn = new LiferayGroupManager().getGroupId(session.getGroupName());
} catch (UserManagementSystemException | GroupRetrievalFault e) {
logger.error("Unable to retrieve id for group " + session.getGroupName());
}
return toReturn;
PortalContext pContext = PortalContext.getConfiguration();
return pContext.getCurrentGroupId(getThreadLocalRequest());
}
public static Company getCompany() throws PortalException, SystemException {
return CompanyLocalServiceUtil.getCompanyByWebId(getDefaultCompanyWebId());
}
/**
*
* @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!");
}
}