diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/AuthREST.java b/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/AuthREST.java index 8485a5d..a9263da 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/AuthREST.java +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/AuthREST.java @@ -6,7 +6,6 @@ import javax.inject.Inject; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import javax.ws.rs.Consumes; -import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; @@ -24,25 +23,28 @@ public class AuthREST { private SecurityContext securityContext; @POST + @Path("login") @ValidatePayload - @Produces("application/json") @Consumes("application/json") - public void login(CredentialsData data) { + @Produces("application/json") + public Principal login(CredentialsBody body) { Credentials credentials = Beans.getReference(Credentials.class); - credentials.setUsername(data.username); - credentials.setPassword(data.password); + credentials.setUsername(body.username); + credentials.setPassword(body.password); securityContext.login(); + return securityContext.getUser(); } - @GET + @POST @LoggedIn - @Produces("application/json") - public Principal getLoggedInUser() { - return securityContext.getUser(); + @Path("logout") + @ValidatePayload + public void logout() { + securityContext.logout(); } - public static class CredentialsData { + public static class CredentialsBody { @NotNull(message = "{required.field}") @Size(min = 1, message = "{required.field}") diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/BookmarkREST.java b/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/BookmarkREST.java index 5deb8b6..c6f2635 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/BookmarkREST.java +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/BookmarkREST.java @@ -65,10 +65,10 @@ public class BookmarkREST { @ValidatePayload @Produces("application/json") @Consumes("application/json") - public Response insert(Bookmark entity, @Context UriInfo uriInfo) throws Exception { - checkId(entity); + public Response insert(Bookmark body, @Context UriInfo uriInfo) throws Exception { + checkId(body); - String id = bc.insert(entity).getId().toString(); + String id = bc.insert(body).getId().toString(); URI location = uriInfo.getRequestUriBuilder().path(id).build(); return Response.created(location).entity(id).build(); @@ -81,12 +81,12 @@ public class BookmarkREST { @ValidatePayload @Produces("application/json") @Consumes("application/json") - public void update(@PathParam("id") Long id, Bookmark entity) throws Exception { - checkId(entity); + public void update(@PathParam("id") Long id, Bookmark body) throws Exception { + checkId(body); load(id); - entity.setId(id); - bc.update(entity); + body.setId(id); + bc.update(body); } @DELETE diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-list.js b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-list.js index 4f89b62..9a2a490 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-list.js +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/bookmark-list.js @@ -1,6 +1,5 @@ $(function() { $("#new").focus(); - BookmarkProxy.findAll().done(findAllOk); MetadataProxy.getDemoiselleVersion().done(function(data) { diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/login.js b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/login.js index a0598de..cebc55e 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/login.js +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/login.js @@ -10,24 +10,26 @@ $(function() { $("[id$='-message']").hide(); - var data = { + var credentials = { 'username' : $("#username").val().trim(), 'password' : $("#password").val().trim() }; - AuthProxy.login(data).done(loginOk).fail(loginFail); + AuthProxy.login(credentials).done(loginOk).fail(loginFail); }); }); -function loginOk(data, status, request) { - App.setToken(request.getResponseHeader('Set-Token')); - location.href = "home.html"; +function loginOk(data, textStatus, jqXHR) { + App.auth.setToken(jqXHR.getResponseHeader('Set-Token')); + App.auth.setLoggedInUser(data); + + App.restoreSavedLocation(); } -function loginFail(request) { - switch (request.status) { +function loginFail(jqXHR, textStatus, errorThrown) { + switch (jqXHR.status) { case 401: - $("#global-message").html(request.responseText).show(); + $("#global-message").html(jqXHR.responseText).show(); break; case 422: @@ -35,7 +37,7 @@ function loginFail(request) { var id = $(this).attr('id'); var message = null; - $.each(request.responseJSON, function(index, value) { + $.each(jqXHR.responseJSON, function(index, value) { if (id == value.property) { message = value.message; return; diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/menu.js b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/menu.js index b6abbdb..9f0c409 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/menu.js +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/controller/menu.js @@ -1,14 +1,15 @@ $(function() { $("#menu").load("menu.html", function() { - AuthProxy.getUser().done(getUserOk); + $("#username").html(App.auth.getLoggedInUser().name); - $("#logout").on("click", function() { - App.removeToken(); - location.href = "index.html"; + $("#logout").click(function(event) { + event.preventDefault(); + AuthProxy.logout().done(logoutOk); }); }); }); -function getUserOk(data) { - $("#username").html(data.name); +function logoutOk() { + App.auth.clearAuthentication(); + location.href = ""; } diff --git a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/lib/app.js b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/lib/app.js index 4c41ef7..6c174be 100644 --- a/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/lib/app.js +++ b/archetype/html-rest/src/main/resources/archetype-resources/src/main/webapp/js/lib/app.js @@ -1,41 +1,131 @@ -$.ajaxSetup({ - error : function(request) { - switch (request.status) { - case 401: - bootbox.alert("Você não está autenticado!", function() { - location.href = "login.html"; - }); - - break; - } - } -}); - var App = { - tokenKey : "Token", + savedLocationKey : "Saved Location", - getToken : function() { - return sessionStorage.getItem(this.tokenKey); + restoreSavedLocation : function() { + var url = sessionStorage.getItem(this.savedLocationKey); + location.href = (url ? url : ""); }, - setToken : function(token) { - console.log(token); - sessionStorage.setItem(this.tokenKey, token); + saveLocation : function(url) { + sessionStorage.setItem(this.savedLocationKey, url); }, - setHeader : function(request) { - request.setRequestHeader("Authorization", "Token " + App.getToken()); - }, - - removeToken : function() { - sessionStorage.removeItem(this.tokenKey); - $.removeCookie("Token"); + clearSavedLocation : function() { + sessionStorage.removeItem(this.savedLocationKey); }, getUrlParameterByName : function(name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new RegExp("[\\?&]" + name + "=([^]*)"), results = regex.exec(location.search); return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); + }, + + auth : { + tokenKey : "Token", + + userKey : "User", + + getLoggedInUser : function() { + return JSON.parse(sessionStorage.getItem(this.userKey)); + }, + + setLoggedInUser : function(user) { + sessionStorage.setItem(this.userKey, JSON.stringify(user)); + }, + + isLoggedIn : function() { + return this.getToken() != null; + }, + + getToken : function() { + return sessionStorage.getItem(this.tokenKey); + }, + + setToken : function(token) { + sessionStorage.setItem(this.tokenKey, token); + }, + + clearAuthentication : function() { + sessionStorage.removeItem(this.userKey); + sessionStorage.removeItem(this.tokenKey); + }, + + setHeader : function(request) { + request.setRequestHeader("Authorization", "Token " + this.getToken()); + } + }, + + handling : { + handle401 : function(request) { + App.auth.clearAuthentication(); + App.saveLocation(location.href); + location.href = "login.html"; + }, + + handle422 : function(request) { + var elements = $("form input, form select, form textarea").get().reverse(); + + $(elements).each(function() { + var id = $(this).attr('id'); + var messages = []; + + $.each(request.responseJSON, function(index, value) { + var aux = value.property ? value.property : "global"; + + if (id == aux) { + messages.push(value.message); + return; + } + }); + + if (!id) { + return; + } + + var message = $("#" + id.replace(".", "\\.") + "-message"); + + if (messages.length > 1) { + message.empty(); + var ul = message.append("