All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.convergence_platform.services.security.AccessControlLayer Maven / Gradle / Ivy

Go to download

Holds the common functionality needed by all Convergence Platform-based services written in Java.

The newest version!
package io.convergence_platform.services.security;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

public class AccessControlLayer implements IAccessControlLayer {
    @Override
    public boolean allowAll() {
        return true;
    }

    @Override
    public boolean isSignedIn() {
        Object auth = SecurityContextHolder.getContext().getAuthentication();
        return auth instanceof ServiceAuthenticationToken;
    }

    @Override
    public boolean notSignedIn() {
        Object auth = SecurityContextHolder.getContext().getAuthentication();
        return auth instanceof AnonymousAuthenticationToken;
    }

    @Override
    public boolean hasAuthority(String authority) {
        // TODO: Make a better case for the system admin case
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication.getAuthorities().contains(new SimpleGrantedAuthority(authority))
                || authentication.getAuthorities().contains(new SimpleGrantedAuthority("authority::*"));
    }

    @Override
    public boolean isService() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication instanceof ServiceAuthenticationToken && ((ServiceAuthenticationToken) authentication).isService;
    }

    @Override
    public boolean isSystemAdmin() {
        return hasAuthority("authority::*");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy