1 19 20 package com.sslexplorer.security.actions; 21 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.struts.Globals; 28 import org.apache.struts.action.ActionForm; 29 import org.apache.struts.action.ActionForward; 30 import org.apache.struts.action.ActionMapping; 31 import org.apache.struts.action.ActionMessage; 32 import org.apache.struts.action.ActionMessages; 33 34 import com.sslexplorer.boot.Util; 35 import com.sslexplorer.core.CoreAttributeConstants; 36 import com.sslexplorer.core.CoreEvent; 37 import com.sslexplorer.core.CoreEventConstants; 38 import com.sslexplorer.core.CoreServlet; 39 import com.sslexplorer.core.CoreUtil; 40 import com.sslexplorer.core.UserDatabaseManager; 41 import com.sslexplorer.policyframework.Permission; 42 import com.sslexplorer.policyframework.PolicyConstants; 43 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 44 import com.sslexplorer.policyframework.PolicyUtil; 45 import com.sslexplorer.security.Constants; 46 import com.sslexplorer.security.LogonControllerFactory; 47 import com.sslexplorer.security.Role; 48 import com.sslexplorer.security.SessionInfo; 49 import com.sslexplorer.security.UserDatabase; 50 import com.sslexplorer.security.forms.ShowAvailableRolesForm; 51 import com.sslexplorer.table.actions.AbstractPagerAction; 52 53 66 public class ShowAvailableRolesDispatchAction extends AbstractPagerAction { 67 final static Log log = LogFactory.getLog(ShowAvailableRolesDispatchAction.class); 68 69 72 public ShowAvailableRolesDispatchAction() { 73 super(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, new Permission[] { 74 PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, 75 PolicyConstants.PERM_DELETE 76 }); 77 } 78 79 82 public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, 83 HttpServletResponse response) throws Exception { 84 return list(mapping, form, request, response); 85 } 86 87 97 public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 98 throws Exception { 99 CoreUtil.clearFlow(request); 100 101 try { 102 UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm()); 103 Role[] roles = null; 104 try { 105 roles = udb.listAllRoles("*"); 106 } catch (Exception e) { 107 log.error("Failed to get available roles.", e); 108 ActionMessages errs = new ActionMessages(); 109 errs.add(Globals.ERROR_KEY, new ActionMessage("availableRoles.cannotListRoles", Util.getExceptionMessageChain(e))); 110 saveErrors(request, errs); 111 roles = new Role[0]; 112 } 113 ((ShowAvailableRolesForm) form).initialize(roles, request.getSession()); 114 ActionMessages msgs = new ActionMessages(); 115 if (!udb.supportsAccountCreation()) { 116 msgs.add(Globals.MESSAGE_KEY, new ActionMessage("availableRoles.noRoleCreation.text")); 117 } 118 if (msgs.size() > 0) { 119 saveMessages(request, msgs); 120 } 121 } catch (Exception ex) { 122 log.error("Failed to reset administration form", ex); 123 } 124 return mapping.findForward("success"); 125 } 126 127 128 138 public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 139 throws Exception { 140 return mapping.findForward("create"); 141 } 142 143 153 public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 154 throws Exception { 155 UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm()); 156 Role r = udb.getRole( 157 ((ShowAvailableRolesForm) form).getSelectedItem()); 158 request.setAttribute(Constants.EDITING_ITEM, r); 159 return mapping.findForward("edit"); 160 } 161 162 172 public ActionForward confirmRoleDeletion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 173 throws Exception { 174 PolicyUtil.checkPermission(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, PolicyConstants.PERM_DELETE, request); 175 String rolename = request.getParameter("rolename"); 176 if (rolename == null) { 177 ActionMessages mesgs = new ActionMessages(); 178 mesgs.add(Globals.ERROR_KEY, new ActionMessage("availableRoles.singleRoleNotSelected")); 179 saveErrors(request, mesgs); 180 return list(mapping, form, request, response); 181 } else { 182 return mapping.findForward("confirmRoleDeletion"); 183 } 184 } 185 186 196 public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 197 throws Exception { 198 PolicyUtil.checkPermission(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, PolicyConstants.PERM_DELETE, request); 199 UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm()); 200 String rolename = request.getParameter("rolename"); 201 Role role = udb.getRole(rolename); 202 SessionInfo info = this.getSessionInfo(request); 203 try { 204 PolicyDatabaseFactory.getInstance().revokeAllPoliciesFromPrincipal(role); 206 207 udb.deleteRole(rolename); 208 CoreServlet.getServlet().fireCoreEvent( 209 new CoreEvent(this, CoreEventConstants.GROUP_REMOVED, role, info) 210 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, rolename)); 211 return mapping.findForward("refresh"); 212 } catch (Exception e) { 213 CoreServlet.getServlet().fireCoreEvent( 214 new CoreEvent(this, CoreEventConstants.GROUP_REMOVED, role, info, CoreEvent.STATE_UNSUCCESSFUL) 215 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, rolename)); 216 throw e; 217 } 218 finally{ 219 LogonControllerFactory.getInstance().applyMenuItemChanges(request); 221 } 222 } 223 224 227 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 228 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT; 229 } 230 } | Popular Tags |