-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from folded-ear/profile-from-graphql
Profile from graphql
- Loading branch information
Showing
11 changed files
with
107 additions
and
45 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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/brennaswitzer/cookbook/graphql/resolvers/UserResolver.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,33 @@ | ||
package com.brennaswitzer.cookbook.graphql.resolvers; | ||
|
||
import com.brennaswitzer.cookbook.domain.User; | ||
import com.brennaswitzer.cookbook.graphql.support.PrincipalUtil; | ||
import com.brennaswitzer.cookbook.security.UserPrincipal; | ||
import graphql.kickstart.tools.GraphQLResolver; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
@SuppressWarnings("unused") // component-scanned for graphql-java | ||
@Component | ||
public class UserResolver implements GraphQLResolver<User> { | ||
|
||
public List<String> roles(User user, | ||
DataFetchingEnvironment env) { | ||
UserPrincipal principal = PrincipalUtil.from(env); | ||
// if not the current user, create a new instance | ||
if (!Objects.equals(principal.getId(), user.getId())) { | ||
principal = UserPrincipal.create(user); | ||
} | ||
return principal.getAuthorities() | ||
.stream() | ||
.map(GrantedAuthority::getAuthority) | ||
.filter(it -> it.startsWith("ROLE_")) | ||
.map(it -> it.substring(5)) | ||
.toList(); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/brennaswitzer/cookbook/graphql/support/PrincipalUtil.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,15 @@ | ||
package com.brennaswitzer.cookbook.graphql.support; | ||
|
||
import com.brennaswitzer.cookbook.security.UserPrincipal; | ||
import com.brennaswitzer.cookbook.util.NoUserPrincipalException; | ||
import graphql.schema.DataFetchingEnvironment; | ||
|
||
public class PrincipalUtil { | ||
|
||
public static UserPrincipal from(DataFetchingEnvironment env) { | ||
return env.getGraphQlContext() | ||
.<UserPrincipal>getOrEmpty(UserPrincipal.class) | ||
.orElseThrow(NoUserPrincipalException::new); | ||
} | ||
|
||
} |
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
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