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

org.ikasan.dashboard.ui.util.ComponentSecurityVisibility Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
package org.ikasan.dashboard.ui.util;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasEnabled;
import org.ikasan.security.service.authentication.IkasanAuthentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class ComponentSecurityVisibility
{
    public static void applySecurity(Component component, String ... securityConstants) {
        IkasanAuthentication authentication = (IkasanAuthentication) SecurityContextHolder.getContext().getAuthentication();
        applySecurity(authentication, component, securityConstants);
    }

    public static void applySecurity(IkasanAuthentication authentication, Component component, String ... securityConstants) {
        if(component == null) {
            return;
        }

        component.setVisible(false);

        if(authentication == null) {
            return;
        }

        for(String securityConstant: securityConstants) {
            if(authentication.hasGrantedAuthority(securityConstant)) {
                component.setVisible(true);
            }
        }
    }

    public static void applyEnabledSecurity(IkasanAuthentication authentication, HasEnabled component, String ... securityConstants) {
        if(component == null) {
            return;
        }

        component.setEnabled(false);

        if(authentication == null) {
            return;
        }

        for(String securityConstant: securityConstants) {
            if(authentication.hasGrantedAuthority(securityConstant)) {
                component.setEnabled(true);
            }
        }
    }

    public static void applyEnabledSecurity(HasEnabled component, String ... securityConstants) {
        IkasanAuthentication authentication = (IkasanAuthentication) SecurityContextHolder.getContext().getAuthentication();
        applyEnabledSecurity(authentication, component, securityConstants);
    }

    public static boolean hasAuthorisation(String ... securityConstants)
    {
        if(SecurityContextHolder.getContext().getAuthentication() instanceof  IkasanAuthentication) {
            IkasanAuthentication authentication = (IkasanAuthentication) SecurityContextHolder.getContext().getAuthentication();

            return hasAuthorisation(authentication, securityConstants);
        }

        return false;
    }

    public static boolean hasAuthorisation(IkasanAuthentication authentication, String ... securityConstants)
    {
        for (String securityConstant : securityConstants) {
            if (authentication.hasGrantedAuthority(securityConstant)) {
                return true;
            }
        }

        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy