1 package org
.argeo
.app
.ui
.people
;
3 import static org
.argeo
.eclipse
.ui
.EclipseUiUtils
.isEmpty
;
5 import java
.util
.HashMap
;
6 import java
.util
.HashSet
;
11 import org
.argeo
.api
.acr
.Content
;
12 import org
.argeo
.app
.core
.SuiteUtils
;
13 import org
.argeo
.app
.ui
.SuiteMsg
;
14 import org
.argeo
.app
.ui
.SuiteUiUtils
;
15 import org
.argeo
.cms
.CmsUserManager
;
16 import org
.argeo
.cms
.acr
.ContentUtils
;
17 import org
.argeo
.cms
.swt
.dialogs
.CmsFeedback
;
18 import org
.argeo
.cms
.swt
.widgets
.SwtGuidedFormPage
;
19 import org
.argeo
.cms
.ux
.widgets
.AbstractGuidedForm
;
20 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
21 import org
.argeo
.util
.directory
.HierarchyUnit
;
22 import org
.argeo
.util
.naming
.LdapAttrs
;
23 import org
.argeo
.util
.naming
.LdapObjs
;
24 import org
.eclipse
.swt
.SWT
;
25 import org
.eclipse
.swt
.events
.ModifyEvent
;
26 import org
.eclipse
.swt
.events
.ModifyListener
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Text
;
31 import org
.osgi
.service
.useradmin
.User
;
33 /** Ask first & last name. Update the passed node on finish */
34 public class NewUserForm
extends AbstractGuidedForm
{
35 private Content hierarchyUnit
;
36 private CmsUserManager cmsUserManager
;
38 protected Text lastNameT
;
39 protected Text firstNameT
;
40 protected Text emailT
;
42 public NewUserForm(CmsUserManager cmsUserManager
, Content hierarchyUnit
) {
43 this.hierarchyUnit
= hierarchyUnit
;
44 if (!hierarchyUnit
.hasContentClass(LdapObjs
.posixGroup
.qName()))
45 throw new IllegalArgumentException(hierarchyUnit
+ " is not a POSIX group");
46 this.cmsUserManager
= cmsUserManager
;
50 public void addPages() {
52 MainInfoPage page
= new MainInfoPage("Main page");
54 } catch (Exception e
) {
55 throw new RuntimeException("Cannot add page to wizard", e
);
57 setFormTitle(SuiteMsg
.personWizardWindowTitle
.lead());
61 * Called when the user click on 'Finish' in the wizard. The task is then
62 * created and the corresponding session saved.
65 public boolean performFinish() {
66 String lastName
= lastNameT
.getText();
67 String firstName
= firstNameT
.getText();
68 String email
= emailT
.getText();
69 if (EclipseUiUtils
.isEmpty(lastName
) || EclipseUiUtils
.isEmpty(firstName
) || EclipseUiUtils
.isEmpty(email
)) {
70 CmsFeedback
.show(SuiteMsg
.allFieldsMustBeSet
.lead());
73 UUID uuid
= UUID
.randomUUID();
74 String shortId
= uuid
.toString().split("-")[0];
75 String uid
= "u" + shortId
;
76 HierarchyUnit hu
= hierarchyUnit
.adapt(HierarchyUnit
.class);
77 String username
= "uid=" + uid
+ ",ou=People," + hu
.getBase();
79 Map
<String
, Object
> properties
= new HashMap
<>();
80 properties
.put(LdapAttrs
.givenName
.name(), firstName
);
81 properties
.put(LdapAttrs
.sn
.name(), lastName
);
82 properties
.put(LdapAttrs
.mail
.name(), email
);
83 properties
.put(LdapAttrs
.cn
.name(), firstName
+ " " + lastName
);
84 properties
.put(LdapAttrs
.employeeNumber
.name(), uuid
.toString());
86 Map
<String
, Object
> credentials
= new HashMap
<>();
87 User user
= cmsUserManager
.createUser(username
, properties
, credentials
);
89 Long huGidNumber
= hierarchyUnit
.get(LdapAttrs
.gidNumber
.qName(), Long
.class).orElseThrow();
90 Long nextUserId
= SuiteUtils
.findNextId(hierarchyUnit
, LdapObjs
.posixAccount
.qName());
91 String homeDirectory
= "/home/" + uid
;
92 Map
<String
, Object
> additionalProperties
= new HashMap
<>();
93 additionalProperties
.put(LdapAttrs
.uidNumber
.name(), nextUserId
.toString());
94 additionalProperties
.put(LdapAttrs
.gidNumber
.name(), huGidNumber
.toString());
95 additionalProperties
.put(LdapAttrs
.homeDirectory
.name(), homeDirectory
);
97 Set
<String
> objectClasses
= new HashSet
<>();
98 objectClasses
.add(LdapObjs
.posixAccount
.name());
99 cmsUserManager
.addObjectClasses(user
, objectClasses
, additionalProperties
);
105 public boolean performCancel() {
110 public boolean canFinish() {
111 String lastName
= lastNameT
.getText();
112 String firstName
= firstNameT
.getText();
113 String email
= emailT
.getText();
114 if (isEmpty(lastName
) || isEmpty(firstName
) || isEmpty(email
)) {
120 protected class MainInfoPage
extends SwtGuidedFormPage
{
122 public MainInfoPage(String pageName
) {
124 setTitle(SuiteMsg
.personWizardPageTitle
.lead());
127 public void createControl(Composite parent
) {
128 parent
.setLayout(new GridLayout(2, false));
131 SuiteUiUtils
.createBoldLabel(parent
, SuiteMsg
.firstName
);
132 firstNameT
= new Text(parent
, SWT
.BORDER
);
133 // firstNameTxt.setMessage("a first name");
134 firstNameT
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
137 SuiteUiUtils
.createBoldLabel(parent
, SuiteMsg
.lastName
);
138 lastNameT
= new Text(parent
, SWT
.BORDER
);
139 // lastNameTxt.setMessage("a last name");
140 lastNameT
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
142 SuiteUiUtils
.createBoldLabel(parent
, SuiteMsg
.email
);
143 emailT
= new Text(parent
, SWT
.BORDER
);
144 emailT
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
146 ModifyListener ml
= new ModifyListener() {
147 private static final long serialVersionUID
= 1939491923843870844L;
150 public void modifyText(ModifyEvent event
) {
151 getView().updateButtons();
155 firstNameT
.addModifyListener(ml
);
156 lastNameT
.addModifyListener(ml
);
157 emailT
.addModifyListener(ml
);
159 firstNameT
.setFocus();