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 com.ether.user.UserState; |
---|
8 | import com.medias.mail.Mail; |
---|
9 | import com.medias.mail.MailFactory; |
---|
10 | import net.sf.json.JSONObject; |
---|
11 | import org.apache.commons.logging.Log; |
---|
12 | import org.apache.commons.logging.LogFactory; |
---|
13 | import org.jetbrains.annotations.NotNull; |
---|
14 | import org.jetbrains.annotations.Nullable; |
---|
15 | |
---|
16 | import javax.mail.MessagingException; |
---|
17 | import java.security.DigestException; |
---|
18 | import java.security.NoSuchAlgorithmException; |
---|
19 | import java.util.ArrayList; |
---|
20 | import java.util.Date; |
---|
21 | import java.util.HashMap; |
---|
22 | import java.util.List; |
---|
23 | import java.util.Map; |
---|
24 | |
---|
25 | /** |
---|
26 | * @author vmipsl |
---|
27 | * @date 13 nov 2011 |
---|
28 | */ |
---|
29 | public class ControllerBackoffice |
---|
30 | extends ControllerEther |
---|
31 | { |
---|
32 | // TODO : mettre un pwd pour chaque méthode !!! |
---|
33 | |
---|
34 | /** *********************************************************** **/ |
---|
35 | /** *********************** VIEWS ***************************** **/ |
---|
36 | /** *********************************************************** **/ |
---|
37 | @ControllerMethod(view = VIEW_BO_INDEX, backofficeMethod = true, defaultView = VIEW_BO_INDEX) |
---|
38 | public Map<String, Object> home() |
---|
39 | throws ServiceException |
---|
40 | { |
---|
41 | return new HashMap<String, Object>(); |
---|
42 | } |
---|
43 | |
---|
44 | @ControllerMethod(view = VIEW_BO_USER, backofficeMethod = true, defaultView = VIEW_BO_INDEX) |
---|
45 | public Map<String, Object> viewUser() |
---|
46 | throws ServiceException |
---|
47 | { |
---|
48 | final List<User> waitingUsers = getEtherService().getUsersByState( UserState.WAITING ); |
---|
49 | final List<User> users = getEtherService().getAllUsersByNameOrder(); |
---|
50 | |
---|
51 | final Map<String, Object> model = new HashMap<String, Object>(); |
---|
52 | model.put( "jSonWaitingUsers", getJsonHelper().toJSON( waitingUsers ) ); |
---|
53 | model.put( "jSonUsers", getJsonHelper().toJSON( users ) ); |
---|
54 | model.put( "jSonUserStates", getJSONUserStates() ); |
---|
55 | model.put( "jSonUserRoles", getJSONUserRoles() ); |
---|
56 | return model; |
---|
57 | } |
---|
58 | |
---|
59 | /** *********************************************************** **/ |
---|
60 | /** *********************** CALLS ***************************** **/ |
---|
61 | /** *********************************************************** **/ |
---|
62 | @ControllerMethod(jsonResult = true) |
---|
63 | public JSONObject addUser( @NotNull @ParamName(ParameterConstants.PARAMETER_NAME) final String lastName, |
---|
64 | @Nullable @ParamName(ParameterConstants.PARAMETER_FIRST_NAME) final String firstName, |
---|
65 | @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email, |
---|
66 | @NotNull @ParamName(ParameterConstants.PARAMETER_PWD) final String pwd, |
---|
67 | @Nullable @ParamName(ParameterConstants.PARAMETER_ROLE) final String role, |
---|
68 | @NotNull @ParamName(ParameterConstants.PARAMETER_STATE) final String state, |
---|
69 | @NotNull @ParamName(ParameterConstants.PARAMETER_HAS_ACCESS) final Boolean hasAccessToBO ) |
---|
70 | throws WebException, ServiceException |
---|
71 | { |
---|
72 | try |
---|
73 | { |
---|
74 | final User existingUser = getEtherService().getUserByEmail( email ); |
---|
75 | if( null == existingUser ) |
---|
76 | { |
---|
77 | final Date creationDate = new Date(); |
---|
78 | final String encryptedPassword = EtherHelper.encryptPassword( pwd ); |
---|
79 | final User user = new User( lastName, firstName, email, encryptedPassword, role, state, hasAccessToBO, creationDate ); |
---|
80 | |
---|
81 | getEtherService().createUser( user ); |
---|
82 | } |
---|
83 | else |
---|
84 | throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); |
---|
85 | |
---|
86 | } |
---|
87 | catch( DigestException e ) |
---|
88 | { |
---|
89 | throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); |
---|
90 | } |
---|
91 | catch( NoSuchAlgorithmException e ) |
---|
92 | { |
---|
93 | throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); |
---|
94 | } |
---|
95 | |
---|
96 | return getAllAndWaitingUsers(); |
---|
97 | } |
---|
98 | |
---|
99 | @ControllerMethod(jsonResult = true) |
---|
100 | public JSONObject removeUser( @NotNull @ParamName(ParameterConstants.PARAMETER_ID) final Integer userId ) |
---|
101 | throws WebException, ServiceException |
---|
102 | { |
---|
103 | getEtherService().removeUserById( userId ); |
---|
104 | |
---|
105 | return getAllAndWaitingUsers(); |
---|
106 | } |
---|
107 | |
---|
108 | @ControllerMethod(jsonResult = true) |
---|
109 | public JSONObject modifyUser( @NotNull @ParamName(ParameterConstants.PARAMETER_ID) final Integer userId, |
---|
110 | @NotNull @ParamName(ParameterConstants.PARAMETER_NAME) final String lastName, |
---|
111 | @Nullable @ParamName(ParameterConstants.PARAMETER_FIRST_NAME) final String firstName, |
---|
112 | @NotNull @ParamName(ParameterConstants.PARAMETER_EMAIL) final String email, |
---|
113 | @Nullable @ParamName(ParameterConstants.PARAMETER_PWD) final String pwd, |
---|
114 | @Nullable @ParamName(ParameterConstants.PARAMETER_ROLE) final String role, |
---|
115 | @NotNull @ParamName(ParameterConstants.PARAMETER_STATE) final String state, |
---|
116 | @NotNull @ParamName(ParameterConstants.PARAMETER_HAS_ACCESS) final Boolean hasAccessToBO, |
---|
117 | @NotNull @ParamName(ParameterConstants.PARAMETER_KEEP_SAME_PASSWORD) final Boolean keepSamePassword ) |
---|
118 | throws WebException, ServiceException |
---|
119 | { |
---|
120 | try |
---|
121 | { |
---|
122 | final User existingUser = getEtherService().getUserByEmail( email ); |
---|
123 | if( null == existingUser || userId.equals( existingUser.getId() ) ) |
---|
124 | { |
---|
125 | final User user = getEtherService().getUserById( userId ); |
---|
126 | user.setLastName( lastName ); |
---|
127 | user.setFirstName( firstName ); |
---|
128 | user.setEmail( email ); |
---|
129 | if( !keepSamePassword && null != pwd ) |
---|
130 | { |
---|
131 | final String encryptedPassword = EtherHelper.encryptPassword( pwd ); |
---|
132 | user.setPassword( encryptedPassword ); |
---|
133 | } |
---|
134 | user.setRole( UserRole.valueOf( role ) ); |
---|
135 | user.setState( UserState.valueOf( state ) ); |
---|
136 | user.setAccessToBO( hasAccessToBO ); |
---|
137 | |
---|
138 | getEtherService().updateUser( user ); |
---|
139 | } |
---|
140 | else |
---|
141 | throw new WebException( WebException.WebCode.USER_ALREADY_EXISTS, "This email already corresponds to an User", WebException.getExceptionThrowable() ); |
---|
142 | } |
---|
143 | catch( DigestException e ) |
---|
144 | { |
---|
145 | throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); |
---|
146 | } |
---|
147 | catch( NoSuchAlgorithmException e ) |
---|
148 | { |
---|
149 | throw new WebException( WebException.WebCode.ERROR_ENCRYPT_PASSWORD, "This password cannot be encrypted" ); |
---|
150 | } |
---|
151 | |
---|
152 | return getAllAndWaitingUsers(); |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * This method changes the state user and sends an email to inform of the administrator decision. |
---|
157 | * If the email can't be sent, the state will be not changed |
---|
158 | * |
---|
159 | * @param userId |
---|
160 | * @param isAccepted |
---|
161 | * @return |
---|
162 | * @throws WebException |
---|
163 | * @throws ServiceException |
---|
164 | */ |
---|
165 | @ControllerMethod(jsonResult = true) |
---|
166 | public JSONObject acceptOrRefuseUser( @NotNull @ParamName(ParameterConstants.PARAMETER_ID) final Integer userId, |
---|
167 | @ParamName(ParameterConstants.PARAMETER_OK) final boolean isAccepted ) |
---|
168 | throws WebException, ServiceException |
---|
169 | { |
---|
170 | final User user = getEtherService().getUserById( userId ); |
---|
171 | try |
---|
172 | { |
---|
173 | sendEmailToUser( user, isAccepted ); |
---|
174 | getEtherService().acceptOrRefuseUser( userId, isAccepted ); |
---|
175 | } |
---|
176 | catch( MessagingException e ) |
---|
177 | { |
---|
178 | throw new WebException( WebException.WebCode.ERROR_EMAIL_CANNOT_BE_SEND, "The email cannot be send to the user : " + user.getEmail(), WebException.getExceptionThrowable() ); |
---|
179 | } |
---|
180 | |
---|
181 | return getAllAndWaitingUsers(); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * This method create and send an email to the user to inform of the administrator decision |
---|
186 | * |
---|
187 | * @param user |
---|
188 | * @param isAccepted |
---|
189 | * @throws WebException |
---|
190 | */ |
---|
191 | private void sendEmailToUser( @NotNull final User user, final boolean isAccepted ) |
---|
192 | throws MessagingException |
---|
193 | { |
---|
194 | final MailFactory mailFactory = (MailFactory) getServletContext().getAttribute( "APP_MAILFACTORY" ); |
---|
195 | final String from = (String) getServletContext().getAttribute( "APP_WEBMASTER" ); |
---|
196 | final String subject = "[MEGAPOLI] User inscription"; |
---|
197 | String content = "Dear user, \n\nThe Megapoli administrator has "; |
---|
198 | if( isAccepted ) |
---|
199 | content += "accepted your inscription. You can now access to data with you login (" + user.getEmail() + ") and the password you gave during inscription.\n"; |
---|
200 | else |
---|
201 | content += "refused your inscription. You can reply to this email if you want more information.\n"; |
---|
202 | |
---|
203 | content += "\nBest regards,\nMegapoli administrator"; |
---|
204 | |
---|
205 | final Mail mail = new Mail( from, user.getEmail(), null, content, subject ); |
---|
206 | mailFactory.sendMail( mail ); |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | private JSONObject getAllAndWaitingUsers() |
---|
211 | throws ServiceException |
---|
212 | { |
---|
213 | final List<User> waitingUsers = getEtherService().getUsersByState( UserState.WAITING ); |
---|
214 | final List<User> users = getEtherService().getAllUsersByNameOrder(); |
---|
215 | |
---|
216 | final JSONObject result = new JSONObject(); |
---|
217 | result.put( "jSonWaitingUsers", getJsonHelper().toJSON( waitingUsers ) ); |
---|
218 | result.put( "jSonUsers", getJsonHelper().toJSON( users ) ); |
---|
219 | return result; |
---|
220 | } |
---|
221 | |
---|
222 | private List<JSONObject> getJSONUserStates() |
---|
223 | { |
---|
224 | final UserState[] userStates = UserState.values(); |
---|
225 | |
---|
226 | final List<JSONObject> jsonUserStates = new ArrayList<JSONObject>( userStates.length ); |
---|
227 | |
---|
228 | for( final UserState userState : userStates ) |
---|
229 | { |
---|
230 | final JSONObject jsonUserState = new JSONObject(); |
---|
231 | jsonUserState.put( "text", userState.name() ); |
---|
232 | jsonUserState.put( "value", userState.name() ); |
---|
233 | jsonUserStates.add( jsonUserState ); |
---|
234 | } |
---|
235 | return jsonUserStates; |
---|
236 | } |
---|
237 | |
---|
238 | private List<JSONObject> getJSONUserRoles() |
---|
239 | { |
---|
240 | final UserRole[] userRoles = UserRole.values(); |
---|
241 | |
---|
242 | final List<JSONObject> jsonUserRoles = new ArrayList<JSONObject>( userRoles.length ); |
---|
243 | |
---|
244 | for( final UserRole userRole : userRoles ) |
---|
245 | { |
---|
246 | final JSONObject jsonUserRole = new JSONObject(); |
---|
247 | jsonUserRole.put( "text", userRole.name() ); |
---|
248 | jsonUserRole.put( "value", userRole.name() ); |
---|
249 | jsonUserRoles.add( jsonUserRole ); |
---|
250 | } |
---|
251 | return jsonUserRoles; |
---|
252 | } |
---|
253 | |
---|
254 | private static final Log LOGGER = LogFactory.getLog( ControllerBackoffice.class ); |
---|
255 | |
---|
256 | private static final String VIEW_BO_INDEX = "backoffice/index"; |
---|
257 | private static final String VIEW_BO_USER = "backoffice/user"; |
---|
258 | } |
---|