1 19 20 package com.sslexplorer.properties.actions; 21 22 import java.util.List ; 23 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 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.core.CoreUtil; 35 import com.sslexplorer.policyframework.NoPermissionException; 36 import com.sslexplorer.policyframework.OwnedResource; 37 import com.sslexplorer.policyframework.Permission; 38 import com.sslexplorer.policyframework.PolicyConstants; 39 import com.sslexplorer.policyframework.PolicyUtil; 40 import com.sslexplorer.policyframework.Resource; 41 import com.sslexplorer.policyframework.ResourceUtil; 42 import com.sslexplorer.policyframework.actions.AbstractResourcesDispatchAction; 43 import com.sslexplorer.policyframework.forms.AbstractResourcesForm; 44 import com.sslexplorer.properties.ProfilesFactory; 45 import com.sslexplorer.properties.PropertyProfile; 46 import com.sslexplorer.properties.forms.ProfilesForm; 47 import com.sslexplorer.security.Constants; 48 import com.sslexplorer.security.SessionInfo; 49 50 57 58 public class ShowProfilesDispatchAction extends AbstractResourcesDispatchAction { 59 60 63 public ShowProfilesDispatchAction() { 64 super(PolicyConstants.PROFILE_RESOURCE_TYPE, PolicyConstants.PROFILE_RESOURCE_TYPE); 65 } 66 67 75 public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, 76 HttpServletResponse response) throws Exception { 77 ActionForward fwd = super.unspecified(mapping, form, request, response); 78 ProfilesForm profilesForm = (ProfilesForm) form; 79 SessionInfo session = this.getSessionInfo(request); 80 List globalProfiles = null; 81 List personalProfiles = null; 82 if (session.getNavigationContext() == SessionInfo.MANAGEMENT_CONSOLE_CONTEXT) { 83 profilesForm.setProfileScope(Constants.SCOPE_GLOBAL); 84 globalProfiles = ProfilesFactory.getInstance().getPropertyProfiles(null, true, 85 session.getUser().getRealm().getResourceId()); 86 } else { 87 profilesForm.setProfileScope(Constants.SCOPE_PERSONAL); 88 globalProfiles = ResourceUtil.filterOwned(ResourceUtil.getGrantedResource( 89 getSessionInfo(request), getResourceType())); 90 personalProfiles = ResourceUtil.filterResources(session.getUser(), ProfilesFactory.getInstance().getPropertyProfiles( 91 session.getUser().getPrincipalName(), true, session.getUser().getRealm().getResourceId()), false); 92 } 93 PropertyProfile profile = (PropertyProfile) request.getSession().getAttribute(Constants.SELECTED_PROFILE); 94 if (profile != null) { 95 profilesForm.initialize(globalProfiles, personalProfiles, request.getSession(), profile.getResourceId()); 96 } else { 97 profilesForm.initialize(globalProfiles, personalProfiles, request.getSession(), -1); 98 } 99 profilesForm.checkSelectedView(request, response); 100 return fwd; 101 } 102 103 111 public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 112 throws Exception { 113 if (getSessionInfo(request).getNavigationContext() == SessionInfo.USER_CONSOLE_CONTEXT) { 114 PolicyUtil.checkPermission(PolicyConstants.PERSONAL_PROFILE_RESOURCE_TYPE, PolicyConstants.PERM_MAINTAIN, request); 115 } else { 116 return super.create(mapping, form, request, response); 117 } 118 return mapping.findForward("create"); 119 } 120 121 130 public ActionForward confirmRemove(ActionMapping mapping, ActionForm form, HttpServletRequest request, 131 HttpServletResponse response) throws Exception { 132 ProfilesForm f = (ProfilesForm) form; 133 PropertyProfile profile = (PropertyProfile) getResourceById(f.getSelectedResource()); 134 if (profile == null) { 135 throw new Exception ("Selected profile '" + f.getSelectedResource() + "' doesn't exist."); 136 } 137 if(profile.getOwnerUsername() != null) { 138 SessionInfo session = getSessionInfo(request); 139 if(!session.getUser().getPrincipalName().equals(profile.getOwnerUsername())) { 140 throw new Exception ("Cannot delete profiles owned by others."); 141 } 142 return mapping.findForward("confirmRemove"); 143 } 144 else { 145 return super.confirmRemove(mapping, form, request, response); 146 } 147 } 148 149 150 158 public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 159 throws Exception { 160 ProfilesForm f = (ProfilesForm) form; 161 ActionMessages errors = new ActionMessages(); 162 PropertyProfile profile = (PropertyProfile) getResourceById(f.getSelectedResource()); 163 if (profile == null) { 164 throw new Exception ("Selected profile '" + f.getSelectedResource() + "' doesn't exist."); 165 } 166 if (profile.getOwnerUsername() == null && Constants.SCOPE_PERSONAL.equals(f.getProfileScope())) { 167 errors.add(Globals.ERROR_KEY, new ActionMessage("error.deleteProfile.cantDeleteGlobalProfile")); 168 } 169 if (profile.getResourceName().equalsIgnoreCase("Default") 170 && (profile.getOwnerUsername() == null || profile.getOwnerUsername().equals(""))) { 171 errors.add(Globals.ERROR_KEY, new ActionMessage("error.deleteProfile.cantDeleteDefaultProfile")); 172 } 173 saveErrors(request, errors); 174 CoreUtil.resetMainNavigation(request.getSession()); 175 if (errors.size() > 0) { 176 String returnTo = request.getParameter("returnTo"); 177 return returnTo != null ? new ActionForward(returnTo, false) : mapping.getInputForward(); 178 } else { 179 return super.remove(mapping, form, request, response); 180 } 181 } 182 183 195 public ActionForward select(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 196 throws Exception { 197 ProfilesForm f = (ProfilesForm) form; 198 PropertyProfile profile = ProfilesFactory.getInstance().getPropertyProfile(f.getSelectedResource()); 199 SessionInfo session = getSessionInfo(request); 200 ResourceUtil.checkResourceAccessRights(profile, session); 201 request.getSession().setAttribute(Constants.SELECTED_PROFILE, profile); 202 ActionMessages messages = new ActionMessages(); 203 messages.add(Globals.MESSAGES_KEY, new ActionMessage("message.profileSelected", profile.getResourceName())); 204 saveMessages(request, messages); 205 return mapping.findForward("refresh"); 206 } 207 208 220 public ActionForward selectOrDefault(ActionMapping mapping, ActionForm form, HttpServletRequest request, 221 HttpServletResponse response) throws Exception { 222 return mapping.findForward("selectOrDefault"); 223 } 224 225 233 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 234 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT | SessionInfo.USER_CONSOLE_CONTEXT; 235 } 236 237 protected void checkValid(Resource r, Permission[] permission, ActionMapping mapping, AbstractResourcesForm form, 238 HttpServletRequest request) throws NoPermissionException { 239 if (r instanceof OwnedResource && ((OwnedResource) r).getOwnerUsername() != null) { 240 super.checkValid(r, new Permission[] { PolicyConstants.PERM_MAINTAIN }, mapping, form, request); 241 } else { 242 super.checkValid(r, permission, mapping, form, request); 243 } 244 } 245 } 246 | Popular Tags |