package de.rwth.idsg.brsm.web.rest; import com.codahale.metrics.annotation.Timed; import de.rwth.idsg.brsm.domain.Authority; import de.rwth.idsg.brsm.domain.PersistentToken; import de.rwth.idsg.brsm.domain.User; import de.rwth.idsg.brsm.repository.PersistentTokenRepository; import de.rwth.idsg.brsm.repository.UserRepository; import de.rwth.idsg.brsm.security.SecurityUtils; import de.rwth.idsg.brsm.service.UserService; import de.rwth.idsg.brsm.web.rest.dto.UserDTO; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.List; /** * REST controller for managing the current user's account. */ @RestController @RequestMapping("/app") public class AccountResource { private final Logger log = LoggerFactory.getLogger(AccountResource.class); @Inject private UserRepository userRepository; @Inject private UserService userService; @Inject private PersistentTokenRepository persistentTokenRepository; /** * GET /rest/authenticate -> check if the user is authenticated, and return its login. */ @RequestMapping(value = "/rest/authenticate", method = RequestMethod.GET, produces = "application/json") @Timed public String isAuthenticated(HttpServletRequest request) { log.debug("REST request to check if the current user is authenticated"); return request.getRemoteUser(); } /** * GET /rest/account -> get the current user. */ @RequestMapping(value = "/rest/account", method = RequestMethod.GET, produces = "application/json") @Timed public UserDTO getAccount(HttpServletResponse response) { User user = userService.getUserWithAuthorities(); if (user == null) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return null; } List<String> roles = new ArrayList<>(); for (Authority authority : user.getAuthorities()) { roles.add(authority.getName()); } return new UserDTO(user.getLogin(), user.getFirstName(), user.getLastName(), user.getEmail(), roles); } /** * POST /rest/account -> update the current user information. */ @RequestMapping(value = "/rest/account", method = RequestMethod.POST, produces = "application/json") @Timed public void saveAccount(@RequestBody UserDTO userDTO) throws IOException { userService.updateUserInformation(userDTO.getFirstName(), userDTO.getLastName(), userDTO.getEmail()); } /** * POST /rest/change_password -> changes the current user's password */ @RequestMapping(value = "/rest/account/change_password", method = RequestMethod.POST, produces = "application/json") @Timed public void changePassword(@RequestBody String password, HttpServletResponse response) throws IOException { if (password == null || password.equals("")) { response.sendError(HttpServletResponse.SC_FORBIDDEN, "Password should not be empty"); } else { userService.changePassword(password); } } /** * GET /rest/account/sessions -> get the current open sessions. */ @RequestMapping(value = "/rest/account/sessions", method = RequestMethod.GET, produces = "application/json") @Timed public List<PersistentToken> getCurrentSessions(HttpServletResponse response) { User user = userRepository.findOne(SecurityUtils.getCurrentLogin()); if (user == null) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } return persistentTokenRepository.findByUser(user); } /** * DELETE /rest/account/sessions?series={series} -> invalidate an existing session. */ @RequestMapping(value = "/rest/account/sessions/{series}", method = RequestMethod.DELETE) @Timed public void invalidateSession(@PathVariable String series, HttpServletRequest request) throws UnsupportedEncodingException { String decodedSeries = URLDecoder.decode(series, "UTF-8"); // Check if the session to invalidate if the current user session. // If so, the security session will be invalidated too User user = userRepository.findOne(SecurityUtils.getCurrentLogin()); final List<PersistentToken> persistentTokens = persistentTokenRepository.findByUser(user); for (PersistentToken persistentToken : persistentTokens) { if (StringUtils.equals(persistentToken.getSeries(), decodedSeries)) { request.getSession().invalidate(); SecurityContextHolder.clearContext(); } } persistentTokenRepository.delete(decodedSeries); } }