1 19 package gcc.security; 20 21 import gcc.properties.*; 22 import java.util.*; 23 24 public class User 25 implements java.security.Principal 26 { 27 public static User getInstance(String username) 28 { 29 User user = (User)_userMap.get(username); 30 if (user == null) 31 { 32 synchronized (_userMap) 33 { 34 user = (User)_userMap.get(username); 35 if (user == null) 36 { 37 user = new User(); 38 user.init(username); 39 _userMap.put(username, user); 40 } 41 } 42 } 43 return user; 44 } 45 46 48 50 public static final String GUEST = "guest"; 51 public static final String NOBODY = "[nobody]"; 52 public static final String USER_INFO = "gcc.security.UserInfo"; 53 54 56 private static ThreadLocal _current = new ThreadLocal(); 57 private static HashMap _userMap = new HashMap(); 58 private String _username; 59 private String _lastValidPassword; 60 61 63 protected void init(String username) 64 { 65 _username = username; 66 } 67 68 70 public int hashCode() 71 { 72 return _username.hashCode(); 73 } 74 75 public boolean equals(Object thatObject) 76 { 77 if (thatObject == this) 78 { 79 return true; 80 } 81 if (thatObject == null || ! (thatObject instanceof User)) 82 { 83 return false; 84 } 85 User that = (User)thatObject; 86 return true; 89 } 90 91 public static User getCurrent() 92 { 93 return (User)_current.get(); 94 } 95 96 public static User getCurrentNotNull() 97 { 98 User user = (User)_current.get(); 99 if (user == null) 100 { 101 throw new SecurityException("Error: No Current User"); 102 } 103 return user; 104 } 105 106 public static User getUnauthenticated() 107 { 108 return User.getInstance("unauthenticated"); 109 } 110 111 public static void setCurrent(User user) 112 { 113 _current.set(user); 114 } 115 116 public String getName() 117 { 118 return _username; 119 } 120 121 public String getPassword() 122 { 123 return _lastValidPassword == null ? "" : _lastValidPassword; 124 } 125 126 public String toString() 127 { 128 return User.class.getName() + ":" + _username + "@domain"; 129 } 130 131 134 public void login(String password) 135 { 136 System.out.println( "User.login(): username = " + _username + ", password = " + password ); 137 boolean ok = true; 138 if (ok) 139 { 140 SimpleSubject.setCurrent(new SimpleSubject(_username, password)); 141 } 142 else 143 { 144 SimpleSubject.setCurrent(null); 145 } 146 if (! ok) 147 { 148 throw new SecurityException("Warn: Login Failed. Username: " + _username ); 149 } 150 } 151 152 public boolean hasRole(Role role) 153 { 154 return true; 155 } 156 157 public boolean hasRole(String rolename) 158 { 159 boolean hasRole = true;; 160 return hasRole; 161 } 162 163 165 protected synchronized boolean hasLocalRole(String rolename) 166 { 167 return true; 168 } 169 } 170 | Popular Tags |