<parameter name="param.commandId" value="transaction.commit" />
<visibleWhen>
<with variable="org.argeo.security.ui.admin.userTransactionState">
- <equals value="status.active" />
+ <equals value="status.active" />
</with>
</visibleWhen>
</command>
import org.osgi.service.useradmin.UserAdminEvent;
import org.osgi.service.useradmin.UserAdminListener;
-/** Centralize interaction with the UserAdmin in this bundle */
+/** Centralise interaction with the UserAdmin in this bundle */
public class UserAdminWrapper extends
org.argeo.cms.util.useradmin.UserAdminWrapper {
- // private Log log = LogFactory.getLog(UserAdminWrapper.class);
+
+ // First effort to simplify UX while managing users and groups
+ public final static boolean COMMIT_ON_SAVE = true;
// Registered listeners
List<UserAdminListener> listeners = new ArrayList<UserAdminListener>();
/**
- * Overwrite the normal begin transaction behaviour to also notify the UI.
- * Must be called from the UI Thread.
+ * Starts a transaction if necessary. Should always been called together
+ * with {@link UserAdminWrapper#commitOrNotifyTransactionStateChange()} once
+ * the security model changes have been performed.
*/
public UserTransaction beginTransactionIfNeeded() {
try {
UserTransaction userTransaction = getUserTransaction();
if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION) {
userTransaction.begin();
- UiAdminUtils.notifyTransactionStateChange(userTransaction);
+ // UiAdminUtils.notifyTransactionStateChange(userTransaction);
}
return userTransaction;
} catch (Exception e) {
}
}
+ /**
+ * Depending on the current application configuration, it will either commit
+ * the current transaction or throw a notification that the transaction
+ * state has changed (In the later case, it must be called from the UI
+ * thread).
+ */
+ public void commitOrNotifyTransactionStateChange() {
+ try {
+ UserTransaction userTransaction = getUserTransaction();
+ if (userTransaction.getStatus() == Status.STATUS_NO_TRANSACTION)
+ return;
+
+ if (UserAdminWrapper.COMMIT_ON_SAVE)
+ userTransaction.commit();
+ else
+ UiAdminUtils.notifyTransactionStateChange(userTransaction);
+ } catch (Exception e) {
+ throw new CmsException("Unable to clean transaction", e);
+ }
+ }
+
// TODO implement safer mechanism
public void addListener(UserAdminListener userAdminListener) {
if (!listeners.contains(userAdminListener))
listeners.add(userAdminListener);
}
- // Expose this?
public void removeListener(UserAdminListener userAdminListener) {
if (listeners.contains(userAdminListener))
listeners.remove(userAdminListener);
UserAdmin userAdmin = userAdminWrapper.getUserAdmin();
IWorkbenchPage iwp = HandlerUtil.getActiveWorkbenchWindow(event)
.getActivePage();
-
for (Group group : groups) {
String groupName = group.getName();
-
// TODO find a way to close the editor cleanly if opened. Cannot be
// done through the UserAdminListeners, it causes a
// java.util.ConcurrentModificationException because disposing the
IEditorPart part = iwp.findEditor(new UserEditorInput(groupName));
if (part != null)
iwp.closeEditor(part, false);
-
userAdmin.removeRole(groupName);
+ }
+ userAdminWrapper.commitOrNotifyTransactionStateChange();
+
+ // Update the view
+ for (Group group : groups) {
userAdminWrapper.notifyListeners(new UserAdminEvent(null,
UserAdminEvent.ROLE_REMOVED, group));
}
+
return null;
}
for (User user : users) {
String userName = user.getName();
-
// TODO find a way to close the editor cleanly if opened. Cannot be
// done through the UserAdminListeners, it causes a
// java.util.ConcurrentModificationException because disposing the
IEditorPart part = iwp.findEditor(new UserEditorInput(userName));
if (part != null)
iwp.closeEditor(part, false);
-
userAdmin.removeRole(userName);
+ }
+ userAdminWrapper.commitOrNotifyTransactionStateChange();
+
+ for (User user : users) {
userAdminWrapper.notifyListeners(new UserAdminEvent(null,
UserAdminEvent.ROLE_REMOVED, user));
}
String descStr = descriptionTxt.getText();
if (EclipseUiUtils.notEmpty(descStr))
props.put(LdifName.description.name(), descStr);
+ userAdminWrapper.commitOrNotifyTransactionStateChange();
userAdminWrapper.notifyListeners(new UserAdminEvent(null,
UserAdminEvent.ROLE_CREATED, group));
return true;
if (!canFinish())
return false;
String username = mainUserInfo.getUsername();
+ userAdminWrapper.beginTransactionIfNeeded();
try {
- userAdminWrapper.beginTransactionIfNeeded();
User user = (User) userAdminWrapper.getUserAdmin().createRole(
getDn(username), Role.USER);
char[] password = mainUserInfo.getPassword();
user.getCredentials().put(null, password);
-
+ userAdminWrapper.commitOrNotifyTransactionStateChange();
userAdminWrapper.notifyListeners(new UserAdminEvent(null,
UserAdminEvent.ROLE_CREATED, user));
return true;
try {
IWorkbenchPart iwp = HandlerUtil.getActiveWorkbenchWindow(event)
.getActivePage().getActivePart();
-
if (!(iwp instanceof IEditorPart))
return null;
IEditorPart editor = (IEditorPart) iwp;
UiAdminUtils.notifyTransactionStateChange(userTransaction);
// Try to remove invalid thread access errors when managing users.
// HandlerUtil.getActivePart(event).getSite().getShell().getDisplay()
- // .asyncExec(new Runnable() {
- // @Override
- // public void run() {
- // UiAdminUtils
- // .notifyTransactionStateChange(userTransaction);
- // }
- // });
+ // .asyncExec(new Runnable() {
+ // @Override
+ // public void run() {
+ // UiAdminUtils
+ // .notifyTransactionStateChange(userTransaction);
+ // }
+ // });
} catch (CmsException e) {
throw e;
import java.util.Iterator;
import java.util.List;
+import javax.transaction.UserTransaction;
+
+import org.argeo.cms.CmsException;
import org.argeo.cms.util.useradmin.UserAdminUtils;
import org.argeo.eclipse.ui.ColumnDefinition;
import org.argeo.eclipse.ui.EclipseUiUtils;
@Override
public void initialize(IManagedForm form) {
super.initialize(form);
- listener = editor.new MainInfoListener(parent.getDisplay(), this);
+ listener = editor.new MainInfoListener(parent.getDisplay(),
+ this);
userAdminWrapper.addListener(listener);
}
@SuppressWarnings("unchecked")
Iterator<User> it = ((IStructuredSelection) selection).iterator();
List<User> users = new ArrayList<User>();
- // StringBuilder builder = new StringBuilder();
while (it.hasNext()) {
User currUser = it.next();
- // String groupName = UserAdminUtils.getUsername(currGroup);
- // builder.append(groupName).append("; ");
users.add(currUser);
}
for (User user : users) {
group.removeMember(user);
}
+ userAdminWrapper.commitOrNotifyTransactionStateChange();
userAdminWrapper.notifyListeners(new UserAdminEvent(null,
UserAdminEvent.ROLE_CHANGED, group));
}
@Override
public void initialize(IManagedForm form) {
super.initialize(form);
- listener = editor.new GroupChangeListener(userViewer.getDisplay(), GroupMembersPart.this);
+ listener = editor.new GroupChangeListener(userViewer.getDisplay(),
+ GroupMembersPart.this);
userAdminWrapper.addListener(listener);
}
return;
}
userAdminWrapper.beginTransactionIfNeeded();
- // TODO implement the dirty state
myGroup.addMember(newGroup);
+ userAdminWrapper.commitOrNotifyTransactionStateChange();
userAdminWrapper.notifyListeners(new UserAdminEvent(null,
UserAdminEvent.ROLE_CHANGED, myGroup));
} else if (role.getType() == Role.USER) {
// TODO check if the group is already member of this group
- userAdminWrapper.beginTransactionIfNeeded();
+ UserTransaction transaction = userAdminWrapper
+ .beginTransactionIfNeeded();
User user = (User) role;
myGroup.addMember(user);
+ if (UserAdminWrapper.COMMIT_ON_SAVE)
+ try {
+ transaction.commit();
+ } catch (Exception e) {
+ throw new CmsException("Cannot commit transaction "
+ + "after user group membership update", e);
+ }
userAdminWrapper.notifyListeners(new UserAdminEvent(null,
UserAdminEvent.ROLE_CHANGED, myGroup));
}
import org.argeo.eclipse.ui.parts.LdifUsersTable;
import org.argeo.jcr.ArgeoNames;
import org.argeo.osgi.useradmin.LdifName;
-import org.argeo.security.ui.admin.internal.UiAdminUtils;
import org.argeo.security.ui.admin.internal.UserAdminWrapper;
import org.argeo.security.ui.admin.internal.providers.CommonNameLP;
import org.argeo.security.ui.admin.internal.providers.DomainNameLP;
@SuppressWarnings("unchecked")
protected void doUpdate() {
- UserTransaction userTransaction = userAdminWrapper
- .beginTransactionIfNeeded();
+ userAdminWrapper.beginTransactionIfNeeded();
try {
for (User user : usersToUpdate) {
// the char array is emptied after being used.
user.getCredentials().put(null, newPwd.clone());
}
- userTransaction.commit();
- UiAdminUtils.notifyTransactionStateChange(userTransaction);
+ userAdminWrapper.commitOrNotifyTransactionStateChange();
} catch (Exception e) {
- throw new CmsException(
- "Cannot perform batch update on users", e);
+ throw new CmsException("Cannot perform batch update on users",
+ e);
} finally {
UserTransaction ut = userAdminWrapper.getUserTransaction();
try {
roles = userAdminWrapper.getUserAdmin().getRoles(
builder.toString());
} catch (InvalidSyntaxException e) {
- throw new CmsException(
- "Unable to get roles with filter: " + filter, e);
+ throw new CmsException("Unable to get roles with filter: "
+ + filter, e);
}
List<User> users = new ArrayList<User>();
for (Role role : roles)
public void doSave(IProgressMonitor monitor) {
userAdminWrapper.beginTransactionIfNeeded();
commitPages(true);
+ userAdminWrapper.commitOrNotifyTransactionStateChange();
firePropertyChange(PROP_DIRTY);
userAdminWrapper.notifyListeners(new UserAdminEvent(null,
UserAdminEvent.ROLE_REMOVED, user));
|| !password2.getText().equals("")) {
if (password1.getText().equals(password2.getText())) {
char[] newPassword = password1.getText().toCharArray();
- userAdminWrapper.beginTransactionIfNeeded();
+ // userAdminWrapper.beginTransactionIfNeeded();
user.getCredentials().put(null, newPassword);
password1.setText("");
password2.setText("");
@SuppressWarnings("unchecked")
Iterator<Group> it = ((IStructuredSelection) selection).iterator();
List<Group> groups = new ArrayList<Group>();
- // StringBuilder builder = new StringBuilder();
while (it.hasNext()) {
Group currGroup = it.next();
- // String groupName = UserAdminUtils.getUsername(currGroup);
- // builder.append(groupName).append("; ");
groups.add(currGroup);
}
- // if (!MessageDialog.openQuestion(
- // HandlerUtil.getActiveShell(event),
- // "Re",
- // "Are you sure that you want to delete these users?\n"
- // + builder.substring(0, builder.length() - 2)))
- // return null;
-
userAdminWrapper.beginTransactionIfNeeded();
for (Group group : groups) {
group.removeMember(user);
- // sectionPart.refresh();
+ }
+ userAdminWrapper.commitOrNotifyTransactionStateChange();
+ for (Group group : groups) {
userAdminWrapper.notifyListeners(new UserAdminEvent(null,
UserAdminEvent.ROLE_CHANGED, group));
}
if (role.getType() == Role.GROUP) {
// TODO check if the user is already member of this group
- // Remove invalid thread access errors when managing users.
- // myUserAdminWrapper.beginTransactionIfNeeded();
- event.display.asyncExec(new Runnable() {
- @Override
- public void run() {
- myUserAdminWrapper.beginTransactionIfNeeded();
- }
- });
-
+ myUserAdminWrapper.beginTransactionIfNeeded();
Group group = (Group) role;
group.addMember(myUser);
+ userAdminWrapper.commitOrNotifyTransactionStateChange();
myUserAdminWrapper.notifyListeners(new UserAdminEvent(null,
UserAdminEvent.ROLE_CHANGED, group));
}
@Override
public String[] getProvidedSourceNames() {
- return new String[] { TRANSACTION_STATE };
+ return new String[] { TRANSACTION_STATE};
}
@Override
public Map<String, String> getCurrentState() {
Map<String, String> currentState = new HashMap<String, String>(1);
- currentState.put(TRANSACTION_STATE, getInternalCurrentState());
+ currentState.put(TRANSACTION_STATE, getInternalCurrentState());
return currentState;
}