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

org.ff4j.web.api.security.FF4JSecurityContextAuthenticationManager Maven / Gradle / Ivy

There is a newer version: 1.7.3
Show newest version
package org.ff4j.web.api.security;

/*
 * #%L
 * ff4j-web
 * %%
 * Copyright (C) 2013 - 2015 Ff4J
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.core.SecurityContext;

import org.ff4j.security.AbstractAuthorizationManager;

import com.sun.jersey.spi.container.ContainerRequest;


/**
 * Implementation of Manager to base permissions on incoming user.
 *
 * @author Cedrick LUNVEN
 */
public class FF4JSecurityContextAuthenticationManager extends AbstractAuthorizationManager {
    
    /** {@inheritDoc} */
    public String getCurrentUserName() {
        SecurityContext wrapper = FF4JSecurityContextHolder.getSecurityContext();
        if (wrapper instanceof ContainerRequest) {
            SecurityContext sc = ((ContainerRequest) wrapper).getSecurityContext();
            if (sc != null && sc instanceof FF4jSecurityContext) {
                return ((FF4jSecurityContext) sc).getUserName();
            }
        }
        return null;
    }
    
    /** {@inheritDoc} */
    @Override
    public Set getCurrentUserPermissions() {
        SecurityContext wrapper = FF4JSecurityContextHolder.getSecurityContext();
        if (wrapper instanceof ContainerRequest) {
            SecurityContext sc = ((ContainerRequest) wrapper).getSecurityContext();
            if (sc != null && sc instanceof FF4jSecurityContext) {
                return ((FF4jSecurityContext) sc).getUserRoles();
            }
        }
        return new HashSet();
    }

    /** {@inheritDoc} */
    @Override
    public Set listAllPermissions() {
        Set < String > vars = new HashSet();
        if (FF4jSecurityContextFilter.getSecurityConfig() != null) {
            Map < String, Set > perms = FF4jSecurityContextFilter.getSecurityConfig().getPermissions();
            for (Map.Entry> var : perms.entrySet()) {
                var.getValue().addAll(var.getValue());
            }
        }
        return vars;
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy