source: ether_statistics/web/src/com/ether/ControllerEponge.java @ 569

Last change on this file since 569 was 569, checked in by vmipsl, 12 years ago

Nouveau projet

File size: 8.8 KB
Line 
1package com.ether;
2
3import com.ether.annotation.ControllerMethod;
4import com.ether.annotation.ParamName;
5import com.ether.user.User;
6import com.ether.user.UserRole;
7import com.ether.user.UserState;
8import com.medias.Constantes;
9import com.medias.Context;
10import com.medias.annuaire.Personne;
11import com.medias.mail.Mail;
12import com.medias.mail.MailFactory;
13import net.sf.json.JSONObject;
14import org.apache.commons.logging.Log;
15import org.apache.commons.logging.LogFactory;
16import org.jetbrains.annotations.NotNull;
17import org.jetbrains.annotations.Nullable;
18
19import javax.mail.MessagingException;
20import javax.servlet.http.HttpServletRequest;
21import java.security.DigestException;
22import java.security.NoSuchAlgorithmException;
23import java.util.ArrayList;
24import java.util.Date;
25import java.util.HashMap;
26import java.util.Map;
27
28/**
29 * @author vmipsl
30 * @date 12 nov 2011
31 */
32public class ControllerEponge
33        extends ControllerEther
34{
35    /** *********************************************************** **/
36    /** *********************** VIEWS ***************************** **/
37    /** *********************************************************** **/
38    @ControllerMethod(view = VIEW_ERRORS)
39    public Map<String, Object> viewErrors()
40            throws ServiceException
41    {
42        return new HashMap<String, Object>();
43    }
44
45    @ControllerMethod(view = VIEW_LOGIN)
46    public Map<String, Object> viewLogin()
47            throws ServiceException
48    {
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 ServiceException
55    {
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        else
61            methodDescription.setView( VIEW_DATA_PROTOCOL_EN );
62        return new HashMap<String, Object>();
63    }
64
65    /** *********************************************************** **/
66    /** *********************** CALLS ***************************** **/
67    /** *********************************************************** **/
68    @ControllerMethod(requestMandatory = true, jsonResult = true)
69    public JSONObject login( @ParamName(ParameterConstants.PARAMETER_LOGIN) final String login,
70                             @ParamName(ParameterConstants.PARAMETER_PWD) final String password,
71                             @NotNull final HttpServletRequest request )
72            throws ServiceException
73    {
74        final JSONObject jSONPeople = new JSONObject();
75        final JSONObject result = new JSONObject();
76
77        if( login != null && password != null )
78        {
79            try
80            {
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                }
109            }
110            catch( Exception e )
111            {
112                result.put( "errors", "login.error.failed" );
113            }
114        }
115
116        if( !jSONPeople.isEmpty() )
117            result.put( "jSONPeople", jSONPeople );
118        return result;
119    }
120
121    @ControllerMethod(requestMandatory = true, jsonResult = true)
122    public JSONObject logout( @NotNull final HttpServletRequest request )
123            throws ServiceException
124    {
125        request.getSession().setAttribute( "SES_USER", null );
126        request.getSession().invalidate();
127
128        return new JSONObject();
129    }
130
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    }
167
168    /**
169     * This method create and send an email to the administrator to inform there is a new account to validate
170     *
171     * @param user
172     * @throws WebException
173     */
174    private void sendEmailToAdministrator( @NotNull final User user )
175            throws WebException
176    {
177        try
178        {
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" +
184                    "   - Nom : " + user.getLastName() + '\n' +
185                    "   - Prénom : " + user.getFirstName() + '\n' +
186                    "   - 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 );
194        }
195        catch( MessagingException e )
196        {
197            throw new WebException( WebException.WebCode.ERROR_EMAIL_CANNOT_BE_SEND, "The email cannot be send to the megapoli administrator" );
198        }
199    }
200
201    private static final Log LOGGER = LogFactory.getLog( ControllerEponge.class );
202
203    private static final String VIEW_ERRORS = "project/errors";
204    private static final String VIEW_LOGIN = "project/login";
205    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";
207}
Note: See TracBrowser for help on using the repository browser.