urAssoc.insert(viewApiUser, editorRole);
admService.add(viewerRole, viewApiUser);
admService.add(editorRole, viewApiUser);
admService.save(viewApiUser);
}
PaloConnection conToUse = null;
for (PaloConnection conn: admService.getConnections()) {
if (psd.host.equals(conn.getHost()) &&
psd.port.equals(conn.getService())) {
conToUse = conn;
break;
}
}
if (conToUse == null) {
conToUse = admService.createConnection("PaloCon", psd.host, psd.port, PaloConnection.TYPE_HTTP);
admService.save(conToUse);
}
Account accountToUse = null;
AuthUser authenticatedUser = null;
try {
if (viewApiUser.getLoginName().equals("admin")) {
authenticatedUser = ServiceProvider.getAuthenticationService().authenticateAdmin();
} else {
authenticatedUser = ServiceProvider.getAuthenticationService().authenticate(viewApiUser.getLoginName(), pass);
}
if (!viewApiUser.getLoginName().equals("admin")) {
admService.setPassword(pass, viewApiUser);
admService.save(viewApiUser);
}
} catch (AuthenticationFailedException e) {
if (!viewApiUser.getLoginName().equals("admin")) {
admService.setPassword(pass, viewApiUser);
admService.save(viewApiUser);
}
authenticatedUser = ServiceProvider.getAuthenticationService().authenticate(viewApiUser.getLoginName(), pass);
}
if (authenticatedUser != null) {
for (Account acc: authenticatedUser.getAccounts()) {
if (acc.getConnection().getId().equals(conToUse.getId())) {
accountToUse = acc;
}
}
if (accountToUse == null) {
accountToUse = admService.createAccount(user, pass, viewApiUser, conToUse);