Coverage Report - net.sourceforge.pebble.web.action.SaveUserAction
 
Classes in this File Line Coverage Branch Coverage Complexity
SaveUserAction
0%
0/47
0%
0/30
10.5
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 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 are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     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 OWNER 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  
 package net.sourceforge.pebble.web.action;
 33  
 
 34  
 import net.sourceforge.pebble.Constants;
 35  
 import net.sourceforge.pebble.PebbleContext;
 36  
 import net.sourceforge.pebble.domain.AbstractBlog;
 37  
 import net.sourceforge.pebble.security.PebbleUserDetails;
 38  
 import net.sourceforge.pebble.security.SecurityRealm;
 39  
 import net.sourceforge.pebble.security.SecurityRealmException;
 40  
 import net.sourceforge.pebble.web.security.RequireSecurityToken;
 41  
 import net.sourceforge.pebble.web.validation.ValidationContext;
 42  
 import net.sourceforge.pebble.web.view.RedirectView;
 43  
 import net.sourceforge.pebble.web.view.View;
 44  
 import net.sourceforge.pebble.web.view.impl.UserView;
 45  
 import org.apache.commons.logging.Log;
 46  
 import org.apache.commons.logging.LogFactory;
 47  
 
 48  
 import javax.servlet.ServletException;
 49  
 import javax.servlet.http.HttpServletRequest;
 50  
 import javax.servlet.http.HttpServletResponse;
 51  
 import java.util.Map;
 52  
 import java.util.HashMap;
 53  
 import java.util.Enumeration;
 54  
 
 55  
 /**
 56  
  * Saves user details (this is the blog admin version, where roles
 57  
  * can be changed).
 58  
  *
 59  
  * @author    Simon Brown
 60  
  */
 61  
 @RequireSecurityToken
 62  0
 public class SaveUserAction extends SecureAction {
 63  
 
 64  
   /** the log used by this class */
 65  0
   private static final Log log = LogFactory.getLog(SaveUserAction.class);
 66  
 
 67  
   private static final String PREFERENCE = "preference.";
 68  
 
 69  
   /**
 70  
    * Peforms the processing associated with this action.
 71  
    *
 72  
    * @param request  the HttpServletRequest instance
 73  
    * @param response the HttpServletResponse instance
 74  
    * @return the name of the next view
 75  
    */
 76  
   public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
 77  
     try {
 78  0
       AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
 79  0
       String username = request.getParameter("username");
 80  0
       String password1 = request.getParameter("password1");
 81  0
       String password2 = request.getParameter("password2");
 82  0
       String name = request.getParameter("name");
 83  0
       String emailAddress = request.getParameter("emailAddress");
 84  0
       String website = request.getParameter("website");
 85  0
       String profile = request.getParameter("profile");
 86  0
       String roles[] = request.getParameterValues("role");
 87  0
       boolean newUser = request.getParameter("newUser").equalsIgnoreCase("true");
 88  0
       String detailsUpdateableAsString = request.getParameter("detailsUpdateable");
 89  0
       boolean detailsUpdateable = detailsUpdateableAsString != null && detailsUpdateableAsString.equalsIgnoreCase("true");
 90  0
       Map<String,String> preferences = new HashMap<String,String>();
 91  0
       Enumeration parameterNames = request.getParameterNames();
 92  0
       while (parameterNames.hasMoreElements()) {
 93  0
         String parameterName = (String)parameterNames.nextElement();
 94  0
         if (parameterName.startsWith(PREFERENCE)) {
 95  0
           preferences.put(parameterName.substring(PREFERENCE.length()), request.getParameter(parameterName));
 96  
         }
 97  0
       }
 98  
 
 99  0
       SecurityRealm realm = PebbleContext.getInstance().getConfiguration().getSecurityRealm();
 100  0
       PebbleUserDetails currentUserDetails = realm.getUser(username);
 101  0
       PebbleUserDetails newUserDetails = new PebbleUserDetails(username, password1, name, emailAddress, website, profile, roles, preferences, detailsUpdateable);
 102  
 
 103  0
       ValidationContext validationContext = new ValidationContext();
 104  
 
 105  0
       if (newUser && currentUserDetails != null) {
 106  0
         validationContext.addError("A user with this username already exists");
 107  0
       } else if (newUser && (username == null || username.trim().length() == 0)) {
 108  0
         validationContext.addError("Username can't be empty");
 109  0
       } else if (password1 != null && password1.length() > 0 && !password1.equals(password2)) {
 110  0
         validationContext.addError("Passwords must match");
 111  
       } else {
 112  
 
 113  0
         if (newUser) {
 114  
           try {
 115  0
             realm.createUser(newUserDetails);
 116  0
           } catch (SecurityRealmException sre) {
 117  0
             validationContext.addError(sre.getMessage());
 118  0
           }
 119  
         } else {
 120  0
           realm.updateUser(newUserDetails);
 121  0
           if (password1 != null && password1.length() > 0) {
 122  0
             realm.changePassword(username, password1);
 123  
           }
 124  
         }
 125  0
         return new RedirectView(blog.getUrl() + "viewUsers.secureaction");
 126  
       }
 127  
 
 128  0
       getModel().put("validationContext", validationContext);
 129  0
       getModel().put("user", newUserDetails);
 130  0
       getModel().put("newUser", newUser);
 131  
 
 132  0
       return new UserView();
 133  0
     } catch (SecurityRealmException e) {
 134  0
       throw new ServletException(e);
 135  
     }
 136  
   }
 137  
 
 138  
   /**
 139  
    * Gets a list of all roles that are allowed to access this action.
 140  
    *
 141  
    * @return  an array of Strings representing role names
 142  
    * @param request
 143  
    */
 144  
   public String[] getRoles(HttpServletRequest request) {
 145  0
     return new String[]{Constants.BLOG_ADMIN_ROLE};
 146  
   }
 147  
 
 148  
 }