KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > admin > users > ManageUsers


1 // $Id: ManageUsers.java 13980 2006-04-20 12:37:34Z cmailleux $
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

15 package org.jahia.admin.users;
16
17 import org.jahia.admin.homepages.HomePageTool;
18 import org.jahia.bin.Jahia;
19 import org.jahia.bin.JahiaAdministration;
20 import org.jahia.data.JahiaData;
21 import org.jahia.data.viewhelper.principal.PrincipalViewHelper;
22 import org.jahia.exceptions.JahiaException;
23 import org.jahia.params.ParamBean;
24 import org.jahia.registries.ServicesRegistry;
25 import org.jahia.resourcebundle.JahiaResourceBundle;
26 import org.jahia.security.license.License;
27 import org.jahia.services.fields.ContentField;
28 import org.jahia.services.homepages.JahiaHomepage;
29 import org.jahia.services.homepages.JahiaHomepageLink;
30 import org.jahia.services.homepages.JahiaHomepagesService;
31 import org.jahia.services.pages.ContentPage;
32 import org.jahia.services.pages.JahiaPageBaseService;
33 import org.jahia.services.sites.JahiaSite;
34 import org.jahia.services.usermanager.*;
35 import org.jahia.services.version.EntryLoadRequest;
36 import org.jahia.utils.JahiaTools;
37 import org.jahia.utils.LanguageCodeConverters;
38
39 import javax.servlet.ServletException JavaDoc;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import javax.servlet.http.HttpSession JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.util.*;
45
46 /**
47  * desc: This class is used by the administration to manage users
48  * (add a user, edit and delete) from the Jahia software.
49  *
50  * Copyright: Copyright (c) 2002
51  * Company: Jahia Ltd
52  *
53  * @author Alexandre Kraft
54  * @author Philippe Martin
55  *
56  * @version 2.0
57  */

58 public class ManageUsers
59 {
60     public static final String JavaDoc REQUEST_KEY_PREFIX = "manage-user-";
61     public static final String JavaDoc REQUEST_PASSWORD_KEY = REQUEST_KEY_PREFIX + "password";
62     public static final String JavaDoc REQUEST_PASSWORD_CONFIRMATION_KEY =
63             REQUEST_KEY_PREFIX + "passwordConfirmation";
64
65     public static final String JavaDoc SEPARATOR = "#";
66     public static final String JavaDoc USER_PROPERTY_PREFIX = REQUEST_KEY_PREFIX + "property" + SEPARATOR;
67
68     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
69
70     private static JahiaUserManagerService uMgr;
71     private static JahiaGroupManagerService gMgr;
72
73     private JahiaSite jahiaSite;
74
75     private HomePageTool homePageTool = null;
76
77     ParamBean jParams;
78
79     private String JavaDoc userMessage = "";
80
81     private License coreLicense;
82
83     // This attribute will be set to true if the super admin change its
84
// properties, false other wise.
85
private static boolean isSuperAdminProp;
86
87     /**
88      * Constructor called from JahiaAdministration
89      *
90      * @param request Servlet request.
91      * @param response Servlet response.
92      * @param session Servlet session for the current user.
93      *
94      * @throws Throwable
95      */

96     public ManageUsers( HttpServletRequest JavaDoc request,
97                         HttpServletResponse JavaDoc response,
98                         HttpSession JavaDoc session )
99     throws Throwable JavaDoc
100     {
101         // get services...
102
ServicesRegistry sReg = ServicesRegistry.getInstance();
103         if (sReg != null) {
104             uMgr = sReg.getJahiaUserManagerService();
105             gMgr = sReg.getJahiaGroupManagerService();
106         }
107
108         // get the current website. get the jahiaserver if it's null...
109
jahiaSite = (JahiaSite) session.getAttribute( ParamBean.SESSION_SITE );
110
111         JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
112         this.jParams = jData.params();
113
114         coreLicense = Jahia.getCoreLicense();
115         if ( coreLicense == null ){
116             // set request attributes...
117
String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.invalidLicenseKey.label",
118                                                jParams, jParams.getLocale());
119             request.setAttribute("jahiaDisplayMessage", dspMsg);
120             // redirect...
121
doRedirect( request, response, session, JSP_PATH + "menu.jsp" );
122             return;
123         }
124
125         // continue the execution of user request...
126
userRequestDispatcher( request, response, session );
127     }
128
129     /**
130      * Default constructor.
131      *
132      * @throws Throwable
133      */

