KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > admin > access > ViewAccess


1 // $Id: ViewAccess.java 16480 2007-02-21 15:25:29Z bpapez $
2
//
3
// ____.
4
// __/\ ______| |__/\. _______
5
// __ .____| | \ | +----+ \
6
// _______| /--| | | - \ _ | : - \_________
7
// \\______: :---| : : | : | \________>
8
// |__\---\_____________:______: :____|____:_____\
9
// /_____|
10
//
11
// . . . i n j a h i a w e t r u s t . . .
12
//
13
//
14
// ViewAccess
15
//
16
// 01.04.2001 AK added in jahia.
17
// 22.04.2001 AK multisite modifications.
18
// 17.05.2001 NK Integrate License Check.
19
//
20

21 package org.jahia.admin.access;
22
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.ServletOutputStream JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.http.HttpSession JavaDoc;
34
35 import org.jahia.bin.Jahia;
36 import org.jahia.bin.JahiaAdministration;
37 import org.jahia.data.JahiaData;
38 import org.jahia.exceptions.JahiaException;
39 import org.jahia.params.ParamBean;
40 import org.jahia.registries.ServicesRegistry;
41 import org.jahia.resourcebundle.JahiaResourceBundle;
42 import org.jahia.services.pages.ContentPage;
43 import org.jahia.services.pages.JahiaPage;
44 import org.jahia.services.sites.JahiaSite;
45 import org.jahia.services.sites.JahiaSitesService;
46 import org.jahia.services.usermanager.JahiaGroupManagerService;
47 import org.jahia.services.usermanager.JahiaUser;
48 import org.jahia.services.usermanager.JahiaUserManagerService;
49 import org.jahia.utils.JahiaTools;
50 import org.jahia.security.license.License;
51
52 /**
53  * desc: This class is used by the administration to view users access
54  *
55  * Copyright: Copyright (c) 2002
56  * Company: Jahia Ltd
57  *
58  * @author Philippe Vollenweider
59  * @version 1.0
60  */

61 public class ViewAccess
62 {
63     private static final String JavaDoc CLASS_NAME = JahiaAdministration.CLASS_NAME;
64     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
65
66     private static final Integer JavaDoc ITEMS_PER_PAGE = new Integer JavaDoc(10);
67
68     private List JavaDoc missingFields = new ArrayList JavaDoc();
69     private List JavaDoc validationErrors = new ArrayList JavaDoc();
70
71     private static JahiaUserManagerService uMgr;
72     private static JahiaGroupManagerService gMgr;
73
74     private JahiaSite site;
75     private JahiaUser user;
76
77     private License coreLicense;
78
79
80
81     /**
82      * Default constructor.
83      * @author Alexandre Kraft
84      *
85      * @param request Servlet request.
86      * @param response Servlet response.
87      * @param session Servlet session for the current user.
88      */

89     public ViewAccess( HttpServletRequest JavaDoc request,
90                         HttpServletResponse JavaDoc response,
91                         HttpSession JavaDoc session )
92     throws Throwable JavaDoc
93     {
94         // get services...
95
ServicesRegistry sReg = ServicesRegistry.getInstance();
96         if (sReg != null) {
97             uMgr = sReg.getJahiaUserManagerService();
98             gMgr = sReg.getJahiaGroupManagerService();
99         }
100
101         // get the current website. get the jahiaserver if it's null...
102
site = (JahiaSite) session.getAttribute( ParamBean.SESSION_SITE );
103         user = (JahiaUser) session.getAttribute( ParamBean.SESSION_USER );
104
105         if(site==null) {
106             JahiaSitesService sitesService = sReg.getJahiaSitesService();
107             site = sitesService.getSite( 0 );
108             session.setAttribute( ParamBean.SESSION_SITE, site );
109         }
110
111         JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
112         ParamBean jParams = null;
113         if (jData != null) {
114             jParams = jData.params();
115         }
116         coreLicense = Jahia.getCoreLicense();
117         if ( coreLicense == null ){
118             // set request attributes...
119
String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.invalidLicense.label",
120                                                jParams, jParams.getLocale());
121             request.setAttribute("jahiaDisplayMessage", dspMsg);
122             // redirect...
123
JahiaAdministration.doRedirect( request, response, session, JSP_PATH + "menu.jsp" );
124             return;
125         }
126
127         // continue the execution of user request...
128
userRequestDispatcher( request, response, session);
129     } // end constructor
130

131
132
133     /**
134      * Default constructor.
135      * @author Alexandre Kraft
136      *
137      * @param request Servlet request.
138      * @param response Servlet response.
139      * @param session Servlet session for the current user.
140      */

141
142     /* FIXME : Not used ?
143     public ViewAccess()
144     throws Throwable
145     {
146         ServicesRegistry sReg = ServicesRegistry.getInstance();
147         if (sReg != null) {
148             uMgr = sReg.getJahiaUserManagerService();
149             gMgr = sReg.getJahiaGroupManagerService();
150         }
151     } // end constructor
152     */

153
154
155
156     /**
157      * This method is used like a dispatcher for user requests.
158      * @author Philippe Vollenweider
159      *
160      * @param request Servlet request.
161      * @param response Servlet response.
162      * @param session Servlet session for the current user.
163      */

