package es.uv.garcosda.security;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;

import es.uv.garcosda.domain.Role;
import es.uv.garcosda.domain.User;

public class AuthUser extends org.springframework.security.core.userdetails.User {

	private static final long serialVersionUID = 1L;
	private User user;
	
	public AuthUser(User user) {
		super(user.getUsername(), user.getPassword(), getAuthorities(user));
		this.user = user;
	}
	
	public User getUser() {
		return user;
	}
	
	private static Collection<? extends GrantedAuthority> getAuthorities(User user) {
		Set<String> roleAndPermissions = new HashSet<>();
		List<Role> roles = user.getRoles();
		
		for (Role role : roles) {
			roleAndPermissions.add(role.getName());
		}
		
		String[] roleNames = new String[roleAndPermissions.size()];
		Collection<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(roleAndPermissions.toArray(roleNames));
		return authorities;
	}
	
}