134     public ManageUsers()
135     throws Throwable JavaDoc
136     {
137         ServicesRegistry sReg = ServicesRegistry.getInstance();
138         if (sReg != null) {
139             uMgr = sReg.getJahiaUserManagerService();
140             gMgr = sReg.getJahiaGroupManagerService();
141         }
142     }
143
144     /**
145      * This method is used like a dispatcher for user requests.
146      *
147      * @param request Servlet request.
148      * @param response Servlet response.
149      * @param session Servlet session for the current user.
150      *
151      * @throws Throwable
152      */

153     private void userRequestDispatcher( HttpServletRequest JavaDoc request,
154                                         HttpServletResponse JavaDoc response,
155                                         HttpSession JavaDoc session )
156     throws Throwable JavaDoc
157     {
158         String JavaDoc operation = request.getParameter("sub");
159
160         if (operation.equals("display")) {
161                 displayUsers(request, response, session);
162         } else if (operation.equals("search")) {
163             displayUsers(request, response, session);
164         } else if (operation.equals("create")) {
165             displayUserCreate(request, response, session);
166         } else if (operation.equals("edit")) {
167             isSuperAdminProp = false;
168             displayUserEdit(request, response, session);
169         } else if (operation.equals("superAdminProps")) {
170             isSuperAdminProp = true;
171             displayUserEdit(request, response, session);
172         } else if (operation.equals("remove")) {
173             displayUserRemove(request, response, session);
174         } else if (operation.equals("processCreate")) {
175             if (processUserCreate(request, response, session)) {
176                 displayUsers(request, response, session);
177             } else {
178                 displayUserCreate(request, response, session);
179             }
180         } else if (operation.equals("processEdit")) {
181             if (processUserEdit(request, response, session)) {
182                 if (isSuperAdminProp) {
183                     JahiaAdministration.displayMenu(request, response, session);
184                 } else {
185                     displayUsers(request, response, session);
186                 }
187             } else {
188                 displayUserEdit(request, response, session);
189             }
190         } else if (operation.equals("processRemove")) {
191             processUserRemove(request, response, session);
192         } else if (operation.equals("homepage")) {
193             processHomePage(request, response, session);
194         }
195     }
196
197
198     /**
199      * Forward the servlet request and servlet response objects, using the request
200      * dispatcher (from the ServletContext). Note: please be careful, use only
201      * context relative path.
202      *
203      * @param request servlet request.
204      * @param response servlet response.
205      * @param session Servlet session for the current user.
206      * @param target target, context-relative path.
207      * @exception IOException an I/O exception occured during the process.
208      * @exception ServletException a servlet exception occured during the process.
209      */

210     private void doRedirect( HttpServletRequest JavaDoc request,
211                              HttpServletResponse JavaDoc response,
212                              HttpSession JavaDoc session,
213                              String JavaDoc target )
214     throws IOException JavaDoc, ServletException JavaDoc
215     {
216         try {
217             request.setAttribute("jahiaSite",jahiaSite);
218
219             // check null warning msg
220
if ( request.getAttribute("warningMsg") == null ) {
221                 request.setAttribute("warningMsg", "");
222             }
223
224             // check null jsp bottom message, and fill in if necessary...
225
if ( request.getAttribute("msg") == null ) {
226                 request.setAttribute("msg",
227                     JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
228                         jParams, jParams.getLocale()));
229             }
230
231             // check null configuration step title, and fill in if necessary...
232
if ( request.getAttribute("title") == null ) {
233                 request.setAttribute("title", "Manage Users");
234             }
235
236             // redirect!
237
JahiaAdministration.doRedirect(request, response, session, target);
238
239         } catch (IOException JavaDoc ie) {
240             logger.error("Error ", ie);
241         } catch (ServletException JavaDoc se) {
242             logger.error("Error ", se);
243             if (se.getRootCause() != null) {
244                 logger.error("Error root cause", se.getRootCause());
245             }
246         }
247     }
248
249     /**
250      * Display the Jahia users and external (if possible) users management menus
251      * including the user search engine.
252      * @param request
253      * @param response
254      * @param session
255      * @throws IOException
256      * @throws ServletException
257      * @throws JahiaException
258      */

