|
| 1 | +package org.wise.portal.presentation.web.controllers.survey; |
| 2 | + |
| 3 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 4 | +import org.springframework.web.bind.annotation.RestController; |
| 5 | +import org.wise.portal.dao.ObjectNotFoundException; |
| 6 | +import org.wise.portal.domain.PeriodNotFoundException; |
| 7 | +import org.wise.portal.domain.RunHasEndedException; |
| 8 | +import org.wise.portal.domain.StudentUserAlreadyAssociatedWithRunException; |
| 9 | +import org.wise.portal.domain.authentication.Gender; |
| 10 | +import org.wise.portal.domain.authentication.impl.StudentUserDetails; |
| 11 | +import org.wise.portal.domain.project.impl.Projectcode; |
| 12 | +import org.wise.portal.domain.run.Run; |
| 13 | +import org.wise.portal.domain.user.User; |
| 14 | +import org.wise.portal.service.authentication.AuthorityNotFoundException; |
| 15 | +import org.wise.portal.service.authentication.DuplicateUsernameException; |
| 16 | +import org.wise.portal.service.authentication.UserDetailsService; |
| 17 | +import org.wise.portal.service.run.RunService; |
| 18 | +import org.wise.portal.service.student.StudentService; |
| 19 | +import org.wise.portal.service.user.UserService; |
| 20 | +import org.wise.portal.service.workgroup.WorkgroupService; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Date; |
| 24 | +import java.util.Locale; |
| 25 | + |
| 26 | +import javax.servlet.http.HttpServletRequest; |
| 27 | +import javax.servlet.http.HttpServletResponse; |
| 28 | +import javax.servlet.http.HttpSession; |
| 29 | + |
| 30 | +import org.apache.commons.lang3.RandomStringUtils; |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.security.authentication.AuthenticationManager; |
| 33 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 34 | +import org.springframework.security.core.Authentication; |
| 35 | +import org.springframework.security.core.context.SecurityContext; |
| 36 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 37 | +import org.springframework.security.web.context.HttpSessionSecurityContextRepository; |
| 38 | +import org.springframework.web.bind.annotation.GetMapping; |
| 39 | +import org.springframework.web.bind.annotation.PathVariable; |
| 40 | + |
| 41 | +@RestController |
| 42 | +@RequestMapping("/run-survey") |
| 43 | +public class SurveyAPIController { |
| 44 | + @Autowired |
| 45 | + private AuthenticationManager authenticationManager; |
| 46 | + |
| 47 | + @Autowired |
| 48 | + private RunService runService; |
| 49 | + |
| 50 | + @Autowired |
| 51 | + private StudentService studentService; |
| 52 | + |
| 53 | + @Autowired |
| 54 | + private UserDetailsService userDetailsService; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + private UserService userService; |
| 58 | + |
| 59 | + @Autowired |
| 60 | + private WorkgroupService workgroupService; |
| 61 | + |
| 62 | + @GetMapping("/{code}") |
| 63 | + public void launchSurveyRun(@PathVariable String code, HttpServletResponse response, |
| 64 | + HttpServletRequest request) throws AuthorityNotFoundException, IOException, |
| 65 | + DuplicateUsernameException, ObjectNotFoundException, PeriodNotFoundException, |
| 66 | + StudentUserAlreadyAssociatedWithRunException, RunHasEndedException { |
| 67 | + Projectcode projectCode = new Projectcode(code); |
| 68 | + Run run = runService.retrieveRunByRuncode(projectCode.getRuncode()); |
| 69 | + if (run.isSurvey() && isActive(run)) { |
| 70 | + handleSurveyLaunched(response, request, run, projectCode); |
| 71 | + } else { |
| 72 | + sendRedirect(response, "/"); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private boolean isActive(Run run) { |
| 77 | + Date now = new Date(); |
| 78 | + Date endTime = run.getEndtime(); |
| 79 | + return run.getStarttime().before(now) && (endTime == null || endTime.after(now)); |
| 80 | + } |
| 81 | + |
| 82 | + private void handleSurveyLaunched(HttpServletResponse response, HttpServletRequest request, |
| 83 | + Run run, Projectcode projectCode) throws AuthorityNotFoundException, IOException, |
| 84 | + DuplicateUsernameException, ObjectNotFoundException, PeriodNotFoundException, |
| 85 | + StudentUserAlreadyAssociatedWithRunException, RunHasEndedException { |
| 86 | + |
| 87 | + Object principal = getSecurityContextHolderPrincipal(); |
| 88 | + if (principal instanceof StudentUserDetails |
| 89 | + && isStudentAssociatedWithRun(run, (StudentUserDetails) principal)) { |
| 90 | + sendRedirect(response, "/student/unit/" + run.getId()); |
| 91 | + return; |
| 92 | + } else { |
| 93 | + SecurityContextHolder.getContext().setAuthentication(null); |
| 94 | + } |
| 95 | + if (underWorkgroupLimit(run)) { |
| 96 | + String password = RandomStringUtils.randomAlphanumeric(10); |
| 97 | + User user = this.createNewStudentAccount(request.getLocale(), password); |
| 98 | + loginStudent(request, user, password); |
| 99 | + studentService.addStudentToRun(user, projectCode); |
| 100 | + sendRedirect(response, "/student/unit/" + run.getId()); |
| 101 | + } else { |
| 102 | + sendRedirect(response, "/survey/workgroupLimitReached"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private void sendRedirect(HttpServletResponse response, String redirectUrl) throws IOException { |
| 107 | + response.sendRedirect(redirectUrl); |
| 108 | + } |
| 109 | + |
| 110 | + private Object getSecurityContextHolderPrincipal() { |
| 111 | + return SecurityContextHolder.getContext().getAuthentication().getPrincipal(); |
| 112 | + } |
| 113 | + |
| 114 | + private boolean isStudentAssociatedWithRun(Run run, StudentUserDetails principal) { |
| 115 | + return run.isStudentAssociatedToThisRun(userService.retrieveStudentById((principal).getId())); |
| 116 | + } |
| 117 | + |
| 118 | + private boolean underWorkgroupLimit(Run run) { |
| 119 | + return workgroupService.getWorkgroupsForRun(run).size() <= 1000; |
| 120 | + } |
| 121 | + |
| 122 | + private void loginStudent(HttpServletRequest request, User user, String password) { |
| 123 | + UsernamePasswordAuthenticationToken authReq = new UsernamePasswordAuthenticationToken( |
| 124 | + user.getUserDetails().getUsername(), password); |
| 125 | + Authentication auth = authenticationManager.authenticate(authReq); |
| 126 | + SecurityContext sc = SecurityContextHolder.getContext(); |
| 127 | + sc.setAuthentication(auth); |
| 128 | + HttpSession session = request.getSession(true); |
| 129 | + session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, sc); |
| 130 | + } |
| 131 | + |
| 132 | + private User createNewStudentAccount(Locale locale, String password) |
| 133 | + throws AuthorityNotFoundException, DuplicateUsernameException { |
| 134 | + StudentUserDetails sud = new StudentUserDetails(); |
| 135 | + sud.setFirstname("survey_student_" + RandomStringUtils.randomAlphanumeric(10)); |
| 136 | + sud.setLastname(RandomStringUtils.randomAlphanumeric(10)); |
| 137 | + sud.setBirthday(new Date()); |
| 138 | + sud.setPassword(password); |
| 139 | + sud.setGender(Gender.UNSPECIFIED); |
| 140 | + sud.setEmailAddress("null@null.com"); |
| 141 | + sud.setLanguage(locale.getLanguage()); |
| 142 | + sud.setNumberOfLogins(1); |
| 143 | + sud.setLastLoginTime(new Date()); |
| 144 | + |
| 145 | + User user = userService.createUser(sud); |
| 146 | + user.getUserDetails().addAuthority( |
| 147 | + userDetailsService.loadAuthorityByName(UserDetailsService.SURVEY_STUDENT_ROLE)); |
| 148 | + return user; |
| 149 | + } |
| 150 | +} |
0 commit comments