package com.rrtx.services.common.authentication;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;


/**
 * ClassName: ShiroRealm<br>
 * Description: 自定义实现 ShiroRealm，包含认证和授权两大模块 <br>
 * Company: rrtx
 *
 * @author rrtx
 * @version v1.0.0    2019/7/24 4:41 PM  xugz    文件初始创建
 */
@Component
public class ShiroRealm extends AuthorizingRealm {

//	@Autowired
//	private IUserService userService;
//	@Autowired
//	private IRoleService roleService;
//	@Autowired
//	private IMenuService menuService;

	/**
	 * 授权模块，获取用户角色和权限
	 *
	 * @param principal principal
	 * @return AuthorizationInfo 权限信息
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
//		User user = (User) SecurityUtils.getSubject().getPrincipal();
//		String userName = user.getUsername();
//
//		SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//
//		// 获取用户角色集
//		List<Role> roleList = this.roleService.findUserRole(userName);
//		Set<String> roleSet = roleList.stream().map(Role::getRoleName).collect(Collectors.toSet());
//		simpleAuthorizationInfo.setRoles(roleSet);
//
//		// 获取用户权限集
//		List<Menu> permissionList = this.menuService.findUserPermissions(userName);
//		Set<String> permissionSet = permissionList.stream().map(Menu::getPerms).collect(Collectors.toSet());
//		simpleAuthorizationInfo.setStringPermissions(permissionSet);
//		return simpleAuthorizationInfo;
		//TODO
		return null;
	}

	/**
	 * 用户认证
	 *
	 * @param token AuthenticationToken 身份认证 token
	 * @return AuthenticationInfo 身份认证信息
	 * @throws AuthenticationException 认证相关异常
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//		// 获取用户输入的用户名和密码
//		String userName = (String) token.getPrincipal();
//		String password = new String((char[]) token.getCredentials());
//
//		// 通过用户名到数据库查询用户信息
//		User user = this.userService.findByName(userName);
//
//		if (user == null)
//			throw new UnknownAccountException("账号未注册！");
//		if (!StringUtils.equals(password, user.getPassword()))
//			throw new IncorrectCredentialsException("用户名或密码错误！");
//		if (User.STATUS_LOCK.equals(user.getStatus()))
//			throw new LockedAccountException("账号已被锁定,请联系管理员！");
//		return new SimpleAuthenticationInfo(user, password, getName());
		//TODO
		return null;
	}

	/**
	 * 清除当前用户权限缓存
	 * 使用方法：在需要清除用户权限的地方注入 ShiroRealm,
	 * 然后调用其 clearCache方法。
	 */
	public void clearCache() {
		PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
		super.clearCache(principals);
	}
}
