Implement user-specific tutorial retrieval and enhance authentication details

This commit is contained in:
liosha84
2025-07-06 14:04:19 +03:00
parent 83c1917651
commit f7b2b189b3
12 changed files with 143 additions and 6 deletions
@@ -37,6 +37,34 @@ public class TutorialController {
@Autowired
TutorialRepository tutorialRepository;
@GetMapping("/user/tutorials")
public ResponseEntity<List<Tutorial>> getUserTutorials(@RequestParam(required = false) String title) {
try {
List<Tutorial> tutorials = new ArrayList<Tutorial>();
User user = userRepository.findById(authenticationFacade.getUserDetails().getId()).get();
if(title == null){
// If no title is provided, return all tutorials for the user
tutorialRepository.findByUserId(user.getId()).forEach(tutorials::add);
} else {
// If a title is provided, filter tutorials by user and title
tutorialRepository.findByUserIdAndTitle(user.getId(), title).forEach(tutorials::add);
}
if (tutorials.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(tutorials, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@GetMapping("/tutorials")
public ResponseEntity<List<Tutorial>> getAllTutorials(@RequestParam(required = false) String title) {
try {
@@ -12,6 +12,7 @@ import java.util.List;
public interface TutorialRepository extends JpaRepository<Tutorial, Long> {
List<Tutorial> findByUserId(Long userId);
List<Tutorial> findByUserIdAndTitle(Long userId, String title);
List<Tutorial> findByPublished(boolean published);
List<Tutorial> findByTitleContaining(String title);
}
@@ -1,7 +1,9 @@
package com.jambotronGroup.jambotron.security;
import com.jambotronGroup.jambotron.security.services.UserDetailsImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
@Component
@@ -12,5 +14,16 @@ public class AuthenticationFacade implements IAuthenticationFacade {
return SecurityContextHolder.getContext().getAuthentication();
}
@Override
public UserDetailsImpl getUserDetails() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return (UserDetailsImpl) userDetails;
}
}
@@ -1,7 +1,10 @@
package com.jambotronGroup.jambotron.security;
import com.jambotronGroup.jambotron.security.services.UserDetailsImpl;
import org.springframework.security.core.Authentication;
public interface IAuthenticationFacade {
Authentication getAuthentication();
UserDetailsImpl getUserDetails();
}
@@ -116,7 +116,7 @@ public class WebSecurityConfig implements WebMvcConfigurer {// extends WebSecuri
.requestMatchers("/main/**").permitAll()
.requestMatchers("/api/test/**").permitAll()
.requestMatchers("/api/tutorials").hasRole("ADMIN")
//.requestMatchers("/api/tutorials").hasRole("ADMIN")
.requestMatchers("/api/zhipuai/image/**").permitAll()