added keycloak user
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package de.anxietyprime.swajodel;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
|
||||
77
src/main/java/de/anxietyprime/swajodel/User.java
Normal file
77
src/main/java/de/anxietyprime/swajodel/User.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package de.anxietyprime.swajodel;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
|
||||
public class User {
|
||||
static public String getUUID () {
|
||||
return SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
}
|
||||
|
||||
static public Long getUserID () {
|
||||
String uuid = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
Long id = -1L;
|
||||
|
||||
// DB connection and statement
|
||||
Connection c;
|
||||
PreparedStatement stmt;
|
||||
|
||||
// try to get data from db
|
||||
try {
|
||||
// check for the driver
|
||||
Class.forName("org.postgresql.Driver");
|
||||
// get the connection with credentials from env variables
|
||||
c = DriverManager
|
||||
.getConnection("jdbc:postgresql://"+
|
||||
System.getenv("POSTGRES_IP")+"/"+System.getenv("POSTGRES_DB"),
|
||||
System.getenv("POSTGRES_USER"), System.getenv("POSTGRES_PASSWORD"));
|
||||
// disable auto commits
|
||||
c.setAutoCommit(false);
|
||||
|
||||
// create a new statement
|
||||
stmt = c.prepareStatement("SELECT id FROM users WHERE username = (?)");
|
||||
|
||||
stmt.setObject(1, uuid);
|
||||
|
||||
// insert post and get its id
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
// check if there is a parent
|
||||
if (!rs.next()) {
|
||||
// create a new statement
|
||||
stmt = c.prepareStatement("INSERT INTO users(username, password) VALUES (?, 'unused') RETURNING id");
|
||||
|
||||
// fill statement
|
||||
stmt.setObject(1, uuid);
|
||||
|
||||
// execute statement
|
||||
rs = stmt.executeQuery();
|
||||
|
||||
// commit the changes
|
||||
c.commit();
|
||||
|
||||
rs.next();
|
||||
}
|
||||
|
||||
id = rs.getLong("id");
|
||||
|
||||
// close all connections to db
|
||||
stmt.close();
|
||||
c.close();
|
||||
}
|
||||
|
||||
// else log the error
|
||||
catch ( Exception e ) {
|
||||
System.err.println( e.getClass().getName()+": "+ e.getMessage() );
|
||||
throw new ResponseStatusException(HttpStatus.SERVICE_UNAVAILABLE, "Database is offline");
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user