1 19 20 package com.sslexplorer.security.actions; 21 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 28 import org.apache.struts.Globals; 29 import org.apache.struts.action.ActionForm; 30 import org.apache.struts.action.ActionForward; 31 import org.apache.struts.action.ActionMapping; 32 import org.apache.struts.action.ActionMessage; 33 import org.apache.struts.action.ActionMessages; 34 35 import com.sslexplorer.boot.PropertyList; 36 import com.sslexplorer.core.CoreAttributeConstants; 37 import com.sslexplorer.core.CoreEvent; 38 import com.sslexplorer.core.CoreEventConstants; 39 import com.sslexplorer.core.CoreServlet; 40 import com.sslexplorer.core.CoreUtil; 41 import com.sslexplorer.core.RedirectWithMessages; 42 import com.sslexplorer.core.UserDatabaseManager; 43 import com.sslexplorer.core.actions.AuthenticatedDispatchAction; 44 import com.sslexplorer.policyframework.Permission; 45 import com.sslexplorer.policyframework.PolicyConstants; 46 import com.sslexplorer.policyframework.PolicyUtil; 47 import com.sslexplorer.properties.Property; 48 import com.sslexplorer.properties.attributes.AttributeDefinition; 49 import com.sslexplorer.properties.attributes.AttributeValueItem; 50 import com.sslexplorer.properties.impl.userattributes.UserAttributeKey; 51 import com.sslexplorer.security.Constants; 52 import com.sslexplorer.security.GroupsRequiredForUserException; 53 import com.sslexplorer.security.LogonControllerFactory; 54 import com.sslexplorer.security.Role; 55 import com.sslexplorer.security.SessionInfo; 56 import com.sslexplorer.security.User; 57 import com.sslexplorer.security.UserDatabase; 58 import com.sslexplorer.security.UserDatabaseException; 59 import com.sslexplorer.security.forms.UserAccountForm; 60 61 62 72 public class ShowAccountAction extends AuthenticatedDispatchAction { 73 76 public ShowAccountAction() { 77 super(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, new Permission[] { 78 PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, 79 PolicyConstants.PERM_DELETE 80 }); 81 } 82 83 86 public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, 87 HttpServletResponse response) throws Exception { 88 request.getSession().removeAttribute(Constants.EDITING_ITEM); 89 return mapping.findForward("display"); 90 } 91 92 93 103 public ActionForward setPassword(ActionMapping mapping, ActionForm form, 104 HttpServletRequest request, HttpServletResponse response) 105 throws Exception { 106 UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm()); 107 User user = udb.getAccount(((UserAccountForm) form).getUsername()); 108 request.getSession().setAttribute("setPassword.user", user); 109 return mapping.findForward("setPassword"); 110 } 111 112 122 public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 123 throws Exception { 124 PolicyUtil.checkPermission(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, request); 125 ((UserAccountForm) form).initialize(null, false, request); 126 ((UserAccountForm) form).setReferer(CoreUtil.getReferer(request)); 127 CoreUtil.addRequiredFieldMessage(this, request); 128 return mapping.findForward("refresh"); 129 } 130 131 141 public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 142 throws Exception { 143 PolicyUtil.checkPermission(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, request); 144 String username = request.getParameter("username"); 145 UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm()); 146 User user = udb.getAccount(username); 147 ((UserAccountForm) form).initialize(user, true, request); 148 ((UserAccountForm) form).setReferer(CoreUtil.getReferer(request)); 149 return mapping.findForward("display"); 150 } 151 152 160 public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 161 throws Exception { 162 CoreUtil.addRequiredFieldMessage(this, request); 163 return mapping.findForward("display"); 164 } 165 166 176 public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 177 throws Exception { 178 UserAccountForm account = (UserAccountForm) form; 179 PolicyUtil.checkPermission(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, request); 180 SessionInfo info = this.getSessionInfo(request); 181 UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm()); 182 User user = null; 183 if(udb.supportsAccountCreation()) { 184 PropertyList roleList = account.getRolesList(); 185 int idx = 0; 186 Role[] roles = new Role[roleList.size()]; 187 for(Iterator i = roleList.iterator(); i.hasNext(); ) { 188 roles[idx++] = udb.getRole((String )i.next()); 189 } 190 191 if (account.getEditing()) { 192 user = udb.getAccount(account.getUsername()); 193 try { 194 udb.updateAccount(user, account.getEmail(), account.getFullname(), roles); 195 CoreEvent coreEvent = new CoreEvent(this, CoreEventConstants.USER_EDITED, user, info) 196 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, user.getPrincipalName()) 197 .addAttribute(CoreAttributeConstants.EVENT_ATTR_FULL_NAME, user.getFullname()) 198 .addAttribute(CoreAttributeConstants.EVENT_ATTR_ACCOUNT_EMAIL, user.getEmail()); 199 200 if(roles.length != 0) { 201 for(int i = 0; i < roles.length; i++ ) { 202 coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_GROUP + Integer.toString(i+1), roles[i].getPrincipalName()); 203 } 204 } 205 CoreServlet.getServlet().fireCoreEvent(coreEvent); 206 } catch (GroupsRequiredForUserException e) { 207 saveError(request, "createAccount.error.groupsRequired"); 208 return mapping.findForward("display"); 209 } catch (UserDatabaseException e) { 210 if(UserDatabaseException.INTERNAL_ERROR == e.getCode()) { 211 handleException(CoreEventConstants.USER_CREATED, account, info, roles, e); 212 throw e; 213 } else { 214 saveError(request, e.getBundleActionMessage()); 215 return mapping.findForward("display"); 216 } 217 } catch (Exception e) { 218 handleException(CoreEventConstants.USER_EDITED, account, info, roles, e); 219 throw e; 220 } 221 } else { 222 try { 223 user = udb.createAccount(account.getUsername(), String.valueOf((int) (Math.random() * 100000)), 224 account.getEmail(), account.getFullname(), roles); 226 CoreEvent coreEvent = new CoreEvent(this, CoreEventConstants.USER_CREATED, null, info, CoreEvent.STATE_SUCCESSFUL) 227 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, account.getUsername()) 228 .addAttribute(CoreAttributeConstants.EVENT_ATTR_FULL_NAME, account.getFullname()) 229 .addAttribute(CoreAttributeConstants.EVENT_ATTR_ACCOUNT_EMAIL, account.getEmail()); 230 231 if(roles.length != 0) { 232 for(int i = 0; i < roles.length; i++ ) { 233 coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_GROUP + Integer.toString(i+1), roles[i].getPrincipalName()); 234 } 235 } 236 CoreServlet.getServlet().fireCoreEvent(coreEvent); 237 } catch (GroupsRequiredForUserException e) { 238 saveError(request, "createAccount.error.groupsRequired"); 239 return mapping.findForward("display"); 240 } catch (UserDatabaseException e) { 241 if(UserDatabaseException.INTERNAL_ERROR == e.getCode()) { 242 handleException(CoreEventConstants.USER_CREATED, account, info, roles, e); 243 throw e; 244 } else { 245 saveError(request, e.getBundleActionMessage()); 246 return mapping.findForward("display"); 247 } 248 } catch (Exception e) { 249 handleException(CoreEventConstants.USER_CREATED, account, info, roles, e); 250 throw e; 251 } 252 } 253 } 254 else { 255 user = udb.getAccount(account.getUsername()); 256 } 257 258 for(Iterator i = account.getAttributeValueItems().iterator(); i.hasNext(); ) { 260 AttributeValueItem v = (AttributeValueItem)i.next(); 261 if(v.getDefinition().getVisibility() != AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) { 262 Property.setProperty(new UserAttributeKey(user, v.getDefinition().getName()), v.getDefinition().formatAttributeValue(v.getValue()), info); 263 } 264 } 265 for(Iterator j = LogonControllerFactory.getInstance().getActiveSessions().entrySet().iterator(); j.hasNext(); ) { 267 Map.Entry e = (Map.Entry )j.next(); 268 SessionInfo sinfo = (SessionInfo)e.getValue(); 269 if(sinfo.getUser().getPrincipalName().equals(user.getPrincipalName())) { 270 sinfo.setUser(user); 271 } 272 } 273 274 if (PolicyUtil.isEnabled(user) != account.isEnabled()) { 276 PolicyUtil.setEnabled(user, account.isEnabled(), null, null); 277 } 278 279 LogonControllerFactory.getInstance().applyMenuItemChanges(request); 281 282 if (udb.supportsPasswordChange() && (account.isSetPassword() || !account.getEditing())) { 284 request.getSession().setAttribute("setPassword.user", user); 285 ActionMessages msgs = new ActionMessages(); 286 msgs.add(Globals.MESSAGE_KEY, new ActionMessage("createAccount.message.accountSaved")); 287 saveMessages(request, msgs); 288 return mapping.findForward("setPassword"); 289 } else { 290 return new RedirectWithMessages(mapping.findForward("success"), request); 291 } 292 } 293 294 private void handleException(int eventId, UserAccountForm account, SessionInfo info, Role[] roles, Exception e) { 295 CoreEvent coreEvent = new CoreEvent(this, eventId, null, info, e) 296 .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, account.getUsername()) 297 .addAttribute(CoreAttributeConstants.EVENT_ATTR_FULL_NAME, account.getFullname()) 298 .addAttribute(CoreAttributeConstants.EVENT_ATTR_ACCOUNT_EMAIL, account.getEmail()); 299 300 if(roles.length != 0) { 301 for(int i = 0; i < roles.length; i++ ) { 302 coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_GROUP + Integer.toString(i+1), roles[i].getPrincipalName()); 303 } 304 } 305 CoreServlet.getServlet().fireCoreEvent(coreEvent); 306 } 307 308 316 public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 317 throws Exception { 318 return new RedirectWithMessages(mapping.findForward("cancel"), request); 319 } 320 321 324 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 325 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT; 326 } 327 328 338 public ActionForward resetUserAttributes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 339 throws Exception { 340 UserAccountForm account = (UserAccountForm) form; 341 PolicyUtil.checkPermission(PolicyConstants.ACCOUNTS_AND_GROUPS_RESOURCE_TYPE, PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN, request); 342 for(Iterator i = account.getAttributeValueItems().iterator(); i.hasNext(); ) { 343 AttributeValueItem v = (AttributeValueItem)i.next(); 344 v.setValue(v.getDefinition().parseValue(v.getDefinition().getDefaultValue())); 345 } 346 return mapping.findForward("display"); 347 } 348 } | Popular Tags |