package com.mca.sec.service;

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

import javax.annotation.Resource;

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 com.mca.sec.mapper.SecurityMapper;
import com.mca.sec.vo.LoginUserVO;

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


public class SecurityService implements UserDetailsService{

	@Resource(name = "securityMapper")
	SecurityMapper securityMapper;
	
	
	@Override
	public UserDetails loadUserByUsername(String userid) throws UsernameNotFoundException {
		// TODO Auto-generated method stub
		
		LoginUserVO member = securityMapper.getUser(userid);
		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		
		if (member != null) {
			authorities.add(securityMapper.getAuthoritiesUser(member.getAuth()));
		} else {
			throw new UsernameNotFoundException("접속자 정보를 찾을 수 없습니다.");
		}
    if (authorities.size() == 0) {
			throw new UsernameNotFoundException("User " + member.getUserid()+ " has no GrantedAuthority");
    }
		
		return new EgovUserDetails(member.getUserid(), member.getPassword(), true, true, true, true, authorities, member);
	}

}
