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

167 lines
3.8 KiB
Java

/**
*
*/
package de.bstly.board.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import de.bstly.board.businesslogic.SettingsManager;
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 userManager;
@Autowired
private SettingsManager settingsManager;
/**
* 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 page size.
*
* @return the page size
*/
protected int getPageSize() {
String username = getCurrentUsername();
if (username != null) {
LocalUser localUser = userManager.getByUsername(username);
if (localUser.getSettings() != null
&& localUser.getSettings().containsKey("pageSize")) {
try {
return Integer.parseInt(localUser.getSettings().get("pageSize"));
} catch (Exception e) {
}
}
}
return settingsManager.getPageSize();
}
/**
* Gets the gravity.
*
* @return the gravity
*/
protected float getGravity() {
String username = getCurrentUsername();
if (username != null) {
LocalUser localUser = userManager.getByUsername(username);
if (localUser.getSettings() != null && localUser.getSettings().containsKey("gravity")) {
try {
float gravity = Float.parseFloat(localUser.getSettings().get("gravity"));
if (gravity < 0) {
return 0;
}
if (gravity > 2) {
return 2;
}
return gravity;
} catch (Exception e) {
}
}
}
return settingsManager.getGravity();
}
/**
* Gets the entry delay.
*
* @return the entry delay
*/
protected long getEntryDelay() {
String username = getCurrentUsername();
if (username != null) {
LocalUser localUser = userManager.getByUsername(username);
if (localUser.getSettings() != null
&& localUser.getSettings().containsKey("entryDelay")) {
try {
long entryDelay = Long.parseLong(localUser.getSettings().get("entryDelay"));
if (entryDelay < 0) {
return 0;
}
if (entryDelay > 15) {
return 15;
}
return entryDelay;
} catch (Exception e) {
}
}
}
return settingsManager.getEntryDelay();
}
/**
* Gets the comment delay.
*
* @return the comment delay
*/
protected long getCommentDelay() {
String username = getCurrentUsername();
if (username != null) {
LocalUser localUser = userManager.getByUsername(username);
if (localUser.getSettings() != null
&& localUser.getSettings().containsKey("commentDelay")) {
try {
long commentDelay = Long.parseLong(localUser.getSettings().get("commentDelay"));
if (commentDelay < 0) {
return 0;
}
if (commentDelay > 15) {
return 15;
}
return commentDelay;
} catch (Exception e) {
}
}
}
return settingsManager.getCommentDelay();
}
}