259     private void displayUsers(HttpServletRequest JavaDoc request,
260                               HttpServletResponse JavaDoc response,
261                               HttpSession JavaDoc session)
262     throws IOException JavaDoc, ServletException JavaDoc, JahiaException
263     {
264         request.setAttribute("providerList", uMgr.getProviderList());
265         Set userSet = PrincipalViewHelper.getSearchResult(request, jahiaSite.getID());
266         userSet = PrincipalViewHelper.removeJahiaAdministrators(userSet);
267         request.setAttribute("resultList", userSet);
268         request.setAttribute("currentSite", jahiaSite.getServerName());
269         request.setAttribute("jspSource", JSP_PATH + "user_management/user_management.jsp");
270         request.setAttribute("directMenu", JSP_PATH + "direct_menu.jsp");
271         request.setAttribute("userSearch", JSP_PATH + "user_management/user_search.jsp");
272         session.setAttribute("jahiaDisplayMessage",
273             JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
274                 jParams, jParams.getLocale()));
275         session.setAttribute("userMessage", userMessage);
276         // Remove any home page definition from the session.
277
session.setAttribute("homePageID", null);
278         session.setAttribute("homePageLabel", null);
279         doRedirect(request, response, session, JSP_PATH + "admin.jsp");
280         userMessage = "";
281     }
282
283     /**
284      * Display the user creation interface.
285      *
286      * @param request
287      * @param response
288      * @param session
289      * @throws IOException
290      * @throws ServletException
291      * @throws JahiaException
292      */

293     private void displayUserCreate(HttpServletRequest JavaDoc request,
294                                    HttpServletResponse JavaDoc response,
295                                    HttpSession JavaDoc session)
296     throws IOException JavaDoc, ServletException JavaDoc, JahiaException
297     {
298         logger.debug("Started");
299         // Check user limitation according to license.
300
int nbUserSite = ServicesRegistry.getInstance().getJahiaUserManagerService().getNbUsers();
301         int nbUserLic = Jahia.getUserLimit();
302         /** @todo >= because default user 'root' is not considerated as a real user. */
303         if (!(nbUserLic == -1 || nbUserLic >= nbUserSite)) {
304           userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.licenseLimited.label",
305               jParams, jParams.getLocale());
306           userMessage += " " + nbUserLic + " ";
307           userMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.users.label",
308              jParams, jParams.getLocale());
309             displayUsers(request, response, session);
310             return;
311         }
312         Map userProperties = new HashMap();
313         userProperties.put("username", JahiaTools.nnString(request.getParameter("username")));
314         userProperties.put("passwd", JahiaTools.nnString(request.getParameter("passwd")));
315         userProperties.put("passwdconfirm", JahiaTools.nnString(request.getParameter("passwdconfirm")));
316
317         //userProperties.put("firstname", JahiaTools.nnString(request.getParameter("firstname")));
318
//userProperties.put("lastname", JahiaTools.nnString(request.getParameter("lastname")));
319
//userProperties.put("email", JahiaTools.nnString(request.getParameter("email")));
320
//userProperties.put("organization", JahiaTools.nnString(request.getParameter("organization")));
321
Enumeration names = jParams.getRequest ().getParameterNames ();
322         if (names != null) {
323             while (names.hasMoreElements ()) {
324                 String JavaDoc name = (String JavaDoc) names.nextElement ();
325                 if (name != null && name.startsWith (USER_PROPERTY_PREFIX)) {
326                     String JavaDoc newValue = jParams.getRequest ().getParameter (name);
327                     int index = name.indexOf (SEPARATOR);
328                     String JavaDoc key = name.substring (index + 1);
329                     String JavaDoc currentValue = (String JavaDoc) userProperties.get(key);
330                     if (newValue == null) {
331                         continue;
332                     }
333                     if (currentValue == null) {
334                         userProperties.put (key, newValue);
335                     } else if (!currentValue.equals (newValue)) {
336                         userProperties.put (key, currentValue);
337                     }
338                 }
339             }
340         }
341
342         // Is here any home page definition ?
343
JahiaHomepagesService hpServ = ServicesRegistry.getInstance().getJahiaHomepagesService();
344         JahiaHomepage homePage = hpServ.getHomepage(jahiaSite.getUserDefaultHomepageDef());
345         if (homePage != null) {
346             JahiaHomepageLink homePageLink = (JahiaHomepageLink)homePage;
347             session.setAttribute("homePageID", new Integer JavaDoc(homePageLink.getPageID()));
348             ContentPage contentPage = JahiaPageBaseService.getInstance().
349                 lookupContentPage(homePageLink.getPageID(),jParams.getEntryLoadRequest(),false);
350             request.setAttribute("defaultHomePage", contentPage.getTitle(jParams.getEntryLoadRequest()) +
351                 " (" + homePage.getName() + ")");
352         }
353
354         // set request attributes...
355
request.setAttribute("userProperties", userProperties);
356         session.setAttribute("userMessage", userMessage);
357         if (session.getAttribute("homePageLabel") == null) {
358             // only set this value the first time we arrive on the form, not
359
// when processing an update.
360
String JavaDoc homePageLabelValue =
361                 JahiaResourceBundle.getAdminResource("org.jahia.admin.none.label",
362                     jParams, jParams.getLocale());
363             session.setAttribute("homePageLabel", homePageLabelValue);
364         }
365         request.setAttribute("jspSource", JSP_PATH + "user_management/user_create.jsp");
366         request.setAttribute("directMenu", JSP_PATH + "direct_menu.jsp");
367         session.setAttribute("jahiaDisplayMessage",
368             JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
369                 jParams, jParams.getLocale()));
370         doRedirect(request, response, session, JSP_PATH + "admin.jsp");
371         userMessage = "";
372     }
373
374     /**
375      * Process the user creation interface. If any problem, the "userRequestDispatcher"
376      * method is in charge to redisplay the user creation interface.
377      * 1) Check for a valid user name.
378      * 2) Check for a valid password.
379      * 3) Create a new user and put it into the Jahia DB.
380      *
381      * @param request
382      * @param response
383      * @param session
384      * @return true if operation successed, false otherwise.
385      * @throws IOException
386      * @throws ServletException
387      * @throws JahiaException
388      */

