🔨 Add guildcontroller with getMutualGuilds
This commit is contained in:
parent
a1c0bfa080
commit
9b056fdc21
@ -0,0 +1,30 @@
|
|||||||
|
package net.Broken.Api.Controllers;
|
||||||
|
|
||||||
|
import net.Broken.Api.Data.Guild;
|
||||||
|
import net.Broken.Api.Security.Data.JwtPrincipal;
|
||||||
|
import net.Broken.Api.Services.GuildService;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v2/guild")
|
||||||
|
@CrossOrigin(origins = "*", maxAge = 3600)
|
||||||
|
public class GuildController {
|
||||||
|
|
||||||
|
public final GuildService guildService;
|
||||||
|
|
||||||
|
public GuildController(GuildService guildService) {
|
||||||
|
this.guildService = guildService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("mutual-guilds")
|
||||||
|
public List<Guild> getMutualGuilds(Authentication authentication){
|
||||||
|
JwtPrincipal jwtPrincipal = (JwtPrincipal) authentication.getPrincipal();
|
||||||
|
return guildService.getMutualGuilds(jwtPrincipal.user());
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ package net.Broken.Api.Controllers;
|
|||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||||
import io.swagger.v3.oas.annotations.security.SecurityRequirements;
|
import io.swagger.v3.oas.annotations.security.SecurityRequirements;
|
||||||
|
import net.Broken.Api.Security.Data.JwtPrincipal;
|
||||||
import net.Broken.DB.Entity.UserEntity;
|
import net.Broken.DB.Entity.UserEntity;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
@ -18,7 +19,7 @@ public class HelloController {
|
|||||||
|
|
||||||
@GetMapping("world")
|
@GetMapping("world")
|
||||||
public String helloWorld(Authentication authentication){
|
public String helloWorld(Authentication authentication){
|
||||||
UserEntity principal = (UserEntity) authentication.getPrincipal();
|
JwtPrincipal principal = (JwtPrincipal) authentication.getPrincipal();
|
||||||
return "Hello " + principal.getUsername();
|
return "Hello " + principal.user().getUsername();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
src/main/java/net/Broken/Api/Data/Guild.java
Normal file
4
src/main/java/net/Broken/Api/Data/Guild.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package net.Broken.Api.Data;
|
||||||
|
|
||||||
|
public record Guild(String id, String name, String iconUrl) {
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package net.Broken.Api.Security.Data;
|
||||||
|
|
||||||
|
import net.Broken.DB.Entity.UserEntity;
|
||||||
|
|
||||||
|
public record JwtPrincipal(String jwtId, UserEntity user) {
|
||||||
|
}
|
@ -2,6 +2,7 @@ package net.Broken.Api.Security.Filters;
|
|||||||
|
|
||||||
import io.jsonwebtoken.Claims;
|
import io.jsonwebtoken.Claims;
|
||||||
import io.jsonwebtoken.Jws;
|
import io.jsonwebtoken.Jws;
|
||||||
|
import net.Broken.Api.Security.Data.JwtPrincipal;
|
||||||
import net.Broken.Api.Security.Services.JwtService;
|
import net.Broken.Api.Security.Services.JwtService;
|
||||||
import net.Broken.DB.Entity.UserEntity;
|
import net.Broken.DB.Entity.UserEntity;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
@ -32,7 +33,8 @@ public class JwtFilter extends OncePerRequestFilter {
|
|||||||
try {
|
try {
|
||||||
Jws<Claims> jwt = jwtService.verifyAndParseJwt(token);
|
Jws<Claims> jwt = jwtService.verifyAndParseJwt(token);
|
||||||
UserEntity user = jwtService.getUserWithJwt(jwt);
|
UserEntity user = jwtService.getUserWithJwt(jwt);
|
||||||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user, null, new ArrayList<>());
|
JwtPrincipal principal = new JwtPrincipal(jwt.getBody().getId(), user);
|
||||||
|
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(principal, null, new ArrayList<>());
|
||||||
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
||||||
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
|
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -29,7 +29,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
.antMatchers("/swagger-ui/**").permitAll()
|
.antMatchers("/swagger-ui/**").permitAll()
|
||||||
.antMatchers("/swagger-ui.html").permitAll()
|
.antMatchers("/swagger-ui.html").permitAll()
|
||||||
.antMatchers("/v3/api-docs/**").permitAll()
|
.antMatchers("/v3/api-docs/**").permitAll()
|
||||||
.anyRequest().denyAll();
|
.anyRequest().authenticated();
|
||||||
|
|
||||||
http.addFilterBefore(jwtFilter(), UsernamePasswordAuthenticationFilter.class);
|
http.addFilterBefore(jwtFilter(), UsernamePasswordAuthenticationFilter.class);
|
||||||
|
|
||||||
|
26
src/main/java/net/Broken/Api/Services/GuildService.java
Normal file
26
src/main/java/net/Broken/Api/Services/GuildService.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package net.Broken.Api.Services;
|
||||||
|
|
||||||
|
import net.Broken.Api.Data.Guild;
|
||||||
|
import net.Broken.DB.Entity.UserEntity;
|
||||||
|
import net.Broken.MainBot;
|
||||||
|
import net.Broken.Tools.CacheTools;
|
||||||
|
import net.dv8tion.jda.api.entities.User;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GuildService {
|
||||||
|
public List<Guild> getMutualGuilds(UserEntity user){
|
||||||
|
User discordUser = CacheTools.getJdaUser(user);
|
||||||
|
List<net.dv8tion.jda.api.entities.Guild> mutualGuilds = discordUser.getMutualGuilds();
|
||||||
|
List<Guild> guildList = new ArrayList<>();
|
||||||
|
|
||||||
|
for (net.dv8tion.jda.api.entities.Guild guild : mutualGuilds){
|
||||||
|
guildList.add(new Guild(guild.getId(), guild.getName(), guild.getIconUrl()));
|
||||||
|
}
|
||||||
|
return guildList;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -403,6 +403,8 @@ databaseChangeLog:
|
|||||||
referencedTableName: guild_preference_entity
|
referencedTableName: guild_preference_entity
|
||||||
validate: true
|
validate: true
|
||||||
|
|
||||||
|
|
||||||
|
# End base
|
||||||
- changeSet:
|
- changeSet:
|
||||||
id: 1653073535100-14
|
id: 1653073535100-14
|
||||||
author: seb65 (generated)
|
author: seb65 (generated)
|
||||||
|
Loading…
Reference in New Issue
Block a user