*/
package org.argeo.security.ui.admin.editors;
+import java.util.ArrayList;
import java.util.Arrays;
-
-import javax.jcr.RepositoryException;
+import java.util.List;
import org.argeo.ArgeoException;
+import org.argeo.eclipse.ui.EclipseUiUtils;
import org.argeo.jcr.ArgeoNames;
+import org.argeo.security.ui.admin.internal.ColumnDefinition;
+import org.argeo.security.ui.admin.internal.CommonNameLP;
+import org.argeo.security.ui.admin.internal.RoleIconLP;
import org.argeo.security.ui.admin.internal.UserAdminConstants;
+import org.argeo.security.ui.admin.internal.UserAdminWrapper;
+import org.argeo.security.ui.admin.internal.UserNameLP;
+import org.argeo.security.ui.admin.internal.UserTableDefaultDClickListener;
+import org.argeo.security.ui.admin.internal.UserTableViewer;
+import org.eclipse.jface.viewers.TableViewer;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.viewers.ViewerDropAdapter;
import org.eclipse.swt.SWT;
+import org.eclipse.swt.dnd.DND;
+import org.eclipse.swt.dnd.DropTargetEvent;
+import org.eclipse.swt.dnd.TextTransfer;
+import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.swt.dnd.TransferData;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
+import org.osgi.service.useradmin.Group;
+import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.User;
+import org.osgi.service.useradmin.UserAdmin;
+import org.osgi.service.useradmin.UserAdminEvent;
/** Display/edit the properties common to all users */
public class UserMainPage extends FormPage implements ArgeoNames {
// private final static Log log = LogFactory.getLog(UserMainPage.class);
private final UserEditor editor;
+ private UserAdminWrapper userAdminWrapper;
+
private char[] newPassword;
- public UserMainPage(FormEditor editor) {
+ // Local configuration
+ private final int PRE_TITLE_INDENT = 10;
+
+ public UserMainPage(FormEditor editor, UserAdminWrapper userAdminWrapper) {
super(editor, ID, "Main");
this.editor = (UserEditor) editor;
+ this.userAdminWrapper = userAdminWrapper;
+ }
+
+ public String getNewPassword() {
+ if (newPassword != null)
+ return new String(newPassword);
+ else
+ return null;
+ }
+
+ public void resetNewPassword() {
+ if (newPassword != null)
+ Arrays.fill(newPassword, 'x');
+ newPassword = null;
}
protected void createFormContent(final IManagedForm mf) {
- try {
- ScrolledForm form = mf.getForm();
- refreshFormTitle(form);
- GridLayout mainLayout = new GridLayout(1, true);
- form.getBody().setLayout(mainLayout);
-
- createGeneralPart(form.getBody());
- // createPassworPart(form.getBody());
- } catch (RepositoryException e) {
- throw new ArgeoException("Cannot create form content", e);
- }
+ ScrolledForm form = mf.getForm();
+ // Form page main title
+ form.setText(editor.getProperty(UserAdminConstants.KEY_CN));
+
+ // Body
+ Composite body = form.getBody();
+ GridLayout mainLayout = new GridLayout(1, true);
+ body.setLayout(mainLayout);
+ appendOverviewPart(body);
+ appendPasswordPart(body);
+ appendMemberOfPart(body);
}
/** Creates the general section */
- protected void createGeneralPart(Composite parent)
- throws RepositoryException {
+ protected void appendOverviewPart(Composite parent) {
FormToolkit tk = getManagedForm().getToolkit();
- Section section = tk.createSection(parent, Section.TITLE_BAR);
- section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
- section.setText("General");
- Composite body = tk.createComposite(section, SWT.WRAP);
- section.setClient(body);
- GridLayout layout = new GridLayout(2, false);
- body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
- body.setLayout(layout);
+ Composite body = addSection(tk, parent, "Main information");
+ body.setLayout(new GridLayout(2, false));
+
+ Text distinguishedName = createLT(body, "User Name",
+ editor.getProperty(UserAdminConstants.KEY_UID));
+ distinguishedName.setEnabled(false);
final Text commonName = createLT(body, "Common Name",
editor.getProperty(UserAdminConstants.KEY_CN));
commonName.setEnabled(false);
- // final Text firstName = createLT(body, "First name",
- // getProperty(ARGEO_FIRST_NAME));
- // final Text lastName = createLT(body, "Last name",
- // getProperty(ARGEO_LAST_NAME));
+ final Text firstName = createLT(body, "First name",
+ editor.getProperty(UserAdminConstants.KEY_FIRSTNAME));
+
+ final Text lastName = createLT(body, "Last name",
+ editor.getProperty(UserAdminConstants.KEY_LASTNAME));
+
final Text email = createLT(body, "Email",
editor.getProperty(UserAdminConstants.KEY_MAIL));
- // final Text description = createLMT(body, "Description",
- // getProperty(Property.JCR_DESCRIPTION));
// create form part (controller)
- AbstractFormPart part = new SectionPart(section) {
+ AbstractFormPart part = new SectionPart((Section) body.getParent()) {
public void commit(boolean onSave) {
+ // TODO check changed ?
+ // TODO Sanity checks
+
+ editor.setProperty(UserAdminConstants.KEY_FIRSTNAME,
+ firstName.getText());
+ editor.setProperty(UserAdminConstants.KEY_LASTNAME,
+ lastName.getText());
+ editor.setProperty(UserAdminConstants.KEY_CN,
+ commonName.getText());
// TODO check mail validity
editor.setProperty(UserAdminConstants.KEY_MAIL, email.getText());
- // userProfile.getSession().getWorkspace().getVersionManager()
- // .checkout(userProfile.getPath());
- // userProfile.setProperty(Property.JCR_TITLE,
- // commonName.getText());
- // userProfile.setProperty(ARGEO_FIRST_NAME,
- // firstName.getText());
- // userProfile
- // .setProperty(ARGEO_LAST_NAME, lastName.getText());
- // userProfile.setProperty(ARGEO_PRIMARY_EMAIL,
+ // Enable common name ?
+ // editor.setProperty(UserAdminConstants.KEY_CN,
// email.getText());
- // userProfile.setProperty(Property.JCR_DESCRIPTION,
- // description.getText());
- // userProfile.getSession().save();
- // userProfile.getSession().getWorkspace().getVersionManager()
- // .checkin(userProfile.getPath());
super.commit(onSave);
}
};
- // if (username != null)
- // username.addModifyListener(new FormPartML(part));
- // commonName.addModifyListener(new FormPartML(part));
- // firstName.addModifyListener(new FormPartML(part));
- // lastName.addModifyListener(new FormPartML(part));
+ ModifyListener cnML = new ModifyListener() {
+ private static final long serialVersionUID = 4298649222869835486L;
+
+ @Override
+ public void modifyText(ModifyEvent event) {
+ String first = firstName.getText();
+ String last = lastName.getText();
+ String cn = first.trim() + " " + last.trim() + " ";
+ cn = cn.trim();
+ commonName.setText(cn);
+ getManagedForm().getForm().setText(cn);
+ editor.updateEditorTitle(cn);
+ }
+ };
+ firstName.addModifyListener(cnML);
+ lastName.addModifyListener(cnML);
+ firstName.addModifyListener(new FormPartML(part));
+ lastName.addModifyListener(new FormPartML(part));
email.addModifyListener(new FormPartML(part));
getManagedForm().addPart(part);
}
- private void refreshFormTitle(ScrolledForm form) throws RepositoryException {
- // form.setText(getProperty(Property.JCR_TITLE)
- // + (userProfile.getProperty(ARGEO_ENABLED).getBoolean() ? ""
- // : " [DISABLED]"));
+ /** Creates the password section */
+ protected void appendPasswordPart(Composite parent) {
+ FormToolkit tk = getManagedForm().getToolkit();
+ Composite body = addSection(tk, parent, "Password");
+
+ // Section section = tk.createSection(parent, Section.TITLE_BAR);
+ // section.setLayoutData(EclipseUiUtils.fillWidth());
+ // section.setText("Password");
+ // Composite body = tk.createComposite(section, SWT.NO_FOCUS);
+ // section.setClient(body);
+ // body.setLayoutData(EclipseUiUtils.fillWidth());
+
+ body.setLayout(new GridLayout(2, false));
+ // add widgets (view)
+ final Text password1 = createLP(body, "New password", "");
+ final Text password2 = createLP(body, "Repeat password", "");
+ // create form part (controller)
+ AbstractFormPart part = new SectionPart((Section) body.getParent()) {
+
+ public void commit(boolean onSave) {
+ if (!password1.getText().equals("")
+ || !password2.getText().equals("")) {
+ if (password1.getText().equals(password2.getText())) {
+ newPassword = password1.getText().toCharArray();
+ // TODO real set password
+ password1.setText("");
+ password2.setText("");
+ super.commit(onSave);
+ } else {
+ password1.setText("");
+ password2.setText("");
+ throw new ArgeoException("Passwords are not equals");
+ }
+ }
+ }
+
+ };
+ password1.addModifyListener(new FormPartML(part));
+ password2.addModifyListener(new FormPartML(part));
+ getManagedForm().addPart(part);
}
- // /** @return the property, or the empty string if not set */
- // protected String getProperty(String name) throws RepositoryException {
- // return userProfile.hasProperty(name) ? userProfile.getProperty(name)
- // .getString() : "";
- // }
+ private UserTableViewer userTableViewerCmp;
+ private TableViewer userViewer;
- /** Creates the password section */
- // protected void createPassworPart(Composite parent) {
- // FormToolkit tk = getManagedForm().getToolkit();
- // Section section = tk.createSection(parent, Section.TITLE_BAR);
- // section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
- // section.setText("Password");
- //
- // Composite body = tk.createComposite(section, SWT.WRAP);
- // section.setClient(body);
- // GridLayout layout = new GridLayout(2, false);
- // body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
- // body.setLayout(layout);
- //
- // // add widgets (view)
- // final Text password1 = createLP(body, "New password", "");
- // final Text password2 = createLP(body, "Repeat password", "");
- // // create form part (controller)
- // AbstractFormPart part = new SectionPart(section) {
- //
- // public void commit(boolean onSave) {
- // if (!password1.getText().equals("")
- // || !password2.getText().equals("")) {
- // if (password1.getText().equals(password2.getText())) {
- // newPassword = password1.getText().toCharArray();
- // password1.setText("");
- // password2.setText("");
- // super.commit(onSave);
- // } else {
- // password1.setText("");
- // password2.setText("");
- // throw new ArgeoException("Passwords are not equals");
- // }
- // }
- // }
- //
- // };
- // password1.addModifyListener(new FormPartML(part));
- // password2.addModifyListener(new FormPartML(part));
- // getManagedForm().addPart(part);
- // }
+ private void appendMemberOfPart(Composite parent) {
+ FormToolkit tk = getManagedForm().getToolkit();
+ Composite body = addSection(tk, parent, "Roles");
+ body.setLayout(EclipseUiUtils.noSpaceGridLayout());
- /** Creates label and text. */
- protected Text createLT(Composite body, String label, String value) {
- FormToolkit toolkit = getManagedForm().getToolkit();
- Label lbl = toolkit.createLabel(body, label);
- lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
- Text text = toolkit.createText(body, value, SWT.BORDER);
- text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
- return text;
+ // Define the displayed columns
+ List<ColumnDefinition> columnDefs = new ArrayList<ColumnDefinition>();
+ columnDefs.add(new ColumnDefinition(new RoleIconLP(), "", 0, 24));
+ columnDefs.add(new ColumnDefinition(new UserNameLP(),
+ "Distinguished Name", 300));
+ columnDefs.add(new ColumnDefinition(new CommonNameLP(), "Common Name",
+ 150));
+
+ // Create and configure the table
+ userTableViewerCmp = new MyUserTableViewer(body, SWT.MULTI
+ | SWT.H_SCROLL | SWT.V_SCROLL, userAdminWrapper.getUserAdmin());
+
+ userTableViewerCmp.setColumnDefinitions(columnDefs);
+ userTableViewerCmp.populate(true, false);
+ GridData gd = EclipseUiUtils.fillAll();
+ gd.heightHint = 300;
+ userTableViewerCmp.setLayoutData(gd);
+
+ // Links
+ userViewer = userTableViewerCmp.getTableViewer();
+ userViewer.addDoubleClickListener(new UserTableDefaultDClickListener());
+ // Really?
+ userTableViewerCmp.refresh();
+
+ // Drag and drop
+ int operations = DND.DROP_COPY | DND.DROP_MOVE;
+ Transfer[] tt = new Transfer[] { TextTransfer.getInstance() };
+ userViewer.addDropSupport(operations, tt, new GroupDropListener(
+ userViewer, userAdminWrapper.getUserAdmin(), editor.getDisplayedUser()));
+
+ }
+
+ private class MyUserTableViewer extends UserTableViewer {
+ private static final long serialVersionUID = 8467999509931900367L;
+
+ public MyUserTableViewer(Composite parent, int style,
+ UserAdmin userAdmin) {
+ super(parent, style, userAdmin, true);
+ }
+
+ @Override
+ protected List<User> listFilteredElements(String filter) {
+ return (List<User>) editor.getFlatGroups(null);
+ }
+ }
+
+ /**
+ * Defines this table as being a potential target to add group membership
+ * (roles) to this user
+ */
+ private class GroupDropListener extends ViewerDropAdapter {
+ private static final long serialVersionUID = 2893468717831451621L;
+
+ private final UserAdmin myUserAdmin;
+ private final User myUser;
+
+ public GroupDropListener(Viewer viewer, UserAdmin userAdmin, User user) {
+ super(viewer);
+ this.myUserAdmin = userAdmin;
+ this.myUser = user;
+ }
+
+ @Override
+ public boolean validateDrop(Object target, int operation,
+ TransferData transferType) {
+ // Target is always OK in a list only view
+ // TODO check if not a string
+ boolean validDrop = true;
+ return validDrop;
+ }
+
+ @Override
+ public void drop(DropTargetEvent event) {
+ String name = (String) event.data;
+ Role role = myUserAdmin.getRole(name);
+ // TODO this check should be done before.
+ if (role.getType() == Role.GROUP) {
+ // TODO check if the user is already member of this group
+ userAdminWrapper.beginTransactionIfNeeded();
+ Group group = (Group) role;
+ group.addMember(myUser);
+ userAdminWrapper.notifyListeners(new UserAdminEvent(null,
+ UserAdminEvent.ROLE_CHANGED, group));
+ }
+ super.drop(event);
+ }
+
+ @Override
+ public boolean performDrop(Object data) {
+ userTableViewerCmp.refresh();
+ return true;
+ }
+ }
+
+ // LOCAL HELPERS
+ /** Appends a section with a title */
+ private Composite addSection(FormToolkit tk, Composite parent, String title) {
+ Section section = tk.createSection(parent, Section.TITLE_BAR);
+ GridData gd = EclipseUiUtils.fillWidth();
+ gd.verticalAlignment = PRE_TITLE_INDENT;
+ section.setLayoutData(gd);
+ section.setText(title);
+ Composite body = tk.createComposite(section, SWT.WRAP);
+ body.setLayoutData(EclipseUiUtils.fillAll());
+ section.setClient(body);
+ return body;
}
/** Creates label and multiline text. */
return text;
}
+ /** Creates label and text. */
+ protected Text createLT(Composite body, String label, String value) {
+ FormToolkit toolkit = getManagedForm().getToolkit();
+ Label lbl = toolkit.createLabel(body, label);
+ lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
+ Text text = toolkit.createText(body, value, SWT.BORDER);
+ text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+ return text;
+ }
+
private class FormPartML implements ModifyListener {
private static final long serialVersionUID = 6299808129505381333L;
private AbstractFormPart formPart;
public void modifyText(ModifyEvent e) {
formPart.markDirty();
}
-
- }
-
- public String getNewPassword() {
- if (newPassword != null)
- return new String(newPassword);
- else
- return null;
- }
-
- public void resetNewPassword() {
- if (newPassword != null)
- Arrays.fill(newPassword, 'x');
- newPassword = null;
}
}
\ No newline at end of file