File Source: pebbleuserdetails.java
/*
P/P * Method: net.sourceforge.pebble.security.PebbleUserDetails__static_init
*/
1 package net.sourceforge.pebble.security;
2
3 import net.sourceforge.pebble.Constants;
4 import org.acegisecurity.GrantedAuthority;
5 import org.acegisecurity.GrantedAuthorityImpl;
6 import org.acegisecurity.userdetails.UserDetails;
7
8 import java.util.HashSet;
9 import java.util.Set;
10 import java.util.HashMap;
11 import java.util.Map;
12
13 /**
14 * Extension of the Acegi User class that adds additional information
15 * such as the user's e-mail address.
16 *
17 * @author Simon Brown
18 */
19 public class PebbleUserDetails implements UserDetails {
20
21 private String username;
22 private String password;
23
24 /** the name */
25 private String name;
26
27 /** the e-mail address */
28 private String emailAddress;
29
30 /** the website */
31 private String website;
32
33 /** the profile */
34 private String profile;
35
36 /** the user's preferences */
37 private Map<String,String> preferences = new HashMap<String,String>();
38
39 private GrantedAuthority grantedAuthories[];
40
41 private boolean detailsUpdateable = true;
42
/*
P/P * Method: void net.sourceforge.pebble.security.PebbleUserDetails()
*
* Postconditions:
* this.detailsUpdateable == 1
* new HashMap(PebbleUserDetails#1) num objects == 1
* this.preferences == &new HashMap(PebbleUserDetails#1)
*/
43 public PebbleUserDetails() {
44 }
45
/*
P/P * Method: void net.sourceforge.pebble.security.PebbleUserDetails(String, String, String, String, String, String[], Map, bool)
*
* Preconditions:
* (soft) roles.length <= 232-1
* (soft) roles[...] != null
*
* Postconditions:
* this.detailsUpdateable == detailsUpdateable
* init'ed(this.detailsUpdateable)
* this.emailAddress == emailAddress
* init'ed(this.emailAddress)
* init'ed(this.grantedAuthories)
* this.name == name
* init'ed(this.name)
* this.preferences == preferences
* init'ed(this.preferences)
* this.profile == profile
* ...
*/
46 public PebbleUserDetails(String username, String name, String emailAddress, String website, String profile, String roles[], Map<String,String> preferences, boolean detailsUpdateable) {
47 this.username = username;
48 this.name = name;
49 this.emailAddress = emailAddress;
50 this.website = website;
51 this.profile = profile;
52 this.grantedAuthories = createGrantedAuthorities(roles);
53 this.preferences = preferences;
54 this.detailsUpdateable = detailsUpdateable;
55 }
56
/*
P/P * Method: void net.sourceforge.pebble.security.PebbleUserDetails(String, String, String, String, String, String, String[], Map, bool)
*
* Preconditions:
* (soft) roles.length <= 232-1
* (soft) roles[...] != null
*
* Postconditions:
* this.detailsUpdateable == detailsUpdateable
* init'ed(this.detailsUpdateable)
* this.emailAddress == emailAddress
* init'ed(this.emailAddress)
* init'ed(this.grantedAuthories)
* this.name == name
* init'ed(this.name)
* this.password == password
* init'ed(this.password)
* this.preferences == preferences
* ...
*/
57 public PebbleUserDetails(String username, String password, String name, String emailAddress, String website, String profile, String roles[], Map<String,String> preferences, boolean detailsUpdateable) {
58 this.username = username;
59 this.password = password;
60 this.name = name;
61 this.emailAddress = emailAddress;
62 this.website = website;
63 this.profile = profile;
64 this.grantedAuthories = createGrantedAuthorities(roles);
65 this.preferences = preferences;
66 this.detailsUpdateable = detailsUpdateable;
67 }
68
69 public String getUsername() {
/*
P/P * Method: String getUsername()
*
* Preconditions:
* init'ed(this.username)
*
* Postconditions:
* return_value == this.username
* init'ed(return_value)
*/
70 return this.username;
71 }
72
73 public String getPassword() {
/*
P/P * Method: String getPassword()
*
* Preconditions:
* init'ed(this.password)
*
* Postconditions:
* return_value == this.password
* init'ed(return_value)
*/
74 return this.password;
75 }
76
77 public boolean isAccountNonExpired() {
/*
P/P * Method: bool isAccountNonExpired()
*
* Postconditions:
* return_value == 1
*/
78 return true;
79 }
80
81 public boolean isAccountNonLocked() {
/*
P/P * Method: bool isAccountNonLocked()
*
* Postconditions:
* return_value == 1
*/
82 return true;
83 }
84
85 public boolean isCredentialsNonExpired() {
/*
P/P * Method: bool isCredentialsNonExpired()
*
* Postconditions:
* return_value == 1
*/
86 return true;
87 }
88
89 public boolean isEnabled() {
/*
P/P * Method: bool isEnabled()
*
* Postconditions:
* return_value == 1
*/
90 return true;
91 }
92
93 /**
94 * Gets the name.
95 *
96 * @return a String
97 */
98 public String getName() {
/*
P/P * Method: String getName()
*
* Preconditions:
* init'ed(this.name)
*
* Postconditions:
* return_value == this.name
* init'ed(return_value)
*/
99 return name;
100 }
101
102 /**
103 * Gets the e-mail address.
104 *
105 * @return a String
106 */
107 public String getEmailAddress() {
/*
P/P * Method: String getEmailAddress()
*
* Preconditions:
* init'ed(this.emailAddress)
*
* Postconditions:
* return_value == this.emailAddress
* init'ed(return_value)
*/
108 return emailAddress;
109 }
110
111 /**
112 * Gets the website.
113 *
114 * @return a String
115 */
116 public String getWebsite() {
/*
P/P * Method: String getWebsite()
*
* Preconditions:
* init'ed(this.website)
*
* Postconditions:
* return_value == this.website
* init'ed(return_value)
*/
117 return website;
118 }
119
120 /**
121 * Gets the user's profile.
122 *
123 * @return a String
124 */
125 public String getProfile() {
/*
P/P * Method: String getProfile()
*
* Preconditions:
* init'ed(this.profile)
*
* Postconditions:
* return_value == this.profile
* init'ed(return_value)
*/
126 return this.profile;
127 }
128
129 public GrantedAuthority[] getAuthorities() {
/*
P/P * Method: GrantedAuthority[] getAuthorities()
*
* Preconditions:
* init'ed(this.grantedAuthories)
*
* Postconditions:
* return_value == this.grantedAuthories
* init'ed(return_value)
*/
130 return this.grantedAuthories;
131 }
132
133 public String[] getRoles() {
/*
P/P * Method: String[] getRoles()
*
* Preconditions:
* this.grantedAuthories != null
* this.grantedAuthories.length <= 232-1
* (soft) this.grantedAuthories[...] != null
*
* Postconditions:
* return_value == &new String[](getRoles#1)
* new String[](getRoles#1) num objects == 1
* return_value.length == this.grantedAuthories.length
* return_value.length <= 232-1
* possibly_updated(return_value[...])
*/
134 GrantedAuthority[] authorities = getAuthorities();
135 String[] roles = new String[authorities.length];
136 for (int i = 0; i < authorities.length; i++) {
137 roles[i] = authorities[i].getAuthority();
138 }
139
140 return roles;
141 }
142
143 public String getRolesAsString() {
/*
P/P * Method: String getRolesAsString()
*
* Preconditions:
* this.grantedAuthories != null
* this.grantedAuthories.length <= 232-1
* (soft) this.grantedAuthories[...] != null
*
* Postconditions:
* return_value != null
*/
144 StringBuffer buf = new StringBuffer();
145
146 GrantedAuthority[] authorities = getAuthorities();
147 for (int i = 0; i < authorities.length; i++) {
148 buf.append(authorities[i].getAuthority());
149 if (i < authorities.length) {
150 buf.append(",");
151 }
152 }
153
154 return buf.toString();
155 }
156
157 public boolean isUserInRole(String role) {
/*
P/P * Method: bool isUserInRole(String)
*
* Preconditions:
* init'ed(this.grantedAuthories)
* (soft) this.grantedAuthories.length <= 232-1
* (soft) this.grantedAuthories[...] != null
*
* Presumptions:
* org.acegisecurity.GrantedAuthority:getAuthority(...)@161 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.grantedAuthories: Addr_Set{null}, Inverse{null}
* java.lang.String:equals(...)@161: {0}, {1}
*/
158 GrantedAuthority[] authorities = getAuthorities();
159 if (authorities != null) {
160 for (GrantedAuthority authority : authorities) {
161 if (authority.getAuthority().equals(role)) {
162 return true;
163 }
164 }
165 }
166 return false;
167 }
168
169 public boolean isBlogAdmin() {
/*
P/P * Method: bool isBlogAdmin()
*
* Presumptions:
* init'ed(net.sourceforge.pebble.Constants.BLOG_ADMIN_ROLE)
*
* Preconditions:
* init'ed(this.grantedAuthories)
* (soft) this.grantedAuthories.length <= 232-1
* (soft) this.grantedAuthories[...] != null
*
* Postconditions:
* init'ed(return_value)
*/
170 return isUserInRole(Constants.BLOG_ADMIN_ROLE);
171 }
172
173 public boolean isBlogOwner() {
/*
P/P * Method: bool isBlogOwner()
*
* Presumptions:
* init'ed(net.sourceforge.pebble.Constants.BLOG_OWNER_ROLE)
*
* Preconditions:
* init'ed(this.grantedAuthories)
* (soft) this.grantedAuthories.length <= 232-1
* (soft) this.grantedAuthories[...] != null
*
* Postconditions:
* init'ed(return_value)
*/
174 return isUserInRole(Constants.BLOG_OWNER_ROLE);
175 }
176
177 public boolean isBlogPublisher() {
/*
P/P * Method: bool isBlogPublisher()
*
* Presumptions:
* init'ed(net.sourceforge.pebble.Constants.BLOG_PUBLISHER_ROLE)
*
* Preconditions:
* init'ed(this.grantedAuthories)
* (soft) this.grantedAuthories.length <= 232-1
* (soft) this.grantedAuthories[...] != null
*
* Postconditions:
* init'ed(return_value)
*/
178 return isUserInRole(Constants.BLOG_PUBLISHER_ROLE);
179 }
180
181 public boolean isBlogContributor() {
/*
P/P * Method: bool isBlogContributor()
*
* Presumptions:
* init'ed(net.sourceforge.pebble.Constants.BLOG_CONTRIBUTOR_ROLE)
*
* Preconditions:
* init'ed(this.grantedAuthories)
* (soft) this.grantedAuthories.length <= 232-1
* (soft) this.grantedAuthories[...] != null
*
* Postconditions:
* init'ed(return_value)
*/
182 return isUserInRole(Constants.BLOG_CONTRIBUTOR_ROLE);
183 }
184
185 private static GrantedAuthority[] createGrantedAuthorities(String roles[]) {
/*
P/P * Method: GrantedAuthority[] createGrantedAuthorities(String[])
*
* Presumptions:
* init'ed(net.sourceforge.pebble.Constants.BLOG_READER_ROLE)
*
* Preconditions:
* (soft) roles.length <= 232-1
* (soft) roles[...] != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* roles: Addr_Set{null}, Inverse{null}
*/
186 Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
187 if (roles != null) {
188 for (String role : roles) {
189 authorities.add(new GrantedAuthorityImpl(role.trim()));
190 }
191 }
192 authorities.add(new GrantedAuthorityImpl(Constants.BLOG_READER_ROLE));
193
194 return authorities.toArray(new GrantedAuthority[]{});
195 }
196
197 public void setUsername(String username) {
/*
P/P * Method: void setUsername(String)
*
* Postconditions:
* this.username == username
* init'ed(this.username)
*/
198 this.username = username;
199 }
200
201 public void setPassword(String password) {
/*
P/P * Method: void setPassword(String)
*
* Postconditions:
* this.password == password
* init'ed(this.password)
*/
202 this.password = password;
203 }
204
205 public void setName(String name) {
/*
P/P * Method: void setName(String)
*
* Postconditions:
* this.name == name
* init'ed(this.name)
*/
206 this.name = name;
207 }
208
209 public void setEmailAddress(String emailAddress) {
/*
P/P * Method: void setEmailAddress(String)
*
* Postconditions:
* this.emailAddress == emailAddress
* init'ed(this.emailAddress)
*/
210 this.emailAddress = emailAddress;
211 }
212
213 public void setWebsite(String website) {
/*
P/P * Method: void setWebsite(String)
*
* Postconditions:
* this.website == website
* init'ed(this.website)
*/
214 this.website = website;
215 }
216
217 public void setProfile(String profile) {
/*
P/P * Method: void setProfile(String)
*
* Postconditions:
* this.profile == profile
* init'ed(this.profile)
*/
218 this.profile = profile;
219 }
220
221 public void setGrantedAuthories(GrantedAuthority[] grantedAuthories) {
/*
P/P * Method: void setGrantedAuthories(GrantedAuthority[])
*
* Postconditions:
* this.grantedAuthories == grantedAuthories
* init'ed(this.grantedAuthories)
*/
222 this.grantedAuthories = grantedAuthories;
223 }
224
225 public boolean isDetailsUpdateable() {
/*
P/P * Method: bool isDetailsUpdateable()
*
* Preconditions:
* init'ed(this.detailsUpdateable)
*
* Postconditions:
* return_value == this.detailsUpdateable
* init'ed(return_value)
*/
226 return detailsUpdateable;
227 }
228
229 public void setDetailsUpdateable(boolean detailsUpdateable) {
/*
P/P * Method: void setDetailsUpdateable(bool)
*
* Postconditions:
* this.detailsUpdateable == detailsUpdateable
* init'ed(this.detailsUpdateable)
*/
230 this.detailsUpdateable = detailsUpdateable;
231 }
232
233 public Map<String,String> getPreferences() {
/*
P/P * Method: Map getPreferences()
*
* Preconditions:
* init'ed(this.preferences)
*
* Postconditions:
* return_value == &new HashMap(getPreferences#1)
* new HashMap(getPreferences#1) num objects == 1
*/
234 return new HashMap<String,String>(preferences);
235 }
236
237 public String getPreference(String key) {
/*
P/P * Method: String getPreference(String)
*
* Preconditions:
* this.preferences != null
*
* Postconditions:
* init'ed(return_value)
*/
238 return preferences.get(key);
239 }
240
241 public void setPreferences(Map<String,String> preferences) {
/*
P/P * Method: void setPreferences(Map)
*
* Postconditions:
* this.preferences == &new HashMap(setPreferences#1)
* new HashMap(setPreferences#1) num objects == 1
*/
242 this.preferences = new HashMap<String,String>(preferences);
243 }
244
245 }
SofCheck Inspector Build Version : 2.22510
| pebbleuserdetails.java |
2010-Jun-25 19:40:32 |
| pebbleuserdetails.class |
2010-Jul-19 20:23:38 |