public void execute(Event<UIAccountChangePass> event) throws Exception { UIAccountChangePass uiForm = event.getSource(); OrganizationService service = uiForm.getApplicationComponent(OrganizationService.class); WebuiRequestContext context = WebuiRequestContext.getCurrentInstance(); UIApplication uiApp = context.getUIApplication(); String username = Util.getPortalRequestContext().getRemoteUser(); User user = service.getUserHandler().findUserByName(username); String currentPass = uiForm.getUIStringInput("currentpass").getValue(); String newPass = uiForm.getUIStringInput("newpass").getValue(); String confirmnewPass = uiForm.getUIStringInput("confirmnewpass").getValue(); Authenticator authenticator = uiForm.getApplicationComponent(Authenticator.class); boolean authenticated; try { UsernameCredential usernameCred = new UsernameCredential(username); PasswordCredential passwordCred = new PasswordCredential(currentPass); authenticator.validateUser(new Credential[] {usernameCred, passwordCred}); authenticated = true; } catch (Exception ex) { authenticated = false; } if (!authenticated) { uiApp.addMessage( new ApplicationMessage( "UIAccountChangePass.msg.currentpassword-is-not-match", null, 1)); uiForm.reset(); event.getRequestContext().addUIComponentToUpdateByAjax(uiForm); return; } if (!newPass.equals(confirmnewPass)) { uiApp.addMessage( new ApplicationMessage("UIAccountChangePass.msg.password-is-not-match", null, 1)); uiForm.reset(); event.getRequestContext().addUIComponentToUpdateByAjax(uiForm); return; } try { user.setPassword(newPass); service.getUserHandler().saveUser(user, true); uiApp.addMessage( new ApplicationMessage("UIAccountChangePass.msg.change.pass.success", null)); UIAccountSetting ui = uiForm.getParent(); ui.getChild(UIAccountProfiles.class).setRendered(true); ui.getChild(UIAccountChangePass.class).setRendered(false); event.getRequestContext().addUIComponentToUpdateByAjax(ui); } catch (Exception e) { uiApp.addMessage( new ApplicationMessage( "UIAccountChangePass.msg.change.pass.fail", null, ApplicationMessage.ERROR)); } uiForm.reset(); event.getRequestContext().addUIComponentToUpdateByAjax(uiForm); return; }
@GET @Path("/auth/{1}/{2}") @Produces({MediaType.TEXT_PLAIN}) public String authenticate( @PathParam("1") String encodedUsername, @PathParam("2") String encodedPassword) { try { String username = new String(Base64.decodeBase64(encodedUsername.getBytes())); String password = new String(Base64.decodeBase64(encodedPassword.getBytes())); if (log.isDebugEnabled()) log.debug("Authenticating : " + username); if (log.isTraceEnabled()) log.trace("Password: "******"Fetched authenticator instance " + authenticator); } Credential[] credentials = new Credential[] {new UsernameCredential(username), new PasswordCredential(password)}; try { authenticator.validateUser(credentials); if (log.isTraceEnabled()) log.trace("User authentication successful for " + username); return "" + Boolean.TRUE; } catch (LoginException le) { if (log.isTraceEnabled()) log.trace("User authentication failure for " + username); return "" + Boolean.FALSE; } } catch (Exception e) { log.error(this, e); throw new RuntimeException(e); } }