Changeset 574 for ether_statistics/web/src/com/ether/ControllerEponge.java
- Timestamp:
- 10/09/12 15:02:43 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
ether_statistics/web/src/com/ether/ControllerEponge.java
r569 r574 4 4 import com.ether.annotation.ParamName; 5 5 import com.ether.user.User; 6 import com.ether.user.UserRole;7 import com.ether.user.UserState;8 import com.medias.Constantes;9 import com.medias.Context;10 import com.medias.annuaire.Personne;11 import com.medias.mail.Mail;12 import com.medias.mail.MailFactory;13 6 import net.sf.json.JSONObject; 14 7 import org.apache.commons.logging.Log; 15 8 import org.apache.commons.logging.LogFactory; 16 9 import org.jetbrains.annotations.NotNull; 17 import org.jetbrains.annotations.Nullable; 10 import org.springframework.mail.javamail.JavaMailSenderImpl; 11 import org.springframework.mail.javamail.MimeMessageHelper; 18 12 19 13 import javax.mail.MessagingException; 14 import javax.mail.internet.MimeMessage; 20 15 import javax.servlet.http.HttpServletRequest; 21 import java.security.DigestException;22 import java.security.NoSuchAlgorithmException;23 import java.util.ArrayList;24 import java.util.Date;25 16 import java.util.HashMap; 26 17 import java.util.Map; … … 28 19 /** 29 20 * @author vmipsl 30 * @date 1 2 nov 201121 * @date 15 march 2012 31 22 */ 32 23 public class ControllerEponge … … 36 27 /** *********************** VIEWS ***************************** **/ 37 28 /** *********************************************************** **/ 38 @ControllerMethod(view = VIEW_ERRORS) 39 public Map<String, Object> viewErrors() 40 throws ServiceException 29 /** 30 * Home method 31 * 32 * @return 33 * @throws WebException 34 */ 35 @ControllerMethod(view = VIEW_INIT) 36 public Map<String, Object> home() 37 throws WebException 41 38 { 42 39 return new HashMap<String, Object>(); 43 40 } 44 41 45 @ControllerMethod(view = VIEW_LOGIN)46 public Map<String, Object> viewLogin()47 throws ServiceException48 {49 return new HashMap<String, Object>();50 }51 52 @ControllerMethod(view = VIEW_DATA_PROTOCOL_EN, requestMandatory = true)53 public Map<String, Object> viewHomeDataProtocol( @NotNull final HttpServletRequest request )54 throws ServiceException55 {56 final MethodDescription methodDescription = getMethods().get( "viewHomeDataProtocol" );57 58 if( Context.getLangue( request ).equals( Constantes.french_language ) )59 methodDescription.setView( VIEW_DATA_PROTOCOL_FR );60 else61 methodDescription.setView( VIEW_DATA_PROTOCOL_EN );62 return new HashMap<String, Object>();63 }64 65 42 /** *********************************************************** **/ 66 43 /** *********************** CALLS ***************************** **/ 67 44 /** *********************************************************** **/ 45 /** 46 * This method logs a user 47 * 48 * @param login 49 * @param password 50 * @param request 51 * @return 52 * @throws ServiceException 53 */ 68 54 @ControllerMethod(requestMandatory = true, jsonResult = true) 69 55 public JSONObject login( @ParamName(ParameterConstants.PARAMETER_LOGIN) final String login, … … 72 58 throws ServiceException 73 59 { 74 final JSONObject jSON People= new JSONObject();60 final JSONObject jSONUser = new JSONObject(); 75 61 final JSONObject result = new JSONObject(); 76 62 … … 79 65 try 80 66 { 81 final User user = getEtherService().getUserByEmail( login ); 82 final String encryptedPassword = EtherHelper.encryptPassword( password ); 83 if( user == null ) 84 result.put( "errors", "login.error.notFound" ); 85 else if( !encryptedPassword.equals( user.getPassword() ) ) 86 result.put( "errors", "login.error.wrongPassword" ); 87 else if( !UserState.ACCEPTED.equals( user.getState() ) ) 88 result.put( "errors", "login.error.notAccepted" ); 89 else 90 { 91 Personne person = getEtherService().getPeopleByEmail( user.getEmail() ); 92 if( person == null ) 93 { 94 // This is a simple user with no set of data _ Personne in session is used for tests in .jsp 95 person = new Personne(); 96 person.setNom( user.getLastName() ); 97 person.setPrenom( user.getFirstName() ); 98 person.setMail( user.getEmail() ); 99 person.setRoles( user.getRole().name() ); 100 // Can't be null because of the tests in .jsp (upload.jsp by example) 101 person.setJeux( new ArrayList<String>() ); 102 } 103 104 request.getSession().setAttribute( "SES_USER", person ); 105 jSONPeople.put( "name", user.getLastName() ); 106 jSONPeople.put( "firstName", user.getFirstName() ); 107 jSONPeople.put( "role", user.getRole().name() ); 108 } 67 // final User user = getTapasService().getUserByEmail( login ); 68 // final String encryptedPassword = EtherHelper.encryptPassword( password ); 69 // if( user == null ) 70 // result.put( "errors", "login.error.notFound" ); 71 // else if( !encryptedPassword.equals( user.getPassword() ) ) 72 // result.put( "errors", "login.error.wrongPassword" ); 73 // else 74 // { 75 // request.getSession().setAttribute( "SES_USER", user ); 76 // jSONUser.put( "name", user.getLastName() ); 77 // jSONUser.put( "firstName", user.getFirstName() ); 78 // jSONUser.put( "role", user.getRole().name() ); 79 // } 109 80 } 110 81 catch( Exception e ) … … 114 85 } 115 86 116 if( !jSON People.isEmpty() )117 result.put( "jSON People", jSONPeople);87 if( !jSONUser.isEmpty() ) 88 result.put( "jSONUser", jSONUser ); 118 89 return result; 119 90 } … … 129 100 } 130 101 131 @ControllerMethod(jsonResult = true) 132 public JSONObject createAccount( @NotNull @ParamName(ParameterConstants.PARAMETER_NAME) final String lastName, 133 @Nullable @ParamName(ParameterConstants.PARAMETER_FIRST_NAME) final String firstName, 134 @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email, 135 @NotNull @ParamName(ParameterConstants.PARAMETER_PWD) final String password ) 136 throws ServiceException, WebException 137 { 138 try 139 { 140 final User existingUser = getEtherService().getUserByEmail( email ); 141 if( null == existingUser ) 142 { 143 final Date creationDate = new Date(); 144 145 final String encryptedPassword = EtherHelper.encryptPassword( password ); 146 final User user = new User( lastName, firstName, email, encryptedPassword, UserRole.COORDINATOR, UserState.WAITING, false, creationDate ); 147 148 getEtherService().createUser( user ); 149 150 // Send email to administrator to inform there is a new account 151 sendEmailToAdministrator( user ); 152 } 153 else 154 throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); 155 } 156 catch( DigestException e ) 157 { 158 throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 159 } 160 catch( NoSuchAlgorithmException e ) 161 { 162 throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 163 } 164 165 return new JSONObject(); 166 } 102 103 // @ControllerMethod(jsonResult = true) 104 // public JSONObject createAccount( @NotNull @ParamName(ParameterConstants.PARAMETER_NAME) final String lastName, 105 // @Nullable @ParamName(ParameterConstants.PARAMETER_FIRST_NAME) final String firstName, 106 // @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email, 107 // @NotNull @ParamName(ParameterConstants.PARAMETER_PWD) final String password, 108 // @Nullable @ParamName(ParameterConstants.PARAMETER_LABORATORY) final String laboratory, 109 // @Nullable @ParamName(ParameterConstants.PARAMETER_COUNTRY) final String country ) 110 // throws ServiceException, WebException 111 // { 112 // try 113 // { 114 // final User existingUser = getTapasService().getUserByEmail( email ); 115 // if( null == existingUser ) 116 // { 117 // final Date creationDate = new Date(); 118 // 119 // final String encryptedPassword = EtherHelper.encryptPassword( password ); 120 // final User user = new User( lastName, firstName, email, encryptedPassword, UserRole.USER, false, laboratory, country, creationDate ); 121 // 122 // getTapasService().createUser( user ); 123 // 124 // // Send email to administrator to inform there is a new account 125 // sendEmailToAdministratorAndUser( user ); 126 // } 127 // else 128 // throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); 129 // } 130 // catch( DigestException e ) 131 // { 132 // throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 133 // } 134 // catch( NoSuchAlgorithmException e ) 135 // { 136 // throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); 137 // } 138 // 139 // return new JSONObject(); 140 // } 167 141 168 142 /** 169 * This method create and send an email to the administrator to inform there is a new account to validate 143 * This method create and send an email to : 144 * - the administrator to inform there is a new created account 145 * - the user to inform his account is created 170 146 * 171 147 * @param user 172 148 * @throws WebException 173 149 */ 174 private void sendEmailToAdministrator ( @NotNull final User user )150 private void sendEmailToAdministratorAndUser( @NotNull final User user ) 175 151 throws WebException 176 152 { 177 153 try 178 154 { 179 final MailFactory mailFactory = (MailFactory) getServletContext().getAttribute( "APP_MAILFACTORY" ); 180 final String from = (String) getServletContext().getAttribute( "APP_WEBMASTER" ); 181 final String toPI = (String) getServletContext().getAttribute( "APP_PI" ); 182 final String subject = "[MEGAPOLI] Nouvelle demande de compte utilisateur"; 183 final String content = "Hello Matthias,\n\nUne nouvelle demande de compte vient d'arriver.\n\n" + 155 final String host = getProperty( "mail.host" ); 156 final String webMaster = getProperty( "mail.webmaster" ); 157 final JavaMailSenderImpl sender = new JavaMailSenderImpl(); 158 sender.setHost( host ); 159 160 final MimeMessage message = sender.createMimeMessage(); 161 final MimeMessageHelper helper = new MimeMessageHelper( message ); 162 163 // Mail Admin 164 final String subjectAdmin = "[TAPAS] Nouveau compte utilisateur"; 165 final String contentAdmin = "Nouvelle création de compte : \n\n" + 184 166 " - Nom : " + user.getLastName() + '\n' + 185 167 " - Prénom : " + user.getFirstName() + '\n' + 186 168 " - Email : " + user.getEmail() + "\n\n" + 187 "Tu peux accepter ou refuser son inscription via le BO : http://ether.ipsl.jussieu.fr/megapoli/backoffice\n\n" + 188 "Bonne soirée,\nLe serveur masqué"; 189 190 final Mail mailAdministrator = new Mail( from, from, null, content, subject ); 191 mailFactory.sendMail( mailAdministrator ); 192 final Mail mailPI = new Mail( from, toPI, null, content, subject ); 193 mailFactory.sendMail( mailPI ); 169 "Le serveur de mail masqué"; 170 171 helper.setFrom( webMaster ); 172 helper.setTo( webMaster ); 173 helper.setSubject( subjectAdmin ); 174 helper.setText( contentAdmin ); 175 sender.send( message ); 176 177 // Mail User 178 final String subjectUser = "[TAPAS] New account"; 179 final String contentUser = "Dear " + user.getLastName() + "\n\n" + 180 "Your account on TAPAS is now created. You can access to the website with your email as login and your password.\n\n" + 181 "The TAPAS Administrator"; 182 183 helper.setFrom( webMaster ); 184 helper.setTo( user.getEmail() ); 185 helper.setSubject( subjectUser ); 186 helper.setText( contentUser ); 187 sender.send( message ); 194 188 } 195 189 catch( MessagingException e ) 196 190 { 197 throw new WebException( WebException.WebCode.ERROR_EMAIL_CANNOT_BE_SEND, "The email cannot be send to the megapoli administrator" );191 throw new WebException( WebException.WebCode.ERROR_EMAIL_CANNOT_BE_SEND, "The email cannot be send to the TAPAS administrator or USER" ); 198 192 } 199 193 } 200 194 195 // @ControllerMethod(downloadFile = DOWNLOAD_DATA_PROTOCOL_EN) 196 // public void downloadFileDataProtocolEn() 197 // { 198 // } 199 // 200 // @ControllerMethod(downloadFile = DOWNLOAD_DATA_PROTOCOL_FR) 201 // public void downloadFileDataProtocolFr() 202 // { 203 // } 204 // 201 205 private static final Log LOGGER = LogFactory.getLog( ControllerEponge.class ); 202 206 203 private static final String VIEW_ERRORS = "project/errors"; 204 private static final String VIEW_LOGIN = "project/login"; 207 private static final String VIEW_INIT = "init"; 205 208 private static final String VIEW_DATA_PROTOCOL_EN = "project/home_dataProtocol_en"; 206 private static final String VIEW_DATA_PROTOCOL_FR = "project/home_dataProtocol_fr"; 209 // TODO : mettre les bons fichiers de Dataprotocol 210 private static final String DOWNLOAD_DATA_PROTOCOL_EN = "bib.txt"; 211 private static final String DOWNLOAD_DATA_PROTOCOL_FR = "bib.txt"; 207 212 }
Note: See TracChangeset
for help on using the changeset viewer.