use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class AdminController method deleteWebhook.
@PostMapping("/delete-webhook")
public String deleteWebhook(@RequestParam String id, HttpServletRequest req, HttpServletResponse res) {
Profile authUser = utils.getAuthUser(req);
if (!StringUtils.isBlank(id) && utils.isAdmin(authUser) && utils.isWebhooksEnabled()) {
Webhook webhook = new Webhook();
webhook.setId(id);
pc.delete(webhook);
}
if (utils.isAjaxRequest(req)) {
res.setStatus(200);
return "base";
} else {
return "redirect:" + ADMINLINK;
}
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class CommentController method deleteAjax.
@PostMapping("/{id}/delete")
public void deleteAjax(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
if (utils.isAuthenticated(req)) {
Comment comment = pc.read(id);
Profile authUser = utils.getAuthUser(req);
boolean isMod = utils.isMod(authUser);
if (comment != null && (comment.getCreatorid().equals(authUser.getId()) || isMod)) {
// check parent and correct (for multi-parent-object pages)
comment.delete();
if (!isMod) {
utils.addBadgeAndUpdate(authUser, DISCIPLINED, true);
}
}
}
res.setStatus(200);
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class FeedbackController method replyAjax.
@PostMapping({ "/{id}", "/{id}/{title}" })
public String replyAjax(@PathVariable String id, @PathVariable(required = false) String title, HttpServletRequest req, HttpServletResponse res, Model model) throws IOException {
if (!utils.isFeedbackEnabled()) {
return "redirect:" + HOMEPAGE;
}
Post showPost = pc.read(id);
Profile authUser = utils.getAuthUser(req);
if (authUser != null && showPost != null && !showPost.isClosed() && !showPost.isReply()) {
// create new answer
Reply answer = utils.populate(req, new Reply(), "body");
Map<String, String> error = utils.validate(answer);
if (!error.containsKey("body")) {
answer.setTitle(showPost.getTitle());
answer.setCreatorid(authUser.getId());
answer.setParentid(showPost.getId());
answer.create();
showPost.setAnswercount(showPost.getAnswercount() + 1);
if (showPost.getAnswercount() >= MAX_REPLIES_PER_POST) {
showPost.setCloserid("0");
}
// update without adding revisions
pc.update(showPost);
utils.addBadgeAndUpdate(authUser, Profile.Badge.EUREKA, answer.getCreatorid().equals(showPost.getCreatorid()));
answer.setAuthor(authUser);
model.addAttribute("showPost", showPost);
model.addAttribute("answerslist", Collections.singletonList(answer));
} else {
model.addAttribute("error", error);
model.addAttribute("path", "feedback.vm");
res.setStatus(400);
}
return "reply";
}
if (utils.isAjaxRequest(req)) {
res.setStatus(200);
return "reply";
} else {
return "redirect:" + FEEDBACKLINK + "/" + id;
}
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class ApiController method createComment.
@PostMapping("/comments")
public Comment createComment(HttpServletRequest req, HttpServletResponse res) {
Map<String, Object> entity = readEntity(req);
if (entity.isEmpty()) {
badReq("Missing request body.");
}
String comment = (String) entity.get("comment");
String parentid = (String) entity.get(Config._PARENTID);
String creatorid = (String) entity.get(Config._CREATORID);
ParaObject parent = pc.read(parentid);
if (parent == null) {
badReq("Parent object not found. Provide a valid parentid.");
return null;
}
if (!StringUtils.isBlank(creatorid)) {
Profile authUser = pc.read(Profile.id(creatorid));
if (authUser != null) {
req.setAttribute(AUTH_USER_ATTRIBUTE, authUser);
}
}
Model model = new ExtendedModelMap();
commentController.createAjax(comment, parentid, req, model);
Comment created = (Comment) model.getAttribute("showComment");
if (created == null || StringUtils.isBlank(comment)) {
badReq("Failed to create comment.");
return null;
}
res.setStatus(HttpStatus.CREATED.value());
return created;
}
use of com.erudika.scoold.core.Profile in project scoold by Erudika.
the class ApiController method updatePost.
@PatchMapping("/posts/{id}")
public Post updatePost(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
Map<String, Object> entity = readEntity(req);
if (entity.isEmpty()) {
badReq("Missing request body.");
}
String editorid = (String) entity.get("lasteditby");
if (!StringUtils.isBlank(editorid)) {
Profile authUser = pc.read(Profile.id(editorid));
if (authUser != null) {
req.setAttribute(AUTH_USER_ATTRIBUTE, authUser);
}
}
String space = (String) entity.get("space");
String title = (String) entity.get("title");
String body = (String) entity.get("body");
String location = (String) entity.get("location");
String latlng = (String) entity.get("latlng");
List<String> spaces = readSpaces(space);
space = spaces.iterator().hasNext() ? spaces.iterator().next() : null;
Model model = new ExtendedModelMap();
questionController.edit(id, title, body, String.join(",", (List<String>) entity.get("tags")), location, latlng, space, req, res, model);
Post post = (Post) model.getAttribute("post");
if (post == null) {
res.setStatus(HttpStatus.NOT_FOUND.value());
} else if (!utils.canEdit(post, utils.getAuthUser(req))) {
badReq("Update failed - user " + editorid + " is not allowed to update post.");
}
return post;
}
Aggregations