389     private boolean processUserCreate(HttpServletRequest JavaDoc request,
390                                    HttpServletResponse JavaDoc response,
391                                    HttpSession JavaDoc session)
392     throws IOException JavaDoc, ServletException JavaDoc, JahiaException
393     {
394         // get form values...
395
if (!"save".equals(request.getParameter("actionType"))) {
396             // in this case we are just refreshing the form, we do not perform
397
// any saving or checks.
398
return false;
399         }
400         String JavaDoc username = request.getParameter("username").trim();
401         if (username.length() == 0) {
402           userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.specifyUserName.label",
403               jParams, jParams.getLocale());
404             return false;
405         }
406         // The following test is really disputable because we should can enter
407
// as well accentueted char and any internationalized char.
408
else if (!JahiaTools.isAlphaValid(username)) {
409           userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.onlyLettersDigitsUnderscores.label",
410               jParams, jParams.getLocale());
411             return false;
412         }
413         else if (uMgr.userExists(jahiaSite.getID(), username)) {
414           userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.user.label",
415               jParams, jParams.getLocale());
416           userMessage += " [" + username + "] ";
417           userMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.alreadyExist.label",
418               jParams, jParams.getLocale());
419             return false;
420         }
421         String JavaDoc passwd = request.getParameter("passwd").trim();
422         if ("".equals(passwd)) {
423             userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.passwdLeast6Chars.label",
424                 jParams, jParams.getLocale());
425               return false;
426         } else {
427             String JavaDoc passwdConfirm = request.getParameter("passwdconfirm").trim();
428             if (!passwdConfirm.equals(passwd)) {
429               userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.passwdNotMatch.label",
430                   jParams, jParams.getLocale());
431                 return false;
432             } else if (passwd.length() < 6) {
433               userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.passwdLeast6Chars.label",
434                   jParams, jParams.getLocale());
435                 return false;
436             }
437         }
438         Properties userProps = new Properties();
439
440         Enumeration names = jParams.getRequest ().getParameterNames ();
441         if (names != null) {
442             while (names.hasMoreElements ()) {
443                 String JavaDoc name = (String JavaDoc) names.nextElement ();
444                 if (name != null && name.startsWith (USER_PROPERTY_PREFIX)) {
445                     String JavaDoc newValue = jParams.getRequest ().getParameter (name);
446                     int index = name.indexOf (SEPARATOR);
447                     String JavaDoc key = name.substring (index + 1);
448                     String JavaDoc currentValue = (String JavaDoc) userProps.get(key);
449                     if (newValue == null) {
450                         continue;
451                     }
452                     if (currentValue == null) {
453                         userProps.put (key, newValue);
454                     } else if (!currentValue.equals (newValue)) {
455                         userProps.put (key, currentValue);
456                     }
457                 }
458             }
459         }
460
461         JahiaUser usr = uMgr.createUser(username, passwd, username + ":" + jahiaSite.getID(), jahiaSite.getID(), userProps);
462         if (usr == null) {
463           userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.unableCreateUser.label",
464               jParams, jParams.getLocale());
465           userMessage += " " + username;
466             return false;
467         } else {
468             JahiaSiteUserManagerService siteUserManager =
469                 ServicesRegistry.getInstance().getJahiaSiteUserManagerService();
470             siteUserManager.addMember(jahiaSite.getID(), usr);
471             userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.user.label",
472                 jParams, jParams.getLocale());
473             userMessage += " [" + username + "] ";
474             userMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.created.label",
475                 jParams, jParams.getLocale());
476         }
477
478         // Lookup for home page settings and set it.
479
if (request.getParameter("setHomePage") != null) {
480             Integer JavaDoc homePageIDInt = (Integer JavaDoc)session.getAttribute("homePageID");
481             if (homePageIDInt != null) {
482                 int homePageID = homePageIDInt.intValue();
483                 logger.debug("Process Home Page ID : " + homePageID);
484                 usr.setHomepageID(homePageID);
485             }
486         }
487         // Lookup for home page settings and set it.
488
Integer JavaDoc homePageIDInt = (Integer JavaDoc)session.getAttribute("homePageID");
489         if (homePageIDInt != null) {
490             int homePageID = homePageIDInt.intValue();
491             if (homePageID != -1) {
492                 logger.debug("Process Home Page ID : " + homePageID);
493                 usr.setHomepageID(homePageID);
494             } else {
495                 // Why are attributes such JahiaDBUser.mHOMEPAGE_PROP sudenly private ?
496
usr.removeProperty("user_homepage");
497             }
498             session.removeAttribute("homePageID");
499             session.removeAttribute("homePageLabel");
500         }
501         return true;
502     }
503
504     /**
505      * Display all user properties including home page redirection if exists.
506      *
507      * @param request
508      * @param response
509      * @param session
510      * @throws IOException
511      * @throws ServletException
512      * @throws JahiaException
513      */

514     private void displayUserEdit(HttpServletRequest JavaDoc request,
515                                  HttpServletResponse JavaDoc response,
516                                  HttpSession JavaDoc session)
517     throws IOException JavaDoc, ServletException JavaDoc, JahiaException
518     {
519         logger.debug("Started");
520         String JavaDoc userToEdit;
521         JahiaUser theUser;
522         if (isSuperAdminProp) {
523             userToEdit = (String JavaDoc)session.getAttribute(JahiaAdministration.CLASS_NAME + "jahiaLoginUsername");
524             theUser = (JahiaUser)uMgr.lookupUser(0, userToEdit);
525             // Spaces ensure the compatiblity format from the user_management.jsp select box
526
request.setAttribute("isSuperAdminProp", "");
527         } else {
528             userToEdit = (String JavaDoc)request.getParameter("selectedUsers");
529             if (userToEdit == null) { // Get the last user if none was selected.
530
userToEdit = (String JavaDoc)session.getAttribute("selectedUsers");
531             }
532             if (userToEdit == null || "null".equals(userToEdit)) {
533               userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.selectUser.label",
534                   jParams, jParams.getLocale());
535                 displayUsers(request, response, session);
536                 return;
537             }
538             // Consider actual selected user as the last one and store it in session.
539
session.setAttribute("selectedUsers", userToEdit);
540             userToEdit = JahiaTools.replacePattern(userToEdit, "&nbsp;", " ");
541             theUser = (JahiaUser)uMgr.lookupUser(userToEdit.substring(1));
542         }
543         request.setAttribute("theUser", theUser);
544
545         Set groups = (HashSet)getGroups(theUser, jahiaSite.getID());
546         // display the edit form with initial values
547
request.setAttribute("groups", groups);
548
549         UserProperties userProperties = (UserProperties) theUser.getUserProperties().clone();
550         // pick out all the user properties parameters, and set it into the
551
// user properties
552
Enumeration names = jParams.getRequest ().getParameterNames ();
553         if (names != null) {
554             while (names.hasMoreElements ()) {
555                 String JavaDoc name = (String JavaDoc) names.nextElement ();
556                 if (name != null && name.startsWith (USER_PROPERTY_PREFIX)) {
557                     String JavaDoc newValue = jParams.getRequest ().getParameter (name);
558                     int index = name.indexOf (SEPARATOR);
559                     String JavaDoc key = name.substring (index + 1);
560                     UserProperty currentProp = userProperties.getUserProperty (key);
561                     if (newValue == null) {
562                         continue;
563                     }
564                     if (currentProp == null) {
565                         currentProp = new UserProperty(name, newValue, false);
566                         userProperties.setUserProperty (key, currentProp);
567                     } else if (!currentProp.getValue().equals (newValue)) {
568                         //TODO: The new data should be validated here!!
569
if (!currentProp.isReadOnly()) {
570                             currentProp.setValue(newValue);
571                             userProperties.setUserProperty(key, currentProp);
572                         }
573                     }
574                 }
575             }
576         }
577
578         request.setAttribute("passwd", "");
579         request.setAttribute("passwdconfirm", "");
580         request.setAttribute("userProperties", userProperties);
581
582         // Get the home page
583
if (session.getAttribute("homePageLabel") == null) {
584             int homePageID = theUser.getHomepageID();
585             if (homePageID != -1) {
586                 // FIXME : This exception should arrive when the page is
587
// erased. In this case it is the rule of the page remover
588
// to ensure that no references to pages exist before
589
// removing.
590
/** @todo remove this exception catching when a correct page
591                    remover were implemented. */

592                 try {
593                     ArrayList locales = jahiaSite.getLanguageSettingsAsLocales(false);
594                     EntryLoadRequest loadRequest = jParams.getEntryLoadRequest();
595                     if ( !locales.contains(jParams.getLocale()) ){
596                         loadRequest = EntryLoadRequest.STAGED;
597                         locales.add(0,LanguageCodeConverters.languageCodeToLocale(ContentField.SHARED_LANGUAGE));
598                         loadRequest.setLocales(locales);
599                     }
600                     ContentPage contentPage = JahiaPageBaseService.
601                         getInstance().lookupContentPage(homePageID,loadRequest,false);
602                     session.setAttribute("homePageLabel", contentPage.getTitle(loadRequest));
603                     session.setAttribute("homePageID", new Integer JavaDoc(homePageID));
604                 } catch (JahiaException je) {
605                     logger.error("Error ", je);
606                     userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.pageProblem.label",
607                         jParams, jParams.getLocale());
608                     session.setAttribute("homePageLabel", "Disapeared !");
609                 }
610             } else {
611                     String JavaDoc homePageLabelValue =
612                         JahiaResourceBundle.getAdminResource("org.jahia.admin.none.label",
613                             jParams, jParams.getLocale());
614                     session.setAttribute("homePageLabel", homePageLabelValue);
615             }
616         }
617         request.setAttribute("jspSource", JSP_PATH + "user_management/user_edit.jsp");
618         request.setAttribute("directMenu", JSP_PATH + "direct_menu.jsp");
619         session.setAttribute("jahiaDisplayMessage",
620             JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
621                 jParams, jParams.getLocale()));
622         session.setAttribute("userMessage", userMessage);
623         doRedirect(request, response, session, JSP_PATH + "admin.jsp");
624         userMessage = "";
625     }
626
627     /**
628      * Process the user edition formular.
629      * 1) Check if the user is coming out from Jahia DB
630      * 2) Check password validity
631      * 3) Set the user properties in DB
632      * 4) Set the home page redirection if one is set.
633      *
634      * @param request
635      * @param response
636      * @param session
637      * @return true if operation successed, false otherwise.
638      * @throws IOException
639      * @throws ServletException
640      * @throws JahiaException
641      */

642     private boolean processUserEdit(HttpServletRequest JavaDoc request,
643                                     HttpServletResponse JavaDoc response,
644                                     HttpSession JavaDoc session)
645     throws IOException JavaDoc, ServletException JavaDoc, JahiaException
646     {
647         logger.debug("Started");
648         // get form values...
649
String JavaDoc username = request.getParameter("username");
650         JahiaUser usr;
651         final JahiaSiteUserManagerService jahiaSiteUserManagerService = ServicesRegistry.getInstance().getJahiaSiteUserManagerService();
652         if (isSuperAdminProp) {
653             usr = (JahiaUser)uMgr.lookupUser(0, username);
654         } else {
655             usr = jahiaSiteUserManagerService.getMember(jahiaSite.getID(), username);
656         }
657         logger.debug("Update user : " + usr.getUserKey());
658
659         // jahia_db usr processing
660
if ("update".equals(request.getParameter("actionType").trim())) {
661             return false;
662         }
663         String JavaDoc passwd = request.getParameter("passwd");
664         // passwd may be null in case of an LDAP user.
665
if (passwd != null) {
666             passwd = passwd.trim();
667             if (!"".equals(passwd)) {
668                 String JavaDoc passwdConfirm = request.getParameter("passwdconfirm").
669                                        trim();
670                 if (!passwdConfirm.equals(passwd)) {
671                     userMessage = JahiaResourceBundle.getAdminResource(
672                         "org.jahia.admin.userMessage.passwdNotMatch.label",
673                         jParams, jParams.getLocale());
674                     return false;
675                 } else if (passwd.length() < 6) {
676                     userMessage = JahiaResourceBundle.getAdminResource(
677                         "org.jahia.admin.userMessage.passwdLeast6Chars.label",
678                         jParams, jParams.getLocale());
679                     return false;
680                 }
681             }
682             if (!"".equals(passwd) && !usr.setPassword(passwd)) {
683                 userMessage = JahiaResourceBundle.getAdminResource(
684                     "org.jahia.admin.userMessage.cannotChangePasswd.label",
685                     jParams, jParams.getLocale());
686                 userMessage += " [" + username + "] ";
687                 return false;
688             }
689         }
690         // pick out all the user properties parameters, and set it into the
691
// user properties
692
Enumeration names = jParams.getRequest().getParameterNames();
693         if (names != null) {
694             while (names.hasMoreElements()) {
695                 String JavaDoc name = (String JavaDoc) names.nextElement();
696                 if (name != null && name.startsWith(USER_PROPERTY_PREFIX)) {
697                     String JavaDoc newValue = jParams.getRequest().getParameter(name);
698                     int index = name.indexOf(SEPARATOR);
699                     String JavaDoc key = name.substring(index + 1);
700                     UserProperty currentProp = usr.getUserProperty(key);
701                     if (newValue == null) {
702                         continue;
703                     }
704                     if (currentProp == null) {
705                         usr.setProperty(key, newValue);
706                     } else if (!currentProp.getValue().equals(newValue)) {
707                         //TODO: The new data should be validated here!!
708
if (!currentProp.isReadOnly()) {
709                             usr.setProperty(key, newValue);
710                         }
711                     }
712                 }
713             }
714         }
715
716         // Lookup for home page settings and set it.
717
Integer JavaDoc homePageIDInt = (Integer JavaDoc)session.getAttribute("homePageID");
718         if (homePageIDInt != null) {
719             int homePageID = homePageIDInt.intValue();
720             if (homePageID != -1) {
721                 logger.debug("Process Home Page ID : " + homePageID);
722                 usr.setHomepageID(homePageID);
723             } else {
724                 // Why are attributes such JahiaDBUser.mHOMEPAGE_PROP sudenly private ?
725
usr.removeProperty("user_homepage");
726             }
727             session.removeAttribute("homePageID");
728             session.removeAttribute("homePageLabel");
729         }
730         jahiaSiteUserManagerService.refreshUser(jahiaSite.getID(),username);
731         userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.user.label",
732             jParams, jParams.getLocale());
733         userMessage += " [" + username + "] ";
734         userMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.updated.label",
735             jParams, jParams.getLocale());
736         return true;
737     }
738
739     private Set getGroups(JahiaUser usr, int site) {
740         Set groups = new HashSet();
741         JahiaGroupManagerService jahiaGroupManagerService = ServicesRegistry.getInstance().getJahiaGroupManagerService();
742         Vector v = jahiaGroupManagerService.getUserMembership(usr);
743         Enumeration en = v.elements();
744         while (en.hasMoreElements()) {
745             String JavaDoc gname = (String JavaDoc) en.nextElement();
746             JahiaGroup g = jahiaGroupManagerService.lookupGroup(gname);
747             if (g != null && (g.getSiteID() == site || g.getSiteID() ==0)) {
748                 groups.add(g);
749             }
750         }
751         return groups;
752     }
753
754     /**
755      * Display confirmation message for removing or not a Jahia user.
756      * External user cannot be removed actualy.
757      *
758      * @param request
759      * @param response
760      * @param session
761      * @throws IOException
762      * @throws ServletException
763      * @throws JahiaException
764      */

