Changed Find User Methods so its matching to the user management
This commit is contained in:
parent
4b039cb198
commit
0bc1855284
@ -61,8 +61,8 @@ public class Controller {
|
|||||||
return ResponseEntity.status(200).body(new Gson().toJson(cacheRepository.findAll()));
|
return ResponseEntity.status(200).body(new Gson().toJson(cacheRepository.findAll()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO user muss jetzt anders aus dem token geholt werden, da kein subject mehr gesetzt wird und username nichtmehr unique ist
|
// user muss jetzt anders aus dem token geholt werden, da kein subject mehr gesetzt wird und username nichtmehr unique ist
|
||||||
// TODO (über der checkAdmin methode steht ein möglicher lösungsvorschlag dafür)
|
// (über der checkAdmin methode steht ein möglicher lösungsvorschlag dafür)
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/startCache")
|
@RequestMapping("/api/startCache")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@ -86,7 +86,8 @@ public class Controller {
|
|||||||
Claims claims = (Claims) tokenVerification.getBody();
|
Claims claims = (Claims) tokenVerification.getBody();
|
||||||
|
|
||||||
|
|
||||||
ResponseEntity getUser = FinderUtil.findUserByUsername(claims.getSubject());
|
//Sollte jetzt eigentlich funktionieren...hoffe ich
|
||||||
|
ResponseEntity getUser = FinderUtil.findUserFromClaim(claims);
|
||||||
|
|
||||||
if (getUser.getStatusCodeValue() != 200) {
|
if (getUser.getStatusCodeValue() != 200) {
|
||||||
return getUser;
|
return getUser;
|
||||||
@ -145,7 +146,7 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO user muss jetzt anders aus dem token geholt werden, da kein subject mehr gesetzt wird und username nichtmehr unique ist
|
//user muss jetzt anders aus dem token geholt werden, da kein subject mehr gesetzt wird und username nichtmehr unique ist
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/checkStation")
|
@RequestMapping("/api/checkStation")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@ -165,7 +166,7 @@ public class Controller {
|
|||||||
Claims claims = (Claims) tokenVerification.getBody();
|
Claims claims = (Claims) tokenVerification.getBody();
|
||||||
|
|
||||||
|
|
||||||
ResponseEntity getUser = FinderUtil.findUserByUsername(claims.getSubject());
|
ResponseEntity getUser = FinderUtil.findUserFromClaim(claims);
|
||||||
|
|
||||||
if (getUser.getStatusCodeValue() != 200) {
|
if (getUser.getStatusCodeValue() != 200) {
|
||||||
return getUser;
|
return getUser;
|
||||||
@ -276,9 +277,6 @@ public class Controller {
|
|||||||
return createCacheUtil(cache);
|
return createCacheUtil(cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO claims.get funktioniert denk ich jetzt nurnoch mit claims.get("user") und liefert glaub ein userEntity objekt als JSON zurück
|
|
||||||
// TODO wir könnten doch dann die userID aus dem JSON auslesen und mit Hilfe einer findByID methode dann den user aus unserer DB finden oder?
|
|
||||||
// TODO unten sieht man auskommentiert, wie das eventuell funktionieren könnte
|
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/checkAdmin")
|
@RequestMapping("/api/checkAdmin")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@ -292,25 +290,15 @@ public class Controller {
|
|||||||
|
|
||||||
Claims claims = (Claims) verifyToken.getBody();
|
Claims claims = (Claims) verifyToken.getBody();
|
||||||
|
|
||||||
|
ResponseEntity userResponse = FinderUtil.findUserFromClaim(claims);
|
||||||
|
|
||||||
// TODO sieht nach einer lösung aus, sollte aber denk ich in FinderUtil oder so ausgelagert
|
if(userResponse.getStatusCodeValue() != 200){
|
||||||
// TODO werden. Dann haben wir eine Methode, die hierfür aufgerufen wird und ein 'Claim'
|
return userResponse;
|
||||||
// TODO entgegennimmt und dann eine ResponseEntity zurückgibt
|
}
|
||||||
// try {
|
|
||||||
// String userString = (String) claims.get("user");
|
User user = (User) userResponse.getBody();
|
||||||
// JSONParser parser = new JSONParser();
|
|
||||||
// JSONObject userObject = (JSONObject) parser.parse(userString);
|
//TODO Hier Admin Check einfügen
|
||||||
// int userID = (Integer) userObject.get("userID");
|
|
||||||
// Optional<User> userOptional = userRepository.findById(userID);
|
|
||||||
// if (userOptional.isPresent()) {
|
|
||||||
// User user = userOptional.get();
|
|
||||||
// // hier dann admin check etc...
|
|
||||||
// } else {
|
|
||||||
// return ResponseEntity.status(404).body("Couldnt find User " + userID);
|
|
||||||
// }
|
|
||||||
// } catch (ParseException e) {
|
|
||||||
// e.printStackTrace();
|
|
||||||
// }
|
|
||||||
|
|
||||||
return ResponseEntity.status(200).body(claims.get("admin"));
|
return ResponseEntity.status(200).body(claims.get("admin"));
|
||||||
}
|
}
|
||||||
@ -329,7 +317,7 @@ public class Controller {
|
|||||||
return deleteCacheUtil(cacheID);
|
return deleteCacheUtil(cacheID);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/getMyCaches")
|
@RequestMapping("/api/getMyCaches")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@ -344,7 +332,7 @@ public class Controller {
|
|||||||
|
|
||||||
Claims claims = (Claims) verifyToken.getBody();
|
Claims claims = (Claims) verifyToken.getBody();
|
||||||
|
|
||||||
ResponseEntity getUser = FinderUtil.findUserByUsername(claims.getSubject());
|
ResponseEntity getUser = FinderUtil.findUserFromClaim(claims);
|
||||||
|
|
||||||
if (getUser.getStatusCodeValue() != 200) {
|
if (getUser.getStatusCodeValue() != 200) {
|
||||||
return getUser;
|
return getUser;
|
||||||
@ -382,7 +370,6 @@ public class Controller {
|
|||||||
return ResponseEntity.status(200).body(new Gson().toJson(sendBackUsers));
|
return ResponseEntity.status(200).body(new Gson().toJson(sendBackUsers));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO ebenfalls userfindung ändern
|
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/getUser")
|
@RequestMapping("/api/getUser")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@ -396,7 +383,7 @@ public class Controller {
|
|||||||
|
|
||||||
Claims claims = (Claims) verifyToken.getBody();
|
Claims claims = (Claims) verifyToken.getBody();
|
||||||
|
|
||||||
ResponseEntity getUser = FinderUtil.findUserByUsername(claims.getSubject());
|
ResponseEntity getUser = FinderUtil.findUserFromClaim(claims);
|
||||||
|
|
||||||
if (getUser.getStatusCodeValue() != 200) {
|
if (getUser.getStatusCodeValue() != 200) {
|
||||||
return getUser;
|
return getUser;
|
||||||
|
|||||||
@ -2,6 +2,10 @@ package hhn.labsw.bugageocaching.util;
|
|||||||
|
|
||||||
import hhn.labsw.bugageocaching.entities.*;
|
import hhn.labsw.bugageocaching.entities.*;
|
||||||
import hhn.labsw.bugageocaching.repositories.*;
|
import hhn.labsw.bugageocaching.repositories.*;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
|
import org.json.simple.parser.JSONParser;
|
||||||
|
import org.json.simple.parser.ParseException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
@ -91,4 +95,25 @@ public class FinderUtil {
|
|||||||
return ResponseEntity.status(404).body("Couldnt find User_Info " + infoID);
|
return ResponseEntity.status(404).body("Couldnt find User_Info " + infoID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public static ResponseEntity findUserFromClaim(Claims claims) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
String userString = (String) claims.get("user");
|
||||||
|
JSONParser parser = new JSONParser();
|
||||||
|
JSONObject userObject = (JSONObject) parser.parse(userString);
|
||||||
|
int userID = (Integer) userObject.get("userID");
|
||||||
|
Optional<User> userOptional = userRepository.findById(userID);
|
||||||
|
if (userOptional.isPresent()) {
|
||||||
|
User user = userOptional.get();
|
||||||
|
return ResponseEntity.status(200).body(user);
|
||||||
|
} else {
|
||||||
|
return ResponseEntity.status(404).body("Couldnt find User " + userID);
|
||||||
|
}
|
||||||
|
} catch (ParseException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return ResponseEntity.status(404).body("String format was corrupt");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user