1 22 23 package org.meshcms.core; 24 25 import java.io.*; 26 import java.util.*; 27 import org.meshcms.util.*; 28 29 33 public class UserInfo implements Serializable { 34 37 public static final int CAN_ADD_USERS = 1; 38 39 42 public static final int CAN_EDIT_PAGES = 2; 43 44 47 public static final int CAN_MANAGE_FILES = 4; 48 49 52 public static final int CAN_VIEW_OTHER_USERINFO = 8; 53 54 57 public static final int CAN_DO_ADMINTASKS = 16; 58 59 62 public static final int CAN_BROWSE_FILES = 32; 63 64 67 public static final int GUEST = 0; 68 69 72 public static final int MEMBER = CAN_BROWSE_FILES | 73 CAN_VIEW_OTHER_USERINFO; 74 75 78 public static final int EDITOR = CAN_EDIT_PAGES | 79 CAN_MANAGE_FILES | 80 CAN_VIEW_OTHER_USERINFO | 81 CAN_BROWSE_FILES; 82 85 public static final int ADMIN = 0x00FFFFFF; 86 87 protected static final String USERNAME = "P_USN"; 88 protected static final String PASSWORD = "P_PWS"; 89 protected static final String HOME_PATH = "P_HPT"; 90 protected static final String PERMISSIONS = "P_PRM"; 91 protected static final String E_MAIL = "P_EML"; 92 protected static final String LANGUAGE = "P_LNG"; 93 94 97 public static final String [] DETAILS = { 98 "salutation", 99 "name", 100 "surname", 101 "company", 102 "address", 103 "zip", 104 "city", 105 "state", 106 "country", 107 "phone_number", 108 "fax_number", 109 "mobile_phone_number" 110 }; 111 112 115 protected static final String VALID_USERNAME_CHARS = 116 "abcdefghijklmnopqrstuvwxyz._0123456789"; 117 protected static final String SALT = "LV"; 118 119 protected Properties info; 120 protected boolean global; 121 122 125 public UserInfo() { 126 loadGuest(); 127 } 128 129 132 public void setUsername(String username) { 133 if (username != null) { 134 info.setProperty(USERNAME, username); 135 } 136 } 137 138 141 public String getUsername() { 142 return getValue(USERNAME); 143 } 144 145 148 public void setPassword(String password) { 149 info.setProperty(PASSWORD, cryptPassword(password)); 150 } 151 152 158 public boolean updatePassword(String oldPassword, String newPassword) { 159 if (verifyPassword(oldPassword)) { 160 setPassword(newPassword); 161 return true; 162 } 163 164 return false; 165 } 166 167 170 public String getPassword() { 171 return getValue(PASSWORD); 172 } 173 174 181 public boolean setEmail(String email) { 182 if (Utils.checkAddress(email)) { 183 info.setProperty(E_MAIL, email); 184 return true; 185 } 186 187 return false; 188 } 189 190 193 public String getEmail() { 194 return getValue(E_MAIL); 195 } 196 197 201 public void setHomePath(Path homePath) { 202 if (homePath != null) { 203 info.setProperty(HOME_PATH, homePath.toString()); 204 } 205 } 206 207 210 public Path getHomePath() { 211 return new Path(getValue(HOME_PATH)); 212 } 213 214 218 public void setPermissions(int permissions) { 219 info.setProperty(PERMISSIONS, Integer.toHexString(permissions)); 220 } 221 222 225 public int getPermissions() { 226 try { 227 return Integer.parseInt(getValue(PERMISSIONS), 16); 228 } catch (Exception ex) {} 229 230 return GUEST; 231 } 232 233 237 public String getPreferredLocaleCode() { 238 return getValue(LANGUAGE); 239 } 240 241 244 public void setPreferredLocaleCode(String localeCode) { 245 if (localeCode == null || localeCode.length() < 2) { 246 localeCode = "en_US"; 247 } 248 249 info.setProperty(LANGUAGE, localeCode); 250 } 251 252 255 public void loadGuest() { 256 info = new Properties(); 257 global = false; 258 } 259 260 263 public boolean load(WebSite webSite, String username, String password) { 264 if (Utils.isNullOrEmpty(username)) { 265 return false; 266 } 267 268 boolean global = false; 269 Path userPath = getUserPath(webSite, username); 270 271 if (!webSite.getFile(userPath).exists() && 272 webSite instanceof VirtualWebSite) { 273 webSite = ((VirtualWebSite) webSite).getMainWebSite(); 274 userPath = getUserPath(webSite, username); 275 global = true; 276 } 277 278 if (webSite.getFile(userPath).exists()) { 279 Properties p = (Properties) webSite.loadFromXML(userPath); 280 281 if (p != null && p.getProperty(PASSWORD).equals(cryptPassword(password))) { 282 Properties bak = info; 283 info = p; 284 285 if (global) { 286 if (canDo(CAN_DO_ADMINTASKS) && getHomePath().isRoot()) { 287 this.global = true; 288 } else { 289 info = bak; 290 return false; 291 } 292 } 293 294 return true; 295 } 296 } else if (username.equals("admin") && password.equals("admin")) { 297 info = new Properties(); 298 info.setProperty(USERNAME, "admin"); 299 info.setProperty(PASSWORD, cryptPassword("admin")); 300 info.setProperty(HOME_PATH, ""); 301 info.setProperty(PERMISSIONS, Integer.toHexString(ADMIN)); 302 info.setProperty(LANGUAGE, "en_US"); 303 store(webSite); 304 return true; 305 } 306 307 return false; 308 } 309 310 313 public boolean store(WebSite webSite) { 314 return webSite.storeToXML(info, getUserPath(webSite, getUsername())); 315 } 316 317 320 private String cryptPassword(String password) { 321 if (Utils.isNullOrEmpty(password)) { 322 return ""; 323 } 324 325 328 329 return com.kingwoodcable.locutus.jfd.JCrypt.crypt(SALT, password); 330 } 331 332 336 public static boolean verifyUsername(String username) { 337 if (Utils.isNullOrEmpty(username)) { 338 return false; 339 } 340 341 for (int i = 0; i < username.length(); i++) { 342 if (VALID_USERNAME_CHARS.indexOf(username.charAt(i)) == -1) { 343 return false; 344 } 345 } 346 347 return true; 348 } 349 350 353 public boolean verifyPassword(String password) { 354 return getPassword().equals(cryptPassword(password)); 355 } 356 357 private File getUserFile(WebSite webSite, String username) { 358 return webSite.getFile(getUserPath(webSite, username)); 359 } 360 361 private Path getUserPath(WebSite webSite, String username) { 362 return webSite.getUsersPath().add(username + ".xml"); 363 } 364 365 369 public boolean canDo(int what) { 370 return (getPermissions() & what) != 0; 371 } 372 373 377 public boolean canWrite(WebSite webSite, Path filePath) { 378 if (filePath == null || !canDo(CAN_EDIT_PAGES) || 379 filePath.isContainedIn(webSite.getAdminPath())) { 380 return false; 381 } 382 383 return filePath.isContainedIn(getHomePath()); 384 } 385 386 393 public boolean setDetail(String name, String value) { 394 if (Utils.searchString(DETAILS, name, false) != -1) { 395 info.setProperty(name, value); 396 return true; 397 } 398 399 return false; 400 } 401 402 408 public String getValue(String name) { 409 return Utils.noNull(info.getProperty(name)); 410 } 411 412 415 public String getDetailValue(String name) { 416 if (name != null) { 417 name = name.toLowerCase(); 418 419 if (Utils.searchString(DETAILS, name, false) != -1) { 420 return getValue(name); 421 } 422 } 423 424 return null; 425 } 426 427 430 public String getDetailName(int index) { 431 return DETAILS[index]; 432 } 433 434 438 public String getDisplayName() { 439 String name = getValue(DETAILS[1]); 440 String surname = getValue(DETAILS[2]); 441 442 if (name.equals("") && surname.equals("")) { 443 return isGuest() ? "guest" : getUsername(); 444 } 445 446 if (name.equals("")) { 447 return surname; 448 } 449 450 if (surname.equals("")) { 451 return name; 452 } 453 454 return name + " " + surname; 455 } 456 457 460 public boolean isGuest() { 461 return getPermissions() == GUEST; 462 } 463 464 468 public boolean exists(WebSite webSite, String username) { 469 return getUserFile(webSite, username).exists(); 470 } 471 472 public boolean isGlobal() { 473 return global; 474 } 475 } 476 | Popular Tags |