From 96e333afa6206c1b140a257b7cc6166868ee97ca Mon Sep 17 00:00:00 2001 From: Amanda Anganes Date: Wed, 13 Mar 2013 13:45:05 -0400 Subject: [PATCH] Working on error handling --- .../java/org/mitre/oauth2/web/ScopeAPI.java | 20 ++++++++++++++++++- .../src/main/webapp/resources/js/scope.js | 17 +++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/openid-connect-server/src/main/java/org/mitre/oauth2/web/ScopeAPI.java b/openid-connect-server/src/main/java/org/mitre/oauth2/web/ScopeAPI.java index 627592db2..66951c041 100644 --- a/openid-connect-server/src/main/java/org/mitre/oauth2/web/ScopeAPI.java +++ b/openid-connect-server/src/main/java/org/mitre/oauth2/web/ScopeAPI.java @@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import com.google.gson.Gson; @@ -103,6 +104,23 @@ public class ScopeAPI { } } + @PreAuthorize("hasRole('ROLE_ADMIN')") + @RequestMapping(value="/checkScopeValue", method = RequestMethod.GET) + public String checkScopeValue(@RequestParam String value, ModelMap m) { + + SystemScope alreadyExists = scopeService.getByValue(value); + if (alreadyExists != null) { + m.put("code", HttpStatus.CONFLICT); + m.put("entity", "A scope with value " + value + " already exists, please choose a different value."); + } + else { + m.put("code", HttpStatus.ACCEPTED); + m.put("entity", "OK"); + } + + return "jsonEntityView"; + } + @PreAuthorize("hasRole('ROLE_ADMIN')") @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json", consumes = "application/json") public String createScope(@RequestBody String json, ModelMap m) { @@ -113,7 +131,7 @@ public class ScopeAPI { //Error, cannot save a scope with the same value as an existing one logger.error("Error: attempting to save a scope with a value that already exists: " + scope.getValue()); m.put("code", HttpStatus.CONFLICT); - m.put("entity", "A scope with value " + scope.getValue() + "already exists, please choose a different value."); + m.put("entity", "A scope with value " + scope.getValue() + " already exists, please choose a different value."); return "jsonEntityView"; } diff --git a/openid-connect-server/src/main/webapp/resources/js/scope.js b/openid-connect-server/src/main/webapp/resources/js/scope.js index eaa6d01d1..d43297938 100644 --- a/openid-connect-server/src/main/webapp/resources/js/scope.js +++ b/openid-connect-server/src/main/webapp/resources/js/scope.js @@ -207,6 +207,20 @@ var SystemScopeFormView = Backbone.View.extend({ return false; } + var valuedata = {}; + valuedata.value = value; + var alreadyExists = false; + + $.get(this.model.urlRoot + "/checkScopeValue", valuedata) + .success(function() { + console.log("scope value is OK"); + }) + .error(function(error, response) { + $('#value input').addClass('error'); + //add hint + alreadyExists = true; + }); + var valid = this.model.set({ value:value, description:$('#description textarea').val(), @@ -215,7 +229,8 @@ var SystemScopeFormView = Backbone.View.extend({ allowDynReg:$('#allowDynReg input').is(':checked') }); - if (valid) { + if (valid && !alreadyExists) { + var _self = this; this.model.save({}, { success:function() {