@Test public void testCreate() throws Exception { try (OperationContext ctx = new OperationContext(session)) { Map<String, Object> params = new HashMap<>(); params.put("username", USERNAME); params.put("password", PASSWORD); params.put("groups", new String[] { MEMBERS }); Properties properties = new Properties(); properties.put("firstName", FIRSTNAME); params.put("properties", properties); automationService.run(ctx, CreateOrUpdateUser.ID, params); NuxeoPrincipal principal = userManager.getPrincipal(USERNAME); assertEquals(FIRSTNAME, principal.getFirstName()); assertEquals(Arrays.asList(MEMBERS), principal.getGroups()); params.put("mode", "create"); try { automationService.run(ctx, CreateOrUpdateUser.ID, params); } catch (OperationException e) { String msg = e.getMessage(); assertTrue(msg, msg.contains("Cannot create already-existing user: testuser")); } } }
@OperationMethod public void run() throws OperationException { boolean create; DocumentModel userDoc = userManager.getUserModel(username); if (userDoc == null) { if (UPDATE.equals(mode)) { throw new OperationException("Cannot update non-existent user: " + username); } create = true; userDoc = userManager.getBareUserModel(); userDoc.setProperty(SCHEMA_NAME, USERNAME_COLUMN, username); } else { if (CREATE.equals(mode)) { throw new OperationException("Cannot create already-existing user: " + username); } create = false; checkCanCreateOrUpdateUser(userDoc); } if (groups != null) { userDoc.setProperty(SCHEMA_NAME, GROUPS_COLUMN, groups); } for (Entry<String, String> entry : Arrays.asList( new SimpleEntry<>(TENANT_ID_COLUMN, tenantId), new SimpleEntry<>(PASSWORD_COLUMN, password), new SimpleEntry<>(EMAIL_COLUMN, email), new SimpleEntry<>(FIRSTNAME_COLUMN, firstName), new SimpleEntry<>(LASTNAME_COLUMN, lastName), new SimpleEntry<>(COMPANY_COLUMN, company))) { String key = entry.getKey(); String value = entry.getValue(); if (StringUtils.isNotBlank(value)) { properties.put(key, value); } } for (Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key.startsWith(USER_COLON)) { key = key.substring(USER_COLON.length()); } userDoc.setProperty(SCHEMA_NAME, key, value); } checkCanCreateOrUpdateUser(userDoc); if (create) { userDoc = userManager.createUser(userDoc); } else { userManager.updateUser(userDoc); userDoc = userManager.getUserModel(username); } }
CreateOrUpdateUser { @OperationMethod public void run() throws OperationException { boolean create; DocumentModel userDoc = userManager.getUserModel(username); if (userDoc == null) { if (UPDATE.equals(mode)) { throw new OperationException("Cannot update non-existent user: " + username); } create = true; userDoc = userManager.getBareUserModel(); userDoc.setProperty(SCHEMA_NAME, USERNAME_COLUMN, username); } else { if (CREATE.equals(mode)) { throw new OperationException("Cannot create already-existing user: " + username); } create = false; checkCanCreateOrUpdateUser(userDoc); } if (groups != null) { userDoc.setProperty(SCHEMA_NAME, GROUPS_COLUMN, groups); } for (Entry<String, String> entry : Arrays.asList( new SimpleEntry<>(TENANT_ID_COLUMN, tenantId), new SimpleEntry<>(PASSWORD_COLUMN, password), new SimpleEntry<>(EMAIL_COLUMN, email), new SimpleEntry<>(FIRSTNAME_COLUMN, firstName), new SimpleEntry<>(LASTNAME_COLUMN, lastName), new SimpleEntry<>(COMPANY_COLUMN, company))) { String key = entry.getKey(); String value = entry.getValue(); if (StringUtils.isNotBlank(value)) { properties.put(key, value); } } for (Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key.startsWith(USER_COLON)) { key = key.substring(USER_COLON.length()); } userDoc.setProperty(SCHEMA_NAME, key, value); } checkCanCreateOrUpdateUser(userDoc); if (create) { userDoc = userManager.createUser(userDoc); } else { userManager.updateUser(userDoc); userDoc = userManager.getUserModel(username); } } }
CreateOrUpdateUser { @OperationMethod public void run() throws OperationException { boolean create; DocumentModel userDoc = userManager.getUserModel(username); if (userDoc == null) { if (UPDATE.equals(mode)) { throw new OperationException("Cannot update non-existent user: " + username); } create = true; userDoc = userManager.getBareUserModel(); userDoc.setProperty(SCHEMA_NAME, USERNAME_COLUMN, username); } else { if (CREATE.equals(mode)) { throw new OperationException("Cannot create already-existing user: " + username); } create = false; checkCanCreateOrUpdateUser(userDoc); } if (groups != null) { userDoc.setProperty(SCHEMA_NAME, GROUPS_COLUMN, groups); } for (Entry<String, String> entry : Arrays.asList( new SimpleEntry<>(TENANT_ID_COLUMN, tenantId), new SimpleEntry<>(PASSWORD_COLUMN, password), new SimpleEntry<>(EMAIL_COLUMN, email), new SimpleEntry<>(FIRSTNAME_COLUMN, firstName), new SimpleEntry<>(LASTNAME_COLUMN, lastName), new SimpleEntry<>(COMPANY_COLUMN, company))) { String key = entry.getKey(); String value = entry.getValue(); if (StringUtils.isNotBlank(value)) { properties.put(key, value); } } for (Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key.startsWith(USER_COLON)) { key = key.substring(USER_COLON.length()); } userDoc.setProperty(SCHEMA_NAME, key, value); } checkCanCreateOrUpdateUser(userDoc); if (create) { userDoc = userManager.createUser(userDoc); } else { userManager.updateUser(userDoc); userDoc = userManager.getUserModel(username); } }  }
CreateOrUpdateUser { @OperationMethod public void run() throws OperationException { boolean create; DocumentModel userDoc = userManager.getUserModel(username); if (userDoc == null) { if (UPDATE.equals(mode)) { throw new OperationException("Cannot update non-existent user: " + username); } create = true; userDoc = userManager.getBareUserModel(); userDoc.setProperty(SCHEMA_NAME, USERNAME_COLUMN, username); } else { if (CREATE.equals(mode)) { throw new OperationException("Cannot create already-existing user: " + username); } create = false; checkCanCreateOrUpdateUser(userDoc); } if (groups != null) { userDoc.setProperty(SCHEMA_NAME, GROUPS_COLUMN, groups); } for (Entry<String, String> entry : Arrays.asList( new SimpleEntry<>(TENANT_ID_COLUMN, tenantId), new SimpleEntry<>(PASSWORD_COLUMN, password), new SimpleEntry<>(EMAIL_COLUMN, email), new SimpleEntry<>(FIRSTNAME_COLUMN, firstName), new SimpleEntry<>(LASTNAME_COLUMN, lastName), new SimpleEntry<>(COMPANY_COLUMN, company))) { String key = entry.getKey(); String value = entry.getValue(); if (StringUtils.isNotBlank(value)) { properties.put(key, value); } } for (Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key.startsWith(USER_COLON)) { key = key.substring(USER_COLON.length()); } userDoc.setProperty(SCHEMA_NAME, key, value); } checkCanCreateOrUpdateUser(userDoc); if (create) { userDoc = userManager.createUser(userDoc); } else { userManager.updateUser(userDoc); userDoc = userManager.getUserModel(username); } }  @OperationMethod void run(); }
CreateOrUpdateUser { @OperationMethod public void run() throws OperationException { boolean create; DocumentModel userDoc = userManager.getUserModel(username); if (userDoc == null) { if (UPDATE.equals(mode)) { throw new OperationException("Cannot update non-existent user: " + username); } create = true; userDoc = userManager.getBareUserModel(); userDoc.setProperty(SCHEMA_NAME, USERNAME_COLUMN, username); } else { if (CREATE.equals(mode)) { throw new OperationException("Cannot create already-existing user: " + username); } create = false; checkCanCreateOrUpdateUser(userDoc); } if (groups != null) { userDoc.setProperty(SCHEMA_NAME, GROUPS_COLUMN, groups); } for (Entry<String, String> entry : Arrays.asList( new SimpleEntry<>(TENANT_ID_COLUMN, tenantId), new SimpleEntry<>(PASSWORD_COLUMN, password), new SimpleEntry<>(EMAIL_COLUMN, email), new SimpleEntry<>(FIRSTNAME_COLUMN, firstName), new SimpleEntry<>(LASTNAME_COLUMN, lastName), new SimpleEntry<>(COMPANY_COLUMN, company))) { String key = entry.getKey(); String value = entry.getValue(); if (StringUtils.isNotBlank(value)) { properties.put(key, value); } } for (Entry<String, String> entry : properties.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (key.startsWith(USER_COLON)) { key = key.substring(USER_COLON.length()); } userDoc.setProperty(SCHEMA_NAME, key, value); } checkCanCreateOrUpdateUser(userDoc); if (create) { userDoc = userManager.createUser(userDoc); } else { userManager.updateUser(userDoc); userDoc = userManager.getUserModel(username); } }  @OperationMethod void run(); static final String ID; static final String CREATE_OR_UPDATE; static final String CREATE; static final String UPDATE; }