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