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

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,
final Resource emfResource = getEMFResource();
emfResource.getContents().clear();
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);
try {
final Map<Object, Object> options = new HashMap<>();
......
......@@ -111,7 +111,6 @@ public class ManageOrganizationWizard extends Wizard {
addActiveOrganizationAdapter(copy);
}
organizationsWorkingCopy.add(copy);
}
}
......
......@@ -21,10 +21,13 @@ import java.util.stream.Collectors;
import org.bonitasoft.studio.actors.ActorsPlugin;
import org.bonitasoft.studio.actors.i18n.Messages;
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.Organization;
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.Users;
import org.bonitasoft.studio.actors.ui.wizard.page.GroupContentProvider;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
......@@ -35,87 +38,96 @@ import org.eclipse.core.runtime.Status;
/**
* @author Romain Bioteau
*/
public class OrganizationValidator implements IValidator, ValidatorConstants {
public class OrganizationValidator implements IValidator<Organization>, ValidatorConstants {
/*
* (non-Javadoc)
* @see org.eclipse.core.databinding.validation.IValidator#validate(java.lang.Object)
*/
@Override
public IStatus validate(final Object input) {
public IStatus validate(Organization organization) {
MultiStatus validationStatus = new MultiStatus(ActorsPlugin.PLUGIN_ID, 0, null, null);
final Organization organization = (Organization) input;
for (final User u : organization.getUsers().getUser()) {
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())));
}
Users users = organization.getUsers();
if (users != null) {
for (final User u : users.getUser()) {
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.getManager() != null && !u.getManager().isEmpty()) {
final IStatus status = checkManagerCycles(organization, u);
if (!status.isOK()) {
validationStatus.add(status);
if (u.getManager() != null && !u.getManager().isEmpty()) {
final IStatus status = checkManagerCycles(organization, u);
if (!status.isOK()) {
validationStatus.add(status);
}
}
}
boolean membershipFound = false;
for (final Membership membership : organization.getMemberships().getMembership()) {
final String userName = membership.getUserName();
if (userName != null) {
if (userName.equals(u.getUserName())) {
membershipFound = true;
final String groupName = membership.getGroupName();
if (groupName == null) {
validationStatus
.add(ValidationStatus.error(Messages.bind(Messages.missingGroup, u.getUserName())));
}
final String parentPath = membership.getGroupParentPath();
String groupPath = null;
if (parentPath == null) {
groupPath = GroupContentProvider.GROUP_SEPARATOR + groupName;
} else {
groupPath = parentPath + GroupContentProvider.GROUP_SEPARATOR + groupName;
}
final IStatus groupStatus = validateGroupExists(organization, groupPath, membership);
if (groupStatus.getSeverity() != IStatus.OK) {
validationStatus.add(groupStatus);
}
boolean membershipFound = false;
for (final Membership membership : organization.getMemberships().getMembership()) {
final String userName = membership.getUserName();
if (userName != null) {
if (userName.equals(u.getUserName())) {
membershipFound = true;
final String groupName = membership.getGroupName();
if (groupName == null) {
validationStatus
.add(ValidationStatus
.error(Messages.bind(Messages.missingGroup, u.getUserName())));
}
final String parentPath = membership.getGroupParentPath();
String groupPath = null;
if (parentPath == null) {
groupPath = GroupContentProvider.GROUP_SEPARATOR + groupName;
} else {
groupPath = parentPath + GroupContentProvider.GROUP_SEPARATOR + groupName;
}
final IStatus groupStatus = validateGroupExists(organization, groupPath, membership);
if (groupStatus.getSeverity() != IStatus.OK) {
validationStatus.add(groupStatus);
}
final String roleName = membership.getRoleName();
if (roleName == null) {
validationStatus
.add(ValidationStatus.error(Messages.bind(Messages.missingRole, u.getUserName())));
}
final IStatus roleStatus = validateRoleExists(organization, roleName, membership);
if (roleStatus.getSeverity() != IStatus.OK) {
validationStatus.add(roleStatus);
final String roleName = membership.getRoleName();
if (roleName == null) {
validationStatus
.add(ValidationStatus
.error(Messages.bind(Messages.missingRole, u.getUserName())));
}
final IStatus roleStatus = validateRoleExists(organization, roleName, membership);
if (roleStatus.getSeverity() != IStatus.OK) {
validationStatus.add(roleStatus);
}
}
}
}
}
if (!membershipFound) {
validationStatus
.add(ValidationStatus.error(Messages.bind(Messages.missingMembershipForUser, u.getUserName())));
if (!membershipFound) {
validationStatus
.add(ValidationStatus
.error(Messages.bind(Messages.missingMembershipForUser, u.getUserName())));
}
}
}
List<String> invalidGroups = organization.getGroups().getGroup().stream()
.map(Group::getName)
.filter(groupName -> groupName.contains("/"))
.collect(Collectors.toList());
if (!invalidGroups.isEmpty()) {
validationStatus.add(ValidationStatus.error(String.format(Messages.invalidCharInGroupName,
invalidGroups.stream().reduce((group1, group2) -> group1 + "\n" + group2).get())));
Groups groups = organization.getGroups();
if(groups != null) {
List<String> invalidGroups = groups.getGroup().stream()
.map(Group::getName)
.filter(groupName -> groupName.contains("/"))
.collect(Collectors.toList());
if (!invalidGroups.isEmpty()) {
validationStatus.add(ValidationStatus.error(String.format(Messages.invalidCharInGroupName,
invalidGroups.stream().reduce((group1, group2) -> group1 + "\n" + group2).get())));
}
}
return validationStatus;
}
private IStatus checkManagerCycles(final Organization organization, final User u) {
String managerUsername = u.getManager();
final List<String> managers = new ArrayList<String>();
final List<String> managers = new ArrayList<>();
managers.add(u.getUserName());
managers.add(managerUsername);
while (managerUsername != null) {
......@@ -135,31 +147,43 @@ public class OrganizationValidator implements IValidator, ValidatorConstants {
}
private String getManagerOf(final Organization organization, final String managerUsername) {
for (final User u : organization.getUsers().getUser()) {
if (managerUsername.equals(u.getUserName())) {
return u.getManager();
Users users = organization.getUsers();
if (users != null) {
for (final User u : users.getUser()) {
if (managerUsername.equals(u.getUserName())) {
return u.getManager();
}
}
}
return null;
}
private IStatus validateRoleExists(final Organization organization, final String roleName, final Membership membership) {
for (final Role role : organization.getRoles().getRole()) {
if (role.getName().equals(roleName)) {
return ValidationStatus.ok();
private IStatus validateRoleExists(final Organization organization, final String roleName,
final Membership membership) {
Roles roles = organization.getRoles();
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,
final Membership membership) {
for (final Group group : organization.getGroups().getGroup()) {
if (GroupContentProvider.getGroupPath(group).equals(groupPath)) {
return ValidationStatus.ok();
Groups groups = organization.getGroups();
if (groups != null) {
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