1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.codelibs.fess.app.web.profile;
21
22 import javax.annotation.Resource;
23
24 import org.codelibs.fess.app.service.UserService;
25 import org.codelibs.fess.app.web.base.FessSearchAction;
26 import org.codelibs.fess.app.web.login.LoginAction;
27 import org.lastaflute.web.Execute;
28 import org.lastaflute.web.login.credential.UserPasswordCredential;
29 import org.lastaflute.web.response.HtmlResponse;
30 import org.lastaflute.web.validation.VaErrorHook;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class ProfileAction extends FessSearchAction {
35
36 private static final Logger logger = LoggerFactory.getLogger(ProfileAction.class);
37
38
39
40
41
42
43
44
45 @Resource
46 private UserService userService;
47
48
49
50
51
52
53
54
55
56 @Execute
57 public HtmlResponse index() {
58 return asIndexHtml();
59 }
60
61 @Execute
62 public HtmlResponse changePassword(final ProfileForm form) {
63 final VaErrorHook toIndexPage = () -> {
64 form.clearSecurityInfo();
65 return asIndexHtml();
66 };
67 validatePasswordForm(form, toIndexPage);
68 final String username = getUserBean().map(u -> u.getUserId()).get();
69 try {
70 userService.changePassword(username, form.newPassword);
71 saveInfo(messages -> messages.addSuccessChangedPassword(GLOBAL));
72 } catch (final Exception e) {
73 logger.error("Failed to change password for " + username, e);
74 throwValidationError(messages -> messages.addErrorsFailedToChangePassword(GLOBAL), toIndexPage);
75 }
76 return redirect(getClass());
77 }
78
79 private void validatePasswordForm(final ProfileForm form, final VaErrorHook validationErrorLambda) {
80 validate(form, messages -> {}, validationErrorLambda);
81
82 if (!form.newPassword.equals(form.confirmNewPassword)) {
83 form.newPassword = null;
84 form.confirmNewPassword = null;
85 throwValidationError(messages -> {
86 messages.addErrorsInvalidConfirmPassword(GLOBAL);
87 }, validationErrorLambda);
88 }
89
90 fessLoginAssist.findLoginUser(new UserPasswordCredential(getUserBean().get().getUserId(), form.oldPassword)).orElseGet(() -> {
91 throwValidationError(messages -> {
92 messages.addErrorsNoUserForChangingPassword(GLOBAL);
93 }, validationErrorLambda);
94 return null;
95 });
96 }
97
98 protected HtmlResponse asIndexHtml() {
99 return getUserBean().map(u -> asHtml(virtualHost(path_Profile_IndexJsp)).useForm(ProfileForm.class)).orElseGet(
100 () -> redirect(LoginAction.class));
101 }
102 }