source: tapas/web/src/com/ether/ControllerEponge.java @ 414

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

login application
servlet data
ControllerEponge?

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