1 23 24 package com.rift.coad.lib.security.sudo; 26 27 import org.apache.log4j.Logger; 29 30 import com.rift.coad.lib.configuration.Configuration; 32 import com.rift.coad.lib.configuration.ConfigurationFactory; 33 import com.rift.coad.lib.security.AuthorizationException; 34 import com.rift.coad.lib.security.UserSession; 35 import com.rift.coad.lib.security.ThreadPermissionSession; 36 import com.rift.coad.lib.security.ThreadsPermissionContainer; 37 import com.rift.coad.lib.security.ThreadsPermissionContainerAccessor; 38 import com.rift.coad.lib.security.Validator; 39 import com.rift.coad.lib.security.user.UserSessionManager; 40 import com.rift.coad.lib.security.user.UserSessionManagerAccessor; 41 import com.rift.coad.lib.security.user.UserStoreManager; 42 import com.rift.coad.lib.security.user.UserStoreManagerAccessor; 43 import com.rift.coad.lib.thread.BasicThread; 44 45 46 51 public class Sudo { 52 53 private final static String ROLE = "role"; 55 56 private static Logger log = 58 Logger.getLogger(Sudo.class.getName()); 59 private static String role = null; 60 61 static { 63 try { 64 Configuration configuration = 65 ConfigurationFactory.getInstance().getConfig(Sudo.class); 66 role = configuration.getString(ROLE); 67 } catch (Exception ex) { 68 log.error("Failed to retrieve the sudo role : " + ex.getMessage(),ex); 69 } 70 } 71 72 73 76 private Sudo() { 77 } 78 79 80 89 public static void sudoThreadByUser (String username, 90 SudoCallbackHandler handler) throws SudoException, Exception { 91 Validator.validate(Sudo.class,role); 92 ThreadsPermissionContainer threadsPermissionContainer = 93 ThreadsPermissionContainerAccessor.getInstance(). 94 getThreadsPermissionContainer(); 95 UserStoreManager userStoreManager = 96 UserStoreManagerAccessor.getInstance(). 97 getUserStoreManager(); 98 99 Thread currentThread = null; 101 ThreadPermissionSession currentPermissions = null; 102 UserSession newUserSession = null; 103 try { 104 currentThread = Thread.currentThread(); 106 currentPermissions = threadsPermissionContainer.getSession( 107 new Long (currentThread.getId())); 108 newUserSession = userStoreManager.getUserInfo(username); 109 } catch (Exception ex) { 110 throw new SudoException( 111 "Failed to retrieve the necessary user information : " + 112 ex.getMessage(),ex); 113 } 114 115 threadsPermissionContainer.putSession(new Long (currentThread.getId()), 117 new ThreadPermissionSession( 118 new Long (currentThread.getId()),newUserSession)); 119 log.info("Set [" + currentThread.getId() + "] user from [" + 120 currentPermissions.getUser().getName() + "] to [" + 121 newUserSession.getName() + "] to run the command on : " + 122 handler.getClass().getName()); 123 124 try { 125 handler.process(); 126 } finally { 127 threadsPermissionContainer.putSession(new Long (currentThread.getId()), 129 currentPermissions); 130 log.info("Set user back from [" + newUserSession.getName() + 132 "] to [" + currentPermissions.getUser().getName() + 133 "] after running command on : " + handler.getClass().getName()); 134 } 135 } 136 137 138 146 public static void sudoThreadBySessionId(String sessionId, 147 SudoCallbackHandler handler) throws SudoException, Exception { 148 Validator.validate(Sudo.class,role); 149 ThreadsPermissionContainer threadsPermissionContainer = 150 ThreadsPermissionContainerAccessor.getInstance(). 151 getThreadsPermissionContainer(); 152 UserSessionManager userSessionManager = 153 UserSessionManagerAccessor.getInstance(). 154 getUserSessionManager(); 155 Thread currentThread = null; 157 ThreadPermissionSession currentPermissions = null; 158 UserSession newUserSession = null; 159 try { 160 currentThread = Thread.currentThread(); 162 currentPermissions = threadsPermissionContainer.getSession( 163 new Long (currentThread.getId())); 164 newUserSession = userSessionManager.getSessionById(sessionId); 165 } catch (Exception ex) { 166 throw new SudoException( 167 "Failed to retrieve the necessary user information : " + 168 ex.getMessage(),ex); 169 } 170 171 threadsPermissionContainer.putSession(new Long (currentThread.getId()), 173 new ThreadPermissionSession( 174 new Long (currentThread.getId()),newUserSession)); 175 176 log.info("Set [" + currentThread.getId() + "] user from [" 177 + currentPermissions.getUser().getName() + "] to [" + 178 newUserSession.getName() + "] to run the command on : " + 179 handler.getClass().getName()); 180 181 try { 182 handler.process(); 183 } finally { 184 threadsPermissionContainer.putSession(new Long (currentThread.getId()), 186 currentPermissions); 187 188 log.info("Set user back from [" + newUserSession.getName() + 190 "] to [" + currentPermissions.getUser().getName() + 191 "] after running command on : " + handler.getClass().getName()); 192 } 193 } 194 } 195 | Popular Tags |