source: ether_megapoli/trunk/web/src/com/ether/ControllerEponge.java @ 280

Last change on this file since 280 was 280, checked in by vmipsl, 13 years ago

Encrypt password

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