Search in sources :

Example 1 with Configuration

use of com.opensymphony.xwork2.config.Configuration in project qi4j-sdk by Qi4j.

the class Qi4jCodebehindPackageProvider method loadPackageConfig.

/**
     * Finds or creates the package configuration for an Action class.
     *
     * The namespace annotation is honored, if found,
     * and the namespace is checked for a parent configuration.
     *
     * @param actionNamespace The configuration namespace
     * @param actionPackage   The Java package containing our Action classes
     * @param actionClass     The Action class instance
     *
     * @return PackageConfig object for the Action class
     */
protected PackageConfig.Builder loadPackageConfig(String actionNamespace, String actionPackage, Class actionClass) {
    PackageConfig.Builder parent = null;
    // Check for the @Namespace annotation
    if (actionClass != null) {
        Namespace ns = (Namespace) actionClass.getAnnotation(Namespace.class);
        if (ns != null) {
            parent = loadPackageConfig(actionNamespace, actionPackage, null);
            actionNamespace = ns.value();
            actionPackage = actionClass.getName();
        // See if the namespace has been overridden by the @Action annotation
        } else {
            org.apache.struts2.config.Action actionAnn = (org.apache.struts2.config.Action) actionClass.getAnnotation(org.apache.struts2.config.Action.class);
            if (actionAnn != null && !actionAnn.DEFAULT_NAMESPACE.equals(actionAnn.namespace())) {
                // we pass null as the namespace in case the parent package hasn't been loaded yet
                parent = loadPackageConfig(null, actionPackage, null);
                actionPackage = actionClass.getName();
            }
        }
    }
    PackageConfig.Builder pkgConfig = packageLoader.getPackage(actionPackage);
    if (pkgConfig == null) {
        pkgConfig = new PackageConfig.Builder(actionPackage);
        pkgConfig.namespace(actionNamespace);
        if (parent == null) {
            PackageConfig cfg = configuration.getPackageConfig(defaultParentPackage);
            if (cfg != null) {
                pkgConfig.addParent(cfg);
            } else {
                throw new ConfigurationException("ClasspathPackageProvider: Unable to locate default parent package: " + defaultParentPackage);
            }
        }
        packageLoader.registerPackage(pkgConfig);
    // if the parent package was first created by a child, ensure the namespace is correct
    } else if (pkgConfig.getNamespace() == null) {
        pkgConfig.namespace(actionNamespace);
    }
    if (parent != null) {
        packageLoader.registerChildToParent(pkgConfig, parent);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("class:" + actionClass + " parent:" + parent + " current:" + (pkgConfig != null ? pkgConfig.getName() : ""));
    }
    return pkgConfig;
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) org.apache.struts2.config(org.apache.struts2.config) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 2 with Configuration

use of com.opensymphony.xwork2.config.Configuration in project dhis2-core by dhis2.

the class DetectingSystemAuthoritiesProvider method getSystemAuthorities.

// -------------------------------------------------------------------------
// SystemAuthoritiesProvider implementation
// -------------------------------------------------------------------------
@Override
public Collection<String> getSystemAuthorities() {
    HashSet<String> authorities = new HashSet<>();
    Configuration configuration = Dispatcher.getInstance().getConfigurationManager().getConfiguration();
    for (PackageConfig packageConfig : configuration.getPackageConfigs().values()) {
        for (ActionConfig actionConfig : packageConfig.getActionConfigs().values()) {
            authorities.addAll(requiredAuthoritiesProvider.getAllAuthorities(actionConfig));
        }
    }
    return authorities;
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) Configuration(com.opensymphony.xwork2.config.Configuration) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) HashSet(java.util.HashSet)

Example 3 with Configuration

use of com.opensymphony.xwork2.config.Configuration in project dhis2-core by dhis2.

the class SpringSecurityActionAccessResolver method hasAccess.

