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.spring.SpringContextService;
37  import fr.paris.lutece.portal.service.util.AppLogService;
38  
39  import org.springframework.beans.factory.BeanDefinitionStoreException;
40  import org.springframework.beans.factory.CannotLoadBeanClassException;
41  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
42  
43  /**
44   *
45   * DatabaseUserFactory
46   *
47   */
48  public final class DatabaseUserFactory
49  {
50      private static final String BEAN_DATABASE_USER_FACTORY = "mylutece-database.databaseUserFactory";
51      private String _strBeanDatabaseUser;
52      private boolean _bIsEmailUsedAsLogin;
53  
54      /**
55       * Private constructor
56       */
57      private DatabaseUserFactory( )
58      {
59      }
60  
61      /**
62       * Get the instance of the factory
63       * 
64       * @return the instance of the factory
65       */
66      public static DatabaseUserFactory getFactory( )
67      {
68          return SpringContextService.getBean( BEAN_DATABASE_USER_FACTORY );
69      }
70  
71      /**
72       * Set the bean database user
73       * 
74       * @param strBeanDatabaseUser
75       *            the bean database user
76       */
77      public void setBeanDatabaseUser( String strBeanDatabaseUser )
78      {
79          _strBeanDatabaseUser = strBeanDatabaseUser;
80      }
81  
82      /**
83       * Set true if the email is used as login, false otherwise
84       * 
85       * @param bIsEmailUsedAsLogin
86       *            true if the email is used as login, false otherwise
87       */
88      public void setEmailUsedAsLogin( boolean bIsEmailUsedAsLogin )
89      {
90          _bIsEmailUsedAsLogin = bIsEmailUsedAsLogin;
91      }
92  
93      /**
94       * Check if the email is used as login
95       * 
96       * @return true if the email is used as login, false otherwise
97       */
98      public boolean isEmailUsedAsLogin( )
99      {
100         return _bIsEmailUsedAsLogin;
101     }
102 
103     /**
104      * Instanciate a new {@link DatabaseUser} defined in <b>database_context.xml</b>
105      * 
106      * @return a new instance of {@link DatabaseUser}
107      */
108     public DatabaseUser newDatabaseUser( )
109     {
110         DatabaseUser databaseUser = null;
111 
112         try
113         {
114             databaseUser = SpringContextService.getBean( _strBeanDatabaseUser );
115         }
116         catch( BeanDefinitionStoreException e )
117         {
118             if ( AppLogService.isDebugEnabled( ) )
119             {
120                 AppLogService.debug( "DatabaseUserFactory ERROR : could not load bean '" + e.getBeanName( ) + "' - CAUSE : " + e.getMessage( ) );
121             }
122         }
123         catch( NoSuchBeanDefinitionException e )
124         {
125             if ( AppLogService.isDebugEnabled( ) )
126             {
127                 AppLogService.debug( "DatabaseUserFactory ERROR : could not load bean '" + e.getBeanName( ) + "' - CAUSE : " + e.getMessage( ) );
128             }
129         }
130         catch( CannotLoadBeanClassException e )
131         {
132             if ( AppLogService.isDebugEnabled( ) )
133             {
134                 AppLogService.debug( "DatabaseUserFactory ERROR : could not load bean '" + e.getBeanName( ) + "' - CAUSE : " + e.getMessage( ) );
135             }
136         }
137 
138         // New DatabaseUser by default if the plugin cannot load the DatabaseUser by Spring
139         if ( databaseUser == null )
140         {
141             databaseUser = new DatabaseUser( );
142         }
143 
144         return databaseUser;
145     }
146 }