spring-attic / tut-react-and-spring-data-rest

React.js and Spring Data REST :: A tutorial based on the 5-part blog series by Greg Turnquist
https://spring.io/guides/tutorials/react-and-spring-data-rest
883 stars 1.58k forks source link

Update Not working in security/ child project #61

Closed jpizagno closed 6 years ago

jpizagno commented 7 years ago

Error: When clicking the "Update" button while running the tut-react-and-spring-data-rest/security project, the Data is not updated. This is because the Manager is not correctly mapped to the Employee.

Solution (proposed): Add HandleBeforeSave in class com.greglturnquist.payroll.SpringDataRestEventHandler.java Add method @HandleBeforeSave public void applyUserInformationBeforeSave(Employee employee) { String name = SecurityContextHolder.getContext().getAuthentication().getName(); Manager manager = this.managerRepository.findByName(name); if (manager == null) { Manager newManager = new Manager(); newManager.setName(name); newManager.setRoles(new String[]{"ROLE_MANAGER"}); manager = this.managerRepository.save(newManager); } employee.setManager(manager); }

Since there is so much repeated code, an advanced refactoring would be to have a single method adding the manager to Employee for both @HandleBeforeSave and @HandleBeforeCreate.

thanks

jpizagno commented 6 years ago

This issue has been fixed and merged into master branch.

https://github.com/spring-guides/tut-react-and-spring-data-rest/commit/312097d87318772349336c4e2243945bec247a53