Created
November 14, 2021 12:24
-
-
Save baso53/e2702520084b18d972d1a2ca9f641afe to your computer and use it in GitHub Desktop.
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
@Service | |
@RequiredArgsConstructor | |
public class UserManagementService { | |
private final FirebaseAuth firebaseAuth; | |
public void setTokenClaims(String uid, Map<EntityType, Map<Long, Set<Permission>>> requestedPermissions) throws FirebaseAuthException { | |
var claims = toUserClaims(requestedPermissions); | |
firebaseAuth.setCustomUserClaims(uid, claims); | |
} | |
private Map<String, Object> toUserClaims(Map<EntityType, Map<Long, Set<Permission>>> requestedPermissions) { | |
var customClaims = new ArrayList<String>(); | |
requestedPermissions.forEach((entityType, entityTypePermissions) -> | |
entityTypePermissions.forEach((entityId, permissions) -> | |
permissions.forEach(permission -> | |
customClaims.add(generateClaim(entityType, entityId, permission)) | |
))); | |
return Map.of("custom_claims", customClaims); | |
} | |
private String generateClaim(EntityType entityType, Long entityId, Permission permission) { | |
return entityType + | |
":" + | |
entityId + | |
":" + | |
permission; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment