traffic warning forms
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
package de.mbremer.secutity;
|
||||
|
||||
import de.mbremer.configuration.Configuration;
|
||||
import de.mbremer.configuration.ConfigurationService;
|
||||
import de.mbremer.room.Room;
|
||||
import de.mbremer.traffic.TrafficAdminForm;
|
||||
import de.mbremer.traffic.TrafficUserForm;
|
||||
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
|
||||
import de.mbremer.traffic.TrafficWarningConfig;
|
||||
import io.quarkus.panache.common.Sort;
|
||||
import io.quarkus.qute.Location;
|
||||
import io.quarkus.qute.Template;
|
||||
import io.quarkus.qute.TemplateInstance;
|
||||
import io.quarkus.security.identity.SecurityIdentity;
|
||||
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.jboss.resteasy.annotations.jaxrs.PathParam;
|
||||
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
|
||||
@@ -18,15 +18,15 @@ import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
|
||||
import javax.annotation.security.PermitAll;
|
||||
import javax.annotation.security.RolesAllowed;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.transaction.Transactional;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.NewCookie;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.net.URI;
|
||||
import java.util.Optional;
|
||||
|
||||
import static de.mbremer.configuration.Configuration.GOOGLE_API_KEY;
|
||||
import static de.mbremer.configuration.Configuration.OFFICE_ADRESS_KEY;
|
||||
import static de.mbremer.secutity.Role.ADMIN;
|
||||
|
||||
@Path("/user")
|
||||
@@ -40,29 +40,42 @@ public class UserResource {
|
||||
@Inject
|
||||
UserService userService;
|
||||
@Inject
|
||||
ConfigurationService configurationService;
|
||||
@Inject
|
||||
Template userinit;
|
||||
@Inject
|
||||
@Location("user.html")
|
||||
Template userTemplate;
|
||||
// TODO: 12.10.21
|
||||
// @ConfigProperty(name = "google_api_key")
|
||||
// Optional<String> googleApiKey;
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.TEXT_HTML)
|
||||
@RolesAllowed({"USER", "ADMIN"})
|
||||
public TemplateInstance getUser() {
|
||||
|
||||
TrafficWarningConfig trafficWarningConfig =
|
||||
(TrafficWarningConfig) TrafficWarningConfig.find("user", userService.getCurrentUser()).singleResultOptional().orElse(null);
|
||||
|
||||
TemplateInstance templateInstance = userTemplate
|
||||
.data("current_user", User.find("username", identity.getPrincipal().getName()).singleResult());
|
||||
|
||||
// log.info("Google-API-Key" + googleApiKey.orElse("-"));
|
||||
if (trafficWarningConfig != null) {
|
||||
log.info(trafficWarningConfig.toString());
|
||||
templateInstance
|
||||
.data("traffic_email", trafficWarningConfig.getEmailadress())
|
||||
.data("traffic_active", trafficWarningConfig.isActive())
|
||||
.data("traffic_address", trafficWarningConfig.getAddress())
|
||||
.data("traffic_away", trafficWarningConfig.getTimeAway())
|
||||
.data("traffic_back", trafficWarningConfig.getTimeBack())
|
||||
.data("traffic_duration", trafficWarningConfig.getThreshold());
|
||||
}
|
||||
|
||||
if (identity.hasRole("ADMIN")) {
|
||||
templateInstance
|
||||
.data("is_admin", true)
|
||||
.data("users", User.listAll(Sort.by("username")))
|
||||
.data("rooms", Room.listAll(Sort.by("name")));
|
||||
// .data("google_api_key", googleApiKey.orElse(""));
|
||||
.data("is_admin", true)
|
||||
.data("users", User.listAll(Sort.by("username")))
|
||||
.data("rooms", Room.listAll(Sort.by("name")))
|
||||
.data("google_api_key", Configuration.findGooleApiKey())
|
||||
.data("office_address", Configuration.findOfficeAdress());
|
||||
}
|
||||
|
||||
return templateInstance;
|
||||
@@ -74,10 +87,10 @@ public class UserResource {
|
||||
@Path("init")
|
||||
@PermitAll
|
||||
public Response initialAdd(@MultipartForm UserForm userForm) {
|
||||
if(User.count("role", ADMIN.name()) > 0) {
|
||||
if (User.count("role", ADMIN.name()) > 0) {
|
||||
throw new BadRequestException("Only allowed on initial set up");
|
||||
}
|
||||
if(!ADMIN.name().equals(userForm.role)) {
|
||||
if (!ADMIN.name().equals(userForm.role)) {
|
||||
throw new BadRequestException("Only role ADMIN");
|
||||
}
|
||||
|
||||
@@ -94,7 +107,7 @@ public class UserResource {
|
||||
@Path("init")
|
||||
@PermitAll
|
||||
public TemplateInstance initial() {
|
||||
if(User.count("role", ADMIN.name()) > 0) {
|
||||
if (User.count("role", ADMIN.name()) > 0) {
|
||||
throw new BadRequestException("Only allowed on initial set up");
|
||||
}
|
||||
return userinit.instance();
|
||||
@@ -107,7 +120,7 @@ public class UserResource {
|
||||
public Response logout() {
|
||||
return Response
|
||||
.temporaryRedirect(URI.create("/"))
|
||||
.cookie(new NewCookie("quarkus-credential", null, "/", null, null, 0, false,true))
|
||||
.cookie(new NewCookie("quarkus-credential", null, "/", null, null, 0, false, true))
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -165,16 +178,8 @@ public class UserResource {
|
||||
@Transactional
|
||||
@Path("/traffic/config/")
|
||||
public TemplateInstance configureTrafficAdmin(@MultipartForm TrafficAdminForm trafficAdminForm) {
|
||||
log.info("configure traffic");
|
||||
|
||||
try {
|
||||
Configuration apiKeyConfig = Configuration.find("propertyName", "google_api_key").singleResult();
|
||||
apiKeyConfig.setValue(trafficAdminForm.googleApiKey);
|
||||
log.info("update API-Key: " + trafficAdminForm.googleApiKey);
|
||||
} catch (NoResultException e) {
|
||||
new Configuration("google_api_key", trafficAdminForm.googleApiKey).persist();
|
||||
log.info("create API-Key: " + trafficAdminForm.googleApiKey);
|
||||
}
|
||||
configurationService.createOrUpdate(GOOGLE_API_KEY, trafficAdminForm.googleApiKey);
|
||||
configurationService.createOrUpdate(OFFICE_ADRESS_KEY, trafficAdminForm.officeAddress);
|
||||
|
||||
return getUser();
|
||||
}
|
||||
@@ -187,6 +192,18 @@ public class UserResource {
|
||||
public TemplateInstance configureTrafficUser(@PathParam("username") String username, @MultipartForm TrafficUserForm trafficUserForm) {
|
||||
log.info("configure traffic for " + username);
|
||||
|
||||
User currentUser = userService.getCurrentUser();
|
||||
TrafficWarningConfig trafficWarningConfig = (TrafficWarningConfig) TrafficWarningConfig.find("user", currentUser).singleResultOptional().orElse(null);
|
||||
|
||||
if (trafficWarningConfig == null) {
|
||||
log.info("create new trafficWarningConfig. user " + currentUser.getUsername());
|
||||
trafficWarningConfig = new TrafficWarningConfig();
|
||||
trafficWarningConfig.setUser(currentUser);
|
||||
}
|
||||
trafficWarningConfig = trafficUserForm.updateTrafficWarningConfig(trafficWarningConfig);
|
||||
log.info(trafficWarningConfig.toString());
|
||||
|
||||
trafficWarningConfig.persist();
|
||||
return getUser();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user