package com.mycompany.myapp.web.rest; import com.codahale.metrics.annotation.Timed; import com.mycompany.myapp.domain.Authority; import com.mycompany.myapp.domain.PersistentToken; import com.mycompany.myapp.domain.User; import com.mycompany.myapp.repository.PersistentTokenRepository; import com.mycompany.myapp.repository.UserRepository; import com.mycompany.myapp.security.SecurityUtils; import com.mycompany.myapp.service.UserService; import com.mycompany.myapp.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; import java.util.stream.Collectors; /** * 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 = user.getAuthorities().stream().map(Authority::getName).collect(Collectors.toList()); 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. * * - You can only delete your own sessions, not any other user's session * - If you delete one of your existing sessions, and that you are currently logged in on that session, you will * still be able to use that session, until you quit your browser: it does not work in real time (there is * no API for that), it only removes the "remember me" cookie * - This is also true if you invalidate your current session: you will still be able to use it until you close * your browser or that the session times out. But automatic login (the "remember me" cookie) will not work * anymore. * There is an API to invalidate the current session, but there is no API to check which session uses which * cookie. */ @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"); User user = userRepository.findOne(SecurityUtils.getCurrentLogin()); if (persistentTokenRepository.findByUser(user).stream() .filter(persistentToken -> StringUtils.equals(persistentToken.getSeries(), decodedSeries)) .count() > 0) { persistentTokenRepository.delete(decodedSeries); } } }