1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.adminauthenticationwsso.service;
35
36 import java.io.File;
37 import java.io.FileWriter;
38 import java.io.IOException;
39 import java.text.DateFormat;
40 import java.text.SimpleDateFormat;
41 import java.util.Collection;
42 import java.util.Date;
43 import java.util.HashMap;
44
45 import fr.paris.lutece.portal.business.user.AdminUser;
46 import fr.paris.lutece.portal.business.user.AdminUserFilter;
47 import fr.paris.lutece.portal.business.user.AdminUserHome;
48 import fr.paris.lutece.portal.service.plugin.Plugin;
49 import fr.paris.lutece.portal.service.util.AppLogService;
50 import fr.paris.lutece.portal.service.util.AppPathService;
51 import fr.paris.lutece.portal.service.util.AppPropertiesService;
52 import fr.paris.lutece.util.xml.XmlUtil;
53 import java.util.regex.Matcher;
54 import java.util.regex.Pattern;
55
56
57
58
59
60
61 public class WssoAdminUsersFileGeneratorService
62 {
63
64
65 private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
66
67
68
69 private static final String PROPERTY_APP_ID = "adminauthenticationwsso.appID";
70 private static final String PROPERTY_APP_RESPONSABLE = "adminauthenticationwsso.appResponsable";
71 private static final String PROPERTY_XML_STORAGE_FOLDER_PATH = "adminauthenticationwsso.path";
72 private static final String PROPERTY_XML_FILE_NAME = "adminauthenticationwsso.fileName";
73 private static final String PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO = "adminauthenticationwsso.wssofileformat.tag_autorisationWSSO";
74 private static final String PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO = "adminauthenticationwsso.wssofileformat.tag_applicationWSSO";
75 private static final String PROPERTY_XMLFILEFORMAT_ATTR_APPLICATION_WSSO_APP_ID = "adminauthenticationwsso.wssofileformat.tag_appID";
76 private static final String PROPERTY_XMLFILEFORMAT_TRANSMISSION_DATE = "adminauthenticationwsso.wssofileformat.tag_transmissionDate";
77 private static final String PROPERTY_XMLFILEFORMAT_ATTR_TRANSMISSION_DATE_DATE = "adminauthenticationwsso.wssofileformat.tag_date";
78 private static final String PROPERTY_XMLFILEFORMAT_APP_RESPONSABLE = "adminauthenticationwsso.wssofileformat.tag_appResponsable";
79 private static final String PROPERTY_XMLFILEFORMAT_ATTR_APP_RESPONSABLE_MAIL = "adminauthenticationwsso.wssofileformat.tag_mail";
80 private static final String PROPERTY_XMLFILEFORMAT_ALLOWED_USER = "adminauthenticationwsso.wssofileformat.tag_allowedUser";
81 private static final String PROPERTY_XMLFILEFORMAT_ATTR_ALLOWED_USER_WSSO_GUID = "adminauthenticationwsso.wssofileformat.tag_wssoGUID";
82 private static final String LOG_MESSAGE_OK = "\nWssoAdminUserFileGeneratorService : Update OK for file ";
83 private static final String LOG_MESSAGE_NOK = "\nWssoAdminUserFileGeneratorService : Error when updating file ";
84
85
86 private static final String REGEX_WSSO_ID = AppPropertiesService.getProperty( "adminauthenticationwsso.wssoid.regex" );
87
88
89
90
91
92 public WssoAdminUsersFileGeneratorService( )
93 {
94 }
95
96
97
98
99
100
101
102 public static String getXml( Plugin plugin )
103 {
104 StringBuffer strXml = new StringBuffer( );
105 Date date = new Date( );
106 HashMap attrList = new HashMap( );
107
108 AdminUserFilter auFilter = new AdminUserFilter();
109 auFilter.init();
110 auFilter.setStatus(AdminUser.ACTIVE_CODE);
111 Collection<AdminUser> userList = AdminUserHome.findUserByFilter(auFilter);
112
113
114
115
116 XmlUtil.beginElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO ) );
117
118
119 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_APPLICATION_WSSO_APP_ID ),
120 AppPropertiesService.getProperty( PROPERTY_APP_ID ) );
121 XmlUtil.beginElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO ),
122 attrList );
123 attrList.clear( );
124
125
126 DateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
127 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_TRANSMISSION_DATE_DATE ),
128 dateFormat.format( date ) );
129 XmlUtil.addEmptyElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_TRANSMISSION_DATE ),
130 attrList );
131 attrList.clear( );
132
133
134 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_APP_RESPONSABLE_MAIL ),
135 AppPropertiesService.getProperty( PROPERTY_APP_RESPONSABLE ) );
136 XmlUtil.addEmptyElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APP_RESPONSABLE ),
137 attrList );
138 attrList.clear( );
139
140
141 Pattern pattern = Pattern.compile( REGEX_WSSO_ID );
142 for ( AdminUser user : userList )
143 {
144 Matcher matcher = pattern.matcher( user.getAccessCode( ) );
145 if ( matcher.matches( ) )
146 {
147 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_ALLOWED_USER_WSSO_GUID ),
148 user.getAccessCode( ) );
149 XmlUtil.addEmptyElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ALLOWED_USER ),
150 attrList );
151 attrList.clear( );
152 }
153 }
154
155
156 XmlUtil.endElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO ) );
157
158
159 XmlUtil.endElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO ) );
160
161 return strXml.toString( );
162 }
163
164
165
166
167
168
169 public static String createXmlFile( Plugin plugin )
170 {
171 FileWriter fileXmlWriter = null;
172
173
174 StringBuffer sbLogs = new StringBuffer( );
175 String strFileName = AppPropertiesService.getProperty( PROPERTY_XML_FILE_NAME );
176 String strFolderPath = AppPathService.getPath( PROPERTY_XML_STORAGE_FOLDER_PATH, "" );
177
178 try
179 {
180
181 if ( !new File( strFolderPath ).exists( ) )
182 {
183 File fileFolder = new File( strFolderPath );
184 fileFolder.mkdir( );
185 }
186
187
188 File fileXml = new File( strFolderPath + strFileName );
189 File fileXmlDirectory = new File( strFolderPath );
190 File fileXmlTemp = File.createTempFile( "tmp", null, fileXmlDirectory );
191 fileXmlWriter = new FileWriter( fileXmlTemp );
192 fileXmlWriter.write( getXml( plugin ) );
193 fileXmlWriter.flush( );
194 fileXmlWriter.close( );
195
196
197 removeXmlFile( );
198 fileXmlTemp.renameTo( fileXml );
199 sbLogs.append( LOG_MESSAGE_OK + strFileName );
200 }
201 catch ( IOException e )
202 {
203 AppLogService.error( e.getMessage( ), e );
204 sbLogs.append( LOG_MESSAGE_NOK + strFileName );
205 }
206 catch ( NullPointerException e )
207 {
208 AppLogService.error( e.getMessage( ), e );
209 sbLogs.append( LOG_MESSAGE_NOK + strFileName );
210 }
211
212 return sbLogs.toString( );
213 }
214
215
216
217
218
219 public static void removeXmlFile( )
220 {
221 String strFileXml = AppPathService.getPath( PROPERTY_XML_STORAGE_FOLDER_PATH, "" ) +
222 AppPropertiesService.getProperty( PROPERTY_XML_FILE_NAME );
223 File file = new File( strFileXml );
224
225
226 if ( file.exists( ) )
227 {
228 file.delete( );
229 }
230 }
231 }