/
UserMenuPanel.java
329 lines (272 loc) · 13.1 KB
/
UserMenuPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright (C) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.web.component.menu;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.xml.namespace.QName;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.NonCachingImage;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.resource.AbstractResource;
import org.apache.wicket.request.resource.ByteArrayResource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.gui.api.model.ReadOnlyModel;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.model.api.authentication.GuiProfiledPrincipal;
import com.evolveum.midpoint.model.api.authentication.ModuleAuthentication;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.crypto.EncryptionException;
import com.evolveum.midpoint.prism.crypto.Protector;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.AjaxButton;
import com.evolveum.midpoint.web.component.form.MidpointForm;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.page.admin.home.PageMyPasswordQuestions;
import com.evolveum.midpoint.web.page.admin.home.dto.PasswordQuestionsDto;
import com.evolveum.midpoint.web.page.admin.home.dto.SecurityQuestionAnswerDTO;
import com.evolveum.midpoint.web.security.util.SecurityUtils;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
/**
* @author lazyman
*/
public class UserMenuPanel extends BasePanel<UserMenuPanel> {
private static final Trace LOGGER = TraceManager.getTrace(UserMenuPanel.class);
private static final String DOT_CLASS = UserMenuPanel.class.getName() + ".";
private static final String OPERATION_LOAD_USER = DOT_CLASS + "loaduser";
private static final String ID_USERNAME_LINK = "usernameLink";
private static final String ID_LOGOUT_FORM = "logoutForm";
private static final String ID_CSRF_FIELD = "csrfField";
private static final String ID_USERNAME = "username";
private static final String ID_FOCUS_TYPE = "focusType";
private static final String ID_PASSWORD_QUESTIONS = "passwordQuestions";
private static final String ID_ICON_BOX = "menuIconBox";
private static final String ID_PHOTO = "menuPhoto";
private static final String ID_PANEL_ICON_BOX = "menuPanelIconBox";
private static final String ID_PANEL_PHOTO = "menuPanelPhoto";
private final IModel<PrismObject<UserType>> userModel;
public UserMenuPanel(String id) {
super(id);
userModel = new LoadableModel<PrismObject<UserType>>(false) {
@Override
protected PrismObject<UserType> load() {
return loadUser();
}
};
}
@Override
protected void onInitialize() {
super.onInitialize();
initLayout();
}
private void initLayout() {
WebMarkupContainer iconBox = new WebMarkupContainer(ID_ICON_BOX);
add(iconBox);
IModel<AbstractResource> jpegPhotoModel = loadJpegPhotoModel();
NonCachingImage img = new NonCachingImage(ID_PHOTO, jpegPhotoModel);
iconBox.add(img);
Label usernameLink = new Label(ID_USERNAME_LINK, (IModel<String>) this::getShortUserName);
add(usernameLink);
WebMarkupContainer panelIconBox = new WebMarkupContainer(ID_PANEL_ICON_BOX);
add(panelIconBox);
NonCachingImage panelImg = new NonCachingImage(ID_PANEL_PHOTO, jpegPhotoModel);
panelIconBox.add(panelImg);
Label username = new Label(ID_USERNAME, (IModel<String>) this::getShortUserName);
username.setRenderBodyOnly(true);
add(username);
Label focusType = new Label(ID_FOCUS_TYPE, getPageBase().createStringResource("PageTemplate." + getFocusType()));
add(focusType);
MidpointForm<?> form = new MidpointForm<>(ID_LOGOUT_FORM);
form.add(AttributeModifier.replace("action",
(IModel<String>) () -> SecurityUtils.getPathForLogoutWithContextPath(
getRequest().getContextPath(), getAuthenticatedModule())));
add(form);
WebMarkupContainer csrfField = SecurityUtils.createHiddenInputForCsrf(ID_CSRF_FIELD);
form.add(csrfField);
AjaxButton editPasswordQ = new AjaxButton(ID_PASSWORD_QUESTIONS,
createStringResource("UserMenuPanel.editPasswordQuestions")) {
@Override
public void onClick(AjaxRequestTarget target) {
PageMyPasswordQuestions myPasswordQuestions = new PageMyPasswordQuestions(Model.of(getPasswordQuestions()));
setResponsePage(myPasswordQuestions);
}
};
editPasswordQ.add(new VisibleEnableBehaviour() {
@Override
public boolean isVisible() {
return hasQuestions() || (CollectionUtils.isNotEmpty(getSecurityQuestions()));
}
});
add(editPasswordQ);
// securityPolicyQuestionsModel = new LoadableModel<List<SecurityQuestionDefinitionType>>(false) {
// @Override
// protected List<SecurityQuestionDefinitionType> load() {
// return loadSecurityPolicyQuestionsModel();
// }
// };
//
}
private IModel<AbstractResource> loadJpegPhotoModel() {
return new ReadOnlyModel<>(() -> {
if (userModel == null || userModel.getObject() == null) {
return null;
}
byte[] jpegPhoto = userModel.getObject().asObjectable().getJpegPhoto();
if (jpegPhoto == null) {
URL placeholder = UserMenuPanel.class.getClassLoader().getResource("static/img/placeholder.png");
if (placeholder == null) {
return null;
}
try {
jpegPhoto = IOUtils.toByteArray(placeholder);
} catch (IOException e) {
LOGGER.error("Cannot load placeholder for photo.");
return null;
}
// ByteArrayResource("image/png", new InputStream());
// return new ContextRelativeResource("img/placeholder.png");
}
return new ByteArrayResource("image/jpeg", jpegPhoto);
});
}
private ModuleAuthentication getAuthenticatedModule() {
ModuleAuthentication moduleAuthentication = SecurityUtils.getAuthenticatedModule();
if (moduleAuthentication == null) {
String message = "Unauthenticated request";
throw new IllegalArgumentException(message);
}
return moduleAuthentication;
}
private String getShortUserName() {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal == null) {
return "Unknown";
}
if (principal instanceof MidPointPrincipal) {
MidPointPrincipal princ = (MidPointPrincipal) principal;
return WebComponentUtil.getOrigStringFromPoly(princ.getName());
}
return principal.toString();
}
private String getFocusType() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
return "Unknown";
}
Object principal = authentication.getPrincipal();
if (principal == null || principal.equals("anonymousUser")) {
return "Unknown";
}
QName type = WebComponentUtil.classToQName(getPageBase().getPrismContext(), WebModelServiceUtils.getLoggedInFocus().getClass());
return type.getLocalPart();
}
private PasswordQuestionsDto getPasswordQuestions() {
PasswordQuestionsDto dto = new PasswordQuestionsDto();
dto.setSecurityAnswers(createUsersSecurityQuestionsList());
return dto;
}
//TODO this should be probably parametrized to FocusType
private PrismObject<UserType> loadUser() {
OperationResult result = new OperationResult(OPERATION_LOAD_USER);
PageBase parentPage = getPageBase();
try {
GuiProfiledPrincipal principal = SecurityUtils.getPrincipalUser();
if (principal == null) {
result.recordNotApplicableIfUnknown();
return null;
}
String userOid = principal.getOid();
Task task = parentPage.createSimpleTask(OPERATION_LOAD_USER);
OperationResult subResult = result.createSubresult(OPERATION_LOAD_USER);
Collection<SelectorOptions<GetOperationOptions>> options =
getSchemaHelper().getOperationOptionsBuilder()
.item(UserType.F_JPEG_PHOTO).retrieve()
.build();
PrismObject<UserType> user =
parentPage.getModelService().getObject(
UserType.class, userOid, options, task, subResult);
subResult.recordSuccessIfUnknown();
return user;
} catch (Exception ex) {
LoggingUtils.logExceptionOnDebugLevel(LOGGER, "Couldn't get user Questions, Probably not set yet", ex);
} finally {
result.recomputeStatus();
}
return null;
}
public List<SecurityQuestionAnswerDTO> createUsersSecurityQuestionsList() {
GuiProfiledPrincipal principal = SecurityUtils.getPrincipalUser();
if (principal == null) {
return null;
}
FocusType focus = principal.getFocus();
SecurityQuestionsCredentialsType credentialsPolicyType = focus.getCredentials()
.getSecurityQuestions();
if (credentialsPolicyType == null) {
return null;
}
List<SecurityQuestionAnswerType> secQuestAnsList = credentialsPolicyType.getQuestionAnswer();
if (secQuestAnsList != null) {
List<SecurityQuestionAnswerDTO> secQuestAnswListDTO = new ArrayList<>();
for (SecurityQuestionAnswerType securityQuestionAnswerType : secQuestAnsList) {
Protector protector = ((PageBase) getPage()).getPrismContext().getDefaultProtector();
if (securityQuestionAnswerType.getQuestionAnswer() != null && securityQuestionAnswerType.getQuestionAnswer().getEncryptedDataType() != null) {
try {
String decoded = protector.decryptString(securityQuestionAnswerType.getQuestionAnswer());
secQuestAnswListDTO.add(new SecurityQuestionAnswerDTO(securityQuestionAnswerType
.getQuestionIdentifier(), decoded));
} catch (EncryptionException e) {
// TODO do we need to thrown exception here?
LOGGER.error("Could not get security questions. Error: " + e.getMessage(), e);
}
}
}
return secQuestAnswListDTO;
} else {
return null;
}
}
private List<SecurityQuestionDefinitionType> getSecurityQuestions() {
GuiProfiledPrincipal principal = SecurityUtils.getPrincipalUser();
CredentialsPolicyType credentialsPolicyType = principal.getApplicableSecurityPolicy().getCredentials();
if (credentialsPolicyType == null) {
return Collections.EMPTY_LIST;
}
SecurityQuestionsCredentialsPolicyType securityQuestionsPolicy = credentialsPolicyType.getSecurityQuestions();
if (securityQuestionsPolicy == null) {
return Collections.EMPTY_LIST;
}
return securityQuestionsPolicy.getQuestion();
}
private boolean hasQuestions() {
PasswordQuestionsDto passwordQuestionsDto = getPasswordQuestions();
return passwordQuestionsDto != null
&& passwordQuestionsDto.getPwdQuestion() != null
&& !passwordQuestionsDto.getPwdQuestion().trim().equals("");
}
}