package org.dicadeveloper.weplantaforest.user;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dicadeveloper.weplantaforest.FileSystemInjector;
import org.dicadeveloper.weplantaforest.common.errorHandling.IpatException;
import org.dicadeveloper.weplantaforest.common.image.ImageHelper;
import org.dicadeveloper.weplantaforest.common.mail.MailHelper;
import org.dicadeveloper.weplantaforest.encryption.PasswordEncrypter;
import org.dicadeveloper.weplantaforest.messages.MessageByLocaleService;
import org.dicadeveloper.weplantaforest.reports.co2.Co2Repository;
import org.dicadeveloper.weplantaforest.reports.rankings.RankingRepository;
import org.dicadeveloper.weplantaforest.security.TokenAuthenticationService;
import org.dicadeveloper.weplantaforest.support.Uris;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired) )
public class UserController {
protected final Log LOG = LogFactory.getLog(UserController.class.getName());
private @NonNull ImageHelper _imageHelper;
private @NonNull UserRepository _userRepository;
private @NonNull RankingRepository _rankingRepository;
private @NonNull Co2Repository _co2Repository;
private @NonNull TokenAuthenticationService _tokenAuthenticationService;
private @NonNull PasswordEncrypter _passwordEncrypter;
private @NonNull UserService _userService;
private @NonNull MailHelper _mailHelper;
private @NonNull Environment _env;
private @NonNull MessageByLocaleService _messageByLocaleService;
@RequestMapping(value = Uris.USER_IMAGE + "{imageName:.+}/{width}/{height}", method = RequestMethod.GET, headers = "Accept=image/jpeg, image/jpg, image/png, image/gif")
public ResponseEntity<?> getImage(HttpServletResponse response, @PathVariable String imageName, @PathVariable int width, @PathVariable int height) {
String filePath = FileSystemInjector.getUserFolder() + "/" + imageName;
try {
_imageHelper.writeImageToOutputStream(response.getOutputStream(), filePath, width, height);
return new ResponseEntity<>(HttpStatus.OK);
} catch (IOException e) {
LOG.error("Error occured while trying to get image " + imageName + " in folder: " + filePath, e);
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
@RequestMapping(value = Uris.USER_DETAILS, method = RequestMethod.GET)
public UserReportData getUserDetails(@RequestHeader(value = "X-AUTH-TOKEN") String userToken, @RequestParam String userName) {
boolean isEditAllowed = _tokenAuthenticationService.isAuthenticatedUser(userToken, userName);
UserReportData userReportData = _userService.getUserDetails(userName, isEditAllowed);
return userReportData;
}
@RequestMapping(value = Uris.EDIT_USER_DETAILS, method = RequestMethod.POST)
public ResponseEntity<?> editUserDetails(@RequestHeader(value = "X-AUTH-TOKEN") String userToken, @RequestParam String userName, @RequestParam String toEdit, @RequestParam String newEntry)
throws IpatException {
if (_tokenAuthenticationService.isAuthenticatedUser(userToken, userName)) {
_userService.editUser(userName, toEdit, newEntry);
return new ResponseEntity<>(HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
}
@RequestMapping(value = Uris.USER_IMAGE_UPLOAD, method = RequestMethod.POST)
public ResponseEntity<?> uploadUserImage(@RequestHeader(value = "X-AUTH-TOKEN") String userToken, @RequestParam String userName, @RequestParam("file") MultipartFile file) throws IpatException {
if (_tokenAuthenticationService.isAuthenticatedUser(userToken, userName)) {
_userService.uploadUserImage(userName, file);
return new ResponseEntity<>(HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
}
@RequestMapping(value = Uris.REGISTRATE_USER, method = RequestMethod.POST)
public ResponseEntity<?> registrateUser(@RequestBody UserRegistrationData userRegistrationData) throws IpatException {
_userService.registrateUser(userRegistrationData);
return new ResponseEntity<>(HttpStatus.OK);
}
@RequestMapping(value = Uris.USER_ACTIVATE, method = RequestMethod.POST)
public ResponseEntity<?> activateUser(@RequestParam long id, @RequestParam String key, @RequestParam String language) throws IpatException {
_userService.activateUser(id, key);
return new ResponseEntity<>(HttpStatus.OK);
}
@RequestMapping(value = Uris.USER_LANGUAGE, method = RequestMethod.GET)
public String getLanguageFromUser(@RequestParam String userName) {
return _userRepository.getUserLanguage(userName)
.toString();
}
@RequestMapping(value = Uris.USER_PASSWORD_RESET_REQUEST, method = RequestMethod.POST)
public ResponseEntity<?> createResetPassword(@RequestParam String userName, @RequestParam String language) throws IpatException {
_userService.createPasswordResetMail(userName);
return new ResponseEntity<>(HttpStatus.OK);
}
@RequestMapping(value = Uris.USER_PASSWORD_RESET_VERIFIY_LINK, method = RequestMethod.POST)
public ResponseEntity<?> verifyPasswordResetLink(@RequestParam long id, @RequestParam String key, @RequestParam String language) throws IpatException {
String userName = _userService.verifiyPasswordResetLink(id, key);
return new ResponseEntity<>(userName, HttpStatus.OK);
}
@RequestMapping(value = Uris.USER_PASSWORD_RESET, method = RequestMethod.POST)
public ResponseEntity<?> resetPasswordForUser(@RequestParam long id, @RequestParam String key, @RequestParam String language, @RequestParam String password) throws IpatException {
String userName = _userService.resetPasswordForUser(id, key, password);
return new ResponseEntity<>(userName, HttpStatus.OK);
}
@RequestMapping(value = Uris.IS_USER_ADMIN, method = RequestMethod.GET)
public ResponseEntity<?> isAdmin(@RequestHeader(value = "X-AUTH-TOKEN") String userToken) {
boolean isAdmin = _tokenAuthenticationService.isAdmin(userToken);
return new ResponseEntity<>(isAdmin, HttpStatus.OK);
}
}