Commit 921dfcc9 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

requestResetPassword moved to controller

parent 41430f3c
......@@ -2,7 +2,6 @@ package lcsb.mapviewer.api.users;
import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
......@@ -30,10 +29,16 @@ import org.springframework.web.bind.annotation.RestController;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.common.exception.NotImplementedException;
import lcsb.mapviewer.model.user.ConfigurationElementType;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.services.InvalidTokenException;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IConfigurationService;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.services.utils.EmailSender;
@RestController
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_VALUE)
......@@ -42,14 +47,19 @@ public class UserController extends BaseController {
Logger logger = LogManager.getLogger();
private IUserService userService;
private IConfigurationService configurationService;
private UserRestImpl userRest;
private PasswordEncoder passwordEncoder;
private EmailSender emailSender;
@Autowired
public UserController(IUserService userService, UserRestImpl userRest, PasswordEncoder passwordEncoder) {
public UserController(IUserService userService, UserRestImpl userRest, PasswordEncoder passwordEncoder, EmailSender emailSender,
IConfigurationService configurationService) {
this.userService = userService;
this.userRest = userRest;
this.passwordEncoder = passwordEncoder;
this.emailSender = emailSender;
this.configurationService = configurationService;
}
/**
......@@ -130,10 +140,27 @@ public class UserController extends BaseController {
@PostMapping(value = "/users/{login}:requestResetPassword")
public Map<String, Object> requestResetPasswordToken(
@PathVariable(value = "login") String login) throws QueryException, MessagingException {
userRest.requestResetPassword(login);
Map<String, Object> result = new HashMap<>();
result.put("status", "OK");
return result;
User user = userService.getUserByLogin(login);
if (user == null) {
throw new ObjectNotFoundException("User does not exist");
}
if (user.getEmail() == null || user.getEmail().equals("")) {
throw new QueryException("User does not have email address defined");
}
if (user.isConnectedToLdap()) {
throw new QueryException("User authentication is provided over LDAP");
}
if (configurationService.getConfigurationValue(ConfigurationElementType.MINERVA_ROOT).trim().isEmpty()) {
throw new InvalidStateException("Cannot create token - minerva root url is not defined");
}
if (!emailSender.canSendEmails()) {
throw new InvalidStateException("Cannot create token - minerva cannot send emails");
}
userService.createResetPasswordToken(user);
return Map.of("status", "OK");
}
@PostMapping(value = "/users:resetPassword")
......
package lcsb.mapviewer.api.users;
import java.io.IOException;
import java.util.*;
import javax.mail.MessagingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......@@ -17,19 +25,32 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MultiValueMap;
import lcsb.mapviewer.api.*;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.OperationNotAllowedException;
import lcsb.mapviewer.api.UpdateConflictException;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.layout.ProjectBackground;
import lcsb.mapviewer.model.security.Privilege;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.*;
import lcsb.mapviewer.model.user.annotator.*;
import lcsb.mapviewer.services.*;
import lcsb.mapviewer.model.user.AnnotatorParamDefinition;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.model.user.UserAnnotationSchema;
import lcsb.mapviewer.model.user.UserClassAnnotators;
import lcsb.mapviewer.model.user.UserClassRequiredAnnotations;
import lcsb.mapviewer.model.user.UserClassValidAnnotations;
import lcsb.mapviewer.model.user.UserGuiPreference;
import lcsb.mapviewer.model.user.annotator.AnnotatorConfigParameter;
import lcsb.mapviewer.model.user.annotator.AnnotatorData;
import lcsb.mapviewer.model.user.annotator.AnnotatorInputParameter;
import lcsb.mapviewer.model.user.annotator.AnnotatorOutputParameter;
import lcsb.mapviewer.model.user.annotator.AnnotatorParameter;
import lcsb.mapviewer.model.user.annotator.BioEntityField;
import lcsb.mapviewer.services.ObjectExistsException;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IProjectBackgroundService;
import lcsb.mapviewer.services.utils.EmailSender;
@Transactional(rollbackFor = UpdateConflictException.class)
@Service
......@@ -43,13 +64,11 @@ public class UserRestImpl extends BaseRestImpl {
private PasswordEncoder passwordEncoder;
private IProjectBackgroundService projectBackgroundService;
private EmailSender emailSender;
@Autowired
public UserRestImpl(PasswordEncoder passwordEncoder, EmailSender emailSender,
public UserRestImpl(PasswordEncoder passwordEncoder,
IProjectBackgroundService projectBackgroundService) {
this.passwordEncoder = passwordEncoder;
this.emailSender = emailSender;
this.projectBackgroundService = projectBackgroundService;
}
......@@ -98,58 +117,58 @@ public class UserRestImpl extends BaseRestImpl {
String column = string.toLowerCase();
Object value;
switch (column) {
case "id":
case "idobject":
value = user.getId();
break;
case "name":
value = user.getName();
break;
case "surname":
value = user.getSurname();
break;
case "login":
value = user.getLogin();
break;
case "email":
value = user.getEmail();
break;
case "mincolor":
value = user.getMinColor();
break;
case "maxcolor":
value = user.getMaxColor();
break;
case "neutralcolor":
value = user.getNeutralColor();
break;
case "simplecolor":
value = user.getSimpleColor();
break;
case "removed":
value = user.isRemoved();
break;
case "termsofuseconsent":
value = user.isTermsOfUseConsent();
break;
case "connectedtoldap":
value = user.isConnectedToLdap();
break;
case "ldapaccountavailable":
if (ldapAvailable == null) {
ldapAvailable = false;
}
value = ldapAvailable;
break;
case "privileges":
value = preparePrivileges(user);
break;
case "preferences":
value = preparePreferences(user);
break;
default:
value = "Unknown column";
break;
case "id":
case "idobject":
value = user.getId();
break;
case "name":
value = user.getName();
break;
case "surname":
value = user.getSurname();
break;
case "login":
value = user.getLogin();
break;
case "email":
value = user.getEmail();
break;
case "mincolor":
value = user.getMinColor();
break;
case "maxcolor":
value = user.getMaxColor();
break;
case "neutralcolor":
value = user.getNeutralColor();
break;
case "simplecolor":
value = user.getSimpleColor();
break;
case "removed":
value = user.isRemoved();
break;
case "termsofuseconsent":
value = user.isTermsOfUseConsent();
break;
case "connectedtoldap":
value = user.isConnectedToLdap();
break;
case "ldapaccountavailable":
if (ldapAvailable == null) {
ldapAvailable = false;
}
value = ldapAvailable;
break;
case "privileges":
value = preparePrivileges(user);
break;
case "preferences":
value = preparePreferences(user);
break;
default:
value = "Unknown column";
break;
}
result.put(string, value);
}
......@@ -758,27 +777,4 @@ public class UserRestImpl extends BaseRestImpl {
return okStatus();
}
public void requestResetPassword(String login) throws MessagingException, QueryException {
User user = getUserService().getUserByLogin(login);
if (user == null) {
throw new ObjectNotFoundException("User does not exist");
}
if (user.getEmail() == null || user.getEmail().equals("")) {
throw new QueryException("User does not have email address defined");
}
if (user.isConnectedToLdap()) {
throw new QueryException("User authentication is provided over LDAP");
}
if (getConfigurationService().getConfigurationValue(ConfigurationElementType.MINERVA_ROOT).trim().isEmpty()) {
throw new InvalidStateException("Cannot create token - minerva root url is not defined");
}
if (!emailSender.canSendEmails()) {
throw new InvalidStateException("Cannot create token - minerva cannot send emails");
}
getUserService().createResetPasswordToken(user);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment