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
.List
;
21 import javax
.jcr
.RepositoryException
;
23 import org
.argeo
.ArgeoException
;
24 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
25 import org
.argeo
.jcr
.ArgeoNames
;
26 import org
.argeo
.security
.ui
.admin
.internal
.ColumnDefinition
;
27 import org
.argeo
.security
.ui
.admin
.internal
.CommonNameLP
;
28 import org
.argeo
.security
.ui
.admin
.internal
.MailLP
;
29 import org
.argeo
.security
.ui
.admin
.internal
.RoleIconLP
;
30 import org
.argeo
.security
.ui
.admin
.internal
.UserAdminConstants
;
31 import org
.argeo
.security
.ui
.admin
.internal
.UserNameLP
;
32 import org
.argeo
.security
.ui
.admin
.internal
.UserTableDefaultDClickListener
;
33 import org
.argeo
.security
.ui
.admin
.internal
.UserTableViewer
;
34 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
35 import org
.eclipse
.jface
.viewers
.TableViewer
;
36 import org
.eclipse
.jface
.viewers
.Viewer
;
37 import org
.eclipse
.jface
.viewers
.ViewerDropAdapter
;
38 import org
.eclipse
.swt
.SWT
;
39 import org
.eclipse
.swt
.dnd
.DND
;
40 import org
.eclipse
.swt
.dnd
.DropTargetEvent
;
41 import org
.eclipse
.swt
.dnd
.TextTransfer
;
42 import org
.eclipse
.swt
.dnd
.Transfer
;
43 import org
.eclipse
.swt
.dnd
.TransferData
;
44 import org
.eclipse
.swt
.layout
.GridData
;
45 import org
.eclipse
.swt
.layout
.GridLayout
;
46 import org
.eclipse
.swt
.widgets
.Composite
;
47 import org
.eclipse
.swt
.widgets
.Label
;
48 import org
.eclipse
.swt
.widgets
.Shell
;
49 import org
.eclipse
.swt
.widgets
.Text
;
50 import org
.eclipse
.ui
.forms
.AbstractFormPart
;
51 import org
.eclipse
.ui
.forms
.IManagedForm
;
52 import org
.eclipse
.ui
.forms
.SectionPart
;
53 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
54 import org
.eclipse
.ui
.forms
.editor
.FormPage
;
55 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
56 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
57 import org
.eclipse
.ui
.forms
.widgets
.Section
;
58 import org
.osgi
.service
.useradmin
.Group
;
59 import org
.osgi
.service
.useradmin
.Role
;
60 import org
.osgi
.service
.useradmin
.User
;
61 import org
.osgi
.service
.useradmin
.UserAdmin
;
63 /** Display/edit main properties of a given group */
64 public class GroupMainPage
extends FormPage
implements ArgeoNames
{
65 final static String ID
= "GroupEditor.mainPage";
67 private final UserEditor editor
;
68 private UserAdmin userAdmin
;
70 // Local configuration
71 private final int PRE_TITLE_INDENT
= 10;
73 public GroupMainPage(FormEditor editor
, UserAdmin userAdmin
) {
74 super(editor
, ID
, "Main");
75 this.editor
= (UserEditor
) editor
;
76 this.userAdmin
= userAdmin
;
79 protected void createFormContent(final IManagedForm mf
) {
81 ScrolledForm form
= mf
.getForm();
85 Composite body
= form
.getBody();
86 GridLayout mainLayout
= new GridLayout();
87 body
.setLayout(mainLayout
);
88 appendOverviewPart(body
);
89 appendMembersPart(body
);
90 } catch (RepositoryException e
) {
91 throw new ArgeoException("Cannot create form content", e
);
95 /** Creates the general section */
96 protected void appendOverviewPart(Composite parent
) {
97 FormToolkit tk
= getManagedForm().getToolkit();
98 Composite body
= addSection(tk
, parent
, "Main information");
99 GridLayout layout
= new GridLayout(2, false);
100 body
.setLayout(layout
);
102 Text distinguishedName
= createLT(body
, "Group Name",
103 editor
.getProperty(UserAdminConstants
.KEY_UID
));
104 distinguishedName
.setEnabled(false);
106 final Text commonName
= createLT(body
, "Common Name",
107 editor
.getProperty(UserAdminConstants
.KEY_CN
));
108 commonName
.setEnabled(false);
110 // create form part (controller)
111 AbstractFormPart part
= new SectionPart((Section
) body
.getParent()) {
112 public void commit(boolean onSave
) {
113 super.commit(onSave
);
116 getManagedForm().addPart(part
);
119 /** Filtered table with members. Has drag & drop ability */
120 protected void appendMembersPart(Composite parent
)
121 throws RepositoryException
{
123 FormToolkit tk
= getManagedForm().getToolkit();
124 Section section
= tk
.createSection(parent
, Section
.TITLE_BAR
);
125 section
.setLayoutData(EclipseUiUtils
.fillAll());
126 section
.setText("Members of group "
127 + editor
.getProperty(UserAdminConstants
.KEY_CN
));
129 // Composite body = tk.createComposite(section, SWT.NONE);
130 Composite body
= new Composite(section
, SWT
.NO_FOCUS
);
131 section
.setClient(body
);
132 body
.setLayoutData(EclipseUiUtils
.fillAll());
134 createMemberPart(body
);
136 // create form part (controller)
137 AbstractFormPart part
= new SectionPart(section
) {
138 public void commit(boolean onSave
) {
139 super.commit(onSave
);
143 getManagedForm().addPart(part
);
147 private UserTableViewer userTableViewerCmp
;
148 private TableViewer userViewer
;
149 private List
<ColumnDefinition
> columnDefs
= new ArrayList
<ColumnDefinition
>();
151 public void createMemberPart(Composite parent
) {
152 parent
.setLayout(EclipseUiUtils
.noSpaceGridLayout());
153 // Define the displayed columns
154 columnDefs
.add(new ColumnDefinition(new RoleIconLP(), "", 0, 24));
155 columnDefs
.add(new ColumnDefinition(new UserNameLP(),
156 "Distinguished Name", 240));
157 columnDefs
.add(new ColumnDefinition(new CommonNameLP(), "Common Name",
159 columnDefs
.add(new ColumnDefinition(new MailLP(), "Primary Mail", 150));
161 // Create and configure the table
162 userTableViewerCmp
= new MyUserTableViewer(parent
, SWT
.MULTI
163 | SWT
.H_SCROLL
| SWT
.V_SCROLL
, userAdmin
);
165 userTableViewerCmp
.setColumnDefinitions(columnDefs
);
166 userTableViewerCmp
.populate(true, false);
167 // userTableViewerCmp.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
169 userTableViewerCmp
.setLayoutData(EclipseUiUtils
.fillAll());
172 userViewer
= userTableViewerCmp
.getTableViewer();
173 userViewer
.addDoubleClickListener(new UserTableDefaultDClickListener());
175 userTableViewerCmp
.refresh();
178 int operations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
179 Transfer
[] tt
= new Transfer
[] { TextTransfer
.getInstance() };
180 userViewer
.addDropSupport(operations
, tt
, new GroupDropListener(
181 userViewer
, userAdmin
, (Group
) editor
.getDisplayedUser()));
184 private class MyUserTableViewer
extends UserTableViewer
{
185 private static final long serialVersionUID
= 8467999509931900367L;
187 public MyUserTableViewer(Composite parent
, int style
,
188 UserAdmin userAdmin
) {
189 super(parent
, style
, userAdmin
, true);
193 protected List
<User
> listFilteredElements(String filter
) {
194 Group group
= (Group
) editor
.getDisplayedUser();
195 Role
[] roles
= group
.getMembers();
196 List
<User
> users
= new ArrayList
<User
>();
197 for (Role role
: roles
)
198 // if (role.getType() == Role.GROUP)
199 users
.add((User
) role
);
204 private void refreshFormTitle() throws RepositoryException
{
205 getManagedForm().getForm().setText(
206 editor
.getProperty(UserAdminConstants
.KEY_CN
));
210 * Defines this table as being a potential target to add group membership
211 * (roles) to this user
213 private class GroupDropListener
extends ViewerDropAdapter
{
214 private static final long serialVersionUID
= 2893468717831451621L;
216 private final UserAdmin myUserAdmin
;
217 private final Group myGroup
;
219 public GroupDropListener(Viewer viewer
, UserAdmin userAdmin
, Group group
) {
221 this.myUserAdmin
= userAdmin
;
222 this.myGroup
= group
;
226 public boolean validateDrop(Object target
, int operation
,
227 TransferData transferType
) {
228 // Target is always OK in a list only view
229 // TODO check if not a string
230 boolean validDrop
= true;
235 public void drop(DropTargetEvent event
) {
236 String newUserName
= (String
) event
.data
;
237 Role role
= myUserAdmin
.getRole(newUserName
);
238 // TODO this check should be done before.
239 if (role
.getType() == Role
.USER
) {
240 // TODO check if the user is already member of this group
241 // we expect here that there is already a begun transaction
242 // TODO implement the dirty state
243 editor
.beginTransactionIfNeeded();
244 User user
= (User
) role
;
245 myGroup
.addMember(user
);
246 } else if (role
.getType() == Role
.GROUP
) {
247 Group newGroup
= (Group
) role
;
249 Shell shell
= getViewer().getControl().getShell();
251 if (myGroup
== newGroup
) { // Equality
253 MessageDialog
.openError(shell
, "Forbidden addition ",
254 "A group cannot be a member of itself.");
259 String myName
= myGroup
.getName();
260 List
<User
> myMemberships
= editor
.getFlatGroups(myGroup
);
261 if (myMemberships
.contains(newGroup
)) {
262 MessageDialog
.openError(shell
, "Forbidden addition: cycle",
263 "Cannot add " + newUserName
+ " to group " + myName
264 + ". This would create a cycle");
269 List
<User
> newGroupMemberships
= editor
.getFlatGroups(newGroup
);
270 if (newGroupMemberships
.contains(myGroup
)) {
271 MessageDialog
.openError(shell
, "Forbidden addition",
272 "Cannot add " + newUserName
+ " to group " + myName
273 + ", this membership already exists");
277 editor
.beginTransactionIfNeeded();
278 // TODO implement the dirty state
279 myGroup
.addMember(newGroup
);
285 public boolean performDrop(Object data
) {
286 userTableViewerCmp
.refresh();
292 private Composite
addSection(FormToolkit tk
, Composite parent
, String title
) {
293 Section section
= tk
.createSection(parent
, Section
.TITLE_BAR
);
294 GridData gd
= EclipseUiUtils
.fillWidth();
295 gd
.verticalAlignment
= PRE_TITLE_INDENT
;
296 section
.setLayoutData(gd
);
297 section
.setText(title
);
298 Composite body
= tk
.createComposite(section
, SWT
.WRAP
);
299 body
.setLayoutData(EclipseUiUtils
.fillAll());
300 section
.setClient(body
);
304 /** Creates label and text. */
305 protected Text
createLT(Composite body
, String label
, String value
) {
306 FormToolkit toolkit
= getManagedForm().getToolkit();
307 Label lbl
= toolkit
.createLabel(body
, label
);
308 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
309 Text text
= toolkit
.createText(body
, value
, SWT
.BORDER
);
310 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));