bstlboard-back/src/main/java/de/bstly/board/controller/BaseController.java

88 lines
2.1 KiB
Java

/**
*
*/
package de.bstly.board.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import de.bstly.board.businesslogic.UserManager;
import de.bstly.board.model.LocalUser;
import de.bstly.board.security.LocalUserDetails;
/**
* The Class BaseController.
*/
public class BaseController {
@Autowired
private UserManager localUserManager;
@Value("${bstly.board.ranking.gravity:1.2}")
private double GRAVITY;
/**
* Authenticated.
*
* @return true, if successful
*/
protected boolean authenticated() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
return auth != null && auth.isAuthenticated();
}
/**
* Gets the current username.
*
* @return the current username
*/
protected String getCurrentUsername() {
LocalUserDetails localUserDetails = getLocalUserDetails();
return localUserDetails != null ? localUserDetails.getUsername() : null;
}
/**
* Gets the local user details.
*
* @return the local user details
*/
protected LocalUserDetails getLocalUserDetails() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
return (auth != null && auth.getPrincipal() instanceof LocalUserDetails)
? (LocalUserDetails) auth.getPrincipal()
: null;
}
/**
* Gets the local user.
*
* @return the local user
*/
protected LocalUser getLocalUser() {
return localUserManager.getByAuth(SecurityContextHolder.getContext().getAuthentication());
}
/**
* Gets the gravity.
*
* @return the gravity
*/
protected double getGravity() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth.isAuthenticated()) {
LocalUser localUser = localUserManager.getByAuth(auth);
if (localUser.getSettings().containsKey("gravity")) {
try {
return Double.parseDouble(localUser.getSettings().get("gravity"));
} catch (Exception e) {
}
}
}
return GRAVITY;
}
}