2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.security
.ui
.admin
.editors
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Arrays
;
20 import java
.util
.List
;
22 import org
.argeo
.ArgeoException
;
23 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
24 import org
.argeo
.jcr
.ArgeoNames
;
25 import org
.argeo
.security
.ui
.admin
.internal
.ColumnDefinition
;
26 import org
.argeo
.security
.ui
.admin
.internal
.CommonNameLP
;
27 import org
.argeo
.security
.ui
.admin
.internal
.RoleIconLP
;
28 import org
.argeo
.security
.ui
.admin
.internal
.UserAdminConstants
;
29 import org
.argeo
.security
.ui
.admin
.internal
.UserNameLP
;
30 import org
.argeo
.security
.ui
.admin
.internal
.UserTableDefaultDClickListener
;
31 import org
.argeo
.security
.ui
.admin
.internal
.UserTableViewer
;
32 import org
.eclipse
.jface
.viewers
.TableViewer
;
33 import org
.eclipse
.jface
.viewers
.Viewer
;
34 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
35 import org
.eclipse
.swt
.SWT
;
36 import org
.eclipse
.swt
.dnd
.DND
;
37 import org
.eclipse
.swt
.dnd
.DropTargetEvent
;
38 import org
.eclipse
.swt
.dnd
.TextTransfer
;
39 import org
.eclipse
.swt
.dnd
.Transfer
;
40 import org
.eclipse
.swt
.dnd
.TransferData
;
41 import org
.eclipse
.swt
.events
.ModifyEvent
;
42 import org
.eclipse
.swt
.events
.ModifyListener
;
43 import org
.eclipse
.swt
.layout
.GridData
;
44 import org
.eclipse
.swt
.layout
.GridLayout
;
45 import org
.eclipse
.swt
.widgets
.Composite
;
46 import org
.eclipse
.swt
.widgets
.Label
;
47 import org
.eclipse
.swt
.widgets
.Text
;
48 import org
.eclipse
.ui
.forms
.AbstractFormPart
;
49 import org
.eclipse
.ui
.forms
.IManagedForm
;
50 import org
.eclipse
.ui
.forms
.SectionPart
;
51 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
52 import org
.eclipse
.ui
.forms
.editor
.FormPage
;
53 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
54 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
55 import org
.eclipse
.ui
.forms
.widgets
.Section
;
56 import org
.osgi
.service
.useradmin
.Group
;
57 import org
.osgi
.service
.useradmin
.Role
;
58 import org
.osgi
.service
.useradmin
.User
;
59 import org
.osgi
.service
.useradmin
.UserAdmin
;
61 /** Display/edit the properties common to all users */
62 public class UserMainPage
extends FormPage
implements ArgeoNames
{
63 final static String ID
= "argeoUserEditor.mainPage";
65 // private final static Log log = LogFactory.getLog(UserMainPage.class);
67 private final UserEditor editor
;
68 private UserAdmin userAdmin
;
70 private char[] newPassword
;
72 // Local configuration
73 private final int PRE_TITLE_INDENT
= 10;
75 public UserMainPage(FormEditor editor
, UserAdmin userAdmin
) {
76 super(editor
, ID
, "Main");
77 this.editor
= (UserEditor
) editor
;
78 this.userAdmin
= userAdmin
;
81 public String
getNewPassword() {
82 if (newPassword
!= null)
83 return new String(newPassword
);
88 public void resetNewPassword() {
89 if (newPassword
!= null)
90 Arrays
.fill(newPassword
, 'x');
94 protected void createFormContent(final IManagedForm mf
) {
95 ScrolledForm form
= mf
.getForm();
96 // Form page main title
97 form
.setText(editor
.getProperty(UserAdminConstants
.KEY_CN
));
100 Composite body
= form
.getBody();
101 GridLayout mainLayout
= new GridLayout(1, true);
102 body
.setLayout(mainLayout
);
103 appendOverviewPart(body
);
104 appendPasswordPart(body
);
105 appendMemberOfPart(body
);
108 /** Creates the general section */
109 protected void appendOverviewPart(Composite parent
) {
110 FormToolkit tk
= getManagedForm().getToolkit();
111 Composite body
= addSection(tk
, parent
, "Main information");
112 body
.setLayout(new GridLayout(2, false));
114 Text distinguishedName
= createLT(body
, "User Name",
115 editor
.getProperty(UserAdminConstants
.KEY_UID
));
116 distinguishedName
.setEnabled(false);
118 final Text commonName
= createLT(body
, "Common Name",
119 editor
.getProperty(UserAdminConstants
.KEY_CN
));
120 commonName
.setEnabled(false);
122 final Text firstName
= createLT(body
, "First name",
123 editor
.getProperty(UserAdminConstants
.KEY_FIRSTNAME
));
125 final Text lastName
= createLT(body
, "Last name",
126 editor
.getProperty(UserAdminConstants
.KEY_LASTNAME
));
128 final Text email
= createLT(body
, "Email",
129 editor
.getProperty(UserAdminConstants
.KEY_MAIL
));
131 // create form part (controller)
132 AbstractFormPart part
= new SectionPart((Section
) body
.getParent()) {
133 public void commit(boolean onSave
) {
134 // TODO check changed ?
135 // TODO Sanity checks
137 editor
.setProperty(UserAdminConstants
.KEY_FIRSTNAME
,
138 firstName
.getText());
139 editor
.setProperty(UserAdminConstants
.KEY_LASTNAME
,
141 editor
.setProperty(UserAdminConstants
.KEY_CN
,
142 commonName
.getText());
143 // TODO check mail validity
144 editor
.setProperty(UserAdminConstants
.KEY_MAIL
, email
.getText());
146 // Enable common name ?
147 // editor.setProperty(UserAdminConstants.KEY_CN,
149 super.commit(onSave
);
153 ModifyListener cnML
= new ModifyListener() {
154 private static final long serialVersionUID
= 4298649222869835486L;
157 public void modifyText(ModifyEvent event
) {
158 String first
= firstName
.getText();
159 String last
= lastName
.getText();
160 String cn
= first
.trim() + " " + last
.trim() + " ";
162 commonName
.setText(cn
);
163 getManagedForm().getForm().setText(cn
);
164 editor
.updateEditorTitle(cn
);
167 firstName
.addModifyListener(cnML
);
168 lastName
.addModifyListener(cnML
);
169 firstName
.addModifyListener(new FormPartML(part
));
170 lastName
.addModifyListener(new FormPartML(part
));
171 email
.addModifyListener(new FormPartML(part
));
172 getManagedForm().addPart(part
);
175 /** Creates the password section */
176 protected void appendPasswordPart(Composite parent
) {
177 FormToolkit tk
= getManagedForm().getToolkit();
178 Composite body
= addSection(tk
, parent
, "Password");
180 // Section section = tk.createSection(parent, Section.TITLE_BAR);
181 // section.setLayoutData(EclipseUiUtils.fillWidth());
182 // section.setText("Password");
183 // Composite body = tk.createComposite(section, SWT.NO_FOCUS);
184 // section.setClient(body);
185 // body.setLayoutData(EclipseUiUtils.fillWidth());
187 body
.setLayout(new GridLayout(2, false));
188 // add widgets (view)
189 final Text password1
= createLP(body
, "New password", "");
190 final Text password2
= createLP(body
, "Repeat password", "");
191 // create form part (controller)
192 AbstractFormPart part
= new SectionPart((Section
) body
.getParent()) {
194 public void commit(boolean onSave
) {
195 if (!password1
.getText().equals("")
196 || !password2
.getText().equals("")) {
197 if (password1
.getText().equals(password2
.getText())) {
198 newPassword
= password1
.getText().toCharArray();
199 // TODO real set password
200 password1
.setText("");
201 password2
.setText("");
202 super.commit(onSave
);
204 password1
.setText("");
205 password2
.setText("");
206 throw new ArgeoException("Passwords are not equals");
212 password1
.addModifyListener(new FormPartML(part
));
213 password2
.addModifyListener(new FormPartML(part
));
214 getManagedForm().addPart(part
);
217 private UserTableViewer userTableViewerCmp
;
218 private TableViewer userViewer
;
220 private void appendMemberOfPart(Composite parent
) {
221 FormToolkit tk
= getManagedForm().getToolkit();
222 Composite body
= addSection(tk
, parent
, "Roles");
223 body
.setLayout(EclipseUiUtils
.noSpaceGridLayout());
225 // Define the displayed columns
226 List
<ColumnDefinition
> columnDefs
= new ArrayList
<ColumnDefinition
>();
227 columnDefs
.add(new ColumnDefinition(new RoleIconLP(), "", 0, 24));
228 columnDefs
.add(new ColumnDefinition(new UserNameLP(),
229 "Distinguished Name", 300));
230 columnDefs
.add(new ColumnDefinition(new CommonNameLP(), "Common Name",
233 // Create and configure the table
234 userTableViewerCmp
= new MyUserTableViewer(body
, SWT
.MULTI
235 | SWT
.H_SCROLL
| SWT
.V_SCROLL
, userAdmin
);
237 userTableViewerCmp
.setColumnDefinitions(columnDefs
);
238 userTableViewerCmp
.populate(true, false);
239 GridData gd
= EclipseUiUtils
.fillAll();
241 userTableViewerCmp
.setLayoutData(gd
);
244 userViewer
= userTableViewerCmp
.getTableViewer();
245 userViewer
.addDoubleClickListener(new UserTableDefaultDClickListener());
247 userTableViewerCmp
.refresh();
250 int operations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
251 Transfer
[] tt
= new Transfer
[] { TextTransfer
.getInstance() };
252 userViewer
.addDropSupport(operations
, tt
, new GroupDropListener(
253 userViewer
, userAdmin
, editor
.getDisplayedUser()));
257 private class MyUserTableViewer
extends UserTableViewer
{
258 private static final long serialVersionUID
= 8467999509931900367L;
260 public MyUserTableViewer(Composite parent
, int style
,
261 UserAdmin userAdmin
) {
262 super(parent
, style
, userAdmin
, true);
266 protected List
<User
> listFilteredElements(String filter
) {
267 return (List
<User
>) editor
.getFlatGroups(null);
272 * Defines this table as being a potential target to add group membership
273 * (roles) to this user
275 private class GroupDropListener
extends ViewerDropAdapter
{
276 private static final long serialVersionUID
= 2893468717831451621L;
278 private final UserAdmin myUserAdmin
;
279 private final User myUser
;
281 public GroupDropListener(Viewer viewer
, UserAdmin userAdmin
, User user
) {
283 this.myUserAdmin
= userAdmin
;
288 public boolean validateDrop(Object target
, int operation
,
289 TransferData transferType
) {
290 // Target is always OK in a list only view
291 // TODO check if not a string
292 boolean validDrop
= true;
297 public void drop(DropTargetEvent event
) {
298 String name
= (String
) event
.data
;
299 Role role
= myUserAdmin
.getRole(name
);
300 // TODO this check should be done before.
301 if (role
.getType() == Role
.GROUP
) {
302 // TODO check if the user is already member of this group
303 // we expect here that there is already a begun transaction
304 // TODO implement the dirty state
305 editor
.beginTransactionIfNeeded();
306 Group group
= (Group
) role
;
307 group
.addMember(myUser
);
313 public boolean performDrop(Object data
) {
314 userTableViewerCmp
.refresh();
320 /** Appends a section with a title */
321 private Composite
addSection(FormToolkit tk
, Composite parent
, String title
) {
322 Section section
= tk
.createSection(parent
, Section
.TITLE_BAR
);
323 GridData gd
= EclipseUiUtils
.fillWidth();
324 gd
.verticalAlignment
= PRE_TITLE_INDENT
;
325 section
.setLayoutData(gd
);
326 section
.setText(title
);
327 Composite body
= tk
.createComposite(section
, SWT
.WRAP
);
328 body
.setLayoutData(EclipseUiUtils
.fillAll());
329 section
.setClient(body
);
333 /** Creates label and multiline text. */
334 protected Text
createLMT(Composite body
, String label
, String value
) {
335 FormToolkit toolkit
= getManagedForm().getToolkit();
336 Label lbl
= toolkit
.createLabel(body
, label
);
337 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
338 Text text
= toolkit
.createText(body
, value
, SWT
.BORDER
| SWT
.MULTI
);
339 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, true));
343 /** Creates label and password. */
344 protected Text
createLP(Composite body
, String label
, String value
) {
345 FormToolkit toolkit
= getManagedForm().getToolkit();
346 Label lbl
= toolkit
.createLabel(body
, label
);
347 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
348 Text text
= toolkit
.createText(body
, value
, SWT
.BORDER
| SWT
.PASSWORD
);
349 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
353 /** Creates label and text. */
354 protected Text
createLT(Composite body
, String label
, String value
) {
355 FormToolkit toolkit
= getManagedForm().getToolkit();
356 Label lbl
= toolkit
.createLabel(body
, label
);
357 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
358 Text text
= toolkit
.createText(body
, value
, SWT
.BORDER
);
359 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
363 private class FormPartML
implements ModifyListener
{
364 private static final long serialVersionUID
= 6299808129505381333L;
365 private AbstractFormPart formPart
;
367 public FormPartML(AbstractFormPart generalPart
) {
368 this.formPart
= generalPart
;
371 public void modifyText(ModifyEvent e
) {
372 formPart
.markDirty();