-
Notifications
You must be signed in to change notification settings - Fork 276
/
ProgrammingExerciseParticipationService.java
328 lines (287 loc) · 19.3 KB
/
ProgrammingExerciseParticipationService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package de.tum.in.www1.artemis.service;
import java.security.Principal;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.validation.constraints.NotNull;
import org.hibernate.Hibernate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import de.tum.in.www1.artemis.domain.*;
import de.tum.in.www1.artemis.domain.enumeration.InitializationState;
import de.tum.in.www1.artemis.domain.enumeration.RepositoryType;
import de.tum.in.www1.artemis.domain.participation.*;
import de.tum.in.www1.artemis.exception.VersionControlException;
import de.tum.in.www1.artemis.repository.*;
import de.tum.in.www1.artemis.service.connectors.VersionControlService;
import de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException;
@Service
public class ProgrammingExerciseParticipationService {
private final Logger log = LoggerFactory.getLogger(ProgrammingExerciseParticipationService.class);
private final ParticipationService participationService;
private final ProgrammingExerciseStudentParticipationRepository studentParticipationRepository;
private final SolutionProgrammingExerciseParticipationRepository solutionParticipationRepository;
private final TemplateProgrammingExerciseParticipationRepository templateParticipationRepository;
private final ParticipationRepository participationRepository;
private final TeamRepository teamRepository;
private final Optional<VersionControlService> versionControlService;
private final AuthorizationCheckService authCheckService;
private final UserService userService;
public ProgrammingExerciseParticipationService(ParticipationService participationService, SolutionProgrammingExerciseParticipationRepository solutionParticipationRepository,
ProgrammingExerciseStudentParticipationRepository studentParticipationRepository, ParticipationRepository participationRepository, TeamRepository teamRepository,
TemplateProgrammingExerciseParticipationRepository templateParticipationRepository, Optional<VersionControlService> versionControlService, UserService userService,
AuthorizationCheckService authCheckService) {
this.participationService = participationService;
this.studentParticipationRepository = studentParticipationRepository;
this.solutionParticipationRepository = solutionParticipationRepository;
this.templateParticipationRepository = templateParticipationRepository;
this.participationRepository = participationRepository;
this.teamRepository = teamRepository;
this.versionControlService = versionControlService;
this.authCheckService = authCheckService;
this.userService = userService;
}
public Participation findParticipation(Long participationId) throws EntityNotFoundException {
return participationService.findOne(participationId);
}
public Optional<ProgrammingExerciseStudentParticipation> findStudentParticipation(Long participationId) {
return studentParticipationRepository.findById(participationId);
}
public Optional<TemplateProgrammingExerciseParticipation> findTemplateParticipation(Long participationId) {
return templateParticipationRepository.findById(participationId);
}
public Optional<SolutionProgrammingExerciseParticipation> findSolutionParticipation(Long participationId) {
return solutionParticipationRepository.findById(participationId);
}
/**
* Retrieve the solution participation of the given programming exercise.
*
* @param programmingExerciseId ProgrammingExercise id
* @return the SolutionProgrammingExerciseParticipation of programming exercise.
* @throws EntityNotFoundException if the SolutionParticipation can't be found (could be that the programming exercise does not exist or it does not have a SolutionParticipation).
*/
public SolutionProgrammingExerciseParticipation findSolutionParticipationByProgrammingExerciseId(Long programmingExerciseId) throws EntityNotFoundException {
Optional<SolutionProgrammingExerciseParticipation> solutionParticipation = solutionParticipationRepository.findByProgrammingExerciseId(programmingExerciseId);
if (solutionParticipation.isEmpty()) {
throw new EntityNotFoundException("Could not find solution participation for programming exercise with id " + programmingExerciseId);
}
return solutionParticipation.get();
}
/**
* Retrieve the template participation of the given programming exercise.
*
* @param programmingExerciseId ProgrammingExercise id
* @return the TemplateProgrammingExerciseParticipation of programming exercise.
* @throws EntityNotFoundException if the TemplateParticipation can't be found (could be that the programming exercise does not exist or it does not have a TemplateParticipation).
*/
public TemplateProgrammingExerciseParticipation findTemplateParticipationByProgrammingExerciseId(Long programmingExerciseId) throws EntityNotFoundException {
Optional<TemplateProgrammingExerciseParticipation> templateParticipation = templateParticipationRepository.findByProgrammingExerciseId(programmingExerciseId);
if (templateParticipation.isEmpty()) {
throw new EntityNotFoundException("Could not find solution participation for programming exercise with id " + programmingExerciseId);
}
return templateParticipation.get();
}
/**
* Tries to retrieve a student participation for the given exercise id and username.
*
* @param exercise the exercise for which to find a participation
* @param username of the user to which the participation belongs.
* @return the participation for the given exercise and user.
* @throws EntityNotFoundException if there is no participation for the given exercise and user.
*/
@Transactional(readOnly = true)
@NotNull
public ProgrammingExerciseStudentParticipation findStudentParticipationByExerciseAndStudentId(Exercise exercise, String username) throws EntityNotFoundException {
Optional<ProgrammingExerciseStudentParticipation> participation;
if (exercise.isTeamMode()) {
Optional<Team> optionalTeam = teamRepository.findOneByExerciseIdAndUserLogin(exercise.getId(), username);
participation = optionalTeam.flatMap(team -> studentParticipationRepository.findByExerciseIdAndTeamId(exercise.getId(), team.getId()));
}
else {
participation = studentParticipationRepository.findByExerciseIdAndStudentLogin(exercise.getId(), username);
}
if (participation.isEmpty()) {
throw new EntityNotFoundException("participation could not be found by exerciseId " + exercise.getId() + " and user " + username);
}
// Make sure the template participation is not a proxy object in this case
Hibernate.initialize(participation.get().getProgrammingExercise().getTemplateParticipation());
return participation.get();
}
public List<ProgrammingExerciseStudentParticipation> findByExerciseId(Long exerciseId) {
return studentParticipationRepository.findByExerciseId(exerciseId);
}
public Optional<ProgrammingExerciseStudentParticipation> findByExerciseIdAndTeamId(Long exerciseId, Long teamId) {
return studentParticipationRepository.findByExerciseIdAndTeamId(exerciseId, teamId);
}
public List<ProgrammingExerciseStudentParticipation> findByExerciseAndParticipationIds(Long exerciseId, Set<Long> participationIds) {
return studentParticipationRepository.findByExerciseIdAndParticipationIds(exerciseId, participationIds);
}
public Optional<ProgrammingExerciseStudentParticipation> findStudentParticipationWithLatestResultAndFeedbacksAndRelatedSubmissions(Long participationId) {
return studentParticipationRepository.findByIdWithLatestResultAndFeedbacksAndRelatedSubmissions(participationId, ZonedDateTime.now());
}
public Optional<ProgrammingExerciseStudentParticipation> findStudentParticipationWithResultsAndFeedbacksAndRelatedSubmissionsAndAssessor(Long participationId) {
return studentParticipationRepository.findByIdWithResultsAndFeedbacksAndRelatedSubmissionsAndAssessor(participationId);
}
/**
* Try to find a programming exercise participation for the given id.
*
* @param participationId ProgrammingExerciseParticipation id
* @return the casted participation
* @throws EntityNotFoundException if the participation with the given id does not exist or is not a programming exercise participation.
*/
public ProgrammingExerciseParticipation findProgrammingExerciseParticipationWithLatestSubmissionAndResult(Long participationId) throws EntityNotFoundException {
Optional<Participation> participation = participationRepository.findByIdWithLatestSubmissionAndResult(participationId);
if (participation.isEmpty() || !(participation.get() instanceof ProgrammingExerciseParticipation)) {
throw new EntityNotFoundException("No programming exercise participation found with id " + participationId);
}
return (ProgrammingExerciseParticipation) participation.get();
}
/**
* Check if the user can access a given participation by accessing the exercise and course connected to this participation
* The method will treat the participation types differently:
* - ProgrammingExerciseStudentParticipations should only be accessible by its owner (student) and the courses instructor/tas.
* - Template/SolutionParticipations should only be accessible by the courses instructor/tas.
*
* @param participation to check permissions for.
* @return true if the user can access the participation, false if not. Also returns false if the participation is not from a programming exercise.
*/
public boolean canAccessParticipation(ProgrammingExerciseParticipation participation) {
if (participation instanceof ProgrammingExerciseStudentParticipation) {
return canAccessParticipation((ProgrammingExerciseStudentParticipation) participation);
}
else if (participation instanceof SolutionProgrammingExerciseParticipation) {
return canAccessParticipation((SolutionProgrammingExerciseParticipation) participation);
}
else if (participation instanceof TemplateProgrammingExerciseParticipation) {
return canAccessParticipation((TemplateProgrammingExerciseParticipation) participation);
}
return false;
}
private boolean canAccessParticipation(@NotNull ProgrammingExerciseStudentParticipation participation) {
User user = userService.getUserWithGroupsAndAuthorities();
Course course = participation.getExercise().getCourseViaExerciseGroupOrCourseMember();
return participation.isOwnedBy(user) || authCheckService.isAtLeastTeachingAssistantInCourse(course, user);
}
private boolean canAccessParticipation(@NotNull SolutionProgrammingExerciseParticipation participation) {
User user = userService.getUserWithGroupsAndAuthorities();
// Note: if this participation was retrieved as Participation (abstract super class) from the database, the programming exercise might not be correctly initialized
// To prevent null pointer exceptions, we therefore retrieve it again as concrete solution programming exercise participation
if (participation.getProgrammingExercise() == null || !Hibernate.isInitialized(participation.getProgrammingExercise())) {
participation = findSolutionParticipation(participation.getId()).get();
}
Course course = participation.getExercise().getCourseViaExerciseGroupOrCourseMember();
return authCheckService.isAtLeastTeachingAssistantInCourse(course, user);
}
private boolean canAccessParticipation(@NotNull TemplateProgrammingExerciseParticipation participation) {
User user = userService.getUserWithGroupsAndAuthorities();
// Note: if this participation was retrieved as Participation (abstract super class) from the database, the programming exercise might not be correctly initialized
// To prevent null pointer exceptions, we therefore retrieve it again as concrete template programming exercise participation
if (participation.getProgrammingExercise() == null || !Hibernate.isInitialized(participation.getProgrammingExercise())) {
participation = findTemplateParticipation(participation.getId()).get();
}
Course course = participation.getExercise().getCourseViaExerciseGroupOrCourseMember();
return authCheckService.isAtLeastTeachingAssistantInCourse(course, user);
}
/**
* Check if the user can access a given participation.
* The method will treat the participation types differently:
* - ProgrammingExerciseStudentParticipations should only be accessible by its owner (student) and the courses instructor/tas.
* - Template/SolutionParticipations should only be accessible by the courses instructor/tas.
*
* @param participation to check permissions for.
* @param principal object to check permissions of the user with.
* @return true if the user can access the participation, false if not. Also returns false if the participation is not from a programming exercise.
*/
public boolean canAccessParticipation(ProgrammingExerciseParticipation participation, Principal principal) {
if (participation instanceof ProgrammingExerciseStudentParticipation) {
return canAccessParticipation((ProgrammingExerciseStudentParticipation) participation, principal);
}
else if (participation instanceof SolutionProgrammingExerciseParticipation) {
return canAccessParticipation((SolutionProgrammingExerciseParticipation) participation, principal);
}
else if (participation instanceof TemplateProgrammingExerciseParticipation) {
return canAccessParticipation((TemplateProgrammingExerciseParticipation) participation, principal);
}
return false;
}
public boolean canAccessParticipation(ProgrammingExerciseStudentParticipation participation, Principal principal) {
return participation.isOwnedBy(principal.getName());
}
public boolean canAccessParticipation(SolutionProgrammingExerciseParticipation participation, Principal principal) {
User user = userService.getUserWithGroupsAndAuthorities(principal.getName());
return authCheckService.isAtLeastInstructorForExercise(participation.getExercise(), user);
}
public boolean canAccessParticipation(TemplateProgrammingExerciseParticipation participation, Principal principal) {
User user = userService.getUserWithGroupsAndAuthorities(principal.getName());
return authCheckService.isAtLeastInstructorForExercise(participation.getExercise(), user);
}
/**
* Setup the initial solution participation for an exercise. Creates the new participation entity and sets
* the correct build plan ID and repository URL. Saves the participation after all values have been set.
*
* @param newExercise The new exercise for which a participation should be generated
* @param projectKey The key of the project of the new exercise
* @param solutionPlanName The name for the build plan of the participation
*/
@NotNull
public void setupInitialSolutionParticipation(ProgrammingExercise newExercise, String projectKey, String solutionPlanName) {
final String solutionRepoName = projectKey.toLowerCase() + "-" + RepositoryType.SOLUTION.getName();
SolutionProgrammingExerciseParticipation solutionParticipation = new SolutionProgrammingExerciseParticipation();
newExercise.setSolutionParticipation(solutionParticipation);
solutionParticipation.setBuildPlanId(projectKey + "-" + solutionPlanName);
solutionParticipation.setRepositoryUrl(versionControlService.get().getCloneRepositoryUrl(projectKey, solutionRepoName).toString());
solutionParticipation.setProgrammingExercise(newExercise);
solutionParticipationRepository.save(solutionParticipation);
}
/**
* Setup the initial template participation for an exercise. Creates the new participation entity and sets
* the correct build plan ID and repository URL. Saves the participation after all values have been set.
*
* @param newExercise The new exercise for which a participation should be generated
* @param projectKey The key of the project of the new exercise
* @param templatePlanName The name for the build plan of the participation
*/
@NotNull
public void setupInitalTemplateParticipation(ProgrammingExercise newExercise, String projectKey, String templatePlanName) {
final String exerciseRepoName = projectKey.toLowerCase() + "-" + RepositoryType.TEMPLATE.getName();
TemplateProgrammingExerciseParticipation templateParticipation = new TemplateProgrammingExerciseParticipation();
templateParticipation.setBuildPlanId(projectKey + "-" + templatePlanName);
templateParticipation.setRepositoryUrl(versionControlService.get().getCloneRepositoryUrl(projectKey, exerciseRepoName).toString());
templateParticipation.setProgrammingExercise(newExercise);
newExercise.setTemplateParticipation(templateParticipation);
templateParticipationRepository.save(templateParticipation);
}
/**
* Lock the repository associated with a programming participation
*
* @param programmingExercise the programming exercise
* @param participation the programming exercise student participation whose repository should be locked
* @throws VersionControlException if locking was not successful, e.g. if the repository was already locked
*/
public void lockStudentRepository(ProgrammingExercise programmingExercise, ProgrammingExerciseStudentParticipation participation) {
if (participation.getInitializationState().hasCompletedState(InitializationState.REPO_CONFIGURED)) {
versionControlService.get().setRepositoryPermissionsToReadOnly(participation.getRepositoryUrlAsUrl(), programmingExercise.getProjectKey(), participation.getStudents());
}
else {
log.warn("Cannot lock student repository for participation " + participation.getId() + " because the repository was not copied yet!");
}
}
/**
* Unlock the repository associated with a programming participation
*
* @param programmingExercise the programming exercise
* @param participation the programming exercise student participation whose repository should be unlocked
* @throws VersionControlException if unlocking was not successful, e.g. if the repository was already unlocked
*/
public void unlockStudentRepository(ProgrammingExercise programmingExercise, ProgrammingExerciseStudentParticipation participation) {
if (participation.getInitializationState().hasCompletedState(InitializationState.REPO_CONFIGURED)) {
versionControlService.get().configureRepository(programmingExercise, participation.getRepositoryUrlAsUrl(), participation.getStudents(), true);
}
else {
log.warn("Cannot unlock student repository for participation " + participation.getId() + " because the repository was not copied yet!");
}
}
}