765     private void displayUserRemove(HttpServletRequest JavaDoc request,
766                                    HttpServletResponse JavaDoc response,
767                                    HttpSession JavaDoc session)
768         throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
769
770         logger.debug("Started");
771         String JavaDoc selectedUsers = request.getParameter("selectedUsers");
772         selectedUsers = JahiaTools.replacePattern(selectedUsers, "&nbsp;", " ");
773         if (selectedUsers == null || "null".equals(selectedUsers)) {
774           userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.selectUser.label",
775               jParams, jParams.getLocale());
776             displayUsers(request, response, session);
777         } else {
778             // set request attributes...
779
request.setAttribute("selectedUsers", selectedUsers);
780             session.setAttribute("userMessage", userMessage);
781             request.setAttribute("jspSource", JSP_PATH + "user_management/user_remove.jsp");
782             request.setAttribute("directMenu", JSP_PATH + "direct_menu.jsp");
783             session.setAttribute("jahiaDisplayMessage",
784                 JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
785                     jParams, jParams.getLocale()));
786             doRedirect(request, response, session, JSP_PATH + "admin.jsp");
787             userMessage = "";
788         }
789     }
790
791     /**
792      * Process the Jahia user DB removing.
793      * CAUTION ! The user "guest" cannot be removed.
794      *
795      * @param request
796      * @param response
797      * @param session
798      * @throws IOException
799      * @throws ServletException
800      * @throws JahiaException
801      */

