Coverage Report - net.sourceforge.pebble.security.PebbleUserDetails
 
Classes in this File Line Coverage Branch Coverage Complexity
PebbleUserDetails
39%
39/100
31%
7/22
1.361
 
 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  
 
 33  
 package net.sourceforge.pebble.security;
 34  
 
 35  
 import net.sourceforge.pebble.Constants;
 36  
 import org.springframework.security.core.GrantedAuthority;
 37  
 import org.springframework.security.core.authority.GrantedAuthorityImpl;
 38  
 import org.springframework.security.core.userdetails.UserDetails;
 39  
 
 40  
 import java.util.*;
 41  
 
 42  
 /**
 43  
  * Extension of the Acegi User class that adds additional information
 44  
  * such as the user's e-mail address.
 45  
  *
 46  
  * @author    Simon Brown
 47  
  */
 48  
 public class PebbleUserDetails implements UserDetails {
 49  
 
 50  
   public static final String OPEN_IDS_PREFERENCE = "openids";
 51  
   // Reserved unwise character from RFC-2396
 52  
   private static final String OPEN_IDS_SEPARATOR = "|";
 53  
 
 54  
   private String username;
 55  
   private String password;
 56  
 
 57  
   /** the name */
 58  
   private String name;
 59  
 
 60  
   /** the e-mail address */
 61  
   private String emailAddress;
 62  
 
 63  
   /** the website */
 64  
   private String website;
 65  
 
 66  
   /** the profile */
 67  
   private String profile;
 68  
 
 69  
   /** the user's preferences */
 70  84
   private Map<String,String> preferences = new HashMap<String,String>();
 71  
 
 72  
   private Collection<GrantedAuthority> grantedAuthories;
 73  
 
 74  84
   private boolean detailsUpdateable = true;
 75  
 
 76  
   private Collection<String> openIds;
 77  
 
 78  0
   public PebbleUserDetails() {
 79  0
   }
 80  
 
 81  0
   public PebbleUserDetails(String username, String name, String emailAddress, String website, String profile, String roles[], Map<String,String> preferences, boolean detailsUpdateable) {
 82  0
     this.username = username;
 83  0
     this.name = name;
 84  0
     this.emailAddress = emailAddress;
 85  0
     this.website = website;
 86  0
     this.profile = profile;
 87  0
     this.grantedAuthories = createGrantedAuthorities(roles);
 88  0
     this.preferences = preferences;
 89  0
     this.detailsUpdateable = detailsUpdateable;
 90  0
   }
 91  
 
 92  84
   public PebbleUserDetails(String username, String password, String name, String emailAddress, String website, String profile, String roles[], Map<String,String> preferences, boolean detailsUpdateable) {
 93  84
     this.username = username;
 94  84
     this.password = password;
 95  84
     this.name = name;
 96  84
     this.emailAddress = emailAddress;
 97  84
     this.website = website;
 98  84
     this.profile = profile;
 99  84
     this.grantedAuthories = createGrantedAuthorities(roles);
 100  84
     this.preferences = preferences;
 101  84
     this.detailsUpdateable = detailsUpdateable;
 102  84
   }
 103  
 
 104  
   public String getUsername() {
 105  164
     return this.username;
 106  
   }
 107  
 
 108  
   public String getPassword() {
 109  44
     return this.password;
 110  
   }
 111  
 
 112  
   public boolean isAccountNonExpired() {
 113  0
     return true;
 114  
   }
 115  
 
 116  
   public boolean isAccountNonLocked() {
 117  0
     return true;
 118  
   }
 119  
 
 120  
   public boolean isCredentialsNonExpired() {
 121  0
     return true;
 122  
   }
 123  
 
 124  
   public boolean isEnabled() {
 125  0
     return true;
 126  
   }
 127  
 
 128  
   /**
 129  
    * Gets the name.
 130  
    *
 131  
    * @return  a String
 132  
    */
 133  
   public String getName() {
 134  64
     return name;
 135  
   }
 136  
 
 137  
   /**
 138  
    * Gets the e-mail address.
 139  
    *
 140  
    * @return  a String
 141  
    */
 142  
   public String getEmailAddress() {
 143  40
     return emailAddress;
 144  
   }
 145  
 
 146  
   /**
 147  
    * Gets the website.
 148  
    *
 149  
    * @return  a String
 150  
    */
 151  
   public String getWebsite() {
 152  40
     return website;
 153  
   }
 154  
 
 155  
   /**
 156  
    * Gets the user's profile.
 157  
    *
 158  
    * @return  a String
 159  
    */
 160  
   public String getProfile() {
 161  32
     return this.profile;
 162  
   }
 163  
 
 164  
   public Collection<GrantedAuthority> getAuthorities() {
 165  44
     return this.grantedAuthories;
 166  
   }
 167  
 
 168  
   public String[] getRoles() {
 169  0
     Collection<GrantedAuthority> authorities = getAuthorities();
 170  0
     String[] roles = new String[authorities.size()];
 171  0
     int i = 0;
 172  0
     for (GrantedAuthority authority: authorities) {
 173  0
       roles[i++] = authority.getAuthority();
 174  
     }
 175  
 
 176  0
     return roles;
 177  
   }
 178  
 
 179  
   public String getRolesAsString() {
 180  28
     StringBuffer buf = new StringBuffer();
 181  
 
 182  28
     String sep = "";
 183  28
     for (GrantedAuthority authority: getAuthorities()) {
 184  116
       buf.append(sep);
 185  116
       sep = ",";
 186  116
       buf.append(authority.getAuthority());
 187  
     }
 188  
 
 189  28
     return buf.toString();
 190  
   }
 191  
 
 192  
   public boolean isUserInRole(String role) {
 193  0
     Collection<GrantedAuthority> authorities = getAuthorities();
 194  0
     if (authorities != null) {
 195  0
       for (GrantedAuthority authority : authorities) {
 196  0
         if (authority.getAuthority().equals(role)) {
 197  0
           return true;
 198  
         }
 199  
       }
 200  
     }
 201  0
     return false;
 202  
   }
 203  
 
 204  
   public boolean isBlogAdmin() {
 205  0
     return isUserInRole(Constants.BLOG_ADMIN_ROLE);
 206  
   }
 207  
 
 208  
   public boolean isBlogOwner() {
 209  0
     return isUserInRole(Constants.BLOG_OWNER_ROLE);
 210  
   }
 211  
 
 212  
   public boolean isBlogPublisher() {
 213  0
     return isUserInRole(Constants.BLOG_PUBLISHER_ROLE);
 214  
   }
 215  
 
 216  
   public boolean isBlogContributor() {
 217  0
     return isUserInRole(Constants.BLOG_CONTRIBUTOR_ROLE);
 218  
   }
 219  
 
 220  
   private static Collection<GrantedAuthority> createGrantedAuthorities(String roles[]) {
 221  84
     Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
 222  84
     if (roles != null) {
 223  300
       for (String role : roles) {
 224  220
         authorities.add(new GrantedAuthorityImpl(role.trim()));
 225  
       }
 226  
     }
 227  84
     authorities.add(new GrantedAuthorityImpl(Constants.BLOG_READER_ROLE));
 228  
 
 229  84
     return authorities;
 230  
   }
 231  
 
 232  
   public void setUsername(String username) {
 233  0
     this.username = username;
 234  0
   }
 235  
 
 236  
   public void setPassword(String password) {
 237  0
     this.password = password;
 238  0
   }
 239  
 
 240  
   public void setName(String name) {
 241  0
     this.name = name;
 242  0
   }
 243  
 
 244  
   public void setEmailAddress(String emailAddress) {
 245  0
     this.emailAddress = emailAddress;
 246  0
   }
 247  
 
 248  
   public void setWebsite(String website) {
 249  0
     this.website = website;
 250  0
   }
 251  
 
 252  
   public void setProfile(String profile) {
 253  0
     this.profile = profile;
 254  0
   }
 255  
 
 256  
   public void setGrantedAuthories(Collection<GrantedAuthority> grantedAuthories) {
 257  0
     this.grantedAuthories = grantedAuthories;
 258  0
   }
 259  
 
 260  
   public boolean isDetailsUpdateable() {
 261  28
     return detailsUpdateable;
 262  
   }
 263  
 
 264  
   public void setDetailsUpdateable(boolean detailsUpdateable) {
 265  0
     this.detailsUpdateable = detailsUpdateable;
 266  0
   }
 267  
 
 268  
   public Map<String,String> getPreferences() {
 269  28
     return new HashMap<String,String>(preferences);
 270  
   }
 271  
 
 272  
   public String getPreference(String key) {
 273  24
     return preferences.get(key);
 274  
   }
 275  
 
 276  
   public void setPreferences(Map<String,String> preferences) {
 277  0
     this.preferences = new HashMap<String,String>(preferences);
 278  0
   }
 279  
 
 280  
   public Collection<String> getOpenIds()
 281  
   {
 282  20
     String openIds = getPreference(OPEN_IDS_PREFERENCE);
 283  20
     if (openIds == null || openIds.trim().length() == 0) {
 284  20
       return Collections.emptyList();
 285  
     } else {
 286  
       // Use a regular expression that will automatically trim whitespace
 287  0
       return Arrays.asList(openIds.split("\\s*\\" + OPEN_IDS_SEPARATOR + "\\s*"));
 288  
     }
 289  
   }
 290  
 
 291  
   public void setOpenIds(Collection<String> openIds)
 292  
   {
 293  0
     if (openIds.isEmpty()) {
 294  0
       preferences.remove(OPEN_IDS_PREFERENCE);
 295  
     } else {
 296  0
       StringBuilder builder = new StringBuilder();
 297  0
       String sep = "";
 298  0
       for (String openId : openIds) {
 299  0
         builder.append(sep);
 300  0
         sep = OPEN_IDS_SEPARATOR;
 301  0
         builder.append(openId);
 302  
       }
 303  0
       preferences.put(OPEN_IDS_PREFERENCE, builder.toString());
 304  
     }
 305  0
   }
 306  
 }