CurrentUserService.java
package edu.ucsb.cs156.example.services;
import edu.ucsb.cs156.example.entities.User;
import edu.ucsb.cs156.example.models.CurrentUser;
import java.util.Collection;
import java.util.List;
import org.springframework.security.core.GrantedAuthority;
/**
* This is a service that provides information about the current user.
*
* It is an abstract class because we have different implementations for testing
* and production.
*
*/
public abstract class CurrentUserService {
/**
* This method returns the current user as a User object.
*
* @return the current user
*/
public abstract User getUser();
/**
* This method returns the current user as a CurrentUser object
*
* @return the current user
*/
public abstract CurrentUser getCurrentUser();
/**
* This method returns the roles of the current user.
*
* @return a collection of roles
*/
public abstract Collection<? extends GrantedAuthority> getRoles();
/**
* This method returns whether the current user is logged in.
*
* @return whether the current user is logged in
*/
public final boolean isLoggedIn() {
return getUser() != null;
}
/**
* This method returns the roles of the current user as a sorted list of strings.
*
* @return a collection of roles
*/
public List<String> getRolesSorted() {
Collection<? extends GrantedAuthority> authorities = getRoles();
List<String> roles = authorities.stream()
.map(GrantedAuthority::getAuthority)
.sorted()
.toList();
return roles;
}
}