1 package org
.argeo
.app
.ui
.people
;
3 import java
.util
.Arrays
;
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
;
45 /** Edit a suite user. */
46 public class PersonUiProvider
implements SwtUiProvider
{
47 private CmsUserManager cmsUserManager
;
50 public Control
createUiPart(Composite parent
, Content context
) {
51 SwtSection main
= new SwtSection(parent
, SWT
.NONE
, context
);
52 main
.setLayoutData(CmsSwtUtils
.fillAll());
54 main
.setLayout(new GridLayout(2, false));
56 CmsUser user
= context
.adapt(CmsUser
.class);
58 Content hierarchyUnitContent
= context
.getParent().getParent();
59 HierarchyUnit hierarchyUnit
= hierarchyUnitContent
.adapt(HierarchyUnit
.class);
61 String roleContext
= RoleNameUtils
.getContext(user
.getName());
63 if (context
.hasContentClass(LdapObj
.person
.qName())) {
65 addFormLine(main
, SuiteMsg
.firstName
, context
, LdapAttr
.givenName
);
66 addFormLine(main
, SuiteMsg
.lastName
, context
, LdapAttr
.sn
);
67 addFormLine(main
, SuiteMsg
.email
, context
, LdapAttr
.mail
);
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
,
78 addRoleCheckBox(rolesSection
, hierarchyUnit
, user
, SuiteMsg
.publisherRole
, SuiteRole
.publisher
,
80 addRoleCheckBox(rolesSection
, hierarchyUnit
, user
, SuiteMsg
.userAdminRole
, CmsRole
.userAdmin
,
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);
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
) -> {
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());
117 CmsFeedback
.error(CmsMsg
.invalidPassword
.lead(), null);
119 } catch (Exception e1
) {
120 CmsFeedback
.error(CmsMsg
.invalidPassword
.lead(), e1
);
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
138 text
.setMouseListener(new MouseAdapter() {
140 private static final long serialVersionUID
= 1L;
143 public void mouseDoubleClick(MouseEvent e
) {
144 String currentTxt
= text
.getText();
146 text
.setText(currentTxt
);
147 ((Text
) text
.getControl()).addSelectionListener(new SelectionListener() {
149 private static final long serialVersionUID
= 1L;
152 public void widgetSelected(SelectionEvent e
) {
156 public void widgetDefaultSelected(SelectionEvent e
) {
157 String editedTxt
= text
.getText();
158 content
.put(attr
, editedTxt
);
160 text
.setText(editedTxt
);
161 text
.getParent().layout(new Control
[] { text
.getControl() });
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);
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);
185 radio
.setEnabled(false);
188 radio
.setEnabled(CurrentUser
.implies(CmsRole
.userAdmin
, roleContext
));
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
);
197 cmsUserManager
.removeMember(roleGroup
, user
);
200 new Label(parent
, 0).setText(msg
.lead());
204 public void setCmsUserManager(CmsUserManager cmsUserManager
) {
205 this.cmsUserManager
= cmsUserManager
;
208 // private String getUserProperty(Object element, String key) {
209 // Object value = ((User) element).getProperties().get(key);
210 // return value != null ? value.toString() : null;
213 public void init(Map
<String
, Object
> properties
) {