View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.wssodatabase.authentication.service;
35  
36  import fr.paris.lutece.plugins.mylutece.authentication.MultiLuteceAuthentication;
37  import fr.paris.lutece.plugins.mylutece.business.attribute.IAttribute;
38  import fr.paris.lutece.plugins.mylutece.modules.wssodatabase.authentication.IdxWSSODatabaseAuthentication;
39  import fr.paris.lutece.plugins.mylutece.modules.wssodatabase.authentication.business.WssoProfilHome;
40  import fr.paris.lutece.plugins.mylutece.modules.wssodatabase.authentication.business.WssoUser;
41  import fr.paris.lutece.plugins.mylutece.modules.wssodatabase.authentication.business.WssoUserRoleHome;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.portal.service.plugin.PluginService;
44  import fr.paris.lutece.portal.service.spring.SpringContextService;
45  import fr.paris.lutece.portal.service.util.AppLogService;
46  import fr.paris.lutece.util.xml.XmlUtil;
47  
48  import java.text.SimpleDateFormat;
49  import java.util.Collection;
50  import java.util.List;
51  import java.util.Locale;
52  
53  import org.apache.commons.collections.CollectionUtils;
54  import org.apache.commons.lang.StringUtils;
55  
56  
57  /**
58   * 
59   * LdapDatabaseService
60   * 
61   */
62  public class WssoDatabaseService
63  {
64      private static final String AUTHENTICATION_BEAN_NAME = "mylutece-wssodatabase.authentication";
65  
66      private static final String CONSTANT_XML_USER = "user";
67      private static final String CONSTANT_XML_GUID = "guid";
68      private static final String CONSTANT_XML_LAST_NAME = "last_name";
69      private static final String CONSTANT_XML_FIRST_NAME = "first_name";
70      private static final String CONSTANT_XML_EMAIL = "email";
71      private static final String CONSTANT_XML_DATE = "date";
72  
73      private static final String CONSTANT_XML_ROLES = "roles";
74      private static final String CONSTANT_XML_ROLE = "role";
75  
76      private static final String CONSTANT_XML_PROFILS = "profils";
77      private static final String CONSTANT_XML_PROFIL = "profil";
78  
79      private static WssoDatabaseService _singleton = new WssoDatabaseService( );
80  
81      /**
82       * Initialize the WssoDatabase service
83       * 
84       */
85      public void init( )
86      {
87          WssoUser.init( );
88  
89          IdxWSSODatabaseAuthentication baseAuthentication = (IdxWSSODatabaseAuthentication) SpringContextService
90                  .getPluginBean( WssoDatabasePlugin.PLUGIN_NAME, AUTHENTICATION_BEAN_NAME );
91  
92          if ( baseAuthentication != null )
93          {
94              MultiLuteceAuthentication.registerAuthentication( baseAuthentication );
95          }
96          else
97          {
98              AppLogService
99                      .error( "IdxWSSODatabaseAuthentication not found, please check your wssodatabase_context.xml configuration" );
100         }
101     }
102 
103     /**
104      * Returns the instance of the singleton
105      * 
106      * @return The instance of the singleton
107      */
108     public static WssoDatabaseService getInstance( )
109     {
110         return _singleton;
111     }
112 
113     /**
114      * Get a XML string describing a given user
115      * @param user The user to get the XML of.
116      * @param bExportRoles True to export roles of the user, false otherwise.
117      * @param bExportGroups True to export groups of the user, false otherwise.
118      * @param bExportProfils True to export profils of the user, false
119      *            otherwise.
120      * @param listAttributes The list of attributes to export.
121      * @param locale The locale
122      * @return A string of XML with the information of the user.
123      */
124     public String getXmlFromUser( WssoUser user, boolean bExportRoles, boolean bExportGroups, boolean bExportProfils,
125             List<IAttribute> listAttributes, Locale locale )
126     {
127         Plugin databasePlugin = PluginService.getPlugin( WssoDatabasePlugin.PLUGIN_NAME );
128 
129         StringBuffer sbXml = new StringBuffer( );
130         XmlUtil.beginElement( sbXml, CONSTANT_XML_USER );
131         XmlUtil.addElement( sbXml, CONSTANT_XML_GUID, user.getGuid( ) );
132         XmlUtil.addElement( sbXml, CONSTANT_XML_LAST_NAME, user.getLastName( ) );
133         XmlUtil.addElement( sbXml, CONSTANT_XML_FIRST_NAME, user.getFirstName( ) );
134         XmlUtil.addElement( sbXml, CONSTANT_XML_EMAIL, user.getEmail( ) );
135         if ( user.getDateLastLogin( ) != null )
136         {
137             SimpleDateFormat dateFormat = new SimpleDateFormat( "dd/MM/yyyy" );
138             XmlUtil.addElement( sbXml, CONSTANT_XML_DATE, dateFormat.format( user.getDateLastLogin( ) ) );
139         }
140         else
141         {
142             XmlUtil.addElement( sbXml, CONSTANT_XML_DATE, StringUtils.EMPTY );
143         }
144         if ( bExportRoles )
145         {
146             Collection<String> userRoleList = WssoUserRoleHome.findRolesListForUser( user.getMyluteceWssoUserId( ),
147                     databasePlugin );
148 
149             if ( CollectionUtils.isNotEmpty( userRoleList ) )
150             {
151                 XmlUtil.beginElement( sbXml, CONSTANT_XML_ROLES );
152 
153                 for ( String strRole : userRoleList )
154                 {
155                     XmlUtil.addElement( sbXml, CONSTANT_XML_ROLE, strRole );
156                 }
157 
158                 XmlUtil.endElement( sbXml, CONSTANT_XML_ROLES );
159             }
160         }
161 
162         if ( bExportProfils )
163         {
164             Collection<String> userProfilList = WssoProfilHome.findWssoProfilsForUser( user.getMyluteceWssoUserId( ),
165                     databasePlugin );
166 
167             if ( CollectionUtils.isNotEmpty( userProfilList ) )
168             {
169                 XmlUtil.beginElement( sbXml, CONSTANT_XML_PROFILS );
170 
171                 for ( String strProfil : userProfilList )
172                 {
173                     XmlUtil.addElement( sbXml, CONSTANT_XML_PROFIL, strProfil );
174                 }
175 
176                 XmlUtil.endElement( sbXml, CONSTANT_XML_PROFILS );
177             }
178         }
179 
180         XmlUtil.endElement( sbXml, CONSTANT_XML_USER );
181 
182         return sbXml.toString( );
183     }
184 }