Guest User

Untitled

a guest
Mar 16th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. /**
  2. * This Service class for providing the user credentials from the database.
  3. *
  4. */
  5. @Service
  6. public class CustomUserDetailsService implements UserDetailsService {
  7.  
  8. @Autowired
  9. CustomUserRepository customUserRepository;
  10.  
  11. @Override
  12. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  13. CustomUser cusUser = customUserRepository.findOneByUsername(username);
  14. return cusUser;
  15. }
  16.  
  17. }
  18.  
  19. import org.springframework.data.jpa.repository.JpaRepository;
  20. import project.domain.CustomUser;
  21.  
  22. public interface CustomUserRepository extends JpaRepository<AppUser, Long> {
  23.  
  24. CustomUser findOneByUsername(String username);
  25. }
  26.  
  27. import java.util.ArrayList;
  28. import java.util.Collection;
  29.  
  30. import javax.persistence.Column;
  31. import javax.persistence.Entity;
  32. import javax.persistence.GeneratedValue;
  33. import javax.persistence.GenerationType;
  34. import javax.persistence.Id;
  35.  
  36. import org.springframework.security.core.GrantedAuthority;
  37. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  38. import org.springframework.security.core.userdetails.UserDetails;
  39.  
  40. import com.fasterxml.jackson.annotation.JsonIgnore;
  41.  
  42. @Entity
  43. public class CustomUser implements UserDetails {
  44.  
  45. @Id
  46. @GeneratedValue(strategy = GenerationType.AUTO)
  47. private Long id;
  48. private String name;
  49.  
  50. @Column(unique = true)
  51. private String username;
  52.  
  53. private String password;
  54.  
  55. private String role;
  56. private String location;
  57.  
  58. public Long getId() {
  59. return id;
  60. }
  61.  
  62. public void setId(Long id) {
  63. this.id = id;
  64. }
  65.  
  66. public String getName() {
  67. return name;
  68. }
  69.  
  70. public void setName(String name) {
  71. this.name = name;
  72. }
  73.  
  74. public String getUsername() {
  75. return username;
  76. }
  77.  
  78. public void setUsername(String username) {
  79. this.username = username;
  80. }
  81.  
  82. public String getPassword() {
  83. return password;
  84. }
  85.  
  86. public void setPassword(String password) {
  87. this.password = password;
  88. }
  89.  
  90. public String getRole() {
  91. return role;
  92. }
  93.  
  94. public void setRole(String role) {
  95. this.role = role;
  96. }
  97.  
  98. public String getLocation() {
  99. return location;
  100.  
  101. }
  102.  
  103. public void setLocation(String location) {
  104. this.location = location;
  105. }
  106.  
  107. @JsonIgnore
  108. @Override
  109. public boolean isEnabled() {
  110. return true;
  111. }
  112.  
  113. @JsonIgnore
  114. @Override
  115. public boolean isCredentialsNonExpired() {
  116. return true;
  117. }
  118.  
  119. @JsonIgnore
  120. @Override
  121. public boolean isAccountNonLocked() {
  122. return true;
  123. }
  124.  
  125. @JsonIgnore
  126. @Override
  127. public boolean isAccountNonExpired() {
  128. return true;
  129. }
  130.  
  131. @JsonIgnore
  132. @Override
  133. public Collection<? extends GrantedAuthority> getAuthorities() {
  134. Collection<GrantedAuthority> authorities = new ArrayList<>();
  135. authorities.add(new SimpleGrantedAuthority(role));
  136. return authorities;
  137. }
  138. }
  139.  
  140. import java.util.List;
  141. import org.springframework.data.jpa.repository.JpaRepository;
  142. import org.springframework.data.jpa.repository.Query;
  143. import org.springframework.data.repository.query.Param;
  144.  
  145. import compliance.domain.Results;
  146.  
  147. public interface ResultsRepository extends JpaRepository<Alerts, Long> {
  148.  
  149. @Query(value ="SELECT RESULTS.RESULT, RESULTS.LEAGUEID,RESULTS.HOMETEAM, RESULTS.AWAYTEAM,LOCATION.LOCATION FROM RESULTS n" +
  150. "INNER JOIN n" +
  151. "LOCATION ON RESULTS.LEAGUEID=LOCATION.LEAGUEIDn" +
  152. "WHERE LOCATION.LOCATION= :location", nativeQuery = true)
  153. List<Results> findByLocation(@Param("location") String location);
  154.  
  155. }
  156.  
  157. import java.util.List;
  158. import org.springframework.beans.factory.annotation.Autowired;
  159. import org.springframework.security.core.userdetails.UserDetails;
  160. import org.springframework.web.bind.annotation.RequestMapping;
  161. import org.springframework.web.bind.annotation.RequestMethod;
  162. import org.springframework.web.bind.annotation.RestController;
  163. import compliance.domain.Results;
  164. import compliance.repository.*;
  165. import compliance.services.CustomUserDetailsService;
  166.  
  167. @RestController
  168. @RequestMapping(value = "/api")
  169. public class ResultsController {
  170.  
  171. @Autowired
  172. private ResultsRepository ResultssRepository;
  173. private CustomUserDetailsService userDet;
  174.  
  175. private String location = "England";
  176.  
  177. @RequestMapping(value = "/alerts", method = RequestMethod.GET)
  178. public List<Alerts> Alerts() {
  179.  
  180. // THIS DOESN'T WORK
  181. // user = (CustomUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  182. // System.out.println("UserRestController: " + user.getLocation());
  183. // System.out.println("user.getLocation());
  184.  
  185. /** this returns every result not what I want **/
  186. //return ResultsRepository.findAll(new Sort("leagueid"));
  187.  
  188. /** I think I need to be doing something like this to try get the location of logged in user but it doesn't work **/
  189. ResultsRepository.findByLocation(user.getLocation().toString());
  190.  
  191. /** This works it filters the table based on the hard coded location from above - obvs not really what im looking for but at least I know my SQL query does what it should **/
  192. return AlertsRepository.findByLocation(location);
  193.  
  194. }
  195.  
  196. }
Add Comment
Please, Sign In to add comment