diff --git a/src/main/java/com/bayer/burp/repository/SpringDataRestEventHandler.java b/src/main/java/com/bayer/burp/repository/SpringDataRestEventHandler.java new file mode 100644 index 0000000..1cd0f2c --- /dev/null +++ b/src/main/java/com/bayer/burp/repository/SpringDataRestEventHandler.java @@ -0,0 +1,51 @@ +package com.bayer.burp.repository; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.rest.core.annotation.HandleBeforeCreate; +import org.springframework.data.rest.core.annotation.RepositoryEventHandler; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; + +import com.bayer.burp.domain.Burp; +import com.bayer.burp.domain.Tag; + +// see https://spring.io/blog/2015/10/28/react-js-and-spring-data-rest-part-5-security +@Component +@RepositoryEventHandler(Burp.class) +public class SpringDataRestEventHandler { + + private final UserRepository userRepository; + private final TagRepository tagRepository; + + private static final Pattern TAG_PATTERN = Pattern.compile("(?:\\s|\\A|^)[#]+([A-Za-z0-9-_]+)"); + + @Autowired + public SpringDataRestEventHandler(UserRepository userRepository, TagRepository tagRepository) { + this.userRepository = userRepository; + this.tagRepository = tagRepository; + } + + @HandleBeforeCreate + public void applyUserInformationUsingSecurityContext(Burp burp) { + + String username = SecurityContextHolder.getContext().getAuthentication().getName(); + burp.setUser(userRepository.findByName(username)); + + Matcher matcher = TAG_PATTERN.matcher(burp.getText()); + List tags = new ArrayList(); + while (matcher.find()) { + Tag tag = tagRepository.findByName(matcher.group().replace('#', ' ').trim()); + // TODO create tag if doesn't exist + tags.add(tag) ; + } + burp.setTags(tags); + + burp.setTimestamp(new Date()); + } +} \ No newline at end of file diff --git a/src/main/java/com/bayer/burp/repository/TagRepository.java b/src/main/java/com/bayer/burp/repository/TagRepository.java index 4918212..1794aa1 100644 --- a/src/main/java/com/bayer/burp/repository/TagRepository.java +++ b/src/main/java/com/bayer/burp/repository/TagRepository.java @@ -1,10 +1,15 @@ package com.bayer.burp.repository; import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.data.rest.core.annotation.RestResource; import org.springframework.security.access.prepost.PreAuthorize; import com.bayer.burp.domain.Tag; @PreAuthorize("hasRole('ROLE_USER')") public interface TagRepository extends PagingAndSortingRepository { + + @RestResource(exported = false) + Tag findByName(String name); + } diff --git a/src/main/java/com/bayer/burp/repository/UserRepository.java b/src/main/java/com/bayer/burp/repository/UserRepository.java index 536999d..8b2a7ba 100644 --- a/src/main/java/com/bayer/burp/repository/UserRepository.java +++ b/src/main/java/com/bayer/burp/repository/UserRepository.java @@ -1,6 +1,7 @@ package com.bayer.burp.repository; import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.data.rest.core.annotation.RestResource; import org.springframework.security.access.prepost.PreAuthorize; import com.bayer.burp.domain.User; @@ -15,5 +16,8 @@ public interface UserRepository extends PagingAndSortingRepository { @PreAuthorize("hasRole('ROLE_ADMIN')") @Override void delete(User user); + + @RestResource(exported = false) + User findByName(String name); } diff --git a/src/main/resources/static/js/controllers.js b/src/main/resources/static/js/controllers.js index 2e8f68b..a58594c 100644 --- a/src/main/resources/static/js/controllers.js +++ b/src/main/resources/static/js/controllers.js @@ -1,17 +1,39 @@ var burpControllers = angular.module('burpControllers', ['ngResource']); -burpControllers.controller('BurpListCtrl', [ '$scope', '$resource', function($scope, $resource) { +burpControllers.controller('BurpListCtrl', [ '$scope', '$resource', '$http', function($scope, $resource, $http) { + + $scope.initList = function() { + var burps = $resource('/burps/search/findFirst8ByOrderByTimestampDesc').get(); - var burps = $resource('/burps/search/findFirst8ByOrderByTimestampDesc').get(); + burps.$promise.then(function(data) { + $scope.burps = burps._embedded.burps; - burps.$promise.then(function(data) { - $scope.burps = burps._embedded.burps; + for (var i = 0; i < $scope.burps.length; i++) { + $scope.burps[i].user = $resource($scope.burps[i]._links.user.href).get(); + $scope.burps[i].tags = $resource($scope.burps[i]._links.tags.href).get(); + } + }); + } + + $scope.doBurp = function() { - for (var i = 0; i < $scope.burps.length; i++) { - $scope.burps[i].user = $resource($scope.burps[i]._links.user.href).get(); - $scope.burps[i].tags = $resource($scope.burps[i]._links.tags.href).get(); - } - }); + // simple HTTP POST + $http.post('/burps', { "text" : $scope.text }).success(function() { + console.log('REFRESH'); + $scope.initList(); + }); + + // Resource (might be moved into a service) +// var Burp = $resource('/burps'); +// +// var newBurp = new Burp({ +// "text" : $scope.text +// }); +// +// newBurp.$save(); + }; + + } ]); burpControllers.controller('TagCtrl', [ '$scope', '$routeParams', '$resource', function($scope, $routeParams, $resource) { diff --git a/src/main/resources/static/partials/list.html b/src/main/resources/static/partials/list.html index 0486012..faf74bd 100644 --- a/src/main/resources/static/partials/list.html +++ b/src/main/resources/static/partials/list.html @@ -1,6 +1,15 @@ -
+
+
+
+ +
+
+ +
+
+
{{burp.user.name}}
{{burp.timestamp | date:'HH:mm:ss dd.MM.yyyy'}}