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.key;
35  
36  import fr.paris.lutece.plugins.mylutece.modules.database.authentication.business.key.DatabaseUserKey;
37  import fr.paris.lutece.plugins.mylutece.modules.database.authentication.business.key.DatabaseUserKeyHome;
38  import fr.paris.lutece.plugins.mylutece.modules.database.authentication.util.DatabaseUtils;
39  import fr.paris.lutece.plugins.mylutece.modules.database.authentication.web.MyLuteceDatabaseApp;
40  import fr.paris.lutece.portal.service.spring.SpringContextService;
41  import fr.paris.lutece.portal.service.util.AppPathService;
42  import fr.paris.lutece.util.url.UrlItem;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  /**
47   *
48   * DatabaseUserKeyService
49   *
50   */
51  public final class DatabaseUserKeyService
52  {
53      private static final String BEAN_DATABASE_USER_KEY_SERVICE = "mylutece-database.databaseUserKeyService";
54  
55      // CONSTANTS
56      private static final String SLASH = "/";
57  
58      // PARAMETERS
59      private static final String PARAMETER_KEY = "key";
60  
61      // JSP
62      private static final String JSP_VALIDATE_ACCOUNT = "jsp/site/plugins/mylutece/modules/database/DoValidateAccount.jsp";
63  
64      /**
65       * Private constructor
66       */
67      private DatabaseUserKeyService( )
68      {
69      }
70  
71      /**
72       * Get the instance of the service
73       * 
74       * @return the instance of the service
75       */
76      public static DatabaseUserKeyService getService( )
77      {
78          return SpringContextService.getBean( BEAN_DATABASE_USER_KEY_SERVICE );
79      }
80  
81      // CRUD
82  
83      /**
84       * Create a new user key from a given user id
85       * 
86       * @param nUserId
87       *            the id user
88       * @return the key
89       */
90      public DatabaseUserKey create( int nUserId )
91      {
92          DatabaseUserKeyins/mylutece/modules/database/authentication/business/key/DatabaseUserKey.html#DatabaseUserKey">DatabaseUserKey userKey = new DatabaseUserKey( );
93          userKey.setUserId( nUserId );
94          userKey.setKey( DatabaseUtils.generateNewKey( ) );
95          DatabaseUserKeyHome.create( userKey );
96  
97          return userKey;
98      }
99  
100     /**
101      * Find the key
102      * 
103      * @param strKey
104      *            the key
105      * @return the key
106      */
107     public DatabaseUserKey findByPrimaryKey( String strKey )
108     {
109         return DatabaseUserKeyHome.findByPrimaryKey( strKey );
110     }
111 
112     /**
113      * Get a key of a user by his login
114      * 
115      * @param strLogin
116      *            The login of the user
117      * @return A key associated to the user
118      */
119     public DatabaseUserKey findKeyByLogin( String strLogin )
120     {
121         return DatabaseUserKeyHome.findKeyByLogin( strLogin );
122     }
123 
124     /**
125      * Remove a key
126      * 
127      * @param strKey
128      *            the key
129      */
130     public void remove( String strKey )
131     {
132         DatabaseUserKeyHome.remove( strKey );
133     }
134 
135     /**
136      * Remove a key from a given id user
137      * 
138      * @param nUserId
139      *            the id user
140      */
141     public void removeByIdUser( int nUserId )
142     {
143         DatabaseUserKeyHome.removeByIdUser( nUserId );
144     }
145 
146     // GET
147 
148     /**
149      * Build the validation url
150      * 
151      * @param strKey
152      *            the key
153      * @param request
154      *            the HTTP request
155      * @return the validation url
156      */
157     public String getValidationUrl( String strKey, HttpServletRequest request )
158     {
159         StringBuilder sbBaseUrl = new StringBuilder( AppPathService.getBaseUrl( request ) );
160 
161         if ( ( sbBaseUrl.length( ) > 0 ) && !sbBaseUrl.toString( ).endsWith( SLASH ) )
162         {
163             sbBaseUrl.append( SLASH );
164         }
165 
166         sbBaseUrl.append( JSP_VALIDATE_ACCOUNT );
167 
168         UrlItem url = new UrlItem( sbBaseUrl.toString( ) );
169         url.addParameter( PARAMETER_KEY, strKey );
170 
171         return url.getUrl( );
172     }
173 
174     /**
175      * Get reinit url
176      * 
177      * @param strKey
178      *            the key
179      * @param request
180      *            the HTTP request
181      * @return the url
182      */
183     public String getReinitUrl( String strKey, HttpServletRequest request )
184     {
185         StringBuilder sbBaseUrl = new StringBuilder( AppPathService.getBaseUrl( request ) );
186 
187         if ( ( sbBaseUrl.length( ) > 0 ) && !sbBaseUrl.toString( ).endsWith( SLASH ) )
188         {
189             sbBaseUrl.append( SLASH );
190         }
191 
192         sbBaseUrl.append( MyLuteceDatabaseApp.getReinitPageUrl( ) );
193 
194         UrlItem url = new UrlItem( sbBaseUrl.toString( ) );
195         url.addParameter( PARAMETER_KEY, strKey );
196 
197         return url.getUrl( );
198     }
199 }