// -------------------------------------------------------------------------
// ActionAccessResolver implementation
// -------------------------------------------------------------------------
@Override
public boolean hasAccess(String module, String name) {
    // ---------------------------------------------------------------------
    // Get ObjectDefinitionSource
    // ---------------------------------------------------------------------
    Configuration config = Dispatcher.getInstance().getConfigurationManager().getConfiguration();
    PackageConfig packageConfig = config.getPackageConfig(module);
    if (packageConfig == null) {
        throw new IllegalArgumentException("Module doesn't exist: '" + module + "'");
    }
    ActionConfig actionConfig = packageConfig.getActionConfigs().get(name);
    if (actionConfig == null) {
        throw new IllegalArgumentException("Module " + module + " doesn't have an action named: '" + name + "'");
    }
    SecurityMetadataSource securityMetadataSource = requiredAuthoritiesProvider.createSecurityMetadataSource(actionConfig);
    // ---------------------------------------------------------------------
    // Test access
    // ---------------------------------------------------------------------
    SecurityContext securityContext = SecurityContextHolder.getContext();
    Authentication authentication = securityContext.getAuthentication();
    try {
        if (securityMetadataSource.getAttributes(actionConfig) != null) {
            if (authentication == null || !authentication.isAuthenticated()) {
                return false;
            }
            accessDecisionManager.decide(authentication, actionConfig, securityMetadataSource.getAttributes(actionConfig));
        }
        log.debug("Access to [" + module + ", " + name + "]: TRUE");
        return true;
    } catch (AccessDeniedException e) {
        log.debug("Access to [" + module + ", " + name + "]: FALSE (access denied)");
        return false;
    } catch (InsufficientAuthenticationException e) {
        log.debug("Access to [" + module + ", " + name + "]: FALSE (insufficient authentication)");
        return false;
    }
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Configuration(com.opensymphony.xwork2.config.Configuration) SecurityMetadataSource(org.springframework.security.access.SecurityMetadataSource) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) InsufficientAuthenticationException(org.springframework.security.authentication.InsufficientAuthenticationException) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 4 with Configuration

use of com.opensymphony.xwork2.config.Configuration in project dhis2-core by dhis2.

the class ModuleAccessVoter method vote.

/**
     * Votes. Votes ACCESS_ABSTAIN if the object class is not supported. Votes
     * ACCESS_GRANTED if there is a granted authority which equals attribute
     * prefix + module name, or the module name is in the always accessible set.
     * Otherwise votes ACCESS_DENIED.
     */
@Override
public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {
    if (!supports(object.getClass())) {
        LOG.debug("ACCESS_ABSTAIN [" + object.toString() + "]: Class not supported.");
        return ACCESS_ABSTAIN;
    }
    ActionConfig target = (ActionConfig) object;
    if (alwaysAccessible.contains(target.getPackageName())) {
        LOG.debug("ACCESS_GRANTED [" + target.getPackageName() + "] by configuration.");
        return ACCESS_GRANTED;
    }
    String requiredAuthority = attributePrefix + target.getPackageName();
    for (GrantedAuthority grantedAuthority : authentication.getAuthorities()) {
        if (grantedAuthority.getAuthority().equals(requiredAuthority)) {
            LOG.debug("ACCESS_GRANTED [" + target.getPackageName() + "]");
            return ACCESS_GRANTED;
        }
    }
    LOG.debug("ACCESS_DENIED [" + target.getPackageName() + "]");
    return ACCESS_DENIED;
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) GrantedAuthority(org.springframework.security.core.GrantedAuthority)

Example 5 with Configuration

use of com.opensymphony.xwork2.config.Configuration in project dhis2-core by dhis2.

the class DefaultModuleManager method getPackageConfigs.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private Collection<PackageConfig> getPackageConfigs() {
    Configuration configuration = Dispatcher.getInstance().getConfigurationManager().getConfiguration();
    Map<String, PackageConfig> packageConfigs = configuration.getPackageConfigs();
    return packageConfigs.values();
}
Also used : Configuration(com.opensymphony.xwork2.config.Configuration) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Aggregations

PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)4 Configuration (com.opensymphony.xwork2.config.Configuration)3 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)3 Configuration (Config.Configuration)2 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 HashSet (java.util.HashSet)1 org.apache.struts2.config (org.apache.struts2.config)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1 SecurityMetadataSource (org.springframework.security.access.SecurityMetadataSource)1 InsufficientAuthenticationException (org.springframework.security.authentication.InsufficientAuthenticationException)1 Authentication (org.springframework.security.core.Authentication)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SecurityContext (org.springframework.security.core.context.SecurityContext)1