OrgServiceUserCreator implements UserCreator { @Override public User createUser(String email, String userName, String firstName, String lastName) throws IOException { try { return userManager.getByEmail(email); } catch (NotFoundException e) { try { } catch (Exception ex) { throw new IOException(ex.getLocalizedMessage(), ex); } if (!userSelfCreationAllowed) { throw new IOException( "Currently only admins can create accounts. Please contact our Admin Team for further info."); } final Map<String, String> attributes = new HashMap<>(); if (firstName != null) { attributes.put("firstName", firstName); } if (lastName != null) { attributes.put("lastName", lastName); } attributes.put("email", email); try { User user = createNonReservedUser(userName, email); while (user == null) { user = createNonReservedUser(NameGenerator.generate(userName, 4), email); } ProfileImpl profile = new ProfileImpl(profileManager.getById(user.getId())); profile.getAttributes().putAll(attributes); profileManager.update(profile); final Map<String, String> preferences = new HashMap<>(); preferences.put("codenvy:created", Long.toString(System.currentTimeMillis())); preferences.put("resetPassword", "true"); preferenceManager.save(user.getId(), preferences); return user; } catch (NotFoundException | ServerException ex) { throw new IOException(ex.getLocalizedMessage(), ex); } } catch (ServerException e) { throw new IOException(e.getLocalizedMessage(), e); } } @Inject  OrgServiceUserCreator(
      UserManager userManager,
      ProfileManager profileManager,
      PreferenceManager preferenceManager,
      @Named("che.auth.user_self_creation") boolean userSelfCreationAllowed); @Override User createUser(String email, String userName, String firstName, String lastName); @Override User createTemporary();  }
@Test public void shouldCreateUser() throws Exception { doThrow(NotFoundException.class).when(manager).getByEmail(anyObject()); creator.createUser("user@codenvy.com", "test", "John", "Doe"); ArgumentCaptor<User> user = ArgumentCaptor.forClass(User.class); verify(manager).create(user.capture(), eq(false)); assertTrue(user.getValue().getName().equals("test")); }
@Test public void shouldCreateUserWithGeneratedNameOnConflict() throws Exception { doThrow(NotFoundException.class).when(manager).getByEmail(anyObject()); doAnswer( invocation -> { for (Object arg : invocation.getArguments()) { if (arg instanceof User && ((User) arg).getName().equals("reserved")) { throw new ConflictException("User name is reserved"); } } return null; }) .when(manager) .create(anyObject(), anyBoolean()); creator.createUser("user@codenvy.com", "reserved", "John", "Doe"); ArgumentCaptor<User> user = ArgumentCaptor.forClass(User.class); verify(manager, times(2)).create(user.capture(), eq(false)); assertTrue(user.getValue().getName().startsWith("reserved")); assertFalse(user.getValue().getName().equals("reserved")); }