/** 
 * SecurityServiceImpl.java
 * @author 임새미 
 * @since 2016. 10. 6.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 10. 6. 	    임새미				       최초생성
 * 
 */
package kcg.faics.sec.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import kcg.faics.member.UserType;
import kcg.faics.sec.FaicsUserDetails;
import kcg.faics.sec.LoginUserVO;

import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import egovframework.rte.fdl.security.userdetails.EgovUserDetails;

/** 
 * SecurityServiceImpl.java
 * @author 임새미 
 * @since 2016. 10. 6.
 *
 *  	 수정일                     수정자                                       수정내용
 *  -------------	--------	---------------------------
 *  2016. 10. 6. 	    임새미				       최초생성
 * 
 */
public class SecurityServiceImpl implements UserDetailsService {

	@Resource(name = "securityMapper")
	SecurityMapper securityMapper;
	
	private RoleHierarchy roleHierarchy;
	
	/**
     * Role Hierarchy값을 설정한다.
     * 
     * @param roleHierarchy
     */
    public void setRoleHierarchy(RoleHierarchy roleHierarchy) {
        this.roleHierarchy = roleHierarchy;
    }
	

    /**
     * 사용자 정보를 DB에서 가져와 UserDetails 객체에 담아 반환한다.
     * 
     * @param userId 사용자 아이디
     * @return UserDetails 객체
     */
	@Override
	public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException {
		
		String usertypeStr = "";
		/* 외사경찰 */
		LoginUserVO fa = securityMapper.getFaUser(userId);
		/* 일반경찰 */ 
		LoginUserVO nfa = securityMapper.getCmmnUser(userId);
		
		LoginUserVO member = null;
		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		
		if (fa != null) {
			member = fa;
			member.setType(1);
			usertypeStr = UserType.FA;
			authorities.add(securityMapper.getAuthoritiesFaUser(userId));
		} else if (nfa != null) {
			member = nfa;
			member.setType(2);
			usertypeStr = UserType.CM;
			authorities.add(securityMapper.getAuthoritiesCmmnUser(userId));
		} else {
			throw new UsernameNotFoundException("접속자 정보를 찾을 수 없습니다.");
		}
        if (authorities.size() == 0) {
			throw new UsernameNotFoundException("User " + member.getUserid()+ " has no GrantedAuthority");
        }
        
		/* RoleHierarchyImpl 에서 저장한 Role Hierarchy 정보가 저장된다. */
        Collection<? extends GrantedAuthority> roleAuthorities = roleHierarchy.getReachableGrantedAuthorities(authorities);
		//return new EgovUserDetails(member.getUserid(), member.getPasswd(), true, true, true, true, roleAuthorities, member); 
        return new FaicsUserDetails(member.getUserid(), member.getPasswd(), usertypeStr, member.getChkcnt(), true, true, true, true, roleAuthorities, member);
	}

}
