Browse files

Initial work on #12 - a good part of this involved improving the way

security was managed, and recording the username as part of a
submission. Tests needed to be fixed after requiring a username, as we
may typically need to mock the currentUserService.
  • Loading branch information...
1 parent 4c36474 commit 52fe28f374d46582ff12542d69d0ca9e07dc0ac5 morungos committed Apr 22, 2012
View
16 grails-app/controllers/uk/org/openmentor/controller/HistoryController.groovy
@@ -0,0 +1,16 @@
+package uk.org.openmentor.controller
+
+/**
+ * This controller is used to manage the views and actions associated with the
+ * history. The precise contents of this history may evolve over time, but it
+ * mainly allows people to track what submissions have been uploaded when, and by
+ * whom.
+ *
+ * @author morungos
+ */
+class HistoryController {
+
+ def index() {
+
+ }
+}
View
4 grails-app/controllers/uk/org/openmentor/controller/SubmissionController.groovy
@@ -1,6 +1,7 @@
package uk.org.openmentor.controller
import grails.plugins.springsecurity.Secured;
+import org.springframework.security.core.context.SecurityContextHolder;
import java.util.Map;
@@ -18,6 +19,7 @@ class SubmissionController {
def analyzerService
def assessmentService
+ def currentUserService
private Course getSelectedCourse() {
def courseId = session.current_course
@@ -81,11 +83,13 @@ class SubmissionController {
// well-specified in the previous implementation.
Assignment assignment = Assignment.get(cmd.assignmentId)
+ String userName = currentUserService.currentUserName()
Submission sub = analyzerService.newSubmission(
assignment,
[cmd.studentIds] as Set<String>,
[cmd.tutorIds] as Set<String>,
cmd.grade,
+ userName,
cmd.dataFile.getOriginalFilename(),
cmd.dataFile.getBytes()
)
View
10 grails-app/domain/uk/org/openmentor/data/Submission.groovy
@@ -22,6 +22,11 @@ class Submission {
*/
Date dateSubmitted = new Date()
+ /**
+ * The username of the person who uploaded this submission
+ */
+ String username
+
static belongsTo = [ assignment: Assignment ]
static hasMany = [
@@ -31,10 +36,11 @@ class Submission {
]
static constraints = {
- filename(nullable: false)
- grade(nullable: false)
+ filename(nullable: false, blank: false)
+ grade(nullable: false, blank: false)
fileContents(nullable: true, maxSize: 8*1024*1024) // Allow up to 8Mb
dateSubmitted(nullable: false)
+ username(nullable: false, blank: false)
}
static Set<Submission> findAllCourseSubmissions(String courseId) {
View
9 grails-app/services/uk/org/openmentor/service/AnalyzerService.groovy
@@ -37,14 +37,16 @@ class AnalyzerService {
Set<String> students,
Set<String> tutorIds,
String grade,
- String filename,
- byte[] fileContents) throws Exception {
+ String username,
+ String filename,
+ byte[] fileContents) throws Exception {
if (log.isDebugEnabled()) {
log.debug("Analyzing submission: grade: " + grade
+ ", parameter filename: " + filename
+ ", parameter assignment: " + assignment
+ ", parameter students: " + students
- + ", parameter tutorIds: " + tutorIds);
+ + ", parameter tutorIds: " + tutorIds
+ + ", parameter username: " + username);
}
Submission sub = new Submission();
@@ -53,6 +55,7 @@ class AnalyzerService {
sub.setTutorIds(tutorIds);
sub.setGrade(grade);
sub.setFilename(filename);
+ sub.setUsername(username);
sub.setFileContents(fileContents)
Set<Comment> commentSet = new HashSet<Comment>();
View
25 grails-app/services/uk/org/openmentor/service/CurrentUserService.groovy
@@ -0,0 +1,25 @@
+package uk.org.openmentor.service
+
+import org.springframework.security.core.context.SecurityContextHolder
+
+/**
+ * A small service, primarily used to retrieve the current user name and other details as
+ * needed. As a service, this can be wired into other services and controllers as required.
+ *
+ * @author morungos
+ */
+
+class CurrentUserService {
+
+ static transactional = false
+
+ /**
+ * Returns the current user name, or null if none
+ * @return the user name
+ */
+ def currentUserName() {
+ def context = SecurityContextHolder.getContext()
+ def authentication = context.getAuthentication()
+ return authentication?.principal?.getUsername()
+ }
+}
View
14 grails-app/views/layouts/main.gsp
@@ -125,6 +125,20 @@
</ul>
</sec:ifAnyGranted>
+
+ <sec:ifAnyGranted roles="ROLE_OPENMENTOR-ADMIN">
+
+ <hr/>
+
+ <ul>
+ <li>
+ <g:link action="index" controller="history">
+ History
+ </g:link>
+ </li>
+ </ul>
+
+ </sec:ifAnyGranted>
</div>
<div class="panelBtm"></div>
</div>
View
5 test/integration/uk/org/openmentor/controller/SubmissionControllerIntegrationTests.groovy
@@ -7,6 +7,7 @@ import uk.org.openmentor.data.Assignment
import uk.org.openmentor.data.Submission
import uk.org.openmentor.domain.Categorization;
import uk.org.openmentor.service.AssessmentService;
+import uk.org.openmentor.service.CurrentUserService;
import org.gmock.WithGMock
@@ -56,6 +57,10 @@ class SubmissionControllerIntegrationTests extends GroovyTestCase {
Assignment ass = Assignment.findByCode("TMA03")
+ def mockCurrentUserService = mock(CurrentUserService)
+ mockCurrentUserService.currentUserName().returns("admin").stub()
+ controller.currentUserService = mockCurrentUserService
+
controller.session.putAt('current_course', 'CM2006')
play {
sc.dataFile = mockFile
View
2 test/integration/uk/org/openmentor/data/SubmissionIntegrationTests.groovy
@@ -24,7 +24,7 @@ class SubmissionIntegrationTests extends GroovyTestCase {
def testFile = new File(testFileName)
// Write the submission
- def submission = new Submission(filename: "foo.doc", grade: "A", fileContents: testFile.getBytes())
+ def submission = new Submission(filename: "foo.doc", grade: "A", username: "admin", fileContents: testFile.getBytes())
assignment.addToSubmissions(submission)
assignment.save(validate: true, flush: true)
View
1 test/integration/uk/org/openmentor/pages/SubmissionGroovyPagesTests.groovy
@@ -44,6 +44,7 @@ class SubmissionGroovyPagesTests extends GroovyPagesTestCase {
['09000231'] as Set<String>,
['M4000061'] as Set<String>,
'A',
+ "admin",
inputFile,
IOUtils.toByteArray(new FileInputStream(inputFile))
)
View
16 test/unit/uk/org/openmentor/controller/HistoryControllerTests.groovy
@@ -0,0 +1,16 @@
+package uk.org.openmentor.controller
+
+import grails.test.mixin.*
+import org.junit.*
+
+/**
+ * See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
+ */
+@TestFor(HistoryController)
+class HistoryControllerTests {
+
+ void testHistoryController() {
+
+ //TODO implement me
+ }
+}
View
18 test/unit/uk/org/openmentor/service/CurrentUserServiceTests.groovy
@@ -0,0 +1,18 @@
+package uk.org.openmentor.service
+
+import grails.test.mixin.*
+import org.junit.*
+
+/**
+ * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
+ */
+@TestFor(CurrentUserService)
+class CurrentUserServiceTests {
+
+ void testCurrentUserName() {
+ def currentUserService = new CurrentUserService()
+
+ def value = currentUserService.currentUserName()
+ assertEquals null, value
+ }
+}

0 comments on commit 52fe28f

Please sign in to comment.