}
private final XDirectLinkData legacyAuthentication(Connection con, String user, String pass, PaloSuiteData psd, String viewId, XDirectLinkData data, SimpleLogger log) throws OperationFailedException, SQLException, AuthenticationFailedException {
if (con.getDatabaseCount() > 0) {
AuthUser admin = ServiceProvider.getAuthenticationService().authenticateAdmin();
AdministrationService admService = ServiceProvider.getAdministrationService(admin);
User viewApiUser = null;
for (User usr: admService.getUsers()) {
if (user.equals(usr.getLoginName())) {
viewApiUser = usr;
break;
}
}
if (viewApiUser == null) {
if (user.equals("admin")) {
viewApiUser = admin;
} else {
viewApiUser = admService.createUser("", "", user, pass);
admService.save(viewApiUser);
}
Role viewerRole = admService.getRoleByName("VIEWER");
Role editorRole = admService.getRoleByName("EDITOR");
IUserRoleManagement urAssoc = MapperRegistry.getInstance().getUserRoleAssociation();
urAssoc.insert(viewApiUser, viewerRole);
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);
admService.save(accountToUse);
}
}
if (accountToUse != null && authenticatedUser != null) {
AccountConverter co = new AccountConverter();
XAccount xAccount = (XAccount) co.toXObject(accountToUse);