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.portal.web.documentation;
35
36 import fr.paris.lutece.portal.business.right.FeatureGroup;
37 import fr.paris.lutece.portal.business.right.FeatureGroupHome;
38 import fr.paris.lutece.portal.business.right.Right;
39 import fr.paris.lutece.portal.business.user.AdminUser;
40 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
41 import fr.paris.lutece.portal.service.admin.AdminUserService;
42 import fr.paris.lutece.portal.service.html.XmlTransformerService;
43 import fr.paris.lutece.portal.service.message.AdminMessage;
44 import fr.paris.lutece.portal.service.message.AdminMessageService;
45 import fr.paris.lutece.portal.service.template.AppTemplateService;
46 import fr.paris.lutece.portal.service.util.AppLogService;
47 import fr.paris.lutece.portal.service.util.AppPathService;
48 import fr.paris.lutece.portal.service.util.AppPropertiesService;
49 import fr.paris.lutece.util.html.HtmlTemplate;
50 import fr.paris.lutece.util.http.SecurityUtil;
51
52 import java.io.File;
53
54 import java.util.ArrayList;
55 import java.util.Collection;
56 import java.util.HashMap;
57 import java.util.List;
58 import java.util.Locale;
59 import java.util.Map;
60
61 import javax.servlet.http.HttpServletRequest;
62
63 import javax.xml.transform.stream.StreamSource;
64
65 import org.apache.commons.collections.CollectionUtils;
66 import org.apache.commons.lang3.StringUtils;
67
68
69
70
71
72
73 public class AdminDocumentationJspBean
74 {
75
76 private static final String XSL_PATH = "admin_documentation.xsl";
77
78
79 private static final String PARAMS_LOCAL = "locale";
80 private static final String PARAMS_DEFAULT_LOCAL = "default_locale";
81
82
83 private static final String PARAMETER_FEATURE_DOC = "doc";
84
85
86 private static final String JSP_CLOSE = "javascript:window.close()";
87
88
89 private static final String TEMPLATE_ADMIN_SUMMARY_DOCUMENTATION = "admin/documentation/admin_summary_documentation.html";
90
91
92 private static final String BOOKMARK_FEATURE_GROUP_LIST = "feature_group_list";
93 private static final String BOOKMARK_HELP_ICON = "help_icon";
94
95
96 private static final String IMAGE_HELP_PATH = "images/admin/skin/features/admin_help.png";
97
98
99 private static final String PROPERTY_XSL_BASE_PATH = "lutece.documentation.xsl.path";
100 private static final String ADMIN_DOCUMENTATION_XSL_UNIQUE_PREFIX = "adminDocumentation-";
101
102
103 private static final String MESSAGE_ERROR = "portal.features.documentation.message.error";
104
105
106 private static final String LOCAL_DEFAULT = "en";
107 private static final String PROPERTY_XML_BASE_PATH = "lutece.xml.base.path";
108 private static final String PROPERTY_XML_USER_PATH = "lutece.xml.user.path";
109 private static final String FEATURES_GROUP_SYSTEM = "SYSTEM";
110
111
112
113
114
115
116
117
118
119
120 public String getDocumentation( HttpServletRequest request ) throws AccessDeniedException
121 {
122 String strFeature = request.getParameter( PARAMETER_FEATURE_DOC );
123
124 if ( SecurityUtil.containsPathManipulationChars( request, strFeature ) )
125 {
126 return null;
127 }
128
129 AdminUser user = AdminUserService.getAdminUser( request );
130 Locale locale = user.getLocale( );
131
132
133 String strXslPath = AppPathService.getPath( PROPERTY_XSL_BASE_PATH, XSL_PATH );
134 File fileXsl = new File( strXslPath );
135 StreamSource sourceStyleSheet = new StreamSource( fileXsl );
136
137
138 String xmlBasePath = AppPropertiesService.getProperty( PROPERTY_XML_BASE_PATH );
139 String userBasePath = AppPropertiesService.getProperty( PROPERTY_XML_USER_PATH );
140 String strXmlPath;
141 StreamSource sourceXml;
142
143 if ( LOCAL_DEFAULT.equals( locale.toString( ) ) )
144 {
145 strXmlPath = AppPathService.getWebAppPath( ) + xmlBasePath + userBasePath + strFeature + ".xml";
146 }
147 else
148 {
149 strXmlPath = AppPathService.getWebAppPath( ) + xmlBasePath + locale.toString( ) + userBasePath + strFeature + ".xml";
150 }
151
152 sourceXml = new StreamSource( new File( strXmlPath ) );
153
154 String strHtmlDoc = null;
155
156 Map<String, String> params = new HashMap<>( );
157 params.put( PARAMS_LOCAL, locale.toString( ) );
158 params.put( PARAMS_DEFAULT_LOCAL, LOCAL_DEFAULT );
159
160 XmlTransformerServicervice.html#XmlTransformerService">XmlTransformerService xmlTransformerService = new XmlTransformerService( );
161 String strUniqueId = ADMIN_DOCUMENTATION_XSL_UNIQUE_PREFIX + strXmlPath;
162
163 try
164 {
165 strHtmlDoc = xmlTransformerService.transformBySourceWithXslCache( sourceXml, sourceStyleSheet, strUniqueId, params, null );
166 }
167 catch( Exception e )
168 {
169 AppLogService.error( "Can't parse XML: {}", e.getMessage( ), e );
170
171 return null;
172 }
173
174 return strHtmlDoc;
175 }
176
177
178
179
180
181
182
183
184 public String doAdminMessage( HttpServletRequest request )
185 {
186 return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR, JSP_CLOSE, AdminMessage.TYPE_ERROR );
187 }
188
189
190
191
192
193
194
195
196 public String getSummaryDocumentation( HttpServletRequest request )
197 {
198 AdminUser user = AdminUserService.getAdminUser( request );
199
200 List<FeatureGroup> listFeatureGroups = getFeatureGroupsList( user );
201 Map<String, Object> model = new HashMap<>( );
202
203 model.put( BOOKMARK_FEATURE_GROUP_LIST, listFeatureGroups );
204 model.put( BOOKMARK_HELP_ICON, IMAGE_HELP_PATH );
205
206 HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_ADMIN_SUMMARY_DOCUMENTATION, user.getLocale( ), model );
207
208 return template.getHtml( );
209 }
210
211
212
213
214
215
216
217
218 private List<FeatureGroup> getFeatureGroupsList( AdminUser user )
219 {
220
221 ArrayList<FeatureGroup> aOutFeatureGroupList = new ArrayList<>( );
222
223
224 Map<String, Right> featuresMap = user.getRights( );
225 Collection<Right> features = featuresMap.values( );
226
227
228 for ( FeatureGroup featureGroup : FeatureGroupHome.getFeatureGroupsList( ) )
229 {
230 ArrayList<Right> aLeftFeatures = new ArrayList<>( );
231
232 for ( Right right : features )
233 {
234 right.setLocale( user.getLocale( ) );
235
236 String strFeatureGroup = right.getFeatureGroup( );
237 String strUrlDocumentation = right.getDocumentationUrl( );
238
239 if ( featureGroup.getId( ).equalsIgnoreCase( strFeatureGroup ) && StringUtils.isNotEmpty( strUrlDocumentation ) )
240 {
241 featureGroup.addFeature( right );
242 }
243 else
244 if ( StringUtils.isNotEmpty( strUrlDocumentation ) )
245 {
246 aLeftFeatures.add( right );
247 }
248 }
249
250 if ( !featureGroup.isEmpty( ) )
251 {
252 featureGroup.setLocale( user.getLocale( ) );
253 aOutFeatureGroupList.add( featureGroup );
254 }
255
256 features = aLeftFeatures;
257 }
258
259 FeatureGroup featureGroupSystem = FeatureGroupHome.findByPrimaryKey( FEATURES_GROUP_SYSTEM );
260
261 if ( ( featureGroupSystem != null ) && !features.isEmpty( ) )
262 {
263 boolean bSystemFeaturesGroupExist = false;
264
265
266 for ( FeatureGroup featureGroup : aOutFeatureGroupList )
267 {
268
269 if ( featureGroup.getId( ).equalsIgnoreCase( featureGroupSystem.getId( ) ) )
270 {
271
272 for ( Right right : features )
273 {
274 featureGroup.addFeature( right );
275 }
276
277 bSystemFeaturesGroupExist = true;
278
279 break;
280 }
281 }
282
283
284 if ( !bSystemFeaturesGroupExist )
285 {
286 for ( Right right : features )
287 {
288 featureGroupSystem.addFeature( right );
289 }
290
291 featureGroupSystem.setLocale( user.getLocale( ) );
292 aOutFeatureGroupList.add( featureGroupSystem );
293 }
294 }
295 else
296 if ( CollectionUtils.isNotEmpty( aOutFeatureGroupList ) )
297 {
298 FeatureGroup lastFeatureGroup = aOutFeatureGroupList.get( aOutFeatureGroupList.size( ) - 1 );
299
300 for ( Right right : features )
301 {
302 lastFeatureGroup.addFeature( right );
303 }
304 }
305
306 return aOutFeatureGroupList;
307 }
308 }