File Source: saveuseraction.java

         /* 
    P/P   *  Method: net.sourceforge.pebble.web.action.SaveUserAction__static_init
          * 
          *  Postconditions:
          *    init'ed(log)
          */
     1  /*
     2   * Copyright (c) 2003-2006, 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  
         /* 
    P/P   *  Method: void net.sourceforge.pebble.web.action.SaveUserAction()
          */
    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  public class SaveUserAction extends SecureAction {
    63  
    64    /** the log used by this class */
    65    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 {
               /* 
    P/P         *  Method: View process(HttpServletRequest, HttpServletResponse)
                * 
                *  Preconditions:
                *    net.sourceforge.pebble.PebbleContext__static_init.new PebbleContext(PebbleContext__static_init#1).configuration != null
                *    request != null
                *    this.model != null
                *    this.model.data != null
                * 
                *  Presumptions:
                *    java.util.Enumeration:nextElement(...)@93 != null
                *    java.util.HashMap:get(...)@63 != null
                *    java.util.Set:toArray(...)@194 != null
                *    javax.servlet.http.HttpServletRequest:getParameter(...)@87 != null
                *    javax.servlet.http.HttpServletRequest:getParameterNames(...)@91 != null
                *    ...
                * 
                *  Postconditions:
                *    return_value in Addr_Set{&new RedirectView(process#4),&new UserView(process#6)}
                *    new RedirectView(process#4) num objects <= 1
                *    new UserView(process#6) num objects <= 1
                * 
                *  Test Vectors:
                *    java.lang.String:equals(...)@109: {1}, {0}
                *    java.lang.String:equalsIgnoreCase(...)@87: {0}, {1}
                *    java.lang.String:length(...)@107: {1..232-1}, {0}
                *    java.lang.String:length(...)@109: {0}, {1..232-1}
                *    java.lang.String:length(...)@121: {0}, {1..232-1}
                *    java.lang.String:startsWith(...)@94: {0}, {1}
                *    java.util.Enumeration:hasMoreElements(...)@92: {1}, {0}
                *    javax.servlet.http.HttpServletRequest:getParameter(...)@79: Addr_Set{null}, Inverse{null}
                *    javax.servlet.http.HttpServletRequest:getParameter(...)@80: Addr_Set{null}, Inverse{null}
                */
    78        AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
    79        String username = request.getParameter("username");
    80        String password1 = request.getParameter("password1");
    81        String password2 = request.getParameter("password2");
    82        String name = request.getParameter("name");
    83        String emailAddress = request.getParameter("emailAddress");
    84        String website = request.getParameter("website");
    85        String profile = request.getParameter("profile");
    86        String roles[] = request.getParameterValues("role");
    87        boolean newUser = request.getParameter("newUser").equalsIgnoreCase("true");
    88        String detailsUpdateableAsString = request.getParameter("detailsUpdateable");
    89        boolean detailsUpdateable = detailsUpdateableAsString != null && detailsUpdateableAsString.equalsIgnoreCase("true");
    90        Map<String,String> preferences = new HashMap<String,String>();
    91        Enumeration parameterNames = request.getParameterNames();
    92        while (parameterNames.hasMoreElements()) {
    93          String parameterName = (String)parameterNames.nextElement();
    94          if (parameterName.startsWith(PREFERENCE)) {
    95            preferences.put(parameterName.substring(PREFERENCE.length()), request.getParameter(parameterName));
    96          }
    97        }
    98  
    99        SecurityRealm realm = PebbleContext.getInstance().getConfiguration().getSecurityRealm();
   100        PebbleUserDetails currentUserDetails = realm.getUser(username);
   101        PebbleUserDetails newUserDetails = new PebbleUserDetails(username, password1, name, emailAddress, website, profile, roles, preferences, detailsUpdateable);
   102  
   103        ValidationContext validationContext = new ValidationContext();
   104  
   105        if (newUser && currentUserDetails != null) {
   106          validationContext.addError("A user with this username already exists");
   107        } else if (newUser && (username == null || username.trim().length() == 0)) {
   108          validationContext.addError("Username can't be empty");
   109        } else if (password1 != null && password1.length() > 0 && !password1.equals(password2)) {
   110          validationContext.addError("Passwords must match");
   111        } else {
   112  
   113          if (newUser) {
   114            try {
   115              realm.createUser(newUserDetails);
   116            } catch (SecurityRealmException sre) {
   117              validationContext.addError(sre.getMessage());
   118            }
   119          } else {
   120            realm.updateUser(newUserDetails);
   121            if (password1 != null && password1.length() > 0) {
   122              realm.changePassword(username, password1);
   123            }
   124          }
   125          return new RedirectView(blog.getUrl() + "viewUsers.secureaction");
   126        }
   127  
   128        getModel().put("validationContext", validationContext);
   129        getModel().put("user", newUserDetails);
   130        getModel().put("newUser", newUser);
   131  
   132        return new UserView();
   133      } catch (SecurityRealmException e) {
   134        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) {
             /* 
    P/P       *  Method: String[] getRoles(HttpServletRequest)
              * 
              *  Presumptions:
              *    init'ed(net.sourceforge.pebble.Constants.BLOG_ADMIN_ROLE)
              * 
              *  Postconditions:
              *    return_value == &new String[](getRoles#1)
              *    new String[](getRoles#1) num objects == 1
              *    return_value.length == 1
              *    return_value[0] == net.sourceforge.pebble.Constants.BLOG_ADMIN_ROLE
              *    (soft) init'ed(return_value[0])
              */
   145      return new String[]{Constants.BLOG_ADMIN_ROLE};
   146    }
   147  
   148  }








SofCheck Inspector Build Version : 2.22510
saveuseraction.java 2010-Jun-25 19:40:34
saveuseraction.class 2010-Jul-19 20:23:38