Files
BiCO/src/main/java/de/jottyfan/bico/modules/CommonController.java
Jottyfan 1733821102 omit npe
2025-01-30 22:48:18 +01:00

93 lines
2.8 KiB
Java

package de.jottyfan.bico.modules;
import java.security.Principal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.ModelAttribute;
import de.jottyfan.bico.Main;
import de.jottyfan.bico.modules.profile.ProfileService;
/**
*
* @author jotty
*
*/
public abstract class CommonController {
private static final List<String> admins = List.of("andre.sieber", "tobias.kuehne", "jotty");
@Autowired
private ProfileService profileService;
@Value("${spring.security.oauth2.client.provider.nextcloud.issuer-uri}")
private String nextcloudUrl;
@ModelAttribute("isCampAdmin")
public Boolean isCampAdmin(Principal principal) {
return principal == null ? false : admins.contains(principal.getName());
}
@ModelAttribute("hasBUrole")
public Boolean hasBURole(Principal principal) {
OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) principal;
if (token != null) {
OAuth2User user = token.getPrincipal();
@SuppressWarnings("unchecked")
List<String> roles = (List<String>) user.getAttributes().get("roles");
return roles.contains("Bibelunterricht");
} else {
Main.LOGGER.warn("token is null, no roles can be detected");
return false;
}
}
@ModelAttribute("hasDateRole")
public Boolean hasDateRole(Principal principal) {
OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) principal;
if (token != null) {
OAuth2User user = token.getPrincipal();
@SuppressWarnings("unchecked")
List<String> roles = (List<String>) user.getAttributes().get("roles");
return roles.contains("Kinderstunde klein");
} else {
Main.LOGGER.warn("token is null, no roles can be detected");
return false;
}
}
@ModelAttribute("hasAnyRole")
public Boolean hasAnyRole(Principal principal) {
OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) principal;
if (token != null) {
OAuth2User user = token.getPrincipal();
@SuppressWarnings("unchecked")
List<String> roles = (List<String>) user.getAttributes().get("roles");
return roles.size() > 0;
} else {
Main.LOGGER.warn("token is null, no roles can be detected");
return false;
}
}
/**
* get the theme for the current session
*
* @return the theme; light or dark at the moment
*/
@ModelAttribute("theme")
public String getTheme() {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
return profileService.getTheme(username);
}
@ModelAttribute("nextcloudUrl")
public String getNextcloudUrl() {
return nextcloudUrl;
}
}