package org.constellation.rest.api;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.constellation.auth.transfer.UserTransfer;
import org.constellation.database.api.repository.UserRepository;
import org.constellation.services.web.controller.AuthController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
@Component
@Path("/user")
public class UserResource
{
@Autowired
private UserRepository userRepository;
/**
* Retrieves the currently logged in user.
*
* @return A transfer containing the username and the roles.
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
public UserTransfer getUser()
{
UserDetails userDetails = AuthController.extractUserDetail();
return new UserTransfer(userDetails.getUsername(), this.createRoleMap(userDetails));
}
private Map<String, Boolean> createRoleMap(UserDetails userDetails)
{
Map<String, Boolean> roles = new HashMap<String, Boolean>();
for (GrantedAuthority authority : userDetails.getAuthorities()) {
roles.put(authority.getAuthority(), Boolean.TRUE);
}
return roles;
}
}