public Prediction createPrediction(SecurityContext sec, Prediction pred) { User user = securityCheck(sec, Roles.CREATE_PREDICTION); pred.setTags(TextUtils.tag(pred.getTags())); int id; pred = InputSanitizer.sanitize(pred); if (StringUtils.isBlank(pred.getSourceAuthor())) { pred.setType(Prediction.PredictionType.quote.name()); } else { pred.setType(Prediction.PredictionType.prediction.name()); } if (pred.getTitle() == null || pred.getTitle().trim().isEmpty()) { pred.setTitle(StringUtils.abbreviate(pred.getText(), 64)); } else { pred.setTitle(StringUtils.abbreviate(pred.getTitle(), 64)); } pred.setText(StringUtils.abbreviate(pred.getText(), MAX_PREDICTION_LENGTH)); if (sec != getAdminSecurityContext()) { pred.setCreatedByUserId(user.getId()); pred.setCreatedByUser(user.getFullName()); } pred.setTime(TextUtils.getProbablePredictionTime(pred.getText())); id = db.createPrediction(pred); pred.setId(id); return db.getPrediction(id); }
public User createUser(SecurityContext sec, User user) { User existingUser = getUser(((User) sec.getUserPrincipal()).getId()); if (existingUser == null) { // User never registed return db.createUser(user, ((User) sec.getUserPrincipal())); } else if (existingUser.getEmail().equals(user.getEmail())) { // Email exists throw new DuplicateKeyException(""); } return existingUser; }
public int createComment(SecurityContext sec, int predictionId, Comment comment) { User user = securityCheck(sec, Roles.COMMENT); Prediction prediction = getPrediction(predictionId); comment = InputSanitizer.sanitize(comment); comment.setCreatedByUserId(user.getId()); if (StringUtils.isBlank(comment.getAuthor())) { comment.setAuthor(user.getEmail()); } int cid = db.createComment(prediction.getId(), comment); comment.setId(cid); comment.setPredictionId(predictionId); return cid; }
public boolean isOwner(SecurityContext sec, int predictionId) { User user = ((User) sec.getUserPrincipal()); return isAdmin(sec) || db.getPrediction(predictionId).getCreatedByUserId() == user.getId(); }