]> git.argeo.org Git - gpl/argeo-suite.git/blob - swt/org.argeo.app.ui/src/org/argeo/app/ui/people/PersonUiProvider.java
6343c1bca15238a8bbe5a1508e0541ac7ec233e5
[gpl/argeo-suite.git] / swt / org.argeo.app.ui / src / org / argeo / app / ui / people / PersonUiProvider.java
1 package org.argeo.app.ui.people;
2
3 import java.util.Arrays;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.argeo.api.acr.Content;
8 import org.argeo.api.acr.QNamed;
9 import org.argeo.api.acr.ldap.LdapAttrs;
10 import org.argeo.api.acr.ldap.LdapObjs;
11 import org.argeo.app.api.SuiteRole;
12 import org.argeo.app.ui.SuiteMsg;
13 import org.argeo.app.ui.SuiteStyle;
14 import org.argeo.app.ui.SuiteUiUtils;
15 import org.argeo.cms.CmsMsg;
16 import org.argeo.cms.CmsUserManager;
17 import org.argeo.cms.Localized;
18 import org.argeo.cms.auth.CmsRole;
19 import org.argeo.cms.auth.CurrentUser;
20 import org.argeo.cms.auth.RoleNameUtils;
21 import org.argeo.cms.auth.SystemRole;
22 import org.argeo.cms.swt.CmsSwtUtils;
23 import org.argeo.cms.swt.Selected;
24 import org.argeo.cms.swt.acr.SwtSection;
25 import org.argeo.cms.swt.acr.SwtUiProvider;
26 import org.argeo.cms.swt.dialogs.CmsFeedback;
27 import org.argeo.cms.swt.widgets.EditableText;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.MouseAdapter;
30 import org.eclipse.swt.events.MouseEvent;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.events.SelectionListener;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Text;
40 import org.osgi.service.useradmin.User;
41
42 /** Edit a suite user. */
43 public class PersonUiProvider implements SwtUiProvider {
44 private CmsUserManager cmsUserManager;
45
46 @Override
47 public Control createUiPart(Composite parent, Content context) {
48 SwtSection main = new SwtSection(parent, SWT.NONE, context);
49 main.setLayoutData(CmsSwtUtils.fillAll());
50
51 main.setLayout(new GridLayout(2, false));
52
53 User user = context.adapt(User.class);
54
55 String roleContext = RoleNameUtils.getContext(user.getName());
56
57 if (context.hasContentClass(LdapObjs.person.qName())) {
58
59 addFormLine(main, SuiteMsg.firstName, context, LdapAttrs.givenName);
60 addFormLine(main, SuiteMsg.lastName, context, LdapAttrs.sn);
61 addFormLine(main, SuiteMsg.email, context, LdapAttrs.mail);
62 }
63
64 if (context.hasContentClass(LdapObjs.posixAccount.qName())) {
65
66 SwtSection rolesSection = new SwtSection(main, SWT.NONE);
67 rolesSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
68 rolesSection.setLayout(new GridLayout(2, false));
69 List<String> roles = Arrays.asList(cmsUserManager.getUserRoles(user.getName()));
70 addRoleCheckBox(rolesSection, SuiteMsg.coworkerRole, SuiteRole.coworker, roleContext, roles);
71 addRoleCheckBox(rolesSection, SuiteMsg.publisherRole, SuiteRole.publisher, roleContext, roles);
72 addRoleCheckBox(rolesSection, SuiteMsg.userAdminRole, CmsRole.userAdmin, roleContext, roles);
73
74 // Composite facetsSection = new Composite(main, SWT.NONE);
75 // facetsSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
76 // facetsSection.setLayout(new GridLayout());
77 // if (context.hasContentClass(LdapObjs.groupOfNames.qName())) {
78 // String[] members = context.attr(LdapAttrs.member.qName()).split("\n");
79 // for (String member : members) {
80 // new Label(facetsSection, SWT.NONE).setText(member);
81 // }
82 // }
83 if (CurrentUser.implies(CmsRole.userAdmin, roleContext)) {
84 SwtSection changePasswordSection = new SwtSection(main, SWT.BORDER);
85 changePasswordSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
86 changePasswordSection.setLayout(new GridLayout(2, false));
87 // SuiteUiUtils.addFormLabel(changePasswordSection, CmsMsg.changePassword)
88 // .setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false, 2, 1));
89 SuiteUiUtils.addFormLabel(changePasswordSection, CmsMsg.newPassword);
90 Text newPasswordT = SuiteUiUtils.addFormTextField(changePasswordSection, null, null,
91 SWT.PASSWORD | SWT.BORDER);
92 newPasswordT.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
93 SuiteUiUtils.addFormLabel(changePasswordSection, CmsMsg.repeatNewPassword);
94 Text repeatNewPasswordT = SuiteUiUtils.addFormTextField(changePasswordSection, null, null,
95 SWT.PASSWORD | SWT.BORDER);
96 repeatNewPasswordT.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
97 Button apply = new Button(changePasswordSection, SWT.FLAT);
98 apply.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false, 2, 1));
99 apply.setText(CmsMsg.changePassword.lead());
100 apply.addSelectionListener((Selected) (e) -> {
101 try {
102 char[] newPassword = newPasswordT.getTextChars();
103 char[] repeatNewPassword = repeatNewPasswordT.getTextChars();
104 if (newPassword.length > 0 && Arrays.equals(newPassword, repeatNewPassword)) {
105 cmsUserManager.resetPassword(user.getName(), newPassword);
106 CmsFeedback.show(CmsMsg.passwordChanged.lead());
107 } else {
108 CmsFeedback.error(CmsMsg.invalidPassword.lead(), null);
109 }
110 } catch (Exception e1) {
111 CmsFeedback.error(CmsMsg.invalidPassword.lead(), e1);
112 }
113 });
114 }
115 }
116
117 return main;
118 }
119
120 private void addFormLine(SwtSection parent, Localized msg, Content content, QNamed attr) {
121 SuiteUiUtils.addFormLabel(parent, msg.lead());
122 EditableText text = new EditableText(parent, SWT.SINGLE | SWT.FLAT);
123 text.setLayoutData(CmsSwtUtils.fillWidth());
124 text.setStyle(SuiteStyle.simpleInput);
125 String txt = content.attr(attr);
126 if (txt == null) // FIXME understand why email is not found in IPA
127 txt = "";
128 text.setText(txt);
129 text.setMouseListener(new MouseAdapter() {
130
131 private static final long serialVersionUID = 1L;
132
133 @Override
134 public void mouseDoubleClick(MouseEvent e) {
135 String currentTxt = text.getText();
136 text.startEditing();
137 text.setText(currentTxt);
138 ((Text) text.getControl()).addSelectionListener(new SelectionListener() {
139
140 private static final long serialVersionUID = 1L;
141
142 @Override
143 public void widgetSelected(SelectionEvent e) {
144 }
145
146 @Override
147 public void widgetDefaultSelected(SelectionEvent e) {
148 String editedTxt = text.getText();
149 text.stopEditing();
150 text.setText(editedTxt);
151 text.getParent().layout(new Control[] { text.getControl() });
152 }
153 });
154 }
155
156 });
157 }
158
159 private void addRoleCheckBox(SwtSection parent, Localized msg, SystemRole systemRole, String roleContext,
160 List<String> roles) {
161 Button radio = new Button(parent, SWT.CHECK);
162 radio.setSelection(false);
163 roles: for (String dn : roles) {
164 if (systemRole.implied(dn, roleContext)) {
165 radio.setSelection(true);
166 break roles;
167 }
168 }
169
170 if (systemRole.equals(CmsRole.userAdmin))
171 radio.setEnabled(CurrentUser.implies(CmsRole.groupAdmin, roleContext));
172 else
173 radio.setEnabled(CurrentUser.implies(CmsRole.userAdmin, roleContext));
174
175 new Label(parent, 0).setText(msg.lead());
176
177 }
178
179 public void setCmsUserManager(CmsUserManager cmsUserManager) {
180 this.cmsUserManager = cmsUserManager;
181 }
182
183 // private String getUserProperty(Object element, String key) {
184 // Object value = ((User) element).getProperties().get(key);
185 // return value != null ? value.toString() : null;
186 // }
187
188 public void init(Map<String, Object> properties) {
189 }
190 }