802     private void processUserRemove(HttpServletRequest JavaDoc request,
803                                    HttpServletResponse JavaDoc response,
804                                    HttpSession JavaDoc session)
805     throws IOException JavaDoc, ServletException JavaDoc, JahiaException
806     {
807         logger.debug("Started");
808
809         session.setAttribute("selectedUsers", null);
810         String JavaDoc userName = request.getParameter("username");
811         if (!userName.equals("guest")) {
812             // try to delete the user and memberships...
813
try {
814                 JahiaUser user = uMgr.lookupUser(jahiaSite.getID(), userName);
815                 JahiaUser currentUser = (JahiaUser)session.getAttribute(ParamBean.SESSION_USER);
816                 if (!user.getUserKey().equals(currentUser.getUserKey())) {
817                     JahiaSiteUserManagerService siteUserManager =
818                         ServicesRegistry.getInstance().getJahiaSiteUserManagerService();
819                     siteUserManager.removeMember(user);
820                     uMgr.deleteUser(user);
821                     userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.user.label",
822                         jParams, jParams.getLocale());
823                     userMessage += " [" + userName + "] ";
824                     userMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.removed.label",
825                         jParams, jParams.getLocale());
826                 } else {
827                   userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.cannotRemoveYourUser.label",
828                       jParams, jParams.getLocale());
829                 }
830             } catch (Exception JavaDoc e) {
831               userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.cannotRemoveUser.label",
832                   jParams, jParams.getLocale());
833               userMessage += " " + userName + ".";
834             }
835         } else {
836           userMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.userMessage.cannotRemoveGuest.label",
837               jParams, jParams.getLocale());
838         }
839         displayUsers( request, response, session);
840     }
841
842     /**
843      * Apply the HomePageTool object displaying a home page tool popup window.
844      *
845      * @param request
846      * @param response
847      * @param session
848      * @throws Throwable
849      */

850     private void processHomePage(HttpServletRequest JavaDoc request,
851                                  HttpServletResponse JavaDoc response,
852                                  HttpSession JavaDoc session)
853     throws Throwable JavaDoc
854     {
855         if (homePageTool == null) {
856             homePageTool = new HomePageTool(request, response, session);
857         } else {
858             homePageTool.requestDispatcher(request, response, session);
859         }
860     }
861
862     private static org.apache.log4j.Logger logger =
863              org.apache.log4j.Logger.getLogger(ManageUsers.class);
864
865 }
866
Popular Tags