1 | package com.ether; |
---|
2 | |
---|
3 | import com.ether.annotation.ControllerMethod; |
---|
4 | import com.ether.annotation.ParamName; |
---|
5 | import com.ether.user.User; |
---|
6 | import com.ether.user.UserRole; |
---|
7 | import net.sf.json.JSONObject; |
---|
8 | import org.apache.commons.logging.Log; |
---|
9 | import org.apache.commons.logging.LogFactory; |
---|
10 | import org.jetbrains.annotations.NotNull; |
---|
11 | import org.jetbrains.annotations.Nullable; |
---|
12 | |
---|
13 | import javax.servlet.http.HttpServletRequest; |
---|
14 | import java.security.DigestException; |
---|
15 | import java.security.NoSuchAlgorithmException; |
---|
16 | import java.util.Date; |
---|
17 | import java.util.HashMap; |
---|
18 | import java.util.Map; |
---|
19 | |
---|
20 | /** |
---|
21 | * @author vmipsl |
---|
22 | * @date 15 march 2012 |
---|
23 | */ |
---|
24 | public class ControllerEponge |
---|
25 | extends ControllerEther |
---|
26 | { |
---|
27 | /** *********************************************************** **/ |
---|
28 | /** *********************** VIEWS ***************************** **/ |
---|
29 | /** *********************************************************** **/ |
---|
30 | /** |
---|
31 | * Home method |
---|
32 | * |
---|
33 | * @return |
---|
34 | * @throws WebException |
---|
35 | */ |
---|
36 | @ControllerMethod(view = VIEW_INIT) |
---|
37 | public Map<String, Object> home() |
---|
38 | throws WebException |
---|
39 | { |
---|
40 | return new HashMap<String, Object>(); |
---|
41 | } |
---|
42 | |
---|
43 | /** *********************************************************** **/ |
---|
44 | /** *********************** CALLS ***************************** **/ |
---|
45 | /** *********************************************************** **/ |
---|
46 | /** |
---|
47 | * This method logs a user |
---|
48 | * |
---|
49 | * @param login |
---|
50 | * @param password |
---|
51 | * @param request |
---|
52 | * @return |
---|
53 | * @throws ServiceException |
---|
54 | */ |
---|
55 | @ControllerMethod(requestMandatory = true, jsonResult = true) |
---|
56 | public JSONObject login( @ParamName(ParameterConstants.PARAMETER_LOGIN) final String login, |
---|
57 | @ParamName(ParameterConstants.PARAMETER_PWD) final String password, |
---|
58 | @NotNull final HttpServletRequest request ) |
---|
59 | throws ServiceException |
---|
60 | { |
---|
61 | final JSONObject jSONUser = new JSONObject(); |
---|
62 | final JSONObject result = new JSONObject(); |
---|
63 | |
---|
64 | if( login != null && password != null ) |
---|
65 | { |
---|
66 | try |
---|
67 | { |
---|
68 | final User user = getTapasService().getUserByEmail( login ); |
---|
69 | final String encryptedPassword = EtherHelper.encryptPassword( password ); |
---|
70 | if( user == null ) |
---|
71 | result.put( "errors", "login.error.notFound" ); |
---|
72 | else if( !encryptedPassword.equals( user.getPassword() ) ) |
---|
73 | result.put( "errors", "login.error.wrongPassword" ); |
---|
74 | else |
---|
75 | { |
---|
76 | request.getSession().setAttribute( "SES_USER", user ); |
---|
77 | jSONUser.put( "name", user.getLastName() ); |
---|
78 | jSONUser.put( "firstName", user.getFirstName() ); |
---|
79 | jSONUser.put( "role", user.getRole().name() ); |
---|
80 | } |
---|
81 | } |
---|
82 | catch( Exception e ) |
---|
83 | { |
---|
84 | result.put( "errors", "login.error.failed" ); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | if( !jSONUser.isEmpty() ) |
---|
89 | result.put( "jSONUser", jSONUser ); |
---|
90 | return result; |
---|
91 | } |
---|
92 | |
---|
93 | @ControllerMethod(requestMandatory = true, jsonResult = true) |
---|
94 | public JSONObject logout( @NotNull final HttpServletRequest request ) |
---|
95 | throws ServiceException |
---|
96 | { |
---|
97 | request.getSession().setAttribute( "SES_USER", null ); |
---|
98 | request.getSession().invalidate(); |
---|
99 | |
---|
100 | return new JSONObject(); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | @ControllerMethod(jsonResult = true) |
---|
105 | public JSONObject createAccount( @NotNull @ParamName(ParameterConstants.PARAMETER_NAME) final String lastName, |
---|
106 | @Nullable @ParamName(ParameterConstants.PARAMETER_FIRST_NAME) final String firstName, |
---|
107 | @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email, |
---|
108 | @NotNull @ParamName(ParameterConstants.PARAMETER_PWD) final String password, |
---|
109 | @Nullable @ParamName(ParameterConstants.PARAMETER_LABORATORY) final String laboratory, |
---|
110 | @Nullable @ParamName(ParameterConstants.PARAMETER_COUNTRY) final String country ) |
---|
111 | throws ServiceException, WebException |
---|
112 | { |
---|
113 | try |
---|
114 | { |
---|
115 | final User existingUser = getTapasService().getUserByEmail( email ); |
---|
116 | if( null == existingUser ) |
---|
117 | { |
---|
118 | final Date creationDate = new Date(); |
---|
119 | |
---|
120 | final String encryptedPassword = EtherHelper.encryptPassword( password ); |
---|
121 | final User user = new User( lastName, firstName, email, encryptedPassword, UserRole.USER, false, laboratory, country, creationDate ); |
---|
122 | |
---|
123 | getTapasService().createUser( user ); |
---|
124 | |
---|
125 | // Send email to administrator to inform there is a new account |
---|
126 | sendEmailToAdministrator( user ); |
---|
127 | } |
---|
128 | else |
---|
129 | throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); |
---|
130 | } |
---|
131 | catch( DigestException e ) |
---|
132 | { |
---|
133 | throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); |
---|
134 | } |
---|
135 | catch( NoSuchAlgorithmException e ) |
---|
136 | { |
---|
137 | throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); |
---|
138 | } |
---|
139 | |
---|
140 | return new JSONObject(); |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * This method create and send an email to the administrator to inform there is a new account to validate |
---|
145 | * |
---|
146 | * @param user |
---|
147 | * @throws WebException |
---|
148 | */ |
---|
149 | private void sendEmailToAdministrator( @NotNull final User user ) |
---|
150 | throws WebException |
---|
151 | { |
---|
152 | // try |
---|
153 | // { |
---|
154 | // final MailFactory mailFactory = (MailFactory) getServletContext().getAttribute( "APP_MAILFACTORY" ); |
---|
155 | // final String from = (String) getServletContext().getAttribute( "APP_WEBMASTER" ); |
---|
156 | // final String toPI = (String) getServletContext().getAttribute( "APP_PI" ); |
---|
157 | // final String subject = "[MEGAPOLI] Nouvelle demande de compte utilisateur"; |
---|
158 | // final String content = "Hello Matthias,\n\nUne nouvelle demande de compte vient d'arriver.\n\n" + |
---|
159 | // " - Nom : " + user.getLastName() + '\n' + |
---|
160 | // " - Prénom : " + user.getFirstName() + '\n' + |
---|
161 | // " - Email : " + user.getEmail() + "\n\n" + |
---|
162 | // "Tu peux accepter ou refuser son inscription via le BO : http://ether.ipsl.jussieu.fr/megapoli/backoffice\n\n" + |
---|
163 | // "Bonne soirée,\nLe serveur masqué"; |
---|
164 | // |
---|
165 | // final Mail mailAdministrator = new Mail( from, from, null, content, subject ); |
---|
166 | // mailFactory.sendMail( mailAdministrator ); |
---|
167 | // final Mail mailPI = new Mail( from, toPI, null, content, subject ); |
---|
168 | // mailFactory.sendMail( mailPI ); |
---|
169 | // } |
---|
170 | // catch( MessagingException e ) |
---|
171 | // { |
---|
172 | // throw new WebException( WebException.WebCode.ERROR_EMAIL_CANNOT_BE_SEND, "The email cannot be send to the megapoli administrator" ); |
---|
173 | // } |
---|
174 | } |
---|
175 | |
---|
176 | @ControllerMethod(downloadFile = DOWNLOAD_DATA_PROTOCOL_EN) |
---|
177 | public void downloadFileDataProtocol() |
---|
178 | { |
---|
179 | } |
---|
180 | |
---|
181 | private static final Log LOGGER = LogFactory.getLog( ControllerEponge.class ); |
---|
182 | |
---|
183 | private static final String VIEW_INIT = "init"; |
---|
184 | private static final String VIEW_DATA_PROTOCOL_EN = "project/home_dataProtocol_en"; |
---|
185 | private static final String DOWNLOAD_DATA_PROTOCOL_EN = "bib.txt"; |
---|
186 | } |
---|