Search in sources :

Example 1 with RoleMap

use of com.michelin.cio.hudson.plugins.rolestrategy.RoleMap in project configuration-as-code-plugin by jenkinsci.

the class RoleBasedAuthorizationStrategyConfigurator method retrieveRoleMap.

@Nonnull
private static RoleMap retrieveRoleMap(@Nonnull Object config, @Nonnull String name, Configurator<RoleDefinition> configurator) throws Exception {
    Map map = (Map) config;
    final Collection<?> c = (Collection<?>) map.get(name);
    TreeMap<Role, Set<String>> resMap = new TreeMap<>();
    if (c == null) {
        // we cannot return emptyMap here due to the Role Strategy code
        return new RoleMap(resMap);
    }
    for (Object entry : c) {
        RoleDefinition definition = configurator.configure(entry);
        resMap.put(definition.getRole(), definition.getAssignments());
    }
    return new RoleMap(resMap);
}
Also used : Role(com.michelin.cio.hudson.plugins.rolestrategy.Role) Set(java.util.Set) HashSet(java.util.HashSet) Collection(java.util.Collection) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) RoleMap(com.michelin.cio.hudson.plugins.rolestrategy.RoleMap) RoleMap(com.michelin.cio.hudson.plugins.rolestrategy.RoleMap) Nonnull(javax.annotation.Nonnull)

Example 2 with RoleMap

use of com.michelin.cio.hudson.plugins.rolestrategy.RoleMap in project configuration-as-code-plugin by jenkinsci.

the class RoleBasedAuthorizationStrategyConfigurator method configure.

@Override
public RoleBasedAuthorizationStrategy configure(Object config) throws Exception {
    // TODO: API should return a qualified type
    final Configurator<RoleDefinition> roleDefinitionConfigurator = (Configurator<RoleDefinition>) Configurator.lookup(RoleDefinition.class);
    if (roleDefinitionConfigurator == null) {
        throw new IOException("Cannot find configurator for" + RoleDefinition.class);
    }
    Map map = (Map) config;
    Map<String, RoleMap> grantedRoles = new HashMap<>();
    Object rolesConfig = map.get("roles");
    if (rolesConfig != null) {
        grantedRoles.put(RoleBasedAuthorizationStrategy.GLOBAL, retrieveRoleMap(rolesConfig, "global", roleDefinitionConfigurator));
        grantedRoles.put(RoleBasedAuthorizationStrategy.PROJECT, retrieveRoleMap(rolesConfig, "items", roleDefinitionConfigurator));
        grantedRoles.put(RoleBasedAuthorizationStrategy.SLAVE, retrieveRoleMap(rolesConfig, "agents", roleDefinitionConfigurator));
    }
    return new RoleBasedAuthorizationStrategy(grantedRoles);
}
Also used : HashMap(java.util.HashMap) Configurator(org.jenkinsci.plugins.casc.Configurator) RootElementConfigurator(org.jenkinsci.plugins.casc.RootElementConfigurator) IOException(java.io.IOException) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) RoleMap(com.michelin.cio.hudson.plugins.rolestrategy.RoleMap) RoleBasedAuthorizationStrategy(com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy) RoleMap(com.michelin.cio.hudson.plugins.rolestrategy.RoleMap)

Aggregations

RoleMap (com.michelin.cio.hudson.plugins.rolestrategy.RoleMap)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 Role (com.michelin.cio.hudson.plugins.rolestrategy.Role)1 RoleBasedAuthorizationStrategy (com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Nonnull (javax.annotation.Nonnull)1 Configurator (org.jenkinsci.plugins.casc.Configurator)1 RootElementConfigurator (org.jenkinsci.plugins.casc.RootElementConfigurator)1