| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 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 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public class PebbleUserDetails implements UserDetails { |
| 49 | |
|
| 50 | |
public static final String OPEN_IDS_PREFERENCE = "openids"; |
| 51 | |
|
| 52 | |
private static final String OPEN_IDS_SEPARATOR = "|"; |
| 53 | |
|
| 54 | |
private String username; |
| 55 | |
private String password; |
| 56 | |
|
| 57 | |
|
| 58 | |
private String name; |
| 59 | |
|
| 60 | |
|
| 61 | |
private String emailAddress; |
| 62 | |
|
| 63 | |
|
| 64 | |
private String website; |
| 65 | |
|
| 66 | |
|
| 67 | |
private String profile; |
| 68 | |
|
| 69 | |
|
| 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 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
public String getName() { |
| 134 | 64 | return name; |
| 135 | |
} |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public String getEmailAddress() { |
| 143 | 40 | return emailAddress; |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
public String getWebsite() { |
| 152 | 40 | return website; |
| 153 | |
} |
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 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 | |
|
| 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 | |
} |