-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement summarizing /me payload (replaces redirect)
- Loading branch information
1 parent
0936e7a
commit f3684fc
Showing
7 changed files
with
129 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,91 @@ | ||
package com.faforever.api.user; | ||
|
||
import com.faforever.api.data.domain.Player; | ||
import com.faforever.api.data.domain.UserGroup; | ||
import com.faforever.api.player.PlayerService; | ||
import com.faforever.api.security.FafUserDetails; | ||
import com.faforever.api.web.JsonApiSingleResource; | ||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.annotations.ApiResponse; | ||
import io.swagger.annotations.ApiResponses; | ||
import lombok.Builder; | ||
import lombok.Value; | ||
import org.springframework.security.access.annotation.Secured; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Provides the route {@code /me} which returns the currently logged in user's information. | ||
*/ | ||
@RestController | ||
public class MeController { | ||
private final PlayerService playerService; | ||
|
||
public MeController(PlayerService playerService) { | ||
this.playerService = playerService; | ||
} | ||
|
||
@RequestMapping(method = RequestMethod.GET, value = "/me") | ||
@ApiOperation(value = "Returns the authentication object of the current user") | ||
@ApiResponses(value = { | ||
@ApiResponse(code = 200, message = "Success with JSON { id: ?, type: 'player'}"), | ||
@ApiResponse(code = 400, message = "Bad Request")}) | ||
@Secured({"ROLE_USER"}) | ||
public void me(HttpServletRequest request, | ||
HttpServletResponse response, | ||
@AuthenticationPrincipal FafUserDetails authentication) throws IOException { | ||
// TODO: Find a better way to call elide player route | ||
|
||
response.sendRedirect(String.format("/data/player/%d?%s", authentication.getId(), Objects.toString(request.getQueryString(), ""))); | ||
@ApiOperation("Returns the authentication object of the current user") | ||
@ApiResponse(code = 200, message = "Success with JsonApi compliant MeResult") | ||
@Secured("ROLE_USER") | ||
public JsonApiSingleResource<MeResult> me(@AuthenticationPrincipal FafUserDetails authentication) { | ||
|
||
Player player = playerService.getById(authentication.getId()); | ||
Set<String> grantedAuthorities = authentication.getAuthorities().stream() | ||
.map(GrantedAuthority::getAuthority) | ||
.collect(Collectors.toSet()); | ||
|
||
Set<String> groups = player.getUserGroups().stream() | ||
.map(UserGroup::getTechnicalName) | ||
.collect(Collectors.toSet()); | ||
|
||
return MeResult.builder() | ||
.userId(player.getId()) | ||
.userName(player.getLogin()) | ||
.email(player.getEmail()) | ||
.clan(player.getClan() == null ? null : Clan.builder() | ||
.id(player.getClan().getId()) | ||
.tag(player.getClan().getTag()) | ||
.name(player.getClan().getName()) | ||
.build() | ||
) | ||
.groups(groups) | ||
.permissions(grantedAuthorities) | ||
.build() | ||
.asJsonApi(); | ||
} | ||
|
||
@Value | ||
@Builder | ||
public static class MeResult { | ||
Integer userId; | ||
String userName; | ||
String email; | ||
Clan clan; | ||
Set<String> groups; | ||
Set<String> permissions; | ||
|
||
@SuppressWarnings("unchecked") | ||
JsonApiSingleResource<MeResult> asJsonApi() { | ||
return JsonApiSingleResource.ofProxy( | ||
() -> "me", | ||
() -> "me", | ||
() -> this | ||
); | ||
} | ||
} | ||
|
||
@Value | ||
@Builder | ||
public static class Clan { | ||
Integer id; | ||
String tag; | ||
String name; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/faforever/api/web/JsonApiResourceObject.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.faforever.api.web; | ||
|
||
public interface JsonApiResourceObject { | ||
String getType(); | ||
|
||
String getId(); | ||
|
||
Object getAttributes(); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/faforever/api/web/JsonApiResourceObjectProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.faforever.api.web; | ||
|
||
import java.util.function.Supplier; | ||
|
||
class JsonApiResourceObjectProxy<T> implements JsonApiResourceObject { | ||
private final Supplier<String> typeExtractor; | ||
private final Supplier<String> idExtractor; | ||
private final Supplier<T> attributesExtractor; | ||
|
||
JsonApiResourceObjectProxy(Supplier<String> typeExtractor, Supplier<String> idExtractor, Supplier<T> attributesExtractor) { | ||
this.typeExtractor = typeExtractor; | ||
this.idExtractor = idExtractor; | ||
this.attributesExtractor = attributesExtractor; | ||
} | ||
|
||
public String getType() { | ||
return typeExtractor.get(); | ||
} | ||
|
||
public String getId() { | ||
return idExtractor.get(); | ||
} | ||
|
||
public T getAttributes() { | ||
return attributesExtractor.get(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/faforever/api/web/JsonApiSingleResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.faforever.api.web; | ||
|
||
import lombok.Value; | ||
|
||
import java.util.function.Supplier; | ||
|
||
@Value | ||
public class JsonApiSingleResource<T> { | ||
JsonApiResourceObject data; | ||
|
||
public static JsonApiSingleResource of(JsonApiResourceObject singleDataObject) { | ||
return new JsonApiSingleResource(singleDataObject); | ||
} | ||
|
||
public static <T> JsonApiSingleResource ofProxy(Supplier<String> typeExtractor, Supplier<String> idExtractor, | ||
Supplier<T> attributesExtractor) { | ||
return new JsonApiSingleResource<T>(new JsonApiResourceObjectProxy<>(typeExtractor, idExtractor, attributesExtractor)); | ||
} | ||
} |