Compare commits
No commits in common. "master" and "37d275d537c430be832ceb397cc59bd68e693422" have entirely different histories.
master
...
37d275d537
@ -18,10 +18,16 @@ dependencies {
|
|||||||
runtimeOnly 'mysql:mysql-connector-java'
|
runtimeOnly 'mysql:mysql-connector-java'
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
|
|
||||||
|
// Spring security
|
||||||
|
//implementation 'org.springframework.boot:spring-boot-starter-security'
|
||||||
|
//implementation 'org.springframework.security:spring-security-test'
|
||||||
|
|
||||||
// JSON web token
|
// JSON web token
|
||||||
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
|
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
|
||||||
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2',
|
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2',
|
||||||
'io.jsonwebtoken:jjwt-jackson:0.11.2'
|
// Uncomment the next line if you want to use RSASSA-PSS (PS256, PS384, PS512) algorithms:
|
||||||
|
//'org.bouncycastle:bcprov-jdk15on:1.60',
|
||||||
|
'io.jsonwebtoken:jjwt-jackson:0.11.2' // or 'io.jsonwebtoken:jjwt-gson:0.11.2' for gson
|
||||||
}
|
}
|
||||||
|
|
||||||
test {
|
test {
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
//Marc Beyer//
|
package com.vpr.server;
|
||||||
package com.vpr.server;
|
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
//Marco Kühn//
|
|
||||||
package com.vpr.server.controller;
|
|
||||||
|
|
||||||
import com.vpr.server.data.User;
|
|
||||||
import com.vpr.server.repository.UserRepository;
|
|
||||||
|
|
||||||
public class AuthController {
|
|
||||||
|
|
||||||
public User getAuthUserFromHeader(String authorizationHeader, UserRepository userRepository){
|
|
||||||
String[] splitAuthHeader = authorizationHeader.split("\\s");
|
|
||||||
if(splitAuthHeader.length == 2){
|
|
||||||
return userRepository.findByToken(splitAuthHeader[1]);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +1,8 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.controller;
|
package com.vpr.server.controller;
|
||||||
|
|
||||||
import com.vpr.server.dao.interfaces.EventDAO;
|
|
||||||
import com.vpr.server.data.Event;
|
import com.vpr.server.data.Event;
|
||||||
import com.vpr.server.data.User;
|
import com.vpr.server.data.User;
|
||||||
import com.vpr.server.data.UserEvent;
|
import com.vpr.server.data.UserEvent;
|
||||||
import com.vpr.server.json.JSONMapper;
|
|
||||||
import com.vpr.server.json.Validator;
|
|
||||||
import com.vpr.server.repository.EventRepository;
|
import com.vpr.server.repository.EventRepository;
|
||||||
import com.vpr.server.repository.UserEventRepository;
|
import com.vpr.server.repository.UserEventRepository;
|
||||||
import com.vpr.server.repository.UserRepository;
|
import com.vpr.server.repository.UserRepository;
|
||||||
@ -15,9 +11,12 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.sql.Time;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(path = "/event")
|
@RequestMapping(path = "/event")
|
||||||
@ -29,9 +28,6 @@ public class EventController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserEventRepository userEventRepository;
|
private UserEventRepository userEventRepository;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private EventDAO eventDAO;
|
|
||||||
|
|
||||||
/******************
|
/******************
|
||||||
* POST-ENDPOINTS *
|
* POST-ENDPOINTS *
|
||||||
******************/
|
******************/
|
||||||
@ -39,24 +35,68 @@ public class EventController {
|
|||||||
@PostMapping(path = "/add")
|
@PostMapping(path = "/add")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<String> addEvent(
|
ResponseEntity<String> addEvent(
|
||||||
@RequestHeader("Authorization") String authorizationHeader,
|
@RequestParam Integer userId,
|
||||||
@RequestParam long userId,
|
|
||||||
@RequestParam String date,
|
@RequestParam String date,
|
||||||
@RequestParam String name,
|
@RequestParam String name,
|
||||||
@RequestParam String start,
|
@RequestParam String start,
|
||||||
@RequestParam String end,
|
@RequestParam String end,
|
||||||
@RequestParam Integer priority,
|
@RequestParam Integer prority,
|
||||||
@RequestParam Boolean isFullDay,
|
@RequestParam Boolean isFullDay,
|
||||||
@RequestParam Boolean isPrivate
|
@RequestParam Boolean isPrivate
|
||||||
) {
|
) {
|
||||||
User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]);
|
String errorString = "";
|
||||||
if (authUser == null || (!authUser.isAdmin() && authUser.getId() != userId)) {
|
|
||||||
return new ResponseEntity<>("Du hast keine Rechte um den Termin zu erstellen", HttpStatus.UNAUTHORIZED);
|
Event event = new Event();
|
||||||
|
|
||||||
|
System.out.println(name.length() + ". name " + name);
|
||||||
|
if (name.length() > 3) {
|
||||||
|
event.setName(name);
|
||||||
|
} else {
|
||||||
|
System.out.println("NAME IST ZU KURZ");
|
||||||
|
return new ResponseEntity<>("Der Name ist zu kurz", HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
ResponseEntity<String> BAD_REQUEST = createEventAndUserEvent(userId, date, name, start, end, priority, isFullDay, isPrivate);
|
try {
|
||||||
if (BAD_REQUEST != null) return BAD_REQUEST;
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm");
|
||||||
|
long ms = simpleDateFormat.parse(start).getTime();
|
||||||
|
event.setStart(new Time(ms));
|
||||||
|
} catch (Exception e) {
|
||||||
|
event.setStart(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm");
|
||||||
|
long ms = simpleDateFormat.parse(end).getTime();
|
||||||
|
event.setEnd(new Time(ms));
|
||||||
|
} catch (Exception e) {
|
||||||
|
event.setEnd(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
event.setPriority(prority);
|
||||||
|
event.setFullDay(isFullDay);
|
||||||
|
event.setPrivate(isPrivate);
|
||||||
|
|
||||||
|
UserEvent userEvent = new UserEvent();
|
||||||
|
|
||||||
|
try {
|
||||||
|
System.out.println("date " + date);
|
||||||
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
userEvent.setDate(new java.sql.Date(simpleDateFormat.parse(date).getTime()));
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("DATE FORMAT NOT CORRECT");
|
||||||
|
return new ResponseEntity<>("Datumformat nicht korrekt", HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
userEvent.setEvent(event);
|
||||||
|
long uId = Long.valueOf(userId);
|
||||||
|
User user = userRepository.findById(uId);
|
||||||
|
userEvent.setUser(user);
|
||||||
|
|
||||||
|
System.out.println(userEvent);
|
||||||
|
System.out.println(user);
|
||||||
|
|
||||||
|
eventRepository.save(event);
|
||||||
|
userEventRepository.save(userEvent);
|
||||||
return new ResponseEntity<>("", HttpStatus.OK);
|
return new ResponseEntity<>("", HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,186 +108,46 @@ public class EventController {
|
|||||||
@RequestParam long userId,
|
@RequestParam long userId,
|
||||||
@RequestParam String date
|
@RequestParam String date
|
||||||
) {
|
) {
|
||||||
|
System.out.println("authorizationHeader " + authorizationHeader);
|
||||||
User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]);
|
User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]);
|
||||||
if (authUser == null || (!authUser.isAdmin() && authUser.getId() != userId)) {
|
if(authUser == null || (!authUser.isAdmin() && authUser.getId() != userId)){
|
||||||
return new ResponseEntity<>("Du hast keine Rechte um den Termin zu löschen", HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>( "Du hast keine Rechte um den Termin zu löschen", HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
eventRepository.deleteUserEventsById(userId, eventId, date);
|
EventRepository.UserEventInterface userEvent = eventRepository.findUserEventByEventIdUserIdAndDate(eventId, authUser.getId(), date);
|
||||||
if (eventDAO.getAllEventsWithId(eventId).size() == 0) {
|
|
||||||
eventRepository.deleteById(eventId);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
//Optional<Event> event = eventRepository.findById(eventId);
|
||||||
|
|
||||||
|
if (userEvent == null){
|
||||||
|
return new ResponseEntity<>( "Der Termin exestiert nicht", HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<>( "Der Termin exestiert", HttpStatus.OK);
|
||||||
|
|
||||||
|
/*
|
||||||
|
eventRepository.deleteUserEventsById(eventId);
|
||||||
|
eventRepository.deleteById(eventId);
|
||||||
return new ResponseEntity<>("", HttpStatus.OK);
|
return new ResponseEntity<>("", HttpStatus.OK);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@PostMapping(path = "/all")
|
@PostMapping(path = "/all")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<String> getAllEvents(
|
List<Event> getAllEvents(
|
||||||
@RequestHeader("Authorization") String authorizationHeader,
|
@RequestParam long userId,
|
||||||
@RequestParam String startDate,
|
@RequestParam String startDate,
|
||||||
@RequestParam String endDate
|
@RequestParam String endDate
|
||||||
) {
|
) {
|
||||||
User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]);
|
return eventRepository.findEventsInDateRange(userId, startDate, endDate);
|
||||||
if (authUser == null) {
|
|
||||||
return new ResponseEntity<>("Bitte erneut einloggen", HttpStatus.UNAUTHORIZED);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Event> eventList = eventDAO.getAllEventsInTimespan(authUser.getId(), startDate, endDate);
|
|
||||||
|
|
||||||
return new ResponseEntity<>(JSONMapper.eventListToJSON(eventList), HttpStatus.OK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@PostMapping(path = "/edit")
|
@PostMapping(path = "/edit")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<String> editEvent(
|
String editEvent(
|
||||||
@RequestHeader("Authorization") String authorizationHeader,
|
|
||||||
@RequestParam Long eventId,
|
@RequestParam Long eventId,
|
||||||
@RequestParam Long userId,
|
@RequestParam Long userId,
|
||||||
@RequestParam String date,
|
@RequestParam String date
|
||||||
@RequestParam String newDate,
|
|
||||||
@RequestParam String newName,
|
|
||||||
@RequestParam String newStart,
|
|
||||||
@RequestParam String newEnd,
|
|
||||||
@RequestParam Integer newPriority,
|
|
||||||
@RequestParam Boolean newIsFullDay,
|
|
||||||
@RequestParam Boolean newIsPrivate
|
|
||||||
) {
|
) {
|
||||||
User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]);
|
EventRepository.UserEventInterface userEvent = eventRepository.findUserEventByEventIdUserIdAndDate(eventId, userId, date);
|
||||||
if (authUser == null || (!authUser.isAdmin() && authUser.getId() != userId)) {
|
return "Length: " + userEvent.getDate();
|
||||||
return new ResponseEntity<>(
|
|
||||||
"Du hast keine Rechte um den Termin zu bearbeiten",
|
|
||||||
HttpStatus.UNAUTHORIZED
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Event> eventList = eventDAO.getAllEventsWithIdAndDate(userId, eventId, date);
|
|
||||||
|
|
||||||
if (eventList == null || eventList.size() == 0) {
|
|
||||||
return new ResponseEntity<>("Der Termin exestiert nicht in der Datenbank", HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
if (eventList.size() > 1) {
|
|
||||||
return new ResponseEntity<>(
|
|
||||||
"Der Termin ist doppelt vorhanden. " +
|
|
||||||
"(Um das zu lösen versuche den Termin zu löschen und erneut zu erstellen)",
|
|
||||||
HttpStatus.BAD_REQUEST
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
ResponseEntity<String> error = createEventAndUserEvent(
|
|
||||||
userId,
|
|
||||||
newDate,
|
|
||||||
newName,
|
|
||||||
newStart,
|
|
||||||
newEnd,
|
|
||||||
newPriority,
|
|
||||||
newIsFullDay,
|
|
||||||
newIsPrivate,
|
|
||||||
eventId
|
|
||||||
);
|
|
||||||
|
|
||||||
if (error != null) return error;
|
|
||||||
|
|
||||||
eventRepository.deleteUserEventsById(userId, eventId, date);
|
|
||||||
if (eventDAO.getAllEventsWithId(eventId).size() == 0) {
|
|
||||||
eventRepository.deleteById(eventId);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return new ResponseEntity<>("", HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ResponseEntity<String> createEventAndUserEvent(
|
|
||||||
long userId,
|
|
||||||
String date,
|
|
||||||
String name,
|
|
||||||
String start,
|
|
||||||
String end,
|
|
||||||
Integer priority,
|
|
||||||
Boolean isFullDay,
|
|
||||||
Boolean isPrivate
|
|
||||||
) {
|
|
||||||
return createEventAndUserEvent(
|
|
||||||
userId,
|
|
||||||
date,
|
|
||||||
name,
|
|
||||||
start,
|
|
||||||
end,
|
|
||||||
priority,
|
|
||||||
isFullDay,
|
|
||||||
isPrivate,
|
|
||||||
-1
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ResponseEntity<String> createEventAndUserEvent(
|
|
||||||
long userId,
|
|
||||||
String date,
|
|
||||||
String name,
|
|
||||||
String start,
|
|
||||||
String end,
|
|
||||||
Integer priority,
|
|
||||||
Boolean isFullDay,
|
|
||||||
Boolean isPrivate,
|
|
||||||
long oldEventId
|
|
||||||
) {
|
|
||||||
User user = userRepository.findById(userId);
|
|
||||||
if (user == null) {
|
|
||||||
return new ResponseEntity<>("UserId nicht korrekt", HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
Event event = new Event();
|
|
||||||
|
|
||||||
event.setName(Validator.ValidateEventName(name));
|
|
||||||
event.setStart(Validator.ValidateEventTime(start));
|
|
||||||
event.setEnd(Validator.ValidateEventTime(end));
|
|
||||||
event.setPriority(priority);
|
|
||||||
event.setFullDay(isFullDay);
|
|
||||||
event.setPrivate(isPrivate);
|
|
||||||
|
|
||||||
UserEvent userEvent = new UserEvent();
|
|
||||||
|
|
||||||
userEvent.setDate(Validator.ValidateEventDate(date));
|
|
||||||
userEvent.setEvent(event);
|
|
||||||
userEvent.setUser(user);
|
|
||||||
|
|
||||||
List<UserEvent> userEvents = userEventRepository.findByUserIdAndDate(user.getId(), userEvent.getDate());
|
|
||||||
|
|
||||||
boolean isFullDayButDayHasEvents = event.isFullDay() && userEvents.size() > 0;
|
|
||||||
boolean userEventIsSelf = userEvents.size() == 1 &&
|
|
||||||
isSelf(userEvent.getDate(), userId, oldEventId, userEvents.get(0));
|
|
||||||
|
|
||||||
if (isFullDayButDayHasEvents && !userEventIsSelf) {
|
|
||||||
return new ResponseEntity<>(
|
|
||||||
"Es gibt bereits Termine am " + userEvent.getDate(),
|
|
||||||
HttpStatus.BAD_REQUEST
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
for (UserEvent ue : userEvents) {
|
|
||||||
if (ue.getEvent().isFullDay() && !isSelf(userEvent.getDate(), userId, oldEventId, ue)) {
|
|
||||||
return new ResponseEntity<>(
|
|
||||||
"Der Tag " + userEvent.getDate() + " ist schon mit '"
|
|
||||||
+ ue.getEvent().getName() + "' belegt",
|
|
||||||
HttpStatus.BAD_REQUEST
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
eventRepository.save(event);
|
|
||||||
userEventRepository.save(userEvent);
|
|
||||||
} catch (IllegalArgumentException exception) {
|
|
||||||
return new ResponseEntity<>(exception.getMessage(), HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isSelf(Date date, long userId, long eventId, UserEvent userEvent){
|
|
||||||
return date.equals(userEvent.getDate()) &&
|
|
||||||
userId == userEvent.getUser().getId() &&
|
|
||||||
eventId == userEvent.getEvent().getId();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.controller;
|
package com.vpr.server.controller;
|
||||||
|
|
||||||
|
import com.vpr.server.data.Event;
|
||||||
|
import com.vpr.server.data.User;
|
||||||
|
import com.vpr.server.data.UserEvent;
|
||||||
import com.vpr.server.repository.EventRepository;
|
import com.vpr.server.repository.EventRepository;
|
||||||
import com.vpr.server.repository.UserEventRepository;
|
import com.vpr.server.repository.UserEventRepository;
|
||||||
import com.vpr.server.repository.UserRepository;
|
import com.vpr.server.repository.UserRepository;
|
||||||
@ -8,16 +10,25 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestHeader;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.server.ResponseStatusException;
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
|
import java.sql.Time;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
|
||||||
@Controller // This means that this class is a Controller
|
@Controller // This means that this class is a Controller
|
||||||
@RequestMapping(path = "/vpr") // This means URL's start with /demo (after Application path)
|
@RequestMapping(path = "/vpr") // This means URL's start with /demo (after Application path)
|
||||||
public class MainController {
|
public class MainController {
|
||||||
|
|
||||||
|
// This means to get the bean called userRepository
|
||||||
|
// Which is auto-generated by Spring, we will use it to handle the data
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
@Autowired
|
||||||
|
private EventRepository eventRepository;
|
||||||
|
@Autowired
|
||||||
|
private UserEventRepository userEventRepository;
|
||||||
|
|
||||||
@GetMapping(path = "/status-test")
|
@GetMapping(path = "/status-test")
|
||||||
public String statusTest(){
|
public String statusTest(){
|
||||||
throw new ResponseStatusException(HttpStatus.I_AM_A_TEAPOT, "TestTestTest");
|
throw new ResponseStatusException(HttpStatus.I_AM_A_TEAPOT, "TestTestTest");
|
||||||
|
@ -1,9 +1,6 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.controller;
|
package com.vpr.server.controller;
|
||||||
|
|
||||||
import com.vpr.server.dao.interfaces.UserDAO;
|
|
||||||
import com.vpr.server.data.User;
|
import com.vpr.server.data.User;
|
||||||
import com.vpr.server.json.JSONMapper;
|
|
||||||
import com.vpr.server.repository.UserRepository;
|
import com.vpr.server.repository.UserRepository;
|
||||||
import com.vpr.server.security.Hasher;
|
import com.vpr.server.security.Hasher;
|
||||||
import com.vpr.server.security.Token;
|
import com.vpr.server.security.Token;
|
||||||
@ -12,25 +9,17 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.spec.InvalidKeySpecException;
|
import java.security.spec.InvalidKeySpecException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(path = "/user")
|
@RequestMapping(path = "/user")
|
||||||
public class UserController {
|
public class UserController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
@Autowired
|
|
||||||
private UserDAO userDAO;
|
|
||||||
|
|
||||||
private final AuthController authController;
|
|
||||||
|
|
||||||
public UserController() {
|
|
||||||
this.authController = new AuthController();
|
|
||||||
}
|
|
||||||
|
|
||||||
/******************
|
/******************
|
||||||
* POST-ENDPOINTS *
|
* POST-ENDPOINTS *
|
||||||
@ -46,13 +35,13 @@ public class UserController {
|
|||||||
@RequestParam String password,
|
@RequestParam String password,
|
||||||
@RequestParam Boolean isAdmin
|
@RequestParam Boolean isAdmin
|
||||||
) {
|
) {
|
||||||
User authUser = authController.getAuthUserFromHeader(authorizationHeader, userRepository);
|
User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]);
|
||||||
if (authUser == null || !authUser.isAdmin()) {
|
if(authUser == null || authUser.isAdmin()){
|
||||||
return new ResponseEntity<>("Du hast keine Rechte um einen User an zu legen", HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>( "Du hast keine Rechte um den Termin zu löschen", HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userRepository.findByLogin(login) != null) {
|
if(userRepository.findByLogin(login) != null){
|
||||||
return new ResponseEntity<>("Login exestiert bereits", HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>( "Login exestiert bereits", HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] salt = Hasher.GenerateSalt();
|
byte[] salt = Hasher.GenerateSalt();
|
||||||
@ -61,7 +50,7 @@ public class UserController {
|
|||||||
hash = Hasher.HashPassword(password, salt);
|
hash = Hasher.HashPassword(password, salt);
|
||||||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
|
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return new ResponseEntity<>("Fehler beim hashen", HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>( "Fehler beim hashen", HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
User user = new User();
|
User user = new User();
|
||||||
@ -75,7 +64,7 @@ public class UserController {
|
|||||||
user.setAdmin(isAdmin);
|
user.setAdmin(isAdmin);
|
||||||
|
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
return new ResponseEntity<>("" + user.getId(), HttpStatus.OK);
|
return new ResponseEntity<>( "" + user.getId(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(path = "/login")
|
@PostMapping(path = "/login")
|
||||||
@ -84,32 +73,11 @@ public class UserController {
|
|||||||
@RequestParam String login,
|
@RequestParam String login,
|
||||||
@RequestParam String password
|
@RequestParam String password
|
||||||
) {
|
) {
|
||||||
if(userRepository.findAllUsernames().length == 0){
|
|
||||||
byte[] salt = Hasher.GenerateSalt();
|
|
||||||
byte[] hash;
|
|
||||||
try {
|
|
||||||
hash = Hasher.HashPassword(password, salt);
|
|
||||||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return new ResponseEntity<>("Fehler beim hashen", HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
User user = new User();
|
|
||||||
user.setName("Admin");
|
|
||||||
user.setForename(login);
|
|
||||||
user.setLogin(login);
|
|
||||||
user.setPassword(hash);
|
|
||||||
user.setSalt(salt);
|
|
||||||
user.setToken("");
|
|
||||||
user.setAdmin(true);
|
|
||||||
|
|
||||||
userRepository.save(user);
|
|
||||||
}
|
|
||||||
System.out.println(login + " tries to login.");
|
System.out.println(login + " tries to login.");
|
||||||
User user = userRepository.findByLogin(login);
|
User user = userRepository.findByLogin(login);
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
System.out.println("Login for " + login + " failed.");
|
System.out.println("Login for " + login + " failed.");
|
||||||
return new ResponseEntity<>("Falscher login", HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>( "Falscher login", HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] salt = user.getSalt();
|
byte[] salt = user.getSalt();
|
||||||
@ -118,7 +86,7 @@ public class UserController {
|
|||||||
hash = Hasher.HashPassword(password, salt);
|
hash = Hasher.HashPassword(password, salt);
|
||||||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
|
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return new ResponseEntity<>("Fehler beim hashen", HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>( "Fehler beim hashen", HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Arrays.equals(user.getPassword(), hash)) {
|
if (Arrays.equals(user.getPassword(), hash)) {
|
||||||
@ -129,104 +97,35 @@ public class UserController {
|
|||||||
System.out.println(user.getLogin() + " is now logged in.");
|
System.out.println(user.getLogin() + " is now logged in.");
|
||||||
System.out.println(Token.Verify(Token.Generate(user.getLogin()), user.getLogin()));
|
System.out.println(Token.Verify(Token.Generate(user.getLogin()), user.getLogin()));
|
||||||
|
|
||||||
return new ResponseEntity<>(token + " " + user.getId(), HttpStatus.OK);
|
return new ResponseEntity<>( token + " " + user.getId(), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
System.out.println(user.getLogin() + " failed to logged in.");
|
System.out.println(user.getLogin() + " failed to logged in.");
|
||||||
System.out.println("entered : " + javax.xml.bind.DatatypeConverter.printHexBinary(hash));
|
System.out.println("entered : " + javax.xml.bind.DatatypeConverter.printHexBinary(hash));
|
||||||
System.out.println("required: " + javax.xml.bind.DatatypeConverter.printHexBinary(user.getPassword()));
|
System.out.println("required: " + javax.xml.bind.DatatypeConverter.printHexBinary(user.getPassword()));
|
||||||
|
|
||||||
return new ResponseEntity<>("Falscher login", HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>( "Falscher login", HttpStatus.UNAUTHORIZED);
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping(path = "/login-with-token")
|
|
||||||
public @ResponseBody
|
|
||||||
ResponseEntity<String> loginWithToken(
|
|
||||||
@RequestHeader("Authorization") String authorizationHeader,
|
|
||||||
@RequestParam long userId
|
|
||||||
) {
|
|
||||||
User authUser = authController.getAuthUserFromHeader(authorizationHeader, userRepository);
|
|
||||||
if (authUser == null || authUser.getId() != userId) {
|
|
||||||
return new ResponseEntity<>("Falscher auth-token", HttpStatus.UNAUTHORIZED);
|
|
||||||
}
|
|
||||||
return new ResponseEntity<>("", HttpStatus.OK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(path = "/del")
|
@PostMapping(path = "/del")
|
||||||
public @ResponseBody
|
public @ResponseBody ResponseEntity<String> deleteUser(
|
||||||
ResponseEntity<String> deleteUser(
|
|
||||||
@RequestHeader("Authorization") String authorizationHeader,
|
@RequestHeader("Authorization") String authorizationHeader,
|
||||||
@RequestParam long userId
|
@RequestParam Integer userId
|
||||||
) {
|
) {
|
||||||
User authUser = authController.getAuthUserFromHeader(authorizationHeader, userRepository);
|
User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]);
|
||||||
if (authUser == null || !authUser.isAdmin()) {
|
if(authUser == null || authUser.isAdmin()){
|
||||||
return new ResponseEntity<>("Du hast keine Rechte um den User zu löschen", HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>( "Du hast keine Rechte um den Termin zu löschen", HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
User user = userRepository.findById(userId);
|
userRepository.deleteById(Long.valueOf(userId));
|
||||||
if (user == null) {
|
return new ResponseEntity<>( "", HttpStatus.OK);
|
||||||
return new ResponseEntity<>("User nicht in der Datenbank vorhanden", HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
if(!userDAO.deleteAllUserEvents(user.getId())){
|
|
||||||
return new ResponseEntity<>("User konnte nicht gelöscht werden", HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
userRepository.delete(user);
|
|
||||||
return new ResponseEntity<>("", HttpStatus.OK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(path = "/edit")
|
/*****************
|
||||||
public @ResponseBody ResponseEntity<String> editUser(
|
* GET-ENDPOINTS *
|
||||||
@RequestHeader("Authorization") String authorizationHeader,
|
*****************/
|
||||||
@RequestParam long userId,
|
|
||||||
@RequestParam String name,
|
|
||||||
@RequestParam String forename,
|
|
||||||
@RequestParam String login,
|
|
||||||
@RequestParam(required = false) String password,
|
|
||||||
@RequestParam Boolean isAdmin
|
|
||||||
) {
|
|
||||||
User authUser = authController.getAuthUserFromHeader(authorizationHeader, userRepository);
|
|
||||||
if (authUser == null || (!authUser.isAdmin() && authUser.getId() != userId)) {
|
|
||||||
return new ResponseEntity<>("Du hast keine Rechte um den User zu editieren", HttpStatus.UNAUTHORIZED);
|
|
||||||
}
|
|
||||||
if(isAdmin && !authUser.isAdmin()){
|
|
||||||
return new ResponseEntity<>("Du hast keine Rechte um dich zum Admin zu machen", HttpStatus.UNAUTHORIZED);
|
|
||||||
}
|
|
||||||
User user = userRepository.findById(userId);
|
|
||||||
if (user == null) {
|
|
||||||
return new ResponseEntity<>("User nicht in der Datenbank vorhanden", HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
User userWithLogin = userRepository.findByLogin(login);
|
@GetMapping(path = "/all")
|
||||||
if (userWithLogin != null && userWithLogin.getId() != userId) {
|
|
||||||
return new ResponseEntity<>("Login exestiert bereits", HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(password != null){
|
|
||||||
byte[] salt = Hasher.GenerateSalt();
|
|
||||||
byte[] hash;
|
|
||||||
try {
|
|
||||||
hash = Hasher.HashPassword(password, salt);
|
|
||||||
user.setPassword(hash);
|
|
||||||
user.setSalt(salt);
|
|
||||||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return new ResponseEntity<>("Fehler beim hashen", HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
user.setName(name);
|
|
||||||
user.setForename(forename);
|
|
||||||
user.setLogin(login);
|
|
||||||
user.setToken("");
|
|
||||||
user.setAdmin(isAdmin);
|
|
||||||
|
|
||||||
userRepository.save(user);
|
|
||||||
return new ResponseEntity<>("", HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping(path = "/all")
|
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<String> getAllUser() {
|
Object[] getAllUsers() {
|
||||||
List<User> userList = userDAO.getAllUser();
|
return userRepository.findAllUsernames();
|
||||||
|
|
||||||
return new ResponseEntity<>(JSONMapper.userListToJSON(userList), HttpStatus.OK);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,49 +0,0 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.dao.implementation;
|
|
||||||
|
|
||||||
import com.vpr.server.dao.interfaces.EventDAO;
|
|
||||||
import com.vpr.server.data.Event;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.PersistenceContext;
|
|
||||||
import javax.transaction.Transactional;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
@Transactional
|
|
||||||
public class EventDAOImplementation implements EventDAO {
|
|
||||||
|
|
||||||
@PersistenceContext
|
|
||||||
private EntityManager manager;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Event> getAllEvents() {
|
|
||||||
return manager.createNamedQuery("getAllEvents", Event.class).getResultList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Event> getAllEventsWithId(long eventId) {
|
|
||||||
return manager.createNamedQuery("getAllEventsWithId", Event.class)
|
|
||||||
.setParameter("eventId", eventId)
|
|
||||||
.getResultList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Event> getAllEventsInTimespan(long userId, String startDate, String endDate) {
|
|
||||||
return manager.createNamedQuery("getAllEventsInTimespan", Event.class)
|
|
||||||
.setParameter("userId", userId)
|
|
||||||
.setParameter("startDate", startDate)
|
|
||||||
.setParameter("endDate", endDate)
|
|
||||||
.getResultList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Event> getAllEventsWithIdAndDate(long userId, long eventId, String date) {
|
|
||||||
return manager.createNamedQuery("getAllEventsWithIdAndDate", Event.class)
|
|
||||||
.setParameter("userId", userId)
|
|
||||||
.setParameter("eventId", eventId)
|
|
||||||
.setParameter("date", date)
|
|
||||||
.getResultList();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,37 +0,0 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.dao.implementation;
|
|
||||||
|
|
||||||
import com.vpr.server.dao.interfaces.UserDAO;
|
|
||||||
import com.vpr.server.data.User;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.PersistenceContext;
|
|
||||||
import javax.transaction.Transactional;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
@Transactional
|
|
||||||
public class UserDAOImplementation implements UserDAO {
|
|
||||||
|
|
||||||
@PersistenceContext
|
|
||||||
private EntityManager manager;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<User> getAllUser() {
|
|
||||||
return manager.createNamedQuery("getAllUser", User.class).getResultList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean deleteAllUserEvents(long userId) {
|
|
||||||
try {
|
|
||||||
manager.createNamedQuery("deleteAllUserEvents", User.class)
|
|
||||||
.setParameter("userId", userId)
|
|
||||||
.executeUpdate();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}catch (Exception e){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,17 +0,0 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.dao.interfaces;
|
|
||||||
|
|
||||||
import com.vpr.server.data.Event;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface EventDAO {
|
|
||||||
|
|
||||||
List<Event> getAllEvents();
|
|
||||||
|
|
||||||
List<Event> getAllEventsInTimespan(long userId, String startDate, String endDate);
|
|
||||||
|
|
||||||
List<Event> getAllEventsWithIdAndDate(long userId, long eventId, String date);
|
|
||||||
|
|
||||||
List<Event> getAllEventsWithId(long eventId);
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.dao.interfaces;
|
|
||||||
|
|
||||||
import com.vpr.server.data.User;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface UserDAO {
|
|
||||||
List<User> getAllUser();
|
|
||||||
|
|
||||||
boolean deleteAllUserEvents(long userId);
|
|
||||||
}
|
|
@ -1,4 +1,3 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.data;
|
package com.vpr.server.data;
|
||||||
|
|
||||||
import java.sql.Date;
|
import java.sql.Date;
|
||||||
|
@ -1,53 +1,35 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.data;
|
package com.vpr.server.data;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.io.Serializable;
|
|
||||||
import java.sql.Time;
|
import java.sql.Time;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Entity(name = "Event") // @Entity creates a table out of this class with Hibernate
|
@NamedNativeQuery(name = "Event.findEventsInDateRange",
|
||||||
@Table(name = "event")
|
query = "SELECT e.id as id, e.name as name, e.priority as priority, e.is_full_day as isFullDay, " +
|
||||||
@NamedNativeQueries({
|
"is_private as isPrivate, e.start as start, e.end as end " +
|
||||||
@NamedNativeQuery(
|
"FROM event e " +
|
||||||
name = "getAllEvents",
|
"INNER JOIN user_event ue " +
|
||||||
query = "SELECT * FROM event",
|
"ON e.id = ue.event_id " +
|
||||||
resultClass = Event.class
|
"WHERE (ue.user_id = :userId OR e.is_private = 0) " +
|
||||||
),
|
"AND ue.date > :startDate " +
|
||||||
@NamedNativeQuery(
|
"AND ue.date < :endDate",
|
||||||
name = "getAllEventsInTimespan",
|
resultSetMapping = "Mapping.Event"
|
||||||
query = "SELECT * " +
|
)
|
||||||
"FROM event e " +
|
@SqlResultSetMapping(name = "Mapping.Event",
|
||||||
"INNER JOIN user_event ue " +
|
classes = @ConstructorResult(targetClass = Event.class,
|
||||||
"ON e.id = ue.event_id " +
|
columns = {
|
||||||
"WHERE (ue.user_id = :userId OR e.is_private = 0) " +
|
@ColumnResult(name = "id"),
|
||||||
"AND ue.date >= :startDate " +
|
@ColumnResult(name = "name"),
|
||||||
"AND ue.date < :endDate " +
|
@ColumnResult(name = "priority"),
|
||||||
"ORDER BY ue.date, e.priority DESC, e.start",
|
@ColumnResult(name = "isFullDay"),
|
||||||
resultClass = Event.class
|
@ColumnResult(name = "isPrivate"),
|
||||||
),
|
@ColumnResult(name = "start"),
|
||||||
@NamedNativeQuery(
|
@ColumnResult(name = "end")
|
||||||
name = "getAllEventsWithIdAndDate",
|
}
|
||||||
query = "SELECT * " +
|
|
||||||
"FROM event e " +
|
|
||||||
"INNER JOIN user_event ue " +
|
|
||||||
"ON e.id = ue.event_id " +
|
|
||||||
"WHERE ue.user_id = :userId " +
|
|
||||||
"AND ue.event_id = :eventId " +
|
|
||||||
"AND ue.date = :date",
|
|
||||||
resultClass = Event.class
|
|
||||||
),
|
|
||||||
@NamedNativeQuery(
|
|
||||||
name = "getAllEventsWithId",
|
|
||||||
query = "SELECT * " +
|
|
||||||
"FROM event e " +
|
|
||||||
"INNER JOIN user_event ue " +
|
|
||||||
"ON e.id = ue.event_id " +
|
|
||||||
"WHERE ue.event_id = :eventId",
|
|
||||||
resultClass = Event.class
|
|
||||||
)
|
)
|
||||||
})
|
)
|
||||||
public class Event implements Serializable {
|
@Entity(name = "Event") // @Entity creates a table out of this class with Hibernate
|
||||||
|
public class Event {
|
||||||
// Generate the primary key
|
// Generate the primary key
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
@ -155,20 +137,4 @@ public class Event implements Serializable {
|
|||||||
", userEvent=" + userEvent +
|
", userEvent=" + userEvent +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj){
|
|
||||||
if(!(obj instanceof Event)){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Event event = (Event) obj;
|
|
||||||
System.out.println(event.getId() + " " + getId());
|
|
||||||
return event.getId() == getId();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode(){
|
|
||||||
return (int)getId();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,10 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.data;
|
package com.vpr.server.data;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
// @Entity creates a table out of this class with Hibernate
|
// @Entity creates a table out of this class with Hibernate
|
||||||
@Entity(name = "User")
|
@Entity
|
||||||
@Table(name = "user")
|
|
||||||
|
|
||||||
@SqlResultSetMapping(name="deleteResult", columns = {
|
|
||||||
@ColumnResult(name = "count")
|
|
||||||
})
|
|
||||||
|
|
||||||
@NamedNativeQueries({
|
|
||||||
@NamedNativeQuery(
|
|
||||||
name = "getAllUser",
|
|
||||||
query = "SELECT * FROM user",
|
|
||||||
resultClass = User.class
|
|
||||||
),
|
|
||||||
@NamedNativeQuery(
|
|
||||||
name = "deleteAllUserEvents",
|
|
||||||
query = "DELETE FROM user_event WHERE user_id = :userId",
|
|
||||||
resultSetMapping = "deleteResult"
|
|
||||||
)
|
|
||||||
})
|
|
||||||
public class User {
|
public class User {
|
||||||
// Generate the primary key
|
// Generate the primary key
|
||||||
@Id
|
@Id
|
||||||
@ -129,19 +110,4 @@ public class User {
|
|||||||
public void setEventList(List<UserEvent> userEvent) {
|
public void setEventList(List<UserEvent> userEvent) {
|
||||||
this.userEvent = userEvent;
|
this.userEvent = userEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj){
|
|
||||||
if(!(obj instanceof User)){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
User user = (User) obj;
|
|
||||||
return user.getId() == getId();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode(){
|
|
||||||
return (int)getId();
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,4 +1,3 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.data;
|
package com.vpr.server.data;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
@ -61,41 +60,4 @@ public class UserEvent {
|
|||||||
", date=" + date +
|
", date=" + date +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj){
|
|
||||||
System.out.println("equals");
|
|
||||||
if(!(obj instanceof UserEvent)){
|
|
||||||
System.out.println("not an userevent");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
UserEvent userEvent = (UserEvent) obj;
|
|
||||||
|
|
||||||
System.out.println("date " + userEvent.getDate().equals(getDate()));
|
|
||||||
System.out.println("user " + userEvent.getUser().equals(getUser()));
|
|
||||||
System.out.println("event " + userEvent.getEvent().equals(getEvent()));
|
|
||||||
|
|
||||||
return userEvent.getDate().equals(getDate()) &&
|
|
||||||
userEvent.getUser().equals(getUser()) &&
|
|
||||||
userEvent.getEvent().equals(getEvent());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode(){
|
|
||||||
long hash = getUser().hashCode() +
|
|
||||||
getEvent().hashCode() +
|
|
||||||
getDate().hashCode();
|
|
||||||
|
|
||||||
return (int)hash;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.data;
|
package com.vpr.server.data;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.vpr.server.entries;
|
||||||
|
|
||||||
|
import com.vpr.server.data.Event;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
public class EventEntry {
|
||||||
|
|
||||||
|
}
|
@ -1,80 +0,0 @@
|
|||||||
//Marco Kühn//
|
|
||||||
package com.vpr.server.json;
|
|
||||||
|
|
||||||
import com.vpr.server.data.Event;
|
|
||||||
import com.vpr.server.data.User;
|
|
||||||
import com.vpr.server.data.UserEvent;
|
|
||||||
|
|
||||||
import java.sql.Time;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class JSONMapper {
|
|
||||||
|
|
||||||
public static String userToJSON(User user) {
|
|
||||||
return "{" +
|
|
||||||
"\"userId\": " + user.getId() + ", " +
|
|
||||||
"\"forename\": \"" + user.getForename() + "\", " +
|
|
||||||
"\"name\": \"" + user.getName() + "\", " +
|
|
||||||
"\"login\": \"" + user.getLogin() + "\"," +
|
|
||||||
"\"admin\": " + user.isAdmin() +
|
|
||||||
"}";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String userListToJSON(List<User> userList) {
|
|
||||||
StringBuilder userListJSON = new StringBuilder();
|
|
||||||
for (User user : userList) {
|
|
||||||
userListJSON.append(", ");
|
|
||||||
userListJSON.append(userToJSON(user));
|
|
||||||
}
|
|
||||||
userListJSON.delete(0, 2);
|
|
||||||
|
|
||||||
return "[" + userListJSON + "]";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<String> eventToJSON(Event event) {
|
|
||||||
List<String> eventListJSON = new ArrayList<>();
|
|
||||||
|
|
||||||
for (UserEvent userEvent : event.getUserEvent()) {
|
|
||||||
|
|
||||||
String eventJSON = "{" +
|
|
||||||
"\"ownerId\": " + userEvent.getUser().getId() + ", " +
|
|
||||||
"\"ownerName\": \"" + userEvent.getUser().getForename() + " " + userEvent.getUser().getName() + "\", " +
|
|
||||||
"\"date\": \"" + userEvent.getDate() + "\", " +
|
|
||||||
"\"id\": " + event.getId() + "," +
|
|
||||||
"\"name\": \"" + event.getName() + "\"," +
|
|
||||||
"\"priority\": " + event.getPriority() + "," +
|
|
||||||
"\"fullDay\": " + event.isFullDay() + "," +
|
|
||||||
"\"private\": " + event.isPrivate() + "," +
|
|
||||||
"\"start\": " + timeToJSON(event.getStart()) + "," +
|
|
||||||
"\"end\": " + timeToJSON(event.getEnd()) +
|
|
||||||
"}";
|
|
||||||
|
|
||||||
eventListJSON.add(eventJSON);
|
|
||||||
}
|
|
||||||
|
|
||||||
return eventListJSON;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String eventListToJSON(List<Event> eventList) {
|
|
||||||
StringBuilder eventListJSON = new StringBuilder();
|
|
||||||
for (Event event : eventList) {
|
|
||||||
List<String> eventsJSON = eventToJSON(event);
|
|
||||||
for (String eventJSON : eventsJSON) {
|
|
||||||
eventListJSON.append(", ");
|
|
||||||
eventListJSON.append(eventJSON);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
eventListJSON.delete(0, 2);
|
|
||||||
|
|
||||||
return "[" + eventListJSON + "]";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String timeToJSON(Time time) {
|
|
||||||
if (time == null) {
|
|
||||||
return "null";
|
|
||||||
}
|
|
||||||
|
|
||||||
return "\"" + time + "\"";
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,44 +0,0 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.json;
|
|
||||||
|
|
||||||
import java.sql.Date;
|
|
||||||
import java.sql.Time;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class Validator {
|
|
||||||
public static String ValidateEventName(String name) throws IllegalArgumentException {
|
|
||||||
if (name.length() < 3) {
|
|
||||||
System.out.println("NAME TO SHORT");
|
|
||||||
throw new IllegalArgumentException("Der Name ist zu kurz");
|
|
||||||
}
|
|
||||||
Pattern pattern = Pattern.compile("[A-Za-z\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df0-9 =!?+*/$.:,;_<>()-]*");
|
|
||||||
Matcher matcher = pattern.matcher(name);
|
|
||||||
if(!matcher.matches()){
|
|
||||||
System.out.println("NAME HAS ILLEGALCHARS");
|
|
||||||
throw new IllegalArgumentException("Der Name enthält nicht erlaubte Zeichen");
|
|
||||||
}
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Time ValidateEventTime(String time) throws IllegalArgumentException {
|
|
||||||
try {
|
|
||||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm");
|
|
||||||
long ms = simpleDateFormat.parse(time).getTime();
|
|
||||||
return new Time(ms);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Date ValidateEventDate(String date) throws IllegalArgumentException {
|
|
||||||
try {
|
|
||||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
||||||
return new Date(simpleDateFormat.parse(date).getTime());
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("DATE FORMAT NOT CORRECT");
|
|
||||||
throw new IllegalArgumentException("Datumformat nicht korrekt");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,12 +1,18 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.repository;
|
package com.vpr.server.repository;
|
||||||
|
|
||||||
import com.vpr.server.data.Event;
|
import com.vpr.server.data.Event;
|
||||||
|
import com.vpr.server.data.UserEvent;
|
||||||
import org.springframework.data.jpa.repository.Modifying;
|
import org.springframework.data.jpa.repository.Modifying;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
import javax.persistence.ColumnResult;
|
||||||
|
import javax.persistence.ConstructorResult;
|
||||||
|
import javax.persistence.NamedNativeQuery;
|
||||||
|
import javax.persistence.SqlResultSetMapping;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
// This will be AUTO IMPLEMENTED by Spring into a Bean called eventRepository
|
// This will be AUTO IMPLEMENTED by Spring into a Bean called eventRepository
|
||||||
// CRUD refers Create, Read, Update, Delete
|
// CRUD refers Create, Read, Update, Delete
|
||||||
@ -50,7 +56,7 @@ public interface EventRepository extends CrudRepository<Event, Integer> {
|
|||||||
)
|
)
|
||||||
UserEventInterface findUserEventByEventIdUserIdAndDate(long eventId, long userId, String date);
|
UserEventInterface findUserEventByEventIdUserIdAndDate(long eventId, long userId, String date);
|
||||||
|
|
||||||
interface UserEventInterface{
|
public interface UserEventInterface{
|
||||||
long getEventId();
|
long getEventId();
|
||||||
long getUserId();
|
long getUserId();
|
||||||
long getDate();
|
long getDate();
|
||||||
@ -59,10 +65,10 @@ public interface EventRepository extends CrudRepository<Event, Integer> {
|
|||||||
@Modifying
|
@Modifying
|
||||||
@Transactional
|
@Transactional
|
||||||
@Query(
|
@Query(
|
||||||
value = "DELETE ue FROM user_event ue WHERE ue.event_id = :eventId AND ue.user_id = :userId AND ue.date = :date",
|
value = "DELETE ue FROM user_event ue WHERE ue.event_id = ?1",
|
||||||
nativeQuery = true
|
nativeQuery = true
|
||||||
)
|
)
|
||||||
void deleteUserEventsById(long userId, long eventId, String date);
|
void deleteUserEventsById(long id);
|
||||||
|
|
||||||
|
|
||||||
@Modifying
|
@Modifying
|
||||||
@ -72,4 +78,10 @@ public interface EventRepository extends CrudRepository<Event, Integer> {
|
|||||||
nativeQuery = true
|
nativeQuery = true
|
||||||
)
|
)
|
||||||
void deleteById(long id);
|
void deleteById(long id);
|
||||||
|
|
||||||
|
|
||||||
|
@Query(nativeQuery = true)
|
||||||
|
List<Event> findEventsInDateRange(Long userId, String startDate, String endDate);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -1,15 +1,11 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.repository;
|
package com.vpr.server.repository;
|
||||||
|
|
||||||
import com.vpr.server.data.UserEvent;
|
import com.vpr.server.data.UserEvent;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
import java.sql.Date;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
// This will be AUTO IMPLEMENTED by Spring into a Bean called eventListRepository
|
// This will be AUTO IMPLEMENTED by Spring into a Bean called eventListRepository
|
||||||
// CRUD refers Create, Read, Update, Delete
|
// CRUD refers Create, Read, Update, Delete
|
||||||
|
|
||||||
public interface UserEventRepository extends CrudRepository<UserEvent, Integer> {
|
public interface UserEventRepository extends CrudRepository<UserEvent, Integer> {
|
||||||
List<UserEvent> findByUserIdAndDate(long userId, Date date);
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.repository;
|
package com.vpr.server.repository;
|
||||||
|
|
||||||
import com.vpr.server.data.User;
|
import com.vpr.server.data.User;
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.security;
|
package com.vpr.server.security;
|
||||||
|
|
||||||
import javax.crypto.SecretKeyFactory;
|
import javax.crypto.SecretKeyFactory;
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
//Marc Beyer//
|
|
||||||
package com.vpr.server.security;
|
package com.vpr.server.security;
|
||||||
|
|
||||||
import io.jsonwebtoken.JwtException;
|
import io.jsonwebtoken.JwtException;
|
||||||
import io.jsonwebtoken.Jwts;
|
import io.jsonwebtoken.Jwts;
|
||||||
import io.jsonwebtoken.SignatureAlgorithm;
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
import io.jsonwebtoken.security.Keys;
|
import io.jsonwebtoken.security.Keys;
|
||||||
|
|
||||||
import java.security.Key;
|
import java.security.Key;
|
||||||
|
|
||||||
public class Token {
|
public class Token {
|
||||||
|
|
||||||
private static final Key KEY = Keys.secretKeyFor(SignatureAlgorithm.HS256);
|
private static Key KEY = Keys.secretKeyFor(SignatureAlgorithm.HS256);
|
||||||
|
|
||||||
public static String Generate(String subject){
|
public static String Generate(String subject){
|
||||||
return Jwts.builder().setSubject(subject).signWith(KEY).compact();
|
return Jwts.builder().setSubject(subject).signWith(KEY).compact();
|
||||||
|
Loading…
Reference in New Issue
Block a user