164     private void userRequestDispatcher( HttpServletRequest JavaDoc request,
165                                         HttpServletResponse JavaDoc response,
166                                         HttpSession JavaDoc session )
167     throws Throwable JavaDoc
168     {
169         displayAccess( request, response, session, request.getParameter("sub"));
170     } // userRequestDispatcher
171

172
173
174     /**
175      * Display the complete user access list, using JahiaAdministration.doRedirect().
176      * @author Philippe Vollenweider
177      *
178      * @param request The HttpServlet request.
179      * @param response The HttpServlet response.
180      * @param session The HttpSession object.
181      * @param type Describe the type for output (tab || excel)
182      */

183     protected void displayAccess( HttpServletRequest JavaDoc request,
184                                     HttpServletResponse JavaDoc response,
185                                     HttpSession JavaDoc session,
186                                     String JavaDoc type)
187     throws IOException JavaDoc, ServletException JavaDoc
188     {
189         JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
190         ParamBean jParams = null;
191         if (jData != null) {
192             jParams = jData.params ();
193         }
194         // Set the MIME type for the information being sent to the browser.
195
// In this case, we are going to send excel or plain text
196
if (type.equals("excel")){
197             response.setContentType( "application/ms-excel" );
198             response.setHeader("Content-Disposition","attachment; filename=\"user_access.xls\"");
199         } else {
200             response.setContentType( "text/plain" );
201         }
202         // Get a reference to the output stream.
203
// Anything written to this stream is sent directly to the browser
204
// (The browser sees this as its input).
205
ServletOutputStream JavaDoc result = response.getOutputStream();
206
207         
208         try {
209             String JavaDoc output = JahiaTools.html2text(drawRightsUserList(jParams));
210             result.println( output );
211         } catch ( Throwable JavaDoc t ){
212             // oups...
213
} finally {
214             result.close();
215         }
216
217         // reset message...
218
session.setAttribute(CLASS_NAME + "jahiaDisplayMessage",
219             JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
220                 jParams, jParams.getLocale()));
221     } // end displayAccess
222

223
224     private String JavaDoc drawRightsUserList(ParamBean jParams)
225     throws JahiaException {
226
227         StringBuffer JavaDoc output = new StringBuffer JavaDoc();
228
229         // find user list
230
Vector JavaDoc theUserList = ServicesRegistry.getInstance().getJahiaSiteUserManagerService().getMembers(site.getID());
231
232         // find home page
233
JahiaPage theHomePage = site.getHomePage();
234
235         // draw home page's childs, recursively
236
if (theHomePage != null) {
237
238             output.append("path");
239
240             Enumeration JavaDoc theUserList0 = theUserList.elements();
241             Enumeration JavaDoc theUserList1 = theUserList.elements();
242             while (theUserList0.hasMoreElements()) {
243                 JahiaUser theUser = (JahiaUser) theUserList0.nextElement();
244                 output.append("\t").append(theUser.getUsername());
245             }
246
247             output.append("\n").append(theHomePage.getTitle());
248
249             // display users rights
250
output.append(getPageRights(theUserList1, theHomePage));
251             output.append(drawChildPages( theHomePage, theUserList, jParams ));
252         }
253         return output.toString();
254     }
255
256     private String JavaDoc getPageRights( Enumeration JavaDoc theUserList, JahiaPage thePage )
257     throws JahiaException {
258
259         StringBuffer JavaDoc output = new StringBuffer JavaDoc();
260
261         // Return User Rights for a Page
262
while (theUserList.hasMoreElements()) {
263             JahiaUser theUser = (JahiaUser) theUserList.nextElement();
264             output.append("\t");
265             if (thePage.checkReadAccess(theUser)) {
266                 output.append("r ");
267             }
268             if (thePage.checkWriteAccess(theUser)) {
269                 output.append("w ");
270             }
271             if (thePage.checkAdminAccess(theUser)) {
272                 output.append("a");
273             }
274         }
275         return output.append("\n").toString();
276     }
277
278     private String JavaDoc drawChildPages( JahiaPage parentPage, Vector JavaDoc theUserList, ParamBean jParams)
279     throws JahiaException {
280         StringBuffer JavaDoc output = new StringBuffer JavaDoc();
281
282         if (parentPage != null) {
283             // get list of page childs
284
Enumeration JavaDoc pageChilds = parentPage.getChilds(user);
285
286             // parse list of page childs
287
while (pageChilds.hasMoreElements()) {
288                 JahiaPage nextPage = (JahiaPage) pageChilds.nextElement();
289                 if (nextPage != null) {
290                     Enumeration JavaDoc theUserList1 = theUserList.elements();
291                     // display page path
292
Enumeration JavaDoc thePath = nextPage.getContentPagePath(ParamBean.EDIT, user);
293                     while (thePath.hasMoreElements()) {
294                         ContentPage thePage = (ContentPage) thePath.nextElement();
295                         output.append(thePage.getTitle(jParams));
296                         if (thePath.hasMoreElements()) {
297                             output.append(" : ");
298                         }
299                     }
300
301                     // display users rights
302
output.append(getPageRights(theUserList1, nextPage));
303                     output.append(drawChildPages( nextPage, theUserList, jParams ));
304                 }
305             }
306         }
307
308         // return output string
309
return output.toString();
310     }
311
312
313 } // end ViewAccess
Popular Tags