package com.ether; import com.ether.annotation.ControllerMethod; import com.ether.annotation.ParamName; import com.ether.user.User; import net.sf.json.JSONObject; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.jetbrains.annotations.NotNull; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; /** * @author vmipsl * @date 15 march 2012 */ public class ControllerEponge extends ControllerEther { /** *********************************************************** **/ /** *********************** VIEWS ***************************** **/ /** *********************************************************** **/ /** * Home method * * @return * @throws WebException */ @ControllerMethod(view = VIEW_INIT) public Map home() throws WebException { return new HashMap(); } /** *********************************************************** **/ /** *********************** CALLS ***************************** **/ /** *********************************************************** **/ /** * This method logs a user * * @param login * @param password * @param request * @return * @throws ServiceException */ @ControllerMethod(requestMandatory = true, jsonResult = true) public JSONObject login( @ParamName(ParameterConstants.PARAMETER_LOGIN) final String login, @ParamName(ParameterConstants.PARAMETER_PWD) final String password, @NotNull final HttpServletRequest request ) throws ServiceException { final JSONObject jSONUser = new JSONObject(); final JSONObject result = new JSONObject(); if( login != null && password != null ) { try { // final User user = getTapasService().getUserByEmail( login ); // final String encryptedPassword = EtherHelper.encryptPassword( password ); // if( user == null ) // result.put( "errors", "login.error.notFound" ); // else if( !encryptedPassword.equals( user.getPassword() ) ) // result.put( "errors", "login.error.wrongPassword" ); // else // { // request.getSession().setAttribute( "SES_USER", user ); // jSONUser.put( "name", user.getLastName() ); // jSONUser.put( "firstName", user.getFirstName() ); // jSONUser.put( "role", user.getRole().name() ); // } } catch( Exception e ) { result.put( "errors", "login.error.failed" ); } } if( !jSONUser.isEmpty() ) result.put( "jSONUser", jSONUser ); return result; } @ControllerMethod(requestMandatory = true, jsonResult = true) public JSONObject logout( @NotNull final HttpServletRequest request ) throws ServiceException { request.getSession().setAttribute( "SES_USER", null ); request.getSession().invalidate(); return new JSONObject(); } // @ControllerMethod(jsonResult = true) // public JSONObject createAccount( @NotNull @ParamName(ParameterConstants.PARAMETER_NAME) final String lastName, // @Nullable @ParamName(ParameterConstants.PARAMETER_FIRST_NAME) final String firstName, // @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email, // @NotNull @ParamName(ParameterConstants.PARAMETER_PWD) final String password, // @Nullable @ParamName(ParameterConstants.PARAMETER_LABORATORY) final String laboratory, // @Nullable @ParamName(ParameterConstants.PARAMETER_COUNTRY) final String country ) // throws ServiceException, WebException // { // try // { // final User existingUser = getTapasService().getUserByEmail( email ); // if( null == existingUser ) // { // final Date creationDate = new Date(); // // final String encryptedPassword = EtherHelper.encryptPassword( password ); // final User user = new User( lastName, firstName, email, encryptedPassword, UserRole.USER, false, laboratory, country, creationDate ); // // getTapasService().createUser( user ); // // // Send email to administrator to inform there is a new account // sendEmailToAdministratorAndUser( user ); // } // else // throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); // } // catch( DigestException e ) // { // throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); // } // catch( NoSuchAlgorithmException e ) // { // throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); // } // // return new JSONObject(); // } /** * This method create and send an email to : * - the administrator to inform there is a new created account * - the user to inform his account is created * * @param user * @throws WebException */ private void sendEmailToAdministratorAndUser( @NotNull final User user ) throws WebException { try { final String host = getProperty( "mail.host" ); final String webMaster = getProperty( "mail.webmaster" ); final JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost( host ); final MimeMessage message = sender.createMimeMessage(); final MimeMessageHelper helper = new MimeMessageHelper( message ); // Mail Admin final String subjectAdmin = "[TAPAS] Nouveau compte utilisateur"; final String contentAdmin = "Nouvelle création de compte : \n\n" + " - Nom : " + user.getLastName() + '\n' + " - Prénom : " + user.getFirstName() + '\n' + " - Email : " + user.getEmail() + "\n\n" + "Le serveur de mail masqué"; helper.setFrom( webMaster ); helper.setTo( webMaster ); helper.setSubject( subjectAdmin ); helper.setText( contentAdmin ); sender.send( message ); // Mail User final String subjectUser = "[TAPAS] New account"; final String contentUser = "Dear " + user.getLastName() + "\n\n" + "Your account on TAPAS is now created. You can access to the website with your email as login and your password.\n\n" + "The TAPAS Administrator"; helper.setFrom( webMaster ); helper.setTo( user.getEmail() ); helper.setSubject( subjectUser ); helper.setText( contentUser ); sender.send( message ); } catch( MessagingException e ) { throw new WebException( WebException.WebCode.ERROR_EMAIL_CANNOT_BE_SEND, "The email cannot be send to the TAPAS administrator or USER" ); } } // @ControllerMethod(downloadFile = DOWNLOAD_DATA_PROTOCOL_EN) // public void downloadFileDataProtocolEn() // { // } // // @ControllerMethod(downloadFile = DOWNLOAD_DATA_PROTOCOL_FR) // public void downloadFileDataProtocolFr() // { // } // private static final Log LOGGER = LogFactory.getLog( ControllerEponge.class ); private static final String VIEW_INIT = "init"; private static final String VIEW_DATA_PROTOCOL_EN = "project/home_dataProtocol_en"; // TODO : mettre les bons fichiers de Dataprotocol private static final String DOWNLOAD_DATA_PROTOCOL_EN = "bib.txt"; private static final String DOWNLOAD_DATA_PROTOCOL_FR = "bib.txt"; }