Skip to content
Snippets Groups Projects
Commit 97a37792 authored by Thomas's avatar Thomas
Browse files

Verified models

parent 87c07f35
No related branches found
No related tags found
No related merge requests found
......@@ -12,9 +12,11 @@ import mozen.model.User;
public interface IModelManager {
Long addModel(ModelMessage message, User user) throws Exception;
void addModelFile(MultipartFile file, long id, User user) throws Exception;
void updateModel(Model model, long id, User user) throws Exception;
void updateModel(ModelMessage message, long id, User user) throws Exception;
void removeModel(long id, User user) throws Exception;
void setVerified(long id, User user) throws Exception;
Model getModel(long id);
Collection<Model> getModels();
......
......@@ -56,6 +56,7 @@ public class ModelManager implements IModelManager {
model.setTags(tags);
}
model.setIsVerified(false);
model.setVotes(0);
model.setAdded(new Date());
model.setLastModified(new Date());
......@@ -89,15 +90,32 @@ public class ModelManager implements IModelManager {
}
@Override
public void updateModel(Model model, long id, User user) throws Exception {
Model modelToUpdate = modelRepo.findById(id).get();
public void updateModel(ModelMessage message, long id, User user) throws Exception {
Model model = modelRepo.findById(id).get();
if (modelToUpdate == null)
if (model == null)
throw new Exception("Unknown model");
if (!UserHelper.isAuthor(modelToUpdate.getAuthor(), user))
if (!UserHelper.isAuthor(model.getAuthor(), user))
throw new Exception("Not the author");
model.setId(id);
if (message.getName() != null) model.setName(message.getName());
if (message.getShortDescription() != null) model.setShortDescription(message.getShortDescription());
if (message.getLongDescription() != null) model.setLongDescription(message.getLongDescription());
model.setPerformance(message.getPerformance());
if(message.getPerformanceUnit() != null) model.setPerformanceUnit(message.getPerformanceUnit());
model.setParameterCount(message.getParameterCount());
if (message.getTags() != null) {
Set<Tag> tags = new HashSet<Tag>();
for (String tagName : message.getTags()) {
Tag tag = tagRepo.findByName(tagName);
tags.add(tag);
}
model.setTags(tags);
}
model.setLastModified(new Date());
modelRepo.save(model);
}
......@@ -114,6 +132,19 @@ public class ModelManager implements IModelManager {
modelRepo.deleteById(id);
}
@Override
public void setVerified(long id, User user) throws Exception {
Model model = modelRepo.findById(id).get();
if (model == null)
throw new Exception("Unknown model");
if (!UserHelper.isAuthor(model.getAuthor(), user))
throw new Exception("Not admin");
model.setIsVerified(!model.getIsVerified());
modelRepo.save(model);
}
@Override
public SearchResult findModel(String name, int page, int size, String sort) {
System.err.println("[MODEL MANAGER] search n:" + name + " p:" + page + " si:" + size + " so:" + sort);
......
......@@ -88,6 +88,10 @@ public class Model implements Serializable{
@Column
private Long checksum;
@Basic
@Column
private boolean isVerified;
@Basic
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
private User author;
......@@ -109,7 +113,7 @@ public class Model implements Serializable{
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, Set<Comment> comments) {
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, boolean isVerified, User author, Set<Tag> tags, Set<CustomLayer> customLayers, Set<Comment> comments) {
this.id = id;
this.name = name;
this.shortDescription = shortDescription;
......@@ -123,6 +127,7 @@ public class Model implements Serializable{
this.file = file;
this.fileType = fileType;
this.checksum = checksum;
this.isVerified = isVerified;
this.author = author;
this.tags = tags;
this.customLayers = customLayers;
......@@ -267,5 +272,16 @@ public class Model implements Serializable{
this.comments = comments;
}
public boolean isIsVerified() {
return this.isVerified;
}
public boolean getIsVerified() {
return this.isVerified;
}
public void setIsVerified(boolean isVerified) {
this.isVerified = isVerified;
}
}
\ No newline at end of file
......@@ -11,11 +11,15 @@ import mozen.model.User;
public class UserHelper {
public static boolean isAuthor(User author, User userToCheck) {
if (userToCheck.getRole() == Role.ROLE_ADMIN)
if (isAdmin(userToCheck))
return true;
return author == userToCheck;
}
public static boolean isAdmin(User user) {
return user.getRole() == Role.ROLE_ADMIN;
}
public static User getCurrentUser(IUserManager userManager) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
......
......@@ -57,7 +57,7 @@ public class ModelController {
if(user == null) {
response.setError(true);
response.setMessage("User unknown");
return ResponseEntity.ok(response);
return ResponseEntity.badRequest().body(response);
}
try {
......@@ -66,26 +66,28 @@ public class ModelController {
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
return ResponseEntity.badRequest().body(response);
}
return ResponseEntity.ok(response);
}
@PutMapping("")
public ResponseEntity<ResponseMessage> updateModel(@RequestParam(value = "id", required = true) Long id, @RequestBody @Valid Model model) {
public ResponseEntity<ResponseMessage> updateModel(@RequestParam(value = "id", required = true) Long id, @RequestBody @Valid ModelMessage message) {
ResponseMessage response = new ResponseMessage(false, "");
User user = UserHelper.getCurrentUser(userManager);
if(user == null) {
response.setError(true);
response.setMessage("User unknown");
return ResponseEntity.ok(response);
return ResponseEntity.badRequest().body(response);
}
try {
modelManager.updateModel(model, id, user);
modelManager.updateModel(message, id, user);
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
return ResponseEntity.badRequest().body(response);
}
return ResponseEntity.ok(response);
......@@ -98,7 +100,7 @@ public class ModelController {
if(user == null) {
response.setError(true);
response.setMessage("User unknown");
return ResponseEntity.ok(response);
return ResponseEntity.badRequest().body(response);
}
try {
......@@ -106,6 +108,28 @@ public class ModelController {
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
return ResponseEntity.badRequest().body(response);
}
return ResponseEntity.ok(response);
}
@GetMapping("/setVerified")
public ResponseEntity<ResponseMessage> setVerified(@RequestParam(value = "id", required = true) Long id) {
ResponseMessage response = new ResponseMessage(false, "");
User user = UserHelper.getCurrentUser(userManager);
if(user == null) {
response.setError(true);
response.setMessage("User unknown");
return ResponseEntity.badRequest().body(response);
}
try {
modelManager.setVerified(id, user);
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
return ResponseEntity.badRequest().body(response);
}
return ResponseEntity.ok(response);
......@@ -123,7 +147,7 @@ public class ModelController {
if(user == null) {
response.setError(true);
response.setMessage("User unknown");
return ResponseEntity.ok(response);
return ResponseEntity.badRequest().body(response);
}
try {
......@@ -132,6 +156,7 @@ public class ModelController {
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
return ResponseEntity.badRequest().body(response);
}
return ResponseEntity.ok(response);
......@@ -150,8 +175,7 @@ public class ModelController {
} catch (Exception e) {
response.setError(true);
response.setMessage(e.getMessage());
return ResponseEntity.badRequest().body(response);
}
return ResponseEntity.ok(response);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment