show current camps on start page

This commit is contained in:
Jottyfan
2022-10-01 22:28:08 +02:00
parent d15a6af941
commit cfe4edcf15
12 changed files with 402 additions and 10 deletions

View File

@@ -55,8 +55,12 @@ public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.authorizeRequests().antMatchers("/**").permitAll();
http.anonymous().disable();
http.authorizeRequests()
// @formatter:off
.antMatchers("/user/**", "/business/**").authenticated()
.anyRequest().permitAll();
// @formatter:on
// http.anonymous().disable();
http.csrf().disable();
}
}

View File

@@ -0,0 +1,37 @@
package de.jottyfan.camporganizer.module.common;
import javax.servlet.http.HttpServletRequest;
import org.jooq.exception.DataAccessException;
import org.keycloak.KeycloakSecurityContext;
import org.springframework.ui.Model;
/**
*
* @author jotty
*
*/
public abstract class CommonController {
/**
* try to get current keycloak user
*
* @param request the request
* @return the preferred username or null
*/
private String getCurrentUser(HttpServletRequest request) {
KeycloakSecurityContext ksc = (KeycloakSecurityContext) request
.getAttribute(KeycloakSecurityContext.class.getName());
return ksc == null ? null : ksc.getIdToken().getPreferredUsername();
}
/**
* setup the session for the template
* @param model the model
* @param request the request
*/
public void setupSession(Model model, HttpServletRequest request) {
String username = getCurrentUser(request);
model.addAttribute("currentUser", username);
}
}

View File

@@ -7,6 +7,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
@@ -15,18 +16,27 @@ import org.springframework.web.bind.annotation.GetMapping;
*
*/
@Controller
public class IndexController {
public class IndexController extends CommonController {
private static final Logger LOGGER = LogManager.getLogger(IndexController.class);
@Autowired
private HttpServletRequest request;
@Autowired
private IndexService service;
@GetMapping("/")
public String index() {
if (request == null) {
LOGGER.error("request is null");
}
return "/";
public String index(Model model) {
super.setupSession(model, request);
model.addAttribute("camps", service.getAllCamps());
return "/index";
}
@GetMapping("/user")
public String user(Model model) {
super.setupSession(model, request);
model.addAttribute("camps", service.getAllCamps());
return "/user/index";
}
@GetMapping("/logout")

View File

@@ -0,0 +1,33 @@
package de.jottyfan.camporganizer.module.common;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import de.jottyfan.camporganizer.db.jooq.tables.records.VCampRecord;
/**
*
* @author jotty
*
*/
@Service
public class IndexService {
@Autowired
private IndexGateway gateway;
/**
* get all camps from the database and prepare them for the view
*
* @return the list of found camps
*/
public List<VCampRecord> getAllCamps() {
Stream<VCampRecord> stream = gateway.getAllCamps();
List<VCampRecord> list = new ArrayList<>();
stream.forEach(o -> list.add(o));
return list;
}
}

View File

@@ -0,0 +1,25 @@
package de.jottyfan.camporganizer.module.common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.info.BuildProperties;
import org.springframework.stereotype.Component;
/**
*
* @author jotty
*
*/
@Component
public class ManifestBean {
@Autowired(required = false)
private BuildProperties buildProperties;
/**
* @return the version of this project
*/
public String getVersion() {
return buildProperties != null ? buildProperties.getVersion()
: this.getClass().getPackage().getImplementationVersion();
}
}