diff --git a/mozen/src/main/java/mozen/business/CommentManager.java b/mozen/src/main/java/mozen/business/CommentManager.java new file mode 100644 index 0000000000000000000000000000000000000000..2c4478d10553996ff341fa750478b7a0a4201d72 --- /dev/null +++ b/mozen/src/main/java/mozen/business/CommentManager.java @@ -0,0 +1,80 @@ +package mozen.business; + +import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import mozen.messages.CommentMessage; +import mozen.model.Comment; +import mozen.model.Model; +import mozen.model.User; +import mozen.repos.CommentRepository; +import mozen.repos.ModelRepository; +import mozen.repos.UserRepository; +import mozen.utils.UserHelper; + +@Service +public class CommentManager implements ICommentManager { + @Autowired + private ModelRepository modelRepo; + + @Autowired + private UserRepository userRepo; + + @Autowired + private CommentRepository commentRepo; + + @Override + public void addComment(CommentMessage message, User user) throws Exception { + Model model = modelRepo.findById(message.getModelId()).get(); + + if (model == null) + throw new Exception("Unknown model"); + + Comment comment = new Comment(); + comment.setAuthor(user); + comment.setContent(message.getContent()); + comment.setModel(model); + + commentRepo.save(comment); + } + + @Override + public void removeComment(Long id, User user) throws Exception { + Comment comment = commentRepo.findById(id).get(); + + if (comment == null) + throw new Exception("Unknown comment"); + if (!UserHelper.isAuthor(comment.getAuthor(), user)) + throw new Exception("Not the author"); + + commentRepo.delete(comment); + } + + @Override + public Collection<Comment> getCommentFromModel(Long modelId) throws Exception { + Model model = modelRepo.findById(modelId).get(); + + if (model == null) + throw new Exception("Unknown model"); + + return commentRepo.findByModel(model); + } + + @Override + public Collection<Comment> getCommentFromUser(Long userId) throws Exception { + User user = userRepo.findById(userId).get(); + + if (user == null) + throw new Exception("Unknown user"); + + return commentRepo.findByAuthor(user); + } + + @Override + public Collection<Comment> getComments() { + return commentRepo.findAll(); + } + +} \ No newline at end of file diff --git a/mozen/src/main/java/mozen/business/ICommentManager.java b/mozen/src/main/java/mozen/business/ICommentManager.java new file mode 100644 index 0000000000000000000000000000000000000000..a9a5a9fb31ffb3ef23dbb190f2e1fa098ef0edab --- /dev/null +++ b/mozen/src/main/java/mozen/business/ICommentManager.java @@ -0,0 +1,16 @@ +package mozen.business; + +import java.util.Collection; + +import mozen.messages.CommentMessage; +import mozen.model.Comment; +import mozen.model.User; + +public interface ICommentManager { + void addComment(CommentMessage message, User user) throws Exception; + void removeComment(Long id, User user) throws Exception; + + Collection<Comment> getComments(); + Collection<Comment> getCommentFromModel(Long modelId) throws Exception; + Collection<Comment> getCommentFromUser(Long userId) throws Exception; +} \ No newline at end of file diff --git a/mozen/src/main/java/mozen/business/LayerManager.java b/mozen/src/main/java/mozen/business/LayerManager.java index 0f9d418ff51129763ab2c04a2676343c1ac16197..faeca3dee7b1b7661ab86f15db8470b13f97f347 100644 --- a/mozen/src/main/java/mozen/business/LayerManager.java +++ b/mozen/src/main/java/mozen/business/LayerManager.java @@ -9,10 +9,10 @@ import org.springframework.web.multipart.MultipartFile; import mozen.model.CustomLayer; import mozen.model.Model; -import mozen.model.Role; import mozen.model.User; import mozen.repos.CustomLayerRepository; import mozen.repos.ModelRepository; +import mozen.utils.UserHelper; @Service public class LayerManager implements ILayerManager{ @@ -33,7 +33,7 @@ public class LayerManager implements ILayerManager{ if (model == null) throw new Exception("Unknown model"); - if (!isModelAuthor(model, user)) + if (!UserHelper.isAuthor(model.getAuthor(), user)) throw new Exception("Not the author"); Checksum crc32 = new CRC32(); @@ -57,22 +57,9 @@ public class LayerManager implements ILayerManager{ if (layer == null) throw new Exception("Unknown layer"); - if (!isLayerAuthor(layer, user)) + if (!UserHelper.isAuthor(layer.getModel().getAuthor(), user)) throw new Exception("Not the author"); layerRepo.deleteById(id); } - - private boolean isLayerAuthor(CustomLayer layer, User user) { - if (user.getRole() == Role.ROLE_ADMIN) - return true; - return layer.getModel().getAuthor() == user; - } - - private boolean isModelAuthor(Model model, User user) { - if (user.getRole() == Role.ROLE_ADMIN) - return true; - return model.getAuthor() == user; - } - } \ No newline at end of file diff --git a/mozen/src/main/java/mozen/business/ModelManager.java b/mozen/src/main/java/mozen/business/ModelManager.java index 61a34863071f3335eb64b3ad6303bc881d8de9f0..caad665bbb8c97ab5baed4a2cc78557886a22a29 100644 --- a/mozen/src/main/java/mozen/business/ModelManager.java +++ b/mozen/src/main/java/mozen/business/ModelManager.java @@ -17,12 +17,12 @@ import org.springframework.web.multipart.MultipartFile; import mozen.messages.ModelMessage; import mozen.model.Model; -import mozen.model.Role; import mozen.messages.SearchResult; import mozen.model.Tag; import mozen.model.User; import mozen.repos.ModelRepository; import mozen.repos.TagRepository; +import mozen.utils.UserHelper; @Service public class ModelManager implements IModelManager { @@ -73,7 +73,7 @@ public class ModelManager implements IModelManager { if (model == null) throw new Exception("Unknown model"); - if (!isModelAuthor(model, user)) + if (!UserHelper.isAuthor(model.getAuthor(), user)) throw new Exception("Not the author"); Checksum crc32 = new CRC32(); @@ -91,7 +91,7 @@ public class ModelManager implements IModelManager { if (modelToUpdate == null) throw new Exception("Unknown model"); - if (!isModelAuthor(modelToUpdate, user)) + if (!UserHelper.isAuthor(modelToUpdate.getAuthor(), user)) throw new Exception("Not the author"); model.setId(id); @@ -105,7 +105,7 @@ public class ModelManager implements IModelManager { if (model == null) throw new Exception("Unknown model"); - if (!isModelAuthor(model, user)) + if (!UserHelper.isAuthor(model.getAuthor(), user)) throw new Exception("Not the author"); modelRepo.deleteById(id); @@ -166,10 +166,4 @@ public class ModelManager implements IModelManager { public Collection<Model> getModels() { return modelRepo.findAll(); } - - private boolean isModelAuthor(Model model, User user) { - if (user.getRole() == Role.ROLE_ADMIN) - return true; - return model.getAuthor() == user; - } } \ No newline at end of file diff --git a/mozen/src/main/java/mozen/messages/CommentMessage.java b/mozen/src/main/java/mozen/messages/CommentMessage.java new file mode 100644 index 0000000000000000000000000000000000000000..9a2a5e5412af62ec4112ff410b5988346790488d --- /dev/null +++ b/mozen/src/main/java/mozen/messages/CommentMessage.java @@ -0,0 +1,36 @@ +package mozen.messages; + +import java.io.Serializable; + +public class CommentMessage implements Serializable { + private static final long serialVersionUID = 1L; + + private String content; + + private Long modelId; + + public CommentMessage() { + } + + public CommentMessage(String content, Long modelId) { + this.content = content; + this.modelId = modelId; + } + + public String getContent() { + return this.content; + } + + public void setContent(String content) { + this.content = content; + } + + public Long getModelId() { + return this.modelId; + } + + public void setModelId(Long modelId) { + this.modelId = modelId; + } + +} \ No newline at end of file diff --git a/mozen/src/main/java/mozen/model/Comment.java b/mozen/src/main/java/mozen/model/Comment.java new file mode 100644 index 0000000000000000000000000000000000000000..49f0a4d6b894fdb045109946f124d95e38c732ec --- /dev/null +++ b/mozen/src/main/java/mozen/model/Comment.java @@ -0,0 +1,102 @@ +package mozen.model; + +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.Basic; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; +import javax.validation.constraints.Size; + +import com.fasterxml.jackson.annotation.JsonIgnore; + + +@Entity +@Table +public class Comment implements Serializable{ + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Basic + @Column + @Size(min = 0, max = 1000) + private String content; + + @Basic + @Temporal(TemporalType.DATE) + @Column + private Date added; + + @Basic + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE) + private Model model; + + @Basic + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE) + private User author; + + public Comment() { + } + + public Comment(Long id, String content, Date added, Model model, User author) { + this.id = id; + this.content = content; + this.added = added; + this.model = model; + this.author = author; + } + + public Long getId() { + return this.id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getContent() { + return this.content; + } + + public void setContent(String content) { + this.content = content; + } + + @JsonIgnore + public Model getModel() { + return this.model; + } + + public void setModel(Model model) { + this.model = model; + } + + public User getAuthor() { + return this.author; + } + + public void setAuthor(User author) { + this.author = author; + } + + public Date getAdded() { + return this.added; + } + + public void setAdded(Date added) { + this.added = added; + } + +} \ No newline at end of file diff --git a/mozen/src/main/java/mozen/model/Model.java b/mozen/src/main/java/mozen/model/Model.java index 507c82da5dacdb1121d2a9c632cc77b6334a706e..83b993026fcf091f8a77c308372605ecaab95515 100644 --- a/mozen/src/main/java/mozen/model/Model.java +++ b/mozen/src/main/java/mozen/model/Model.java @@ -102,10 +102,14 @@ public class Model implements Serializable{ @JoinTable(name = "model_customLayers") private Set<CustomLayer> customLayers; + @Basic + @OneToMany(mappedBy = "model", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) + private Set<Comment> comments; + public Model() { } - public Model(Long id, String name, String shortDescription, String longDescription, Date added, Date lastModified, int votes, double performance, String performanceUnit, int parameterCount, byte[] file, String fileType, Long checksum, User author, Set<Tag> tags, Set<CustomLayer> customLayers) { + public Model(Long id, String name, String shortDescription, String longDescription, Date added, Date lastModified, int votes, double performance, String performanceUnit, int parameterCount, byte[] file, String fileType, Long checksum, User author, Set<Tag> tags, Set<CustomLayer> customLayers, Set<Comment> comments) { this.id = id; this.name = name; this.shortDescription = shortDescription; @@ -122,6 +126,7 @@ public class Model implements Serializable{ this.author = author; this.tags = tags; this.customLayers = customLayers; + this.comments = comments; } public Long getId() { @@ -253,4 +258,14 @@ public class Model implements Serializable{ this.checksum = checksum; } + @JsonIgnore + public Set<Comment> getComments() { + return this.comments; + } + + public void setComments(Set<Comment> comments) { + this.comments = comments; + } + + } \ No newline at end of file diff --git a/mozen/src/main/java/mozen/model/User.java b/mozen/src/main/java/mozen/model/User.java index f5666c639a6ea834a053a4edaee94ab4b26ab24e..4a2804437274334024fb1166115547ea8b73bb24 100644 --- a/mozen/src/main/java/mozen/model/User.java +++ b/mozen/src/main/java/mozen/model/User.java @@ -62,18 +62,24 @@ public class User implements Serializable{ @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) private Set<Model> models; + @Basic + @OneToMany(mappedBy = "author", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) + @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) + private Set<Comment> comments; + public User() { } - public User(Long id, String username, String email, String password, Role role, Set<Model> models) { + public User(Long id, String username, String password, String email, Role role, Set<Model> models, Set<Comment> comments) { this.id = id; this.username = username; - this.email = email; this.password = password; + this.email = email; this.role = role; this.models = models; + this.comments = comments; } - + public Long getId() { return this.id; } @@ -123,4 +129,14 @@ public class User implements Serializable{ public void setRole(Role role) { this.role = role; } + + @JsonIgnore + public Set<Comment> getComments() { + return this.comments; + } + + public void setComments(Set<Comment> comments) { + this.comments = comments; + } + } \ No newline at end of file diff --git a/mozen/src/main/java/mozen/repos/CommentRepository.java b/mozen/src/main/java/mozen/repos/CommentRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..f978ddebd82a0e7aeb959c6e55d689667b023487 --- /dev/null +++ b/mozen/src/main/java/mozen/repos/CommentRepository.java @@ -0,0 +1,15 @@ +package mozen.repos; + +import java.util.Collection; + +import org.springframework.data.repository.CrudRepository; + +import mozen.model.Comment; +import mozen.model.Model; +import mozen.model.User; + +public interface CommentRepository extends CrudRepository<Comment, Long>{ + Collection<Comment> findAll(); + Collection<Comment> findByModel(Model model); + Collection<Comment> findByAuthor(User author); +} \ No newline at end of file diff --git a/mozen/src/main/java/mozen/utils/DatabaseFiller.java b/mozen/src/main/java/mozen/utils/DatabaseFiller.java index 3734bd0182d4aeea47da7e27c2fb46cb42c6672b..ffe37fb9ca40ea908c06d7b09c302269074cb8b9 100644 --- a/mozen/src/main/java/mozen/utils/DatabaseFiller.java +++ b/mozen/src/main/java/mozen/utils/DatabaseFiller.java @@ -16,6 +16,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import mozen.model.Comment; import mozen.model.Model; import mozen.model.Role; import mozen.model.Tag; @@ -74,6 +75,31 @@ public class DatabaseFiller { m1.setLastModified(new Date()); m1.setTags(tagLoader()); + Set<Comment> coms = new HashSet<Comment>(); + Comment c1 = new Comment(); + c1.setAuthor(u1); + c1.setContent("Commentaire 1 de user 1 sur testo"); + c1.setModel(m1); + c1.setAdded(new Date()); + + Comment c2 = new Comment(); + c2.setAuthor(u1); + c2.setContent("Commentaire 2 de user 1 sur testo"); + c2.setModel(m1); + c2.setAdded(new Date()); + + Comment c3 = new Comment(); + c3.setAuthor(u2); + c3.setContent("Commentaire de user 2 sur testo"); + c3.setModel(m1); + c3.setAdded(new Date()); + + coms.add(c1); + coms.add(c2); + coms.add(c3); + + m1.setComments(coms); + userRepo.save(u1); userRepo.save(u2); userRepo.save(admin); diff --git a/mozen/src/main/java/mozen/utils/UserHelper.java b/mozen/src/main/java/mozen/utils/UserHelper.java new file mode 100644 index 0000000000000000000000000000000000000000..21f6fd70d78e963221408aaef7b638dc530b0881 --- /dev/null +++ b/mozen/src/main/java/mozen/utils/UserHelper.java @@ -0,0 +1,30 @@ +package mozen.utils; + +import org.springframework.security.authentication.AnonymousAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; + +import mozen.business.IUserManager; +import mozen.model.Role; +import mozen.model.User; + +public class UserHelper { + + public static boolean isAuthor(User author, User userToCheck) { + if (userToCheck.getRole() == Role.ROLE_ADMIN) + return true; + return author == userToCheck; + } + + public static User getCurrentUser(IUserManager userManager) { + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + + if (!(auth instanceof AnonymousAuthenticationToken)) { + if (auth.getPrincipal() instanceof org.springframework.security.core.userdetails.User) return null; + String username = (String) auth.getPrincipal(); + return userManager.getUserByUsername(username); + } else { + return null; + } + } +} \ No newline at end of file diff --git a/mozen/src/main/java/mozen/web/CommentController.java b/mozen/src/main/java/mozen/web/CommentController.java new file mode 100644 index 0000000000000000000000000000000000000000..36f8dc90def3f949de1585aceb28131284eb6d02 --- /dev/null +++ b/mozen/src/main/java/mozen/web/CommentController.java @@ -0,0 +1,87 @@ +package mozen.web; + +import java.util.Collection; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import mozen.business.ICommentManager; +import mozen.business.IUserManager; +import mozen.messages.CommentMessage; +import mozen.messages.ResponseMessage; +import mozen.model.Comment; +import mozen.model.User; +import mozen.utils.UserHelper; + + +@RestController +@RequestMapping("/comments") +@CrossOrigin +public class CommentController { + @Autowired + ICommentManager commentManager; + + @Autowired + IUserManager userManager; + + @GetMapping("/list") + public Collection<Comment> getAllComments() { + return commentManager.getComments(); + } + + @GetMapping("/user") + public ResponseEntity<?> getUserComments(@RequestParam(value = "id", required = true) Long id) { + try { + return ResponseEntity.ok(commentManager.getCommentFromUser(id)); + } catch (Exception e) { + return ResponseEntity.badRequest().build(); + } + } + + @GetMapping("") + public ResponseEntity<?> getModelComments(@RequestParam(value = "id", required = true) Long id) { + try { + return ResponseEntity.ok(commentManager.getCommentFromModel(id)); + } catch (Exception e) { + return ResponseEntity.badRequest().build(); + } + } + + @PostMapping("") + public ResponseEntity<ResponseMessage> addComment(@RequestBody CommentMessage message) { + User user = UserHelper.getCurrentUser(userManager); + ResponseMessage response = new ResponseMessage(false, ""); + + try { + commentManager.addComment(message, user); + return ResponseEntity.ok(response); + } catch (Exception e) { + response.setError(true); + response.setMessage(e.getMessage()); + return ResponseEntity.ok(response); + } + } + + @DeleteMapping("") + public ResponseEntity<ResponseMessage> deleteComment(@RequestParam(value = "id", required = true) Long id) { + User user = UserHelper.getCurrentUser(userManager); + ResponseMessage response = new ResponseMessage(false, ""); + + try { + commentManager.removeComment(id, user); + return ResponseEntity.ok(response); + } catch (Exception e) { + response.setError(true); + response.setMessage(e.getMessage()); + return ResponseEntity.ok(response); + } + } +} \ No newline at end of file diff --git a/mozen/src/main/java/mozen/web/ModelController.java b/mozen/src/main/java/mozen/web/ModelController.java index 2603aa5586b996b44b1bc547b833c88632b3bb2b..21f9aacb698f6c4ed9bfeb001b86948de519d540 100644 --- a/mozen/src/main/java/mozen/web/ModelController.java +++ b/mozen/src/main/java/mozen/web/ModelController.java @@ -9,9 +9,6 @@ import org.springframework.core.io.ByteArrayResource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; -import org.springframework.security.authentication.AnonymousAuthenticationToken; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -25,6 +22,7 @@ import mozen.model.Model; import mozen.messages.ModelMessage; import mozen.messages.ResponseMessage; import mozen.model.User; +import mozen.utils.UserHelper; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; @@ -43,7 +41,7 @@ public class ModelController { @GetMapping("") public ResponseEntity<?> getModelDetails(@RequestParam(value = "id", required = false) Long id) { - User user = getCurrentUser(); + User user = UserHelper.getCurrentUser(userManager); if(user != null) { return ResponseEntity.ok().body(user.getModels()); } else { @@ -55,7 +53,7 @@ public class ModelController { @PostMapping("") public ResponseEntity<ResponseMessage> addModel(@RequestBody @Valid ModelMessage message) { ResponseMessage response = new ResponseMessage(false, ""); - User user = getCurrentUser(); + User user = UserHelper.getCurrentUser(userManager); if(user == null) { response.setError(true); response.setMessage("User unknown"); @@ -76,7 +74,7 @@ public class ModelController { @PutMapping("") public ResponseEntity<ResponseMessage> updateModel(@RequestParam(value = "id", required = true) Long id, @RequestBody @Valid Model model) { ResponseMessage response = new ResponseMessage(false, ""); - User user = getCurrentUser(); + User user = UserHelper.getCurrentUser(userManager); if(user == null) { response.setError(true); response.setMessage("User unknown"); @@ -96,7 +94,7 @@ public class ModelController { @DeleteMapping("") public ResponseEntity<ResponseMessage> deleteModel(@RequestParam(value = "id", required = true) Long id) { ResponseMessage response = new ResponseMessage(false, ""); - User user = getCurrentUser(); + User user = UserHelper.getCurrentUser(userManager); if(user == null) { response.setError(true); response.setMessage("User unknown"); @@ -121,7 +119,7 @@ public class ModelController { @PostMapping("/upload") public ResponseEntity<ResponseMessage> uploadModelFile(@RequestParam("file") MultipartFile file, @RequestParam(value = "id", required = true) Long id) { ResponseMessage response = new ResponseMessage(false, ""); - User user = getCurrentUser(); + User user = UserHelper.getCurrentUser(userManager); if(user == null) { response.setError(true); response.setMessage("User unknown"); @@ -156,16 +154,4 @@ public class ModelController { return ResponseEntity.ok(response); } - - private User getCurrentUser() { - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - - if (!(auth instanceof AnonymousAuthenticationToken)) { - if (auth.getPrincipal() instanceof org.springframework.security.core.userdetails.User) return null; - String username = (String) auth.getPrincipal(); - return userManager.getUserByUsername(username); - } else { - return null; - } - } } \ No newline at end of file diff --git a/mozen/src/main/java/mozen/web/TagController.java b/mozen/src/main/java/mozen/web/TagController.java index 66f4721e9dd2471453a5ac2c6d5b475093e10600..e245ba6df9a9e4cc4bd528ae3af6ae67ac3ecd57 100644 --- a/mozen/src/main/java/mozen/web/TagController.java +++ b/mozen/src/main/java/mozen/web/TagController.java @@ -28,18 +28,18 @@ public class TagController { @Autowired ITagManager tagManager; - @GetMapping("/") + @GetMapping("") public Collection<TagCategory> getTags() { return tagManager.getTags(); } - @PostMapping("/") + @PostMapping("") public ResponseEntity<ResponseMessage> addTag(@RequestBody @Valid TagMessage message) { tagManager.addTag(message); return ResponseEntity.ok().build(); } - @DeleteMapping("/") + @DeleteMapping("") public ResponseEntity<ResponseMessage> deleteTag(@RequestParam(value = "id", required = true) Long id) { tagManager.removeTag(id); return ResponseEntity.ok().build(); diff --git a/mozen/src/main/java/mozen/web/UserController.java b/mozen/src/main/java/mozen/web/UserController.java index fe4de261347fe0119fb785ad6a473699f8791608..6d8978f85bbd8e2f15a8fdea1e6415b9d8b65fdd 100644 --- a/mozen/src/main/java/mozen/web/UserController.java +++ b/mozen/src/main/java/mozen/web/UserController.java @@ -6,8 +6,6 @@ import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; -import org.springframework.security.authentication.AnonymousAuthenticationToken; -import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.CrossOrigin; @@ -24,29 +22,30 @@ import mozen.messages.ResponseMessage; import mozen.messages.SignupMessage; import mozen.model.User; import mozen.utils.JwtUtils; +import mozen.utils.UserHelper; @RestController @RequestMapping("/user") @CrossOrigin public class UserController { @Autowired - IUserManager manager; + IUserManager userManager; @GetMapping("") public User getUser() { String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); - return manager.getUserByUsername(username); + return userManager.getUserByUsername(username); } @GetMapping("/list") public Collection<User> getUserList() { - return manager.getUsers(); + return userManager.getUsers(); } @PostMapping("") public ResponseEntity<ResponseMessage> changeUserDetails(@RequestParam User userToUpdate) { ResponseMessage response = new ResponseMessage(false, ""); - User user = getCurrentUser(); + User user = UserHelper.getCurrentUser(userManager); if(user == null) { response.setError(true); response.setMessage("User unknown"); @@ -54,7 +53,7 @@ public class UserController { } try { - manager.updateUser(user ,userToUpdate); + userManager.updateUser(user ,userToUpdate); } catch (Exception e) { response.setError(true); response.setMessage(e.getMessage()); @@ -66,7 +65,7 @@ public class UserController { @DeleteMapping("") public ResponseEntity<ResponseMessage> deleteUser(@RequestParam Long id) { ResponseMessage response = new ResponseMessage(false, ""); - User user = getCurrentUser(); + User user = UserHelper.getCurrentUser(userManager); if(user == null) { response.setError(true); response.setMessage("User unknown"); @@ -74,7 +73,7 @@ public class UserController { } try { - manager.removeUser(user, id); + userManager.removeUser(user, id); } catch (Exception e) { response.setError(true); response.setMessage(e.getMessage()); @@ -88,7 +87,7 @@ public class UserController { ResponseMessage response = new ResponseMessage(false, ""); try { - manager.addUser(message); + userManager.addUser(message); response.setMessage(JwtUtils.generateToken(message.getUsername())); } catch (Exception e) { response.setError(true); @@ -98,15 +97,4 @@ public class UserController { return ResponseEntity.ok(response); } - private User getCurrentUser() { - Authentication auth = SecurityContextHolder.getContext().getAuthentication(); - - if (!(auth instanceof AnonymousAuthenticationToken)) { - String username = (String) auth.getPrincipal(); - return manager.getUserByUsername(username); - } else { - return null; - } - } - } \ No newline at end of file