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.business;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  import fr.paris.lutece.portal.service.util.AppPropertiesService;
38  import fr.paris.lutece.util.url.UrlItem;
39  
40  import java.io.UnsupportedEncodingException;
41  
42  import java.net.URLEncoder;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  /**
47   * This class provides a filter for users search function
48   */
49  public class DatabaseUserFilter
50  {
51      // Constants
52      private static final String EQUAL = "=";
53      private static final String AMPERSAND = "&";
54  
55      // Parameteres
56      private static final String PARAMETER_SEARCH_LOGIN = "search_login";
57      private static final String PARAMETER_SEARCH_LAST_NAME = "search_last_name";
58      private static final String PARAMETER_SEARCH_FIRST_NAME = "search_first_name";
59      private static final String PARAMETER_SEARCH_EMAIL = "search_email";
60      private static final String PARAMETER_SEARCH_IS_SEARCH = "search_is_search";
61  
62      // Properties
63      private static final String PROPERTY_ENCODING_URL = "lutece.encoding.url";
64      private String _strLogin;
65      private String _strLastName;
66      private String _strFirstName;
67      private String _strEmail;
68  
69      /**
70       * Initialize each component of the object
71       */
72      public void init( )
73      {
74          _strLogin = "";
75          _strLastName = "";
76          _strFirstName = "";
77          _strEmail = "";
78      }
79  
80      /**
81       * Get the access code
82       * 
83       * @return The access code
84       */
85      public String getLogin( )
86      {
87          return _strLogin;
88      }
89  
90      /**
91       * Set the access code
92       * 
93       * @param strAccessCode
94       *            The Access Code
95       */
96      public void setAccessCode( String strAccessCode )
97      {
98          _strLogin = strAccessCode;
99      }
100 
101     /**
102      * Get the last name
103      * 
104      * @return The last name
105      */
106     public String getLastName( )
107     {
108         return _strLastName;
109     }
110 
111     /**
112      * Set the last name
113      * 
114      * @param strLastName
115      *            The Last Name
116      */
117     public void setLastName( String strLastName )
118     {
119         _strLastName = strLastName;
120     }
121 
122     /**
123      * Get the first name
124      * 
125      * @return The first name
126      */
127     public String getFirstName( )
128     {
129         return _strFirstName;
130     }
131 
132     /**
133      * Set the first name
134      * 
135      * @param strFirstName
136      *            The First Name
137      */
138     public void setFirstName( String strFirstName )
139     {
140         _strFirstName = strFirstName;
141     }
142 
143     /**
144      * Get the email
145      * 
146      * @return The email
147      */
148     public String getEmail( )
149     {
150         return _strEmail;
151     }
152 
153     /**
154      * Set the email
155      * 
156      * @param strEmail
157      *            The email
158      */
159     public void setEmail( String strEmail )
160     {
161         _strEmail = strEmail;
162     }
163 
164     /**
165      * Set the value of the AdminUserFilter
166      * 
167      * @param request
168      *            HttpServletRequest
169      * @return true if there is a search
170      */
171     public boolean setDatabaseUserFilter( HttpServletRequest request )
172     {
173         boolean bIsSearch = false;
174         String strIsSearch = request.getParameter( PARAMETER_SEARCH_IS_SEARCH );
175 
176         if ( strIsSearch != null )
177         {
178             bIsSearch = true;
179             _strLogin = request.getParameter( PARAMETER_SEARCH_LOGIN );
180             _strLastName = request.getParameter( PARAMETER_SEARCH_LAST_NAME );
181             _strFirstName = request.getParameter( PARAMETER_SEARCH_FIRST_NAME );
182             _strEmail = request.getParameter( PARAMETER_SEARCH_EMAIL );
183         }
184         else
185         {
186             init( );
187         }
188 
189         return bIsSearch;
190     }
191 
192     /**
193      * Build url attributes
194      * 
195      * @param url
196      *            the url
197      */
198     public void setUrlAttributes( UrlItem url )
199     {
200         url.addParameter( PARAMETER_SEARCH_IS_SEARCH, Boolean.TRUE.toString( ) );
201 
202         try
203         {
204             url.addParameter( PARAMETER_SEARCH_LOGIN, URLEncoder.encode( _strLogin, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
205             url.addParameter( PARAMETER_SEARCH_LAST_NAME, URLEncoder.encode( _strLastName, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
206             url.addParameter( PARAMETER_SEARCH_FIRST_NAME, URLEncoder.encode( _strFirstName, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
207             url.addParameter( PARAMETER_SEARCH_EMAIL, URLEncoder.encode( _strEmail, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
208         }
209         catch( UnsupportedEncodingException e )
210         {
211             AppLogService.error( e );
212         }
213     }
214 
215     /**
216      * Build url attributes
217      * 
218      * @return the url attributes
219      */
220     public String getUrlAttributes( )
221     {
222         StringBuilder sbUrlAttributes = new StringBuilder( );
223         sbUrlAttributes.append( PARAMETER_SEARCH_IS_SEARCH + EQUAL + Boolean.TRUE );
224 
225         try
226         {
227             sbUrlAttributes.append(
228                     AMPERSAND + PARAMETER_SEARCH_LOGIN + EQUAL + URLEncoder.encode( _strLogin, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
229             sbUrlAttributes.append( AMPERSAND + PARAMETER_SEARCH_LAST_NAME + EQUAL
230                     + URLEncoder.encode( _strLastName, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
231             sbUrlAttributes.append( AMPERSAND + PARAMETER_SEARCH_FIRST_NAME + EQUAL
232                     + URLEncoder.encode( _strFirstName, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
233             sbUrlAttributes.append(
234                     AMPERSAND + PARAMETER_SEARCH_EMAIL + EQUAL + URLEncoder.encode( _strEmail, AppPropertiesService.getProperty( PROPERTY_ENCODING_URL ) ) );
235         }
236         catch( UnsupportedEncodingException e )
237         {
238             AppLogService.error( e );
239         }
240 
241         return sbUrlAttributes.toString( );
242     }
243 }