Java tutorial
/* * Copyright 2016 Brigham Young University * * 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. */ package edu.byu.mpn.rest; import com.google.common.base.Function; import com.google.common.collect.Collections2; import com.google.common.collect.Sets; import edu.byu.jwt.domain.CasUser; import edu.byu.jwt.spring.domain.JwtUserDetails; import edu.byu.mpn.client.interfaces.MpnClient; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import java.util.Collections; import java.util.HashSet; import java.util.Set; /** * Created by cwoodfie on 4/22/16. */ public abstract class BaseController { private static final Logger LOG = LogManager.getLogger(BaseController.class); public static final String APPLE = "Apple"; public static final String ANDROID = "Android"; protected MpnClient mpnClient; @Autowired public void setMpnClient(@Qualifier("mpnClient") MpnClient mpnClient) { this.mpnClient = mpnClient; } protected static final Set<String> proxyRoles = new HashSet<String>( Collections.singletonList("GRO_BYU_MPN_ADMIN")); private static final Function<GrantedAuthority, String> AUTHORITY_TO_STRING_FUNCTION = new Function<GrantedAuthority, String>() { @Override public String apply(GrantedAuthority input) { return input.getAuthority(); } }; protected boolean personHasProxyRole() { return !Sets.intersection(proxyRoles, getLoggedInUserRoles()).isEmpty(); } private Set<String> getLoggedInUserRoles() { JwtUserDetails principal = getJwtUserDetails(); if (principal != null) { return new HashSet<String>( Collections2.transform(principal.getAuthorities(), AUTHORITY_TO_STRING_FUNCTION)); } return new HashSet<String>(1); } protected String getPersonId(String providedTargetId) { return personHasProxyRole() && !providedTargetId.isEmpty() ? providedTargetId : getLoggedInPersonId(); } protected String getLoggedInPersonId() { JwtUserDetails principal = getJwtUserDetails(); if (principal != null) { CasUser endUser = (CasUser) principal.getEndUser(); if (endUser != null) { return endUser.getPersonId(); } } return null; } private JwtUserDetails getJwtUserDetails() { SecurityContext context = SecurityContextHolder.getContext(); if (context != null) { Authentication auth = context.getAuthentication(); if (auth != null) { return (JwtUserDetails) auth.getPrincipal(); } } return null; } }