From f9d14c9b79e808d2786ec025e38b86b0dc0301bc Mon Sep 17 00:00:00 2001 From: nbaars Date: Tue, 9 Sep 2014 18:18:45 +0200 Subject: [PATCH] Added service for fetching the title of a lesson --- .gitignore | 1 + .../webgoat/service/LessonTitleService.java | 40 +++++++++++++++++++ src/main/webapp/js/goatConstants.js | 1 + src/main/webapp/js/goatControllers.js | 13 +++--- src/main/webapp/js/goatData.js | 5 ++- src/main/webapp/js/goatUtil.js | 9 ----- 6 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 src/main/java/org/owasp/webgoat/service/LessonTitleService.java diff --git a/.gitignore b/.gitignore index a03a5fc01f..fae16a7e31 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ /.settings/org.eclipse.wst.jsdt.ui.superType.container /.settings/org.eclipse.wst.jsdt.ui.superType.name /.settings/org.eclipse.wst.validation.prefs +/.externalToolBuilders/ diff --git a/src/main/java/org/owasp/webgoat/service/LessonTitleService.java b/src/main/java/org/owasp/webgoat/service/LessonTitleService.java new file mode 100644 index 0000000000..938a068507 --- /dev/null +++ b/src/main/java/org/owasp/webgoat/service/LessonTitleService.java @@ -0,0 +1,40 @@ +package org.owasp.webgoat.service; + +import javax.servlet.http.HttpSession; + +import org.owasp.webgoat.lessons.AbstractLesson; +import org.owasp.webgoat.session.Course; +import org.owasp.webgoat.session.WebSession; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class LessonTitleService extends BaseService { + + /** + * Returns the title for the current attack + * + * @param session + * @return + */ + @RequestMapping(value = "/lessontitle.mvc", produces = "application/html") + public @ResponseBody + String showPlan(HttpSession session) { + WebSession ws = getWebSession(session); + return getLessonTitle(ws); + } + + private String getLessonTitle(WebSession s) { + String title = ""; + int scr = s.getCurrentScreen(); + Course course = s.getCourse(); + + if (s.isUser() || s.isChallenge()) { + AbstractLesson lesson = course.getLesson(s, scr, AbstractLesson.USER_ROLE); + title = lesson != null ? lesson.getTitle() : ""; + } + return title; + } + +} diff --git a/src/main/webapp/js/goatConstants.js b/src/main/webapp/js/goatConstants.js index bc1e5ed9a5..2baf060349 100644 --- a/src/main/webapp/js/goatConstants.js +++ b/src/main/webapp/js/goatConstants.js @@ -19,6 +19,7 @@ var goatConstants = { solutionService:'service/solution.mvc', lessonPlanService:'service/lessonplan.mvc', menuService: 'service/lessonmenu.mvc', + lessonTitleService: 'service/lessontitle.mvc', // literals notFound: 'Could not find', noHints: 'There are no hints defined.' diff --git a/src/main/webapp/js/goatControllers.js b/src/main/webapp/js/goatControllers.js index 88aa793795..157401a4e2 100644 --- a/src/main/webapp/js/goatControllers.js +++ b/src/main/webapp/js/goatControllers.js @@ -27,19 +27,20 @@ goat.controller('goatLesson', function($scope, $http, $modal, $log, $templateCac $scope.hintIndex = 0; var curScope = $scope; - - + curScope.parameters = goat.utils.scrapeParams(url); goat.data.loadLessonContent(url).then( function(reply) { $("#lesson_content").html(reply); + goat.data.loadLessonTitle().then( + function(reply) { + $("#lessonTitle").text(reply); + } + ); + //hook forms goat.utils.makeFormsAjax(); $('#hintsView').hide(); - //render lesson title - $('#lessonTitle').text(goat.utils.extractLessonTitle($(reply))); - //@KLUGE to remove h1 after extracting and moving it to top - $('#lesson_content h1').remove() // adjust menu to lessonContent size if necssary //@TODO: this is still clunky ... needs some TLC if ($('div.panel-body').height() > 400) { diff --git a/src/main/webapp/js/goatData.js b/src/main/webapp/js/goatData.js index a2e99c6d82..e94ee1eee9 100644 --- a/src/main/webapp/js/goatData.js +++ b/src/main/webapp/js/goatData.js @@ -18,7 +18,7 @@ goat.data = { return $.get(goatConstants.sourceService, {}); }, loadSolution: function () { - return $.get(goatConstants.solutionService, {}) + return $.get(goatConstants.solutionService, {}); }, loadPlan: function () { return $.get(goatConstants.lessonPlanService, {}); @@ -30,5 +30,8 @@ goat.data = { loadMenuData: function() { //TODO use goatConstants var for url return $http({method: 'GET', url: goatConstants.menuService}); + }, + loadLessonTitle: function () { + return $.get(goatConstants.lessonTitleService, {}); } }; diff --git a/src/main/webapp/js/goatUtil.js b/src/main/webapp/js/goatUtil.js index d291dea8d0..7cbed8b866 100644 --- a/src/main/webapp/js/goatUtil.js +++ b/src/main/webapp/js/goatUtil.js @@ -15,15 +15,6 @@ goat.utils = { //console.log("Hooking any lesson forms to make them ajax"); $("form").ajaxForm(options); }, - /**goatApp.extractLessonTitle - *pulls lesson title from html fragment returned (looks for it in h1 element) - *@param - html rendered to object passed in - */ - extractLessonTitle: function(el) { - var title = $('h1', el).text(); - // remove title - return title; - }, displayButton: function(id,show) { if ($('#'+id)) { if (show) {