Improve collab error email report.

This commit is contained in:
Paulo Gustavo Veiga 2022-03-24 00:14:38 -03:00
parent 9d08ad57b9
commit c5c47892cb

View File

@ -41,6 +41,7 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
@Controller @Controller
@ -289,25 +290,26 @@ public class MindmapController extends BaseController {
@RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}/collabs/", consumes = {"application/json", "application/xml"}, produces = {"application/json", "application/xml"}) @RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}/collabs/", consumes = {"application/json", "application/xml"}, produces = {"application/json", "application/xml"})
@ResponseStatus(value = HttpStatus.NO_CONTENT) @ResponseStatus(value = HttpStatus.NO_CONTENT)
public void addCollab(@PathVariable int id, @NotNull @RequestBody RestCollaborationList restCollabs) throws CollaborationException, MapCouldNotFoundException, AccessDeniedSecurityException { public void addCollab(@PathVariable int id, @NotNull @RequestBody RestCollaborationList restCollabs) throws CollaborationException, MapCouldNotFoundException, AccessDeniedSecurityException, InvalidEmailException {
final Mindmap mindMap = findMindmapById(id); final Mindmap mindMap = findMindmapById(id);
// Only owner can change collaborators... // Only owner can change collaborators...
final User user = Utils.getUser(); final User user = Utils.getUser();
if (!mindMap.hasPermissions(user, CollaborationRole.OWNER)) { if (!mindMap.hasPermissions(user, CollaborationRole.OWNER)) {
throw new IllegalArgumentException("No enough permissions"); throw new AccessDeniedSecurityException("User must be owner to share mindmap");
} }
// Is valid email address ? // Is valid email address ?
final EmailValidator emailValidator = EmailValidator.getInstance(); final EmailValidator emailValidator = EmailValidator.getInstance();
restCollabs final Set<String> invalidEmails = restCollabs
.getCollaborations() .getCollaborations()
.forEach(collab -> { .stream()
// Is a valid email address ? .map(RestCollaboration::getEmail)
if (!emailValidator.isValid(collab.getEmail())) { .filter(e -> !emailValidator.isValid(e)).collect(Collectors.toSet());
throw new IllegalArgumentException(collab.getEmail() + " is not valid email address");
if (!invalidEmails.isEmpty()) {
throw new InvalidEmailException(String.join(", ", invalidEmails));
} }
});
// Has any role changed ?. Just removed it. // Has any role changed ?. Just removed it.
final Map<String, Collaboration> mapsByEmail = mindMap final Map<String, Collaboration> mapsByEmail = mindMap