Commit 8dc900c1 authored by Romain Bioteau's avatar Romain Bioteau Committed by GitHub
Browse files

fix(organization) import robustness (#2496)

* handle file with missing main section

Closes STUDIO-3696
parent 29fade03
...@@ -115,7 +115,20 @@ public class OrganizationFileStore extends EMFFileStore implements IDeployable, ...@@ -115,7 +115,20 @@ public class OrganizationFileStore extends EMFFileStore implements IDeployable,
final Resource emfResource = getEMFResource(); final Resource emfResource = getEMFResource();
emfResource.getContents().clear(); emfResource.getContents().clear();
final DocumentRoot root = OrganizationFactory.eINSTANCE.createDocumentRoot(); final DocumentRoot root = OrganizationFactory.eINSTANCE.createDocumentRoot();
root.setOrganization((Organization) EcoreUtil.copy((EObject) content)); Organization organization = (Organization) EcoreUtil.copy((EObject) content);
if(organization.getUsers() == null) {
organization.setUsers(OrganizationFactory.eINSTANCE.createUsers());
}
if(organization.getGroups() == null) {
organization.setGroups(OrganizationFactory.eINSTANCE.createGroups());
}
if(organization.getRoles() == null) {
organization.setRoles(OrganizationFactory.eINSTANCE.createRoles());
}
if(organization.getMemberships() == null) {
organization.setMemberships(OrganizationFactory.eINSTANCE.createMemberships());
}
root.setOrganization(organization);
emfResource.getContents().add(root); emfResource.getContents().add(root);
try { try {
final Map<Object, Object> options = new HashMap<>(); final Map<Object, Object> options = new HashMap<>();
......
...@@ -111,7 +111,6 @@ public class ManageOrganizationWizard extends Wizard { ...@@ -111,7 +111,6 @@ public class ManageOrganizationWizard extends Wizard {
addActiveOrganizationAdapter(copy); addActiveOrganizationAdapter(copy);
} }
organizationsWorkingCopy.add(copy); organizationsWorkingCopy.add(copy);
} }
} }
......
...@@ -21,10 +21,13 @@ import java.util.stream.Collectors; ...@@ -21,10 +21,13 @@ import java.util.stream.Collectors;
import org.bonitasoft.studio.actors.ActorsPlugin; import org.bonitasoft.studio.actors.ActorsPlugin;
import org.bonitasoft.studio.actors.i18n.Messages; import org.bonitasoft.studio.actors.i18n.Messages;
import org.bonitasoft.studio.actors.model.organization.Group; import org.bonitasoft.studio.actors.model.organization.Group;
import org.bonitasoft.studio.actors.model.organization.Groups;
import org.bonitasoft.studio.actors.model.organization.Membership; import org.bonitasoft.studio.actors.model.organization.Membership;
import org.bonitasoft.studio.actors.model.organization.Organization; import org.bonitasoft.studio.actors.model.organization.Organization;
import org.bonitasoft.studio.actors.model.organization.Role; import org.bonitasoft.studio.actors.model.organization.Role;
import org.bonitasoft.studio.actors.model.organization.Roles;
import org.bonitasoft.studio.actors.model.organization.User; import org.bonitasoft.studio.actors.model.organization.User;
import org.bonitasoft.studio.actors.model.organization.Users;
import org.bonitasoft.studio.actors.ui.wizard.page.GroupContentProvider; import org.bonitasoft.studio.actors.ui.wizard.page.GroupContentProvider;
import org.eclipse.core.databinding.validation.IValidator; import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus; import org.eclipse.core.databinding.validation.ValidationStatus;
...@@ -35,87 +38,96 @@ import org.eclipse.core.runtime.Status; ...@@ -35,87 +38,96 @@ import org.eclipse.core.runtime.Status;
/** /**
* @author Romain Bioteau * @author Romain Bioteau
*/ */
public class OrganizationValidator implements IValidator, ValidatorConstants { public class OrganizationValidator implements IValidator<Organization>, ValidatorConstants {
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object) * @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object)
*/ */
@Override @Override
public IStatus validate(final Object input) { public IStatus validate(Organization organization) {
MultiStatus validationStatus = new MultiStatus(ActorsPlugin.PLUGIN_ID, 0, null, null); MultiStatus validationStatus = new MultiStatus(ActorsPlugin.PLUGIN_ID, 0, null, null);
final Organization organization = (Organization) input; Users users = organization.getUsers();
for (final User u : organization.getUsers().getUser()) { if (users != null) {
if (u.getUserName() == null || u.getUserName().isEmpty()) { for (final User u : users.getUser()) {
validationStatus.add(ValidationStatus.error(Messages.userNameMissing)); if (u.getUserName() == null || u.getUserName().isEmpty()) {
} validationStatus.add(ValidationStatus.error(Messages.userNameMissing));
if (u.getPassword() == null || u.getPassword().getValue() == null || u.getPassword().getValue().isEmpty()) { }
validationStatus.add(ValidationStatus.error(Messages.bind(Messages.userPasswordMissing, u.getUserName()))); if (u.getPassword() == null || u.getPassword().getValue() == null
} || u.getPassword().getValue().isEmpty()) {
validationStatus
.add(ValidationStatus.error(Messages.bind(Messages.userPasswordMissing, u.getUserName())));
}
if (u.getManager() != null && !u.getManager().isEmpty()) { if (u.getManager() != null && !u.getManager().isEmpty()) {
final IStatus status = checkManagerCycles(organization, u); final IStatus status = checkManagerCycles(organization, u);
if (!status.isOK()) { if (!status.isOK()) {
validationStatus.add(status); validationStatus.add(status);
}
} }
}
boolean membershipFound = false; boolean membershipFound = false;
for (final Membership membership : organization.getMemberships().getMembership()) { for (final Membership membership : organization.getMemberships().getMembership()) {
final String userName = membership.getUserName(); final String userName = membership.getUserName();
if (userName != null) { if (userName != null) {
if (userName.equals(u.getUserName())) { if (userName.equals(u.getUserName())) {
membershipFound = true; membershipFound = true;
final String groupName = membership.getGroupName(); final String groupName = membership.getGroupName();
if (groupName == null) { if (groupName == null) {
validationStatus validationStatus
.add(ValidationStatus.error(Messages.bind(Messages.missingGroup, u.getUserName()))); .add(ValidationStatus
} .error(Messages.bind(Messages.missingGroup, u.getUserName())));
final String parentPath = membership.getGroupParentPath(); }
String groupPath = null; final String parentPath = membership.getGroupParentPath();
if (parentPath == null) { String groupPath = null;
groupPath = GroupContentProvider.GROUP_SEPARATOR + groupName; if (parentPath == null) {
} else { groupPath = GroupContentProvider.GROUP_SEPARATOR + groupName;
groupPath = parentPath + GroupContentProvider.GROUP_SEPARATOR + groupName; } else {
} groupPath = parentPath + GroupContentProvider.GROUP_SEPARATOR + groupName;
final IStatus groupStatus = validateGroupExists(organization, groupPath, membership); }
if (groupStatus.getSeverity() != IStatus.OK) { final IStatus groupStatus = validateGroupExists(organization, groupPath, membership);
validationStatus.add(groupStatus); if (groupStatus.getSeverity() != IStatus.OK) {
} validationStatus.add(groupStatus);
}
final String roleName = membership.getRoleName(); final String roleName = membership.getRoleName();
if (roleName == null) { if (roleName == null) {
validationStatus validationStatus
.add(ValidationStatus.error(Messages.bind(Messages.missingRole, u.getUserName()))); .add(ValidationStatus
} .error(Messages.bind(Messages.missingRole, u.getUserName())));
final IStatus roleStatus = validateRoleExists(organization, roleName, membership); }
if (roleStatus.getSeverity() != IStatus.OK) { final IStatus roleStatus = validateRoleExists(organization, roleName, membership);
validationStatus.add(roleStatus); if (roleStatus.getSeverity() != IStatus.OK) {
validationStatus.add(roleStatus);
}
} }
} }
} }
} if (!membershipFound) {
if (!membershipFound) { validationStatus
validationStatus .add(ValidationStatus
.add(ValidationStatus.error(Messages.bind(Messages.missingMembershipForUser, u.getUserName()))); .error(Messages.bind(Messages.missingMembershipForUser, u.getUserName())));
}
} }
} }
List<String> invalidGroups = organization.getGroups().getGroup().stream() Groups groups = organization.getGroups();
.map(Group::getName) if(groups != null) {
.filter(groupName -> groupName.contains("/")) List<String> invalidGroups = groups.getGroup().stream()
.collect(Collectors.toList()); .map(Group::getName)
if (!invalidGroups.isEmpty()) { .filter(groupName -> groupName.contains("/"))
validationStatus.add(ValidationStatus.error(String.format(Messages.invalidCharInGroupName, .collect(Collectors.toList());
invalidGroups.stream().reduce((group1, group2) -> group1 + "\n" + group2).get()))); if (!invalidGroups.isEmpty()) {
validationStatus.add(ValidationStatus.error(String.format(Messages.invalidCharInGroupName,
invalidGroups.stream().reduce((group1, group2) -> group1 + "\n" + group2).get())));
}
} }
return validationStatus; return validationStatus;
} }
private IStatus checkManagerCycles(final Organization organization, final User u) { private IStatus checkManagerCycles(final Organization organization, final User u) {
String managerUsername = u.getManager(); String managerUsername = u.getManager();
final List<String> managers = new ArrayList<String>(); final List<String> managers = new ArrayList<>();
managers.add(u.getUserName()); managers.add(u.getUserName());
managers.add(managerUsername); managers.add(managerUsername);
while (managerUsername != null) { while (managerUsername != null) {
...@@ -135,31 +147,43 @@ public class OrganizationValidator implements IValidator, ValidatorConstants { ...@@ -135,31 +147,43 @@ public class OrganizationValidator implements IValidator, ValidatorConstants {
} }
private String getManagerOf(final Organization organization, final String managerUsername) { private String getManagerOf(final Organization organization, final String managerUsername) {
for (final User u : organization.getUsers().getUser()) { Users users = organization.getUsers();
if (managerUsername.equals(u.getUserName())) { if (users != null) {
return u.getManager(); for (final User u : users.getUser()) {
if (managerUsername.equals(u.getUserName())) {
return u.getManager();
}
} }
} }
return null; return null;
} }
private IStatus validateRoleExists(final Organization organization, final String roleName, final Membership membership) { private IStatus validateRoleExists(final Organization organization, final String roleName,
for (final Role role : organization.getRoles().getRole()) { final Membership membership) {
if (role.getName().equals(roleName)) { Roles roles = organization.getRoles();
return ValidationStatus.ok(); if (roles != null) {
for (final Role role : roles.getRole()) {
if (role.getName().equals(roleName)) {
return ValidationStatus.ok();
}
} }
} }
return ValidationStatus.error(Messages.bind(Messages.missingRoleInMembership, roleName, membership.getUserName())); return ValidationStatus
.error(Messages.bind(Messages.missingRoleInMembership, roleName, membership.getUserName()));
} }
private IStatus validateGroupExists(final Organization organization, final String groupPath, private IStatus validateGroupExists(final Organization organization, final String groupPath,
final Membership membership) { final Membership membership) {
for (final Group group : organization.getGroups().getGroup()) { Groups groups = organization.getGroups();
if (GroupContentProvider.getGroupPath(group).equals(groupPath)) { if (groups != null) {
return ValidationStatus.ok(); for (final Group group : groups.getGroup()) {
if (GroupContentProvider.getGroupPath(group).equals(groupPath)) {
return ValidationStatus.ok();
}
} }
} }
return ValidationStatus.error(Messages.bind(Messages.missingGroupInMembership, groupPath, membership.getUserName())); return ValidationStatus
.error(Messages.bind(Messages.missingGroupInMembership, groupPath, membership.getUserName()));
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment