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

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

Login

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