View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.mylutece.modules.openiddatabase.authentication.business;
35  
36  import fr.paris.lutece.portal.service.i18n.I18nService;
37  import fr.paris.lutece.portal.service.mail.MailService;
38  import fr.paris.lutece.portal.service.plugin.Plugin;
39  import fr.paris.lutece.portal.service.template.AppTemplateService;
40  import fr.paris.lutece.portal.service.util.AppPropertiesService;
41  import fr.paris.lutece.util.html.HtmlTemplate;
42  import fr.paris.lutece.util.sql.DAOUtil;
43  
44  import java.util.Calendar;
45  import java.util.Date;
46  import java.util.HashMap;
47  import java.util.Locale;
48  
49  
50  public class PasswordRecoveryByLinkDAO implements IPasswordRecoveryService
51  {
52      private static final String TEMPLATE_EMAIL_BODY_LINK = "skin/plugins/mylutece/modules/openiddatabase/email_body_link.html";
53      private static final String PROPERTY_EMAIL_OBJECT_LINK = "module.mylutece.openiddatabase.email_link.object";
54      private static final String PROPERTY_MYLUTECE_RECOVERY_LINK_VALIDITY = "mylutece-openiddatabase.email.link.validity";
55      private static final String PROPERTY_NOREPLY_EMAIL = "mail.noreply.email";
56      private static final String MARK_UNIQUE_ID = "operation_id";
57      private static final String MARK_USER = "user";
58      private static final String PROPERTY_PROD_BASE_URL = "lutece.prod.url";
59      private static final String MARK_PROD_URL = "prod_url";
60      private static final String SQL_QUERY_INSERT_USER_OPERATION_DEPENDENCY = "INSERT INTO mylutece_database_openid_recovery_user ( mylutece_database_openid_user_id, id_recovery_operation ) VALUES ( ? , ? )";
61      private static final String SQL_QUERY_INSERT = "INSERT  INTO `mylutece_database_openid_recovery`(`id_recovery_operation`,`date_recovery_creation`,`date_recovery_expiration`,`operation_recovery_accomplished`) VALUES ( ? , ? , ? , ? )";
62      private static final String SQL_QUERY_SELECT_BY_TOKEN = "SELECT mylutece_database_openid_user_id FROM mylutece_database_openid_recovery_user where id_recovery_operation= ?";
63      private static final String SQL_QUERY_SELECT_EXPIRATION_DATE_BY_TOKEN = "SELECT date_recovery_expiration FROM mylutece_database_openid_recovery WHERE id_recovery_operation= ? ";
64      
65      /** This class implements the Singleton design pattern. */
66      private static PasswordRecoveryByLinkDAO _dao = new PasswordRecoveryByLinkDAO(  );
67  
68      /**
69       * Returns the unique instance of the singleton.
70       *
71       * @return the instance
72       */
73      static PasswordRecoveryByLinkDAO getInstance(  )
74      {
75          return _dao;
76      }
77  
78      public boolean verifyOperationValid( String strOperationId, Plugin plugin )
79      {
80          // TODO Auto-generated method stub
81          return false;
82      }
83  
84      public String newOperationKey(  )
85      {
86          return java.util.UUID.randomUUID(  ).toString(  );
87      }
88  
89      /**
90       * Inserts the depedency between the user and the operation
91       * @param nUserId The id of the user
92       * @param strIdOperation The id of the operation
93       * @param plugin The plugin
94       */
95      public void insertDependency( int nUserId, String strIdOperation, Plugin plugin )
96      {
97          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_USER_OPERATION_DEPENDENCY, plugin );
98  
99          daoUtil.setInt( 1, nUserId );
100         daoUtil.setString( 2, strIdOperation );
101 
102         daoUtil.executeUpdate(  );
103         daoUtil.free(  );
104     }
105 
106     public void storeOperation( OpenIdDatabaseUser user, String strOperationId, Plugin plugin )
107     {
108         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
109         Date dateNow = Calendar.getInstance(  ).getTime(  );
110         int nDaysValidity = AppPropertiesService.getPropertyInt( PROPERTY_MYLUTECE_RECOVERY_LINK_VALIDITY, 1 );
111         Calendar calExpiration = Calendar.getInstance(  );
112         calExpiration.add( Calendar.DATE, nDaysValidity );
113 
114         Date dateExpiration = calExpiration.getTime(  );
115 
116         daoUtil.setString( 1, strOperationId );
117 
118         daoUtil.setDate( 2, new java.sql.Date( dateNow.getTime(  ) ) );
119         daoUtil.setDate( 3, new java.sql.Date( dateExpiration.getTime(  ) ) );
120         daoUtil.setBoolean( 4, true );
121         insertDependency( user.getUserId(  ), strOperationId, plugin );
122 
123         daoUtil.executeUpdate(  );
124         daoUtil.free(  );
125     }
126 
127     public void processOperations( OpenIdDatabaseUser user, Locale locale, Plugin plugin )
128     {
129         String strNewOperationKey = newOperationKey(  );
130         storeOperation( user, strNewOperationKey, plugin ); //Store
131 
132         HashMap<String, Object> model = new HashMap<String, Object>(  );
133         String strSender = AppPropertiesService.getProperty( PROPERTY_NOREPLY_EMAIL );
134         model.put( MARK_UNIQUE_ID, strNewOperationKey );
135         model.put( MARK_USER, user );
136         model.put( MARK_PROD_URL, AppPropertiesService.getProperty( PROPERTY_PROD_BASE_URL ));
137 
138         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_EMAIL_BODY_LINK, locale, model );
139         MailService.sendMailHtml( user.getEmail(  ), user.getFirstName(  ), strSender, getMailSubject( locale ),
140             template.getHtml(  ) ); //Send Mail
141     }
142 
143     private String getMailSubject( Locale locale )
144     {
145         return I18nService.getLocalizedString( PROPERTY_EMAIL_OBJECT_LINK, locale );
146     }
147 
148     public int getUserId( String strIdToken, Plugin plugin )
149     {
150         int nUserId = 0;
151         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_TOKEN, plugin );
152         daoUtil.setString( 1, strIdToken );
153 
154         daoUtil.executeQuery(  );
155 
156         if ( daoUtil.next(  ) )
157         {
158             nUserId = daoUtil.getInt( 1 );
159         }
160 
161         daoUtil.free(  );
162 
163         return nUserId;
164     }
165 
166     public boolean isExpired( String strIdToken, Plugin plugin )
167     {
168         boolean bIsExpired = false;
169         Date dateExpiration = null;
170         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_EXPIRATION_DATE_BY_TOKEN, plugin );
171         daoUtil.setString( 1, strIdToken );
172         daoUtil.executeQuery(  );
173 
174         if ( daoUtil.next(  ) )
175         {
176             dateExpiration = daoUtil.getDate( 1 );
177         }
178 
179         daoUtil.free(  );
180 
181         Date dateNow = Calendar.getInstance(  ).getTime(  );
182 
183         if ( dateNow.after( dateExpiration ) )
184         {
185             bIsExpired = true;
186         }
187 
188         return bIsExpired;
189     }
190 }