}
}
}
private final AuthUser attachAccounts(String userName, String decodedPass, AdministrationService admService, AuthUser admin, User user, String pass, ArrayList <ConnectionDescriptor> connections, SimpleLogger log) throws SQLException, OperationFailedException, AuthenticationFailedException {
AuthUser authenticatedUser = null;
try {
if (user.getLoginName().equals("admin")) {
authenticatedUser = ServiceProvider.getAuthenticationService().authenticateAdmin();
} else {
authenticatedUser = ServiceProvider.getAuthenticationService().authenticate(user.getLoginName(), pass);
}
if (!user.getLoginName().equals("admin")) {
admService.setPassword(AuthUserImpl.encrypt(pass), user);
admService.save(user);
}
} catch (AuthenticationFailedException e) {
if (!user.getLoginName().equals("admin")) {
admService.setPassword(AuthUserImpl.encrypt(pass), user);
admService.save(user);
}
authenticatedUser = ServiceProvider.getAuthenticationService().authenticate(user.getLoginName(), pass);
}
if (authenticatedUser == null) {
log.error("Authentication of PaloPivot user failed! Aborting.", new NullPointerException());
return authenticatedUser;
}
for (ConnectionDescriptor cd: connections) {
boolean found = false;
for (Account acc: authenticatedUser.getAccounts()) {
if (acc.getConnection().getHost() != null && acc.getConnection().getHost().equals(cd.host) &&
acc.getConnection().getService() != null && acc.getConnection().getService().equals(cd.port)) {
// if (acc.getConnection() != null && acc.getConnection().getName().equals(cd.name)) {
// found = true;
// break;
// }
if (/*cd.type.equalsIgnoreCase("dynamic") ||*/
cd.useLoginCredentials) {
admService.setLoginName(userName, acc);
admService.setPassword(decodedPass, acc);
admService.save(acc);
}
log.debug("Account already exists: " + acc.getConnection().getHost() + ", " + acc.getConnection().getService());
found = true;
break;
}
}
if (!found) {
PaloConnection con = null;
for (PaloConnection conn: admService.getConnections()) {
if (cd.host.equals(conn.getHost()) &&
cd.port.equals(conn.getService())) {
con = conn;
break;
}
}
if (con == null) {
log.warn("No connection found for " + cd.host + ":" + cd.port + ". Ignoring.");
continue;
}
Account acc = null;
if (cd.type.equalsIgnoreCase("palo")) {
if (cd.useLoginCredentials) {
log.debug("Creating account (dynamic) " + userName + ", " + decodedPass + " for user " + user.getLoginName());
admService.createAccount(userName, decodedPass, authenticatedUser, con);
} else {
log.debug("Creating account (palo) " + cd.user + ", " + cd.pass + " for user " + user.getLoginName());
admService.createAccount(cd.user, cd.pass, authenticatedUser, con);
}
}
// else if (cd.type.equalsIgnoreCase("dynamic")) {
// log.debug("Creating account (dynamic) " + userName + ", " + decodedPass + " for user " + user.getLoginName());
// admService.createAccount(userName, decodedPass, authenticatedUser, con);
// }
admService.save(acc);
admService.save(user);
}
}
log.debug(" ==> After account creation, the authenticated user has the following accounts:");
for (Account a: authenticatedUser.getAccounts()) {
log.debug(" => " + a.getLoginName() + ", " + a.getConnection().getHost() + ", " + a.getConnection().getService());
}
return authenticatedUser;
}