1 43 package net.jforum.repository; 44 45 import net.jforum.JForumExecutionContext; 46 import net.jforum.SessionFacade; 47 import net.jforum.cache.CacheEngine; 48 import net.jforum.cache.Cacheable; 49 import net.jforum.dao.DataAccessDriver; 50 import net.jforum.dao.UserDAO; 51 import net.jforum.dao.security.GroupSecurityDAO; 52 import net.jforum.entities.User; 53 import net.jforum.entities.UserSession; 54 import net.jforum.exceptions.SecurityLoadException; 55 import net.jforum.security.PermissionControl; 56 57 import org.apache.log4j.Logger; 58 59 63 public class SecurityRepository implements Cacheable 64 { 65 private static final Logger logger = Logger.getLogger(SecurityRepository.class); 66 private static CacheEngine cache; 67 private static final String FQN = "security"; 68 69 72 public void setCacheEngine(CacheEngine engine) 73 { 74 cache = engine; 75 } 76 77 89 public static PermissionControl load(int userId, boolean force) throws Exception 90 { 91 if (force || cache.get(FQN, Integer.toString(userId)) == null) { 92 UserDAO um = DataAccessDriver.getInstance().newUserDAO(); 93 94 return SecurityRepository.load(um.selectById(userId), force); 95 } 96 97 return SecurityRepository.get(userId); 98 } 99 100 110 public static PermissionControl load(int userId) throws Exception 111 { 112 return SecurityRepository.load(userId, false); 113 } 114 115 125 public static PermissionControl load(User user) throws Exception 126 { 127 return SecurityRepository.load(user, false); 128 } 129 130 142 public static PermissionControl load(User user, boolean force) throws Exception 143 { 144 String userId = Integer.toString(user.getId()); 145 if (force || cache.get(FQN, userId) == null) { 146 PermissionControl pc = new PermissionControl(); 147 148 GroupSecurityDAO dao = DataAccessDriver.getInstance().newGroupSecurityDAO(); 150 pc.setRoles(dao.loadRolesByUserGroups(user)); 151 152 cache.add(FQN, userId, pc); 153 154 return pc; 155 } 156 157 return SecurityRepository.get(user.getId()); 158 } 159 160 171 public static boolean canAccess(String roleName) 172 { 173 return canAccess(roleName, null); 174 } 175 176 public static boolean canAccess(int userId, String roleName) 177 { 178 return canAccess(userId, roleName, null); 179 } 180 181 189 public static boolean canAccess(String roleName, String value) 190 { 191 UserSession us = SessionFacade.getUserSession(); 192 if (us == null) { 193 logger.warn("Found null userSession. Going anonymous. Session id #" 194 + JForumExecutionContext.getRequest().getSession().getId()); 195 us = new UserSession(); 196 us.makeAnonymous(); 197 } 198 199 return canAccess(us.getUserId(), roleName, value); 200 } 201 202 public static boolean canAccess(int userId, String roleName, String value) 203 { 204 PermissionControl pc = SecurityRepository.get(userId); 205 206 if (pc == null) { 207 throw new SecurityLoadException("Failed to load security roles for userId " + userId + " (null PermissionControl returned). " 208 + "roleName=" + roleName + ", roleValue=" + value); 209 } 210 211 return (value != null ? pc.canAccess(roleName, value) : pc.canAccess(roleName)); 212 } 213 214 225 public static PermissionControl get(int userId) 226 { 227 PermissionControl pc = (PermissionControl)cache.get(FQN, Integer.toString(userId)); 228 229 if (pc == null) { 230 try { 231 pc = load(userId); 232 } 233 catch (Exception e) { 234 throw new SecurityLoadException(e); 235 } 236 } 237 238 return pc; 239 } 240 241 247 public static synchronized void add(int userId, PermissionControl pc) 248 { 249 cache.add(FQN, Integer.toString(userId), pc); 250 } 251 252 257 public static synchronized void remove(int userId) 258 { 259 cache.remove(FQN, Integer.toString(userId)); 260 } 261 262 265 public static synchronized void clean() 266 { 267 cache.remove(FQN); 268 } 269 } 270 | Popular Tags |