Compare commits
41 Commits
f3a6cdb3e9
...
merge
Author | SHA1 | Date | |
---|---|---|---|
095dd861c5 | |||
5cdab5b434 | |||
43c967638b | |||
24794e2085 | |||
fcfeaf0979 | |||
3b711b5d9f | |||
5066afbb92 | |||
609c0353d0 | |||
2918bc46b5 | |||
bba0b6d5d2 | |||
710b661b0d | |||
f22bdf3040 | |||
3575dccf94 | |||
8f04ac7ae8 | |||
56919ab412 | |||
e3408d1566 | |||
eea38d578b | |||
031c1cea2a | |||
154617ce88 | |||
35e7350962 | |||
37d275d537 | |||
3796afb712 | |||
e05faab31e | |||
d5b6d6357e | |||
0531f868d0 | |||
de1dcf7673 | |||
e062d9254f | |||
eeef96ffb6 | |||
68eb7e5863 | |||
8d6a850d30 | |||
bfbcd5c44b | |||
540d3a130f | |||
8382b80e0e | |||
8ffaf525eb | |||
f0e89184a4 | |||
768c57a234 | |||
2cec62c453 | |||
281d2947cd | |||
fcdaf2604f | |||
e953055bce | |||
2ae8237db4 |
24
.gitignore
vendored
Normal file
24
.gitignore
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# Compiled class file
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Log file
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Package Files
|
||||||
|
*.jar
|
||||||
|
*.war
|
||||||
|
*.nar
|
||||||
|
*.ear
|
||||||
|
*.zip
|
||||||
|
*.tar.gz
|
||||||
|
*.rar
|
||||||
|
|
||||||
|
|
||||||
|
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||||
|
hs_err_pid*
|
||||||
|
|
||||||
|
# private file with login-data
|
||||||
|
application.properties
|
||||||
|
|
||||||
|
.idea/
|
||||||
|
.gradle/
|
29
README.md
29
README.md
@@ -1,2 +1,31 @@
|
|||||||
# VPR-Backend
|
# VPR-Backend
|
||||||
|
This project includes the backend for the VPR-project.
|
||||||
|
|
||||||
|
# Installation and Getting Started
|
||||||
|
- To run the server you first have to install a MySQL database.
|
||||||
|
> For example MariaDB (https://mariadb.com/downloads/)
|
||||||
|
|
||||||
|
- Then you have to create a the database with the "create VPR-DB.sql"-file.
|
||||||
|
|
||||||
|
- Create a new user for the db e.g. named "springuser"
|
||||||
|
> The database should only be reachable from localhost to improve the security!
|
||||||
|
|
||||||
|
- Next create a "application.properties"-file in "vpr/src/main/resources". It should contain following content:
|
||||||
|
```
|
||||||
|
# "update" will automatically get the db-structure
|
||||||
|
# It can be "none", "update", "create", or "create-drop", but you have to start with "create" or "update" and should use "none" in production(for security).
|
||||||
|
# see https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#configurations-hbmddl for more information.
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
|
||||||
|
# Specify the database address, port and name
|
||||||
|
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/vpr
|
||||||
|
|
||||||
|
# Add the login-data
|
||||||
|
spring.datasource.username=<USERNAME>
|
||||||
|
spring.datasource.password=<PASSWORD>
|
||||||
|
|
||||||
|
spring.datasource.driver-class-name =com.mysql.jdbc.Driver
|
||||||
|
#spring.jpa.show-sql: true
|
||||||
|
```
|
||||||
|
|
||||||
|
You should now be able to start the application :D
|
91
create VPR-DB.sql
Normal file
91
create VPR-DB.sql
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
-- --------------------------------------------------------
|
||||||
|
-- Host: 127.0.0.1
|
||||||
|
-- Server Version: 10.6.4-MariaDB - mariadb.org binary distribution
|
||||||
|
-- Server Betriebssystem: Win64
|
||||||
|
-- HeidiSQL Version: 11.3.0.6295
|
||||||
|
-- --------------------------------------------------------
|
||||||
|
|
||||||
|
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||||
|
/*!40101 SET NAMES utf8 */;
|
||||||
|
/*!50503 SET NAMES utf8mb4 */;
|
||||||
|
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
|
||||||
|
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
|
||||||
|
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
|
||||||
|
|
||||||
|
|
||||||
|
-- Exportiere Datenbank Struktur für vpr
|
||||||
|
CREATE DATABASE IF NOT EXISTS `vpr` /*!40100 DEFAULT CHARACTER SET utf8mb3 */;
|
||||||
|
USE `vpr`;
|
||||||
|
|
||||||
|
-- Exportiere Struktur von Tabelle vpr.event
|
||||||
|
CREATE TABLE IF NOT EXISTS `event` (
|
||||||
|
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||||
|
`name` varchar(20) NOT NULL DEFAULT '0',
|
||||||
|
`priority` tinyint(2) unsigned NOT NULL DEFAULT 0,
|
||||||
|
`is_full_day` bit(1) NOT NULL DEFAULT b'0',
|
||||||
|
`start` time DEFAULT NULL,
|
||||||
|
`end` time DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id`) USING BTREE
|
||||||
|
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb3;
|
||||||
|
|
||||||
|
-- Exportiere Daten aus Tabelle vpr.event: ~2 rows (ungefähr)
|
||||||
|
/*!40000 ALTER TABLE `event` DISABLE KEYS */;
|
||||||
|
INSERT INTO `event` (`id`, `name`, `priority`, `is_full_day`, `start`, `end`) VALUES
|
||||||
|
(1, 'TestEvent', 0, b'0', '00:00:00', '00:00:00'),
|
||||||
|
(2, 'Event 2', 1, b'0', NULL, NULL);
|
||||||
|
/*!40000 ALTER TABLE `event` ENABLE KEYS */;
|
||||||
|
|
||||||
|
-- Exportiere Struktur von Tabelle vpr.event_list
|
||||||
|
CREATE TABLE IF NOT EXISTS `event_list` (
|
||||||
|
`id` int(10) NOT NULL AUTO_INCREMENT,
|
||||||
|
`user_id` int(10) unsigned NOT NULL,
|
||||||
|
`event_id` int(10) unsigned NOT NULL,
|
||||||
|
`date` date DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
KEY `UID` (`user_id`) USING BTREE,
|
||||||
|
KEY `EID` (`event_id`) USING BTREE,
|
||||||
|
CONSTRAINT `event_id` FOREIGN KEY (`event_id`) REFERENCES `event` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
|
||||||
|
CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
|
||||||
|
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb3;
|
||||||
|
|
||||||
|
-- Exportiere Daten aus Tabelle vpr.event_list: ~2 rows (ungefähr)
|
||||||
|
/*!40000 ALTER TABLE `event_list` DISABLE KEYS */;
|
||||||
|
INSERT INTO `event_list` (`id`, `user_id`, `event_id`, `date`) VALUES
|
||||||
|
(1, 1, 2, '2021-11-11'),
|
||||||
|
(2, 2, 1, '2021-12-24');
|
||||||
|
/*!40000 ALTER TABLE `event_list` ENABLE KEYS */;
|
||||||
|
|
||||||
|
-- Exportiere Struktur von Tabelle vpr.hibernate_sequence
|
||||||
|
CREATE TABLE IF NOT EXISTS `hibernate_sequence` (
|
||||||
|
`next_val` bigint(20) DEFAULT NULL
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
|
||||||
|
|
||||||
|
-- Exportiere Daten aus Tabelle vpr.hibernate_sequence: ~1 rows (ungefähr)
|
||||||
|
/*!40000 ALTER TABLE `hibernate_sequence` DISABLE KEYS */;
|
||||||
|
INSERT INTO `hibernate_sequence` (`next_val`) VALUES
|
||||||
|
(3);
|
||||||
|
/*!40000 ALTER TABLE `hibernate_sequence` ENABLE KEYS */;
|
||||||
|
|
||||||
|
-- Exportiere Struktur von Tabelle vpr.user
|
||||||
|
CREATE TABLE IF NOT EXISTS `user` (
|
||||||
|
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||||
|
`name` varchar(20) NOT NULL DEFAULT '0',
|
||||||
|
`forename` varchar(20) NOT NULL DEFAULT '0',
|
||||||
|
`password` varchar(50) NOT NULL DEFAULT '0',
|
||||||
|
`token` varchar(50) DEFAULT '0',
|
||||||
|
`is_admin` bit(1) NOT NULL,
|
||||||
|
PRIMARY KEY (`id`) USING BTREE
|
||||||
|
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb3;
|
||||||
|
|
||||||
|
-- Exportiere Daten aus Tabelle vpr.user: ~3 rows (ungefähr)
|
||||||
|
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
|
||||||
|
INSERT INTO `user` (`id`, `name`, `forename`, `password`, `token`, `is_admin`) VALUES
|
||||||
|
(1, 'Beyer', 'Marc', 'test', 'test', b'1'),
|
||||||
|
(2, 'Kühn', 'Marco', 'test', 'test', b'0'),
|
||||||
|
(3, 'Rechtin', 'Alex', 'test', 'test', b'1');
|
||||||
|
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
|
||||||
|
|
||||||
|
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
|
||||||
|
/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */;
|
||||||
|
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||||
|
/*!40111 SET SQL_NOTES=IFNULL(@OLD_SQL_NOTES, 1) */;
|
37
server/.gitignore
vendored
Normal file
37
server/.gitignore
vendored
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
HELP.md
|
||||||
|
.gradle
|
||||||
|
build/
|
||||||
|
!gradle/wrapper/gradle-wrapper.jar
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
bin/
|
||||||
|
!**/src/main/**/bin/
|
||||||
|
!**/src/test/**/bin/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
out/
|
||||||
|
!**/src/main/**/out/
|
||||||
|
!**/src/test/**/out/
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
29
server/build.gradle
Normal file
29
server/build.gradle
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
plugins {
|
||||||
|
id 'org.springframework.boot' version '2.5.6'
|
||||||
|
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
|
||||||
|
id 'java'
|
||||||
|
}
|
||||||
|
|
||||||
|
group = 'com.vpr'
|
||||||
|
version = '0.0.1-SNAPSHOT'
|
||||||
|
sourceCompatibility = '11'
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
|
runtimeOnly 'mysql:mysql-connector-java'
|
||||||
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
|
|
||||||
|
// JSON web token
|
||||||
|
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
|
||||||
|
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2',
|
||||||
|
'io.jsonwebtoken:jjwt-jackson:0.11.2'
|
||||||
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
BIN
server/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
server/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
5
server/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
5
server/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
distributionBase=GRADLE_USER_HOME
|
||||||
|
distributionPath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
|
||||||
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
zipStorePath=wrapper/dists
|
234
server/gradlew
vendored
Normal file
234
server/gradlew
vendored
Normal file
@@ -0,0 +1,234 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright © 2015-2021 the original authors.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
#
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
#
|
||||||
|
# Gradle start up script for POSIX generated by Gradle.
|
||||||
|
#
|
||||||
|
# Important for running:
|
||||||
|
#
|
||||||
|
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
||||||
|
# noncompliant, but you have some other compliant shell such as ksh or
|
||||||
|
# bash, then to run this script, type that shell name before the whole
|
||||||
|
# command line, like:
|
||||||
|
#
|
||||||
|
# ksh Gradle
|
||||||
|
#
|
||||||
|
# Busybox and similar reduced shells will NOT work, because this script
|
||||||
|
# requires all of these POSIX shell features:
|
||||||
|
# * functions;
|
||||||
|
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
||||||
|
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
||||||
|
# * compound commands having a testable exit status, especially «case»;
|
||||||
|
# * various built-in commands including «command», «set», and «ulimit».
|
||||||
|
#
|
||||||
|
# Important for patching:
|
||||||
|
#
|
||||||
|
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
||||||
|
# by Bash, Ksh, etc; in particular arrays are avoided.
|
||||||
|
#
|
||||||
|
# The "traditional" practice of packing multiple parameters into a
|
||||||
|
# space-separated string is a well documented source of bugs and security
|
||||||
|
# problems, so this is (mostly) avoided, by progressively accumulating
|
||||||
|
# options in "$@", and eventually passing that to Java.
|
||||||
|
#
|
||||||
|
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
||||||
|
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
||||||
|
# see the in-line comments for details.
|
||||||
|
#
|
||||||
|
# There are tweaks for specific operating systems such as AIX, CygWin,
|
||||||
|
# Darwin, MinGW, and NonStop.
|
||||||
|
#
|
||||||
|
# (3) This script is generated from the Groovy template
|
||||||
|
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||||
|
# within the Gradle project.
|
||||||
|
#
|
||||||
|
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||||
|
#
|
||||||
|
##############################################################################
|
||||||
|
|
||||||
|
# Attempt to set APP_HOME
|
||||||
|
|
||||||
|
# Resolve links: $0 may be a link
|
||||||
|
app_path=$0
|
||||||
|
|
||||||
|
# Need this for daisy-chained symlinks.
|
||||||
|
while
|
||||||
|
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
||||||
|
[ -h "$app_path" ]
|
||||||
|
do
|
||||||
|
ls=$( ls -ld "$app_path" )
|
||||||
|
link=${ls#*' -> '}
|
||||||
|
case $link in #(
|
||||||
|
/*) app_path=$link ;; #(
|
||||||
|
*) app_path=$APP_HOME$link ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
|
||||||
|
|
||||||
|
APP_NAME="Gradle"
|
||||||
|
APP_BASE_NAME=${0##*/}
|
||||||
|
|
||||||
|
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
|
|
||||||
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
|
MAX_FD=maximum
|
||||||
|
|
||||||
|
warn () {
|
||||||
|
echo "$*"
|
||||||
|
} >&2
|
||||||
|
|
||||||
|
die () {
|
||||||
|
echo
|
||||||
|
echo "$*"
|
||||||
|
echo
|
||||||
|
exit 1
|
||||||
|
} >&2
|
||||||
|
|
||||||
|
# OS specific support (must be 'true' or 'false').
|
||||||
|
cygwin=false
|
||||||
|
msys=false
|
||||||
|
darwin=false
|
||||||
|
nonstop=false
|
||||||
|
case "$( uname )" in #(
|
||||||
|
CYGWIN* ) cygwin=true ;; #(
|
||||||
|
Darwin* ) darwin=true ;; #(
|
||||||
|
MSYS* | MINGW* ) msys=true ;; #(
|
||||||
|
NONSTOP* ) nonstop=true ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
|
# Determine the Java command to use to start the JVM.
|
||||||
|
if [ -n "$JAVA_HOME" ] ; then
|
||||||
|
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||||
|
# IBM's JDK on AIX uses strange locations for the executables
|
||||||
|
JAVACMD=$JAVA_HOME/jre/sh/java
|
||||||
|
else
|
||||||
|
JAVACMD=$JAVA_HOME/bin/java
|
||||||
|
fi
|
||||||
|
if [ ! -x "$JAVACMD" ] ; then
|
||||||
|
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
JAVACMD=java
|
||||||
|
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Increase the maximum file descriptors if we can.
|
||||||
|
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||||
|
case $MAX_FD in #(
|
||||||
|
max*)
|
||||||
|
MAX_FD=$( ulimit -H -n ) ||
|
||||||
|
warn "Could not query maximum file descriptor limit"
|
||||||
|
esac
|
||||||
|
case $MAX_FD in #(
|
||||||
|
'' | soft) :;; #(
|
||||||
|
*)
|
||||||
|
ulimit -n "$MAX_FD" ||
|
||||||
|
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Collect all arguments for the java command, stacking in reverse order:
|
||||||
|
# * args from the command line
|
||||||
|
# * the main class name
|
||||||
|
# * -classpath
|
||||||
|
# * -D...appname settings
|
||||||
|
# * --module-path (only if needed)
|
||||||
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
||||||
|
|
||||||
|
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||||
|
if "$cygwin" || "$msys" ; then
|
||||||
|
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
||||||
|
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
||||||
|
|
||||||
|
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
||||||
|
|
||||||
|
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||||
|
for arg do
|
||||||
|
if
|
||||||
|
case $arg in #(
|
||||||
|
-*) false ;; # don't mess with options #(
|
||||||
|
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
||||||
|
[ -e "$t" ] ;; #(
|
||||||
|
*) false ;;
|
||||||
|
esac
|
||||||
|
then
|
||||||
|
arg=$( cygpath --path --ignore --mixed "$arg" )
|
||||||
|
fi
|
||||||
|
# Roll the args list around exactly as many times as the number of
|
||||||
|
# args, so each arg winds up back in the position where it started, but
|
||||||
|
# possibly modified.
|
||||||
|
#
|
||||||
|
# NB: a `for` loop captures its iteration list before it begins, so
|
||||||
|
# changing the positional parameters here affects neither the number of
|
||||||
|
# iterations, nor the values presented in `arg`.
|
||||||
|
shift # remove old arg
|
||||||
|
set -- "$@" "$arg" # push replacement arg
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Collect all arguments for the java command;
|
||||||
|
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
|
||||||
|
# shell script including quotes and variable substitutions, so put them in
|
||||||
|
# double quotes to make sure that they get re-expanded; and
|
||||||
|
# * put everything else in single quotes, so that it's not re-expanded.
|
||||||
|
|
||||||
|
set -- \
|
||||||
|
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||||
|
-classpath "$CLASSPATH" \
|
||||||
|
org.gradle.wrapper.GradleWrapperMain \
|
||||||
|
"$@"
|
||||||
|
|
||||||
|
# Use "xargs" to parse quoted args.
|
||||||
|
#
|
||||||
|
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||||
|
#
|
||||||
|
# In Bash we could simply go:
|
||||||
|
#
|
||||||
|
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
||||||
|
# set -- "${ARGS[@]}" "$@"
|
||||||
|
#
|
||||||
|
# but POSIX shell has neither arrays nor command substitution, so instead we
|
||||||
|
# post-process each arg (as a line of input to sed) to backslash-escape any
|
||||||
|
# character that might be a shell metacharacter, then use eval to reverse
|
||||||
|
# that process (while maintaining the separation between arguments), and wrap
|
||||||
|
# the whole thing up as a single "set" statement.
|
||||||
|
#
|
||||||
|
# This will of course break if any of these variables contains a newline or
|
||||||
|
# an unmatched quote.
|
||||||
|
#
|
||||||
|
|
||||||
|
eval "set -- $(
|
||||||
|
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
||||||
|
xargs -n1 |
|
||||||
|
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
||||||
|
tr '\n' ' '
|
||||||
|
)" '"$@"'
|
||||||
|
|
||||||
|
exec "$JAVACMD" "$@"
|
89
server/gradlew.bat
vendored
Normal file
89
server/gradlew.bat
vendored
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
@rem
|
||||||
|
@rem Copyright 2015 the original author or authors.
|
||||||
|
@rem
|
||||||
|
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
@rem you may not use this file except in compliance with the License.
|
||||||
|
@rem You may obtain a copy of the License at
|
||||||
|
@rem
|
||||||
|
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
@rem
|
||||||
|
@rem Unless required by applicable law or agreed to in writing, software
|
||||||
|
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@rem See the License for the specific language governing permissions and
|
||||||
|
@rem limitations under the License.
|
||||||
|
@rem
|
||||||
|
|
||||||
|
@if "%DEBUG%" == "" @echo off
|
||||||
|
@rem ##########################################################################
|
||||||
|
@rem
|
||||||
|
@rem Gradle startup script for Windows
|
||||||
|
@rem
|
||||||
|
@rem ##########################################################################
|
||||||
|
|
||||||
|
@rem Set local scope for the variables with windows NT shell
|
||||||
|
if "%OS%"=="Windows_NT" setlocal
|
||||||
|
|
||||||
|
set DIRNAME=%~dp0
|
||||||
|
if "%DIRNAME%" == "" set DIRNAME=.
|
||||||
|
set APP_BASE_NAME=%~n0
|
||||||
|
set APP_HOME=%DIRNAME%
|
||||||
|
|
||||||
|
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||||
|
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||||
|
|
||||||
|
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||||
|
|
||||||
|
@rem Find java.exe
|
||||||
|
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||||
|
|
||||||
|
set JAVA_EXE=java.exe
|
||||||
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
|
if "%ERRORLEVEL%" == "0" goto execute
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:findJavaFromJavaHome
|
||||||
|
set JAVA_HOME=%JAVA_HOME:"=%
|
||||||
|
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||||
|
|
||||||
|
if exist "%JAVA_EXE%" goto execute
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:execute
|
||||||
|
@rem Setup the command line
|
||||||
|
|
||||||
|
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
|
@rem Execute Gradle
|
||||||
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
||||||
|
|
||||||
|
:end
|
||||||
|
@rem End local scope for the variables with windows NT shell
|
||||||
|
if "%ERRORLEVEL%"=="0" goto mainEnd
|
||||||
|
|
||||||
|
:fail
|
||||||
|
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||||
|
rem the _cmd.exe /c_ return code!
|
||||||
|
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||||
|
exit /b 1
|
||||||
|
|
||||||
|
:mainEnd
|
||||||
|
if "%OS%"=="Windows_NT" endlocal
|
||||||
|
|
||||||
|
:omega
|
1
server/settings.gradle
Normal file
1
server/settings.gradle
Normal file
@@ -0,0 +1 @@
|
|||||||
|
rootProject.name = 'server'
|
13
server/src/main/java/com/vpr/server/ServerApplication.java
Normal file
13
server/src/main/java/com/vpr/server/ServerApplication.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package com.vpr.server;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class ServerApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ServerApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,15 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,252 @@
|
|||||||
|
package com.vpr.server.controller;
|
||||||
|
|
||||||
|
import com.vpr.server.dao.interfaces.EventDAO;
|
||||||
|
import com.vpr.server.data.Event;
|
||||||
|
import com.vpr.server.data.User;
|
||||||
|
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.UserEventRepository;
|
||||||
|
import com.vpr.server.repository.UserRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(path = "/event")
|
||||||
|
public class EventController {
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
@Autowired
|
||||||
|
private EventRepository eventRepository;
|
||||||
|
@Autowired
|
||||||
|
private UserEventRepository userEventRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EventDAO eventDAO;
|
||||||
|
|
||||||
|
/******************
|
||||||
|
* POST-ENDPOINTS *
|
||||||
|
******************/
|
||||||
|
|
||||||
|
@PostMapping(path = "/add")
|
||||||
|
public @ResponseBody
|
||||||
|
ResponseEntity<String> addEvent(
|
||||||
|
@RequestHeader("Authorization") String authorizationHeader,
|
||||||
|
@RequestParam long userId,
|
||||||
|
@RequestParam String date,
|
||||||
|
@RequestParam String name,
|
||||||
|
@RequestParam String start,
|
||||||
|
@RequestParam String end,
|
||||||
|
@RequestParam Integer priority,
|
||||||
|
@RequestParam Boolean isFullDay,
|
||||||
|
@RequestParam Boolean isPrivate
|
||||||
|
) {
|
||||||
|
User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]);
|
||||||
|
if (authUser == null || (!authUser.isAdmin() && authUser.getId() != userId)) {
|
||||||
|
return new ResponseEntity<>("Du hast keine Rechte um den Termin zu erstellen", HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
|
||||||
|
ResponseEntity<String> BAD_REQUEST = createEventAndUserEvent(userId, date, name, start, end, priority, isFullDay, isPrivate);
|
||||||
|
if (BAD_REQUEST != null) return BAD_REQUEST;
|
||||||
|
|
||||||
|
return new ResponseEntity<>("", HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(path = "/del")
|
||||||
|
public @ResponseBody
|
||||||
|
ResponseEntity<String> delEvent(
|
||||||
|
@RequestHeader("Authorization") String authorizationHeader,
|
||||||
|
@RequestParam long eventId,
|
||||||
|
@RequestParam long userId,
|
||||||
|
@RequestParam String date
|
||||||
|
) {
|
||||||
|
User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]);
|
||||||
|
if (authUser == null || (!authUser.isAdmin() && authUser.getId() != userId)) {
|
||||||
|
return new ResponseEntity<>("Du hast keine Rechte um den Termin zu löschen", HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
|
||||||
|
eventRepository.deleteUserEventsById(userId, eventId, date);
|
||||||
|
if (eventDAO.getAllEventsWithId(eventId).size() == 0) {
|
||||||
|
eventRepository.deleteById(eventId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ResponseEntity<>("", HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping(path = "/all")
|
||||||
|
public @ResponseBody
|
||||||
|
ResponseEntity<String> getAllEvents(
|
||||||
|
@RequestHeader("Authorization") String authorizationHeader,
|
||||||
|
@RequestParam String startDate,
|
||||||
|
@RequestParam String endDate
|
||||||
|
) {
|
||||||
|
User authUser = userRepository.findByToken(authorizationHeader.split("\\s")[1]);
|
||||||
|
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")
|
||||||
|
public @ResponseBody
|
||||||
|
ResponseEntity<String> editEvent(
|
||||||
|
@RequestHeader("Authorization") String authorizationHeader,
|
||||||
|
@RequestParam Long eventId,
|
||||||
|
@RequestParam Long userId,
|
||||||
|
@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]);
|
||||||
|
if (authUser == null || (!authUser.isAdmin() && authUser.getId() != userId)) {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,30 @@
|
|||||||
|
package com.vpr.server.controller;
|
||||||
|
|
||||||
|
import com.vpr.server.repository.EventRepository;
|
||||||
|
import com.vpr.server.repository.UserEventRepository;
|
||||||
|
import com.vpr.server.repository.UserRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@Controller // This means that this class is a Controller
|
||||||
|
@RequestMapping(path = "/vpr") // This means URL's start with /demo (after Application path)
|
||||||
|
public class MainController {
|
||||||
|
|
||||||
|
@GetMapping(path = "/status-test")
|
||||||
|
public String statusTest(){
|
||||||
|
throw new ResponseStatusException(HttpStatus.I_AM_A_TEAPOT, "TestTestTest");
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(path = "/header-test")
|
||||||
|
public ResponseEntity<String> headerTest(@RequestHeader("Authorization") String authorizationHeader){
|
||||||
|
System.out.println("authorizationHeader: " + authorizationHeader);
|
||||||
|
return new ResponseEntity<>(authorizationHeader, HttpStatus.OK);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,231 @@
|
|||||||
|
package com.vpr.server.controller;
|
||||||
|
|
||||||
|
import com.vpr.server.dao.interfaces.UserDAO;
|
||||||
|
import com.vpr.server.data.User;
|
||||||
|
import com.vpr.server.json.JSONMapper;
|
||||||
|
import com.vpr.server.repository.UserRepository;
|
||||||
|
import com.vpr.server.security.Hasher;
|
||||||
|
import com.vpr.server.security.Token;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.spec.InvalidKeySpecException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(path = "/user")
|
||||||
|
public class UserController {
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
@Autowired
|
||||||
|
private UserDAO userDAO;
|
||||||
|
|
||||||
|
private final AuthController authController;
|
||||||
|
|
||||||
|
public UserController() {
|
||||||
|
this.authController = new AuthController();
|
||||||
|
}
|
||||||
|
|
||||||
|
/******************
|
||||||
|
* POST-ENDPOINTS *
|
||||||
|
******************/
|
||||||
|
|
||||||
|
@PostMapping(path = "/add")
|
||||||
|
public @ResponseBody
|
||||||
|
ResponseEntity<String> addNewUser(
|
||||||
|
@RequestHeader("Authorization") String authorizationHeader,
|
||||||
|
@RequestParam String name,
|
||||||
|
@RequestParam String forename,
|
||||||
|
@RequestParam String login,
|
||||||
|
@RequestParam String password,
|
||||||
|
@RequestParam Boolean isAdmin
|
||||||
|
) {
|
||||||
|
User authUser = authController.getAuthUserFromHeader(authorizationHeader, userRepository);
|
||||||
|
if (authUser == null || !authUser.isAdmin()) {
|
||||||
|
return new ResponseEntity<>("Du hast keine Rechte um einen User an zu legen", HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userRepository.findByLogin(login) != null) {
|
||||||
|
return new ResponseEntity<>("Login exestiert bereits", HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
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(name);
|
||||||
|
user.setForename(forename);
|
||||||
|
user.setLogin(login);
|
||||||
|
user.setPassword(hash);
|
||||||
|
user.setSalt(salt);
|
||||||
|
user.setToken("");
|
||||||
|
user.setAdmin(isAdmin);
|
||||||
|
|
||||||
|
userRepository.save(user);
|
||||||
|
return new ResponseEntity<>("" + user.getId(), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(path = "/login")
|
||||||
|
public @ResponseBody
|
||||||
|
ResponseEntity<String> login(
|
||||||
|
@RequestParam String login,
|
||||||
|
@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.");
|
||||||
|
User user = userRepository.findByLogin(login);
|
||||||
|
if (user == null) {
|
||||||
|
System.out.println("Login for " + login + " failed.");
|
||||||
|
return new ResponseEntity<>("Falscher login", HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] salt = user.getSalt();
|
||||||
|
byte[] hash;
|
||||||
|
try {
|
||||||
|
hash = Hasher.HashPassword(password, salt);
|
||||||
|
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return new ResponseEntity<>("Fehler beim hashen", HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Arrays.equals(user.getPassword(), hash)) {
|
||||||
|
String token = Token.Generate(user.getLogin());
|
||||||
|
user.setToken(token);
|
||||||
|
userRepository.save(user);
|
||||||
|
|
||||||
|
System.out.println(user.getLogin() + " is now logged in.");
|
||||||
|
System.out.println(Token.Verify(Token.Generate(user.getLogin()), user.getLogin()));
|
||||||
|
|
||||||
|
return new ResponseEntity<>(token + " " + user.getId(), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
System.out.println(user.getLogin() + " failed to logged in.");
|
||||||
|
System.out.println("entered : " + javax.xml.bind.DatatypeConverter.printHexBinary(hash));
|
||||||
|
System.out.println("required: " + javax.xml.bind.DatatypeConverter.printHexBinary(user.getPassword()));
|
||||||
|
|
||||||
|
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")
|
||||||
|
public @ResponseBody
|
||||||
|
ResponseEntity<String> deleteUser(
|
||||||
|
@RequestHeader("Authorization") String authorizationHeader,
|
||||||
|
@RequestParam long userId
|
||||||
|
) {
|
||||||
|
User authUser = authController.getAuthUserFromHeader(authorizationHeader, userRepository);
|
||||||
|
if (authUser == null || !authUser.isAdmin()) {
|
||||||
|
return new ResponseEntity<>("Du hast keine Rechte um den User zu löschen", HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
User user = userRepository.findById(userId);
|
||||||
|
if (user == null) {
|
||||||
|
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(
|
||||||
|
@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);
|
||||||
|
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
|
||||||
|
ResponseEntity<String> getAllUser() {
|
||||||
|
List<User> userList = userDAO.getAllUser();
|
||||||
|
|
||||||
|
return new ResponseEntity<>(JSONMapper.userListToJSON(userList), HttpStatus.OK);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,48 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,36 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,16 @@
|
|||||||
|
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);
|
||||||
|
}
|
@@ -0,0 +1,11 @@
|
|||||||
|
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);
|
||||||
|
}
|
118
server/src/main/java/com/vpr/server/data/DateEvent.java
Normal file
118
server/src/main/java/com/vpr/server/data/DateEvent.java
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
package com.vpr.server.data;
|
||||||
|
|
||||||
|
import java.sql.Date;
|
||||||
|
import java.sql.Time;
|
||||||
|
|
||||||
|
public class DateEvent {
|
||||||
|
private long userId;
|
||||||
|
private String userName;
|
||||||
|
private String userForename;
|
||||||
|
|
||||||
|
private Date date;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private Integer priority;
|
||||||
|
private boolean isFullDay;
|
||||||
|
private Time start;
|
||||||
|
private Time end;
|
||||||
|
|
||||||
|
public DateEvent(UserEvent userEvent) {
|
||||||
|
this.userId = userEvent.getUser().getId();
|
||||||
|
this.userName = userEvent.getUser().getName();
|
||||||
|
this.userForename = userEvent.getUser().getForename();
|
||||||
|
this.date = userEvent.getDate();
|
||||||
|
this.name = userEvent.getEvent().getName();
|
||||||
|
this.priority = userEvent.getEvent().getPriority();
|
||||||
|
this.isFullDay = userEvent.getEvent().isFullDay();
|
||||||
|
this.start = userEvent.getEvent().getStart();
|
||||||
|
this.end = userEvent.getEvent().getEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DateEvent(long userId, String userName, String userForename, Date date, String name, Integer priority, boolean isFullDay, Time start, Time end) {
|
||||||
|
this.userId = userId;
|
||||||
|
this.userName = userName;
|
||||||
|
this.userForename = userForename;
|
||||||
|
this.date = date;
|
||||||
|
this.name = name;
|
||||||
|
this.priority = priority;
|
||||||
|
this.isFullDay = isFullDay;
|
||||||
|
this.start = start;
|
||||||
|
this.end = end;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************
|
||||||
|
* Getter and Setter *
|
||||||
|
*********************/
|
||||||
|
|
||||||
|
public long getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(long userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserForename() {
|
||||||
|
return userForename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserForename(String userForename) {
|
||||||
|
this.userForename = userForename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getDate() {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDate(Date date) {
|
||||||
|
this.date = date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPriority() {
|
||||||
|
return priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPriority(Integer priority) {
|
||||||
|
this.priority = priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFullDay() {
|
||||||
|
return isFullDay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFullDay(boolean fullDay) {
|
||||||
|
isFullDay = fullDay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Time getStart() {
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStart(Time start) {
|
||||||
|
this.start = start;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Time getEnd() {
|
||||||
|
return end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnd(Time end) {
|
||||||
|
this.end = end;
|
||||||
|
}
|
||||||
|
}
|
173
server/src/main/java/com/vpr/server/data/Event.java
Normal file
173
server/src/main/java/com/vpr/server/data/Event.java
Normal file
@@ -0,0 +1,173 @@
|
|||||||
|
package com.vpr.server.data;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.sql.Time;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity(name = "Event") // @Entity creates a table out of this class with Hibernate
|
||||||
|
@Table(name = "event")
|
||||||
|
@NamedNativeQueries({
|
||||||
|
@NamedNativeQuery(
|
||||||
|
name = "getAllEvents",
|
||||||
|
query = "SELECT * FROM event",
|
||||||
|
resultClass = Event.class
|
||||||
|
),
|
||||||
|
@NamedNativeQuery(
|
||||||
|
name = "getAllEventsInTimespan",
|
||||||
|
query = "SELECT * " +
|
||||||
|
"FROM event e " +
|
||||||
|
"INNER JOIN user_event ue " +
|
||||||
|
"ON e.id = ue.event_id " +
|
||||||
|
"WHERE (ue.user_id = :userId OR e.is_private = 0) " +
|
||||||
|
"AND ue.date >= :startDate " +
|
||||||
|
"AND ue.date < :endDate " +
|
||||||
|
"ORDER BY ue.date, e.priority DESC, e.start",
|
||||||
|
resultClass = Event.class
|
||||||
|
),
|
||||||
|
@NamedNativeQuery(
|
||||||
|
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 {
|
||||||
|
// Generate the primary key
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@Column(name = "name", nullable = false)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Column(name = "priority", nullable = false)
|
||||||
|
private Integer priority;
|
||||||
|
|
||||||
|
@Column(name = "is_full_day", nullable = false)
|
||||||
|
private boolean isFullDay;
|
||||||
|
|
||||||
|
@Column(name = "is_private", nullable = false)
|
||||||
|
private boolean isPrivate;
|
||||||
|
|
||||||
|
@Column(name = "start")
|
||||||
|
private Time start;
|
||||||
|
|
||||||
|
@Column(name = "end")
|
||||||
|
private Time end;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "event")
|
||||||
|
private List<UserEvent> userEvent;
|
||||||
|
|
||||||
|
/*********************
|
||||||
|
* Getter and Setter *
|
||||||
|
*********************/
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPriority() {
|
||||||
|
return priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPriority(Integer priority) {
|
||||||
|
this.priority = priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFullDay() {
|
||||||
|
return isFullDay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFullDay(boolean fullDay) {
|
||||||
|
isFullDay = fullDay;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPrivate() {
|
||||||
|
return isPrivate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrivate(boolean aPrivate) {
|
||||||
|
isPrivate = aPrivate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Time getStart() {
|
||||||
|
return start;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStart(Time start) {
|
||||||
|
this.start = start;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Time getEnd() {
|
||||||
|
return end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnd(Time end) {
|
||||||
|
this.end = end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<UserEvent> getUserEvent() {
|
||||||
|
return userEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserEvent(List<UserEvent> userEvent) {
|
||||||
|
this.userEvent = userEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
// toString
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Event{" +
|
||||||
|
"id=" + id +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
", priority=" + priority +
|
||||||
|
", isFullDay=" + isFullDay +
|
||||||
|
", start=" + start +
|
||||||
|
", end=" + end +
|
||||||
|
", 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();
|
||||||
|
}
|
||||||
|
}
|
146
server/src/main/java/com/vpr/server/data/User.java
Normal file
146
server/src/main/java/com/vpr/server/data/User.java
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
package com.vpr.server.data;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
// @Entity creates a table out of this class with Hibernate
|
||||||
|
@Entity(name = "User")
|
||||||
|
@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 {
|
||||||
|
// Generate the primary key
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy=GenerationType.IDENTITY)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@Column(name="name", nullable=false)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
@Column(name="forename", nullable=false)
|
||||||
|
private String forename;
|
||||||
|
|
||||||
|
@Column(name="login", nullable=false)
|
||||||
|
private String login;
|
||||||
|
|
||||||
|
@Column(name="password", nullable=false)
|
||||||
|
private byte[] password;
|
||||||
|
|
||||||
|
@Column(name="salt", nullable=false)
|
||||||
|
private byte[] salt;
|
||||||
|
|
||||||
|
@Column(name="token")
|
||||||
|
private String token;
|
||||||
|
|
||||||
|
@Column(name="is_admin", nullable=false)
|
||||||
|
private boolean isAdmin;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "user")
|
||||||
|
private List<UserEvent> userEvent;
|
||||||
|
|
||||||
|
/*********************
|
||||||
|
* Getter and Setter *
|
||||||
|
*********************/
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getForename() {
|
||||||
|
return forename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setForename(String forename) {
|
||||||
|
this.forename = forename;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLogin() {
|
||||||
|
return login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogin(String login) {
|
||||||
|
this.login = login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(byte[] password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getSalt() {
|
||||||
|
return salt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSalt(byte[] salt) {
|
||||||
|
this.salt = salt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getToken() {
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setToken(String token) {
|
||||||
|
this.token = token;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isAdmin() {
|
||||||
|
return isAdmin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAdmin(boolean admin) {
|
||||||
|
isAdmin = admin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<UserEvent> getEventList() {
|
||||||
|
return userEvent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEventList(List<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();
|
||||||
|
}
|
||||||
|
}
|
100
server/src/main/java/com/vpr/server/data/UserEvent.java
Normal file
100
server/src/main/java/com/vpr/server/data/UserEvent.java
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package com.vpr.server.data;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.sql.Date;
|
||||||
|
|
||||||
|
// @Entity creates a table out of this class with Hibernate
|
||||||
|
// @Table defines the table-name
|
||||||
|
@Entity
|
||||||
|
@Table(name="user_event")
|
||||||
|
@IdClass(UserEventId.class)
|
||||||
|
public class UserEvent {
|
||||||
|
@Id
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "user_id", referencedColumnName = "id")
|
||||||
|
private User user;
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "event_id", referencedColumnName = "id")
|
||||||
|
private Event event;
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@Column(name="date", columnDefinition = "DATE DEFAULT CURRENT_DATE", nullable = false)
|
||||||
|
private Date date;
|
||||||
|
|
||||||
|
/*********************
|
||||||
|
* Getter and Setter *
|
||||||
|
*********************/
|
||||||
|
|
||||||
|
public User getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUser(User user) {
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Event getEvent() {
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEvent(Event event) {
|
||||||
|
this.event = event;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getDate() {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDate(Date date) {
|
||||||
|
this.date = date;
|
||||||
|
}
|
||||||
|
|
||||||
|
// toString
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserEvent{" +
|
||||||
|
"user=" + user.getId() +
|
||||||
|
", event=" + event.getId() +
|
||||||
|
", 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
38
server/src/main/java/com/vpr/server/data/UserEventId.java
Normal file
38
server/src/main/java/com/vpr/server/data/UserEventId.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package com.vpr.server.data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.sql.Date;
|
||||||
|
|
||||||
|
public class UserEventId implements Serializable {
|
||||||
|
private long user;
|
||||||
|
private long event;
|
||||||
|
private Date date;
|
||||||
|
|
||||||
|
/*********************
|
||||||
|
* Getter and Setter *
|
||||||
|
*********************/
|
||||||
|
|
||||||
|
public long getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUser(long user) {
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getEvent() {
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEvent(long event) {
|
||||||
|
this.event = event;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getDate() {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDate(Date date) {
|
||||||
|
this.date = date;
|
||||||
|
}
|
||||||
|
}
|
79
server/src/main/java/com/vpr/server/json/JSONMapper.java
Normal file
79
server/src/main/java/com/vpr/server/json/JSONMapper.java
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
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 + "\"";
|
||||||
|
}
|
||||||
|
}
|
43
server/src/main/java/com/vpr/server/json/Validator.java
Normal file
43
server/src/main/java/com/vpr/server/json/Validator.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,74 @@
|
|||||||
|
package com.vpr.server.repository;
|
||||||
|
|
||||||
|
import com.vpr.server.data.Event;
|
||||||
|
import org.springframework.data.jpa.repository.Modifying;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
|
// This will be AUTO IMPLEMENTED by Spring into a Bean called eventRepository
|
||||||
|
// CRUD refers Create, Read, Update, Delete
|
||||||
|
|
||||||
|
public interface EventRepository extends CrudRepository<Event, Integer> {
|
||||||
|
@Query(
|
||||||
|
value = "SELECT e.id AS eid, e.name AS ename, e.start, e.end, e.priority , e.is_full_day, " +
|
||||||
|
"ue.date, " +
|
||||||
|
"u.id AS uid, u.forename, u.name AS uname " +
|
||||||
|
"FROM event e " +
|
||||||
|
"INNER JOIN user_event ue " +
|
||||||
|
"ON e.id = ue.event_id " +
|
||||||
|
"INNER JOIN user u " +
|
||||||
|
"ON ue.user_id = u.id " +
|
||||||
|
"WHERE u.id = ?1 " +
|
||||||
|
"OR e.is_private = 0",
|
||||||
|
nativeQuery = true
|
||||||
|
)
|
||||||
|
Object[] findAllVisibleByUserId(long id);
|
||||||
|
|
||||||
|
@Query(
|
||||||
|
value = "SELECT * " +
|
||||||
|
"FROM event e " +
|
||||||
|
"INNER JOIN user_event ue " +
|
||||||
|
"ON e.id = ue.event_id " +
|
||||||
|
"WHERE ue.user_id = ?1",
|
||||||
|
nativeQuery = true
|
||||||
|
)
|
||||||
|
Object[] findAllByUserId(long id);
|
||||||
|
|
||||||
|
|
||||||
|
@Query(
|
||||||
|
value = "SELECT ue.user_id as userId, ue.event_id as eventId, ue.date as date " +
|
||||||
|
"FROM event e " +
|
||||||
|
"INNER JOIN user_event ue " +
|
||||||
|
"ON e.id = ue.event_id " +
|
||||||
|
"WHERE ue.event_id = ?1 " +
|
||||||
|
"AND ue.user_id = ?2 " +
|
||||||
|
"AND ue.date = ?3",
|
||||||
|
nativeQuery = true
|
||||||
|
)
|
||||||
|
UserEventInterface findUserEventByEventIdUserIdAndDate(long eventId, long userId, String date);
|
||||||
|
|
||||||
|
interface UserEventInterface{
|
||||||
|
long getEventId();
|
||||||
|
long getUserId();
|
||||||
|
long getDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Modifying
|
||||||
|
@Transactional
|
||||||
|
@Query(
|
||||||
|
value = "DELETE ue FROM user_event ue WHERE ue.event_id = :eventId AND ue.user_id = :userId AND ue.date = :date",
|
||||||
|
nativeQuery = true
|
||||||
|
)
|
||||||
|
void deleteUserEventsById(long userId, long eventId, String date);
|
||||||
|
|
||||||
|
|
||||||
|
@Modifying
|
||||||
|
@Transactional
|
||||||
|
@Query(
|
||||||
|
value = "DELETE e FROM event e WHERE e.id = ?1",
|
||||||
|
nativeQuery = true
|
||||||
|
)
|
||||||
|
void deleteById(long id);
|
||||||
|
}
|
@@ -0,0 +1,14 @@
|
|||||||
|
package com.vpr.server.repository;
|
||||||
|
|
||||||
|
import com.vpr.server.data.UserEvent;
|
||||||
|
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
|
||||||
|
// CRUD refers Create, Read, Update, Delete
|
||||||
|
|
||||||
|
public interface UserEventRepository extends CrudRepository<UserEvent, Integer> {
|
||||||
|
List<UserEvent> findByUserIdAndDate(long userId, Date date);
|
||||||
|
}
|
@@ -0,0 +1,25 @@
|
|||||||
|
package com.vpr.server.repository;
|
||||||
|
|
||||||
|
import com.vpr.server.data.User;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
|
||||||
|
// CRUD refers Create, Read, Update, Delete
|
||||||
|
public interface UserRepository extends CrudRepository<User, Integer> {
|
||||||
|
|
||||||
|
@Query(value = "SELECT u.id, u.name, u.forename " +
|
||||||
|
"FROM user u",
|
||||||
|
nativeQuery = true)
|
||||||
|
Object[] findAllUsernames();
|
||||||
|
|
||||||
|
User findById(long id);
|
||||||
|
|
||||||
|
User findByLogin(String login);
|
||||||
|
|
||||||
|
User findByLoginAndPassword(String login, byte[] password);
|
||||||
|
|
||||||
|
void deleteById(long id);
|
||||||
|
|
||||||
|
User findByToken(String token);
|
||||||
|
}
|
28
server/src/main/java/com/vpr/server/security/Hasher.java
Normal file
28
server/src/main/java/com/vpr/server/security/Hasher.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package com.vpr.server.security;
|
||||||
|
|
||||||
|
import javax.crypto.SecretKeyFactory;
|
||||||
|
import javax.crypto.spec.PBEKeySpec;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.SecureRandom;
|
||||||
|
import java.security.spec.InvalidKeySpecException;
|
||||||
|
import java.security.spec.KeySpec;
|
||||||
|
|
||||||
|
public class Hasher {
|
||||||
|
|
||||||
|
public static byte[] HashPassword(String password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {
|
||||||
|
// Credit: https://www.baeldung.com/java-password-hashing
|
||||||
|
// Generate hash with PBKDF2
|
||||||
|
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128);
|
||||||
|
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
|
||||||
|
return factory.generateSecret(spec).getEncoded();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] GenerateSalt(){
|
||||||
|
// Credit: https://www.baeldung.com/java-password-hashing
|
||||||
|
// Create a salt
|
||||||
|
SecureRandom random = new SecureRandom();
|
||||||
|
byte[] salt = new byte[16];
|
||||||
|
random.nextBytes(salt);
|
||||||
|
return salt;
|
||||||
|
}
|
||||||
|
}
|
27
server/src/main/java/com/vpr/server/security/Token.java
Normal file
27
server/src/main/java/com/vpr/server/security/Token.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package com.vpr.server.security;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.JwtException;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
import io.jsonwebtoken.security.Keys;
|
||||||
|
|
||||||
|
import java.security.Key;
|
||||||
|
|
||||||
|
public class Token {
|
||||||
|
|
||||||
|
private static final Key KEY = Keys.secretKeyFor(SignatureAlgorithm.HS256);
|
||||||
|
|
||||||
|
public static String Generate(String subject){
|
||||||
|
return Jwts.builder().setSubject(subject).signWith(KEY).compact();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean Verify(String jws, String subject){
|
||||||
|
try {
|
||||||
|
assert Jwts.parserBuilder().setSigningKey(KEY).build().parseClaimsJws(jws)
|
||||||
|
.getBody().getSubject().equals(subject);
|
||||||
|
return true;
|
||||||
|
} catch (JwtException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,13 @@
|
|||||||
|
package com.vpr.server;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class ServerApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user