Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Peer Group): Non admins can not create new peer groupings #237

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,44 +1,64 @@
package org.wise.portal.presentation.web.controllers.peergroup;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.acls.domain.BasePermission;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import org.wise.portal.domain.peergrouping.PeerGrouping;
import org.wise.portal.domain.peergrouping.impl.PeerGroupingImpl;
import org.wise.portal.domain.project.Project;
import org.wise.portal.domain.run.impl.RunImpl;
import org.wise.portal.domain.user.User;
import org.wise.portal.presentation.web.response.ResponseEntityGenerator;
import org.wise.portal.service.acl.AclService;
import org.wise.portal.service.peergrouping.PeerGroupingService;
import org.wise.portal.service.user.UserService;

@RestController
@Secured("ROLE_TEACHER")
@RequestMapping("/api/run/{runId}/peer-grouping")
public class PeerGroupingAPIController {

@Autowired
private AclService<Project> aclService;

@Autowired
private PeerGroupingService peerGroupingService;

@Autowired
private UserService userService;

@PostMapping
@PreAuthorize("hasPermission(#run, 'WRITE') or hasRole('ROLE_ADMINISTRATOR')")
PeerGrouping create(@PathVariable("runId") RunImpl run,
Object create(Authentication auth, @PathVariable("runId") RunImpl run,
@RequestBody PeerGroupingImpl peerGrouping) {
try {
return peerGroupingService.createPeerGrouping(run, peerGrouping);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Duplicate Tag");
if (isAuthorized(auth, run)) {
try {
return peerGroupingService.createPeerGrouping(run, peerGrouping);
} catch (Exception e) {
return ResponseEntityGenerator.createError("genericError");
}
} else {
return ResponseEntityGenerator.createError("notAuthorized");
}
}

@PutMapping("/{tag}")
@PreAuthorize("hasPermission(#run, 'WRITE') or hasRole('ROLE_ADMINISTRATOR')")
PeerGrouping update(@PathVariable("runId") RunImpl run, @PathVariable("tag") String tag,
@RequestBody PeerGroupingImpl peerGrouping) {
return peerGroupingService.updatePeerGrouping(run, tag, peerGrouping);
Object update(Authentication auth, @PathVariable("runId") RunImpl run,
@PathVariable("tag") String tag, @RequestBody PeerGroupingImpl peerGrouping) {
if (isAuthorized(auth, run)) {
return peerGroupingService.updatePeerGrouping(run, tag, peerGrouping);
} else {
return ResponseEntityGenerator.createError("notAuthorized");
}
}

Boolean isAuthorized(Authentication auth, RunImpl run) {
User user = userService.retrieveUserByUsername(auth.getName());
return aclService.hasPermission(run.getProject(), BasePermission.WRITE, user) || user.isAdmin();
}
}
Loading