package com.fjs.scenic.handler;

import com.fjs.scenic.entity.system.Manage;
import com.fjs.scenic.service.system.ManageService;
import com.fjs.scenic.service.system.PermissionService;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
public class MyUserDetailsService implements UserDetailsService {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private PermissionService permissionService;
	@Autowired
	private ManageService manageService;

	public MyUserDetailsService() {
	}

	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		req.getSession().setAttribute("msg", (Object)null);
		Manage manager = this.manageService.getMangerByAccount(username);
		if (manager == null) {
			req.getSession().setAttribute("msg", "帐号不存在或已被冻结！");
			new RuntimeException();
		}

		List<GrantedAuthority> authorities = this.getAuthorities(manager);
		User user = new User(username, manager.getPassword(), authorities);
		return user;
	}

	private List<GrantedAuthority> getAuthorities(Manage manager) {
		List<GrantedAuthority> authorities = new ArrayList();
		authorities.add(new SimpleGrantedAuthority("/manage/index"));
		authorities.add(new SimpleGrantedAuthority("/leaderManage/index"));
		authorities.add(new SimpleGrantedAuthority("/statisticalPermissions/index"));
		authorities.add(new SimpleGrantedAuthority("/statisticalPermissions/page"));
		authorities.add(new SimpleGrantedAuthority("/statisticalPermissions/add"));
		authorities.add(new SimpleGrantedAuthority("/statisticalPermissions/del"));
		authorities.add(new SimpleGrantedAuthority("/statisticalPermissions/update"));
		return authorities;
	}

	private List<GrantedAuthority> getSysAuthorities() {
		List<GrantedAuthority> authorities = new ArrayList();
		authorities.add(new SimpleGrantedAuthority("/index"));
		List<String> urls = this.permissionService.getPermissionAllNotParent();
		Iterator var3 = urls.iterator();

		while(var3.hasNext()) {
			String url = (String)var3.next();
			authorities.add(new SimpleGrantedAuthority(url));
		}

		return authorities;
	}
}
