View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of 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.database.authentication.service.parameter;
35  
36  import fr.paris.lutece.plugins.mylutece.modules.database.authentication.business.DatabaseUserHome;
37  import fr.paris.lutece.plugins.mylutece.modules.database.authentication.business.parameter.DatabaseUserParameterHome;
38  import fr.paris.lutece.plugins.mylutece.modules.database.authentication.service.DatabaseService;
39  import fr.paris.lutece.portal.service.plugin.Plugin;
40  import fr.paris.lutece.portal.service.spring.SpringContextService;
41  import fr.paris.lutece.util.ReferenceItem;
42  import fr.paris.lutece.util.ReferenceList;
43  
44  import java.sql.Timestamp;
45  import java.util.Collections;
46  import java.util.List;
47  
48  /**
49   *
50   * DatabaseUserParameterService
51   *
52   */
53  public final class DatabaseUserParameterService implements IDatabaseUserParameterService
54  {
55      private static final String BEAN_DATABASE_USER_PARAMETER_SERVICE = "mylutece-database.databaseUserParameterService";
56  
57      // PARAMETERS
58      private static final String PARAMETER_ACCOUNT_CREATION_VALIDATION_EMAIL = "account_creation_validation_email";
59      private static final String PARAMETER_ENABLE_JCAPTCHA = "enable_jcaptcha";
60      private static final String PARAMETER_AUTO_LOGIN_AFTER_VALIDATION_EMAIL = "auto_login_after_validation_email";
61  
62      /**
63       * Private constructor
64       */
65      private DatabaseUserParameterService( )
66      {
67      }
68  
69      /**
70       * Get the instance of the service
71       * 
72       * @return the instance of the service
73       */
74      public static DatabaseUserParameterService getService( )
75      {
76          return SpringContextService.getBean( BEAN_DATABASE_USER_PARAMETER_SERVICE );
77      }
78  
79      /**
80       * Get the parameter from a given key
81       * 
82       * @param strParameterKey
83       *            the key
84       * @param plugin
85       *            the plugin
86       * @return the parameter
87       */
88      public ReferenceItem findByKey( String strParameterKey, Plugin plugin )
89      {
90          return DatabaseUserParameterHome.findByKey( strParameterKey, plugin );
91      }
92  
93      /**
94       * Update a parameter
95       * 
96       * @param userParam
97       *            the parameter
98       * @param plugin
99       *            the plugin
100      */
101     public void update( ReferenceItem userParam, Plugin plugin )
102     {
103         if ( userParam != null )
104         {
105             DatabaseUserParameterHome.update( userParam, plugin );
106         }
107     }
108 
109     /**
110      * Find all parameters
111      * 
112      * @param plugin
113      *            the plugin
114      * @return a ReferenceList
115      */
116     public ReferenceList findAll( Plugin plugin )
117     {
118         return DatabaseUserParameterHome.findAll( plugin );
119     }
120 
121     /**
122      * Check if the passwords must be encrypted or not
123      * 
124      * @param plugin
125      *            the plugin
126      * @return <code>false</code>. Passwords are in fact salted and hashed, but we don't want plugin-mylutece to try and hash the password itself
127      */
128     public boolean isPasswordEncrypted( Plugin plugin )
129     {
130         return false;
131     }
132 
133     /**
134      * Get the encryption algorithm
135      * 
136      * @param plugin
137      *            the plugin
138      * @return the encryption algorithm
139      */
140     public String getEncryptionAlgorithm( Plugin plugin )
141     {
142         return "";
143     }
144 
145     /**
146      * Check if the account creation must be validated by email
147      * 
148      * @param plugin
149      *            the plugin
150      * @return true if it must be validated by email, false otherwise
151      */
152     public boolean isAccountCreationValidationEmail( Plugin plugin )
153     {
154         boolean bIsValidationEmail = false;
155         ReferenceItem userParam = findByKey( PARAMETER_ACCOUNT_CREATION_VALIDATION_EMAIL, plugin );
156 
157         if ( ( userParam != null ) && userParam.isChecked( ) )
158         {
159             bIsValidationEmail = true;
160         }
161 
162         return bIsValidationEmail;
163     }
164 
165     /**
166      * {@inheritDoc}
167      */
168     @Override
169     public boolean isAutoLoginAfterValidationEmail( Plugin plugin )
170     {
171         boolean bIsAutoLogin = false;
172         ReferenceItem userParam = findByKey( PARAMETER_AUTO_LOGIN_AFTER_VALIDATION_EMAIL, plugin );
173 
174         if ( ( userParam != null ) && userParam.isChecked( ) )
175         {
176             bIsAutoLogin = true;
177         }
178 
179         return bIsAutoLogin;
180     }
181 
182     /**
183      * Check if the jcaptcha is enable or not
184      * 
185      * @param plugin
186      *            the plugin
187      * @return true if it is enable, false otherwise
188      */
189     public boolean isJcaptchaEnable( Plugin plugin )
190     {
191         boolean bIsEnable = false;
192         ReferenceItem userParam = findByKey( PARAMETER_ENABLE_JCAPTCHA, plugin );
193 
194         if ( ( userParam != null ) && userParam.isChecked( ) && DatabaseService.getService( ).isPluginJcaptchaEnable( ) )
195         {
196             bIsEnable = true;
197         }
198 
199         return bIsEnable;
200     }
201 
202     /**
203      * {@inheritDoc}
204      */
205     @Override
206     public int countUserPasswordHistoryFromDate( Timestamp minDate, int nUserId, Plugin plugin )
207     {
208         return DatabaseUserHome.countUserPasswordHistoryFromDate( minDate, nUserId, plugin );
209     }
210 
211     /**
212      * {@inheritDoc}
213      */
214     @Override
215     public List<String> selectUserPasswordHistory( int nUserID, Plugin plugin )
216     {
217         // the way we store password is incompatible with this function specified by plugin-mulutece
218         return Collections.emptyList( );
219     }
220 }