Skip to content

Commit

Permalink
Fixing some issue with getting information from the token
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendanAndrews committed Nov 15, 2024
1 parent 4d5c4ab commit 7ea4180
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions software/plugins/alert-messenger/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ dependencies {
implementation 'io.quarkus:quarkus-oidc'
implementation 'io.quarkus:quarkus-qute'
implementation 'io.quarkus:quarkus-mailer'
implementation 'io.quarkus:quarkus-hibernate-orm-panache'


// Add Quarkus REST Client dependency
implementation 'io.quarkus:quarkus-rest-client'
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public class UserInfo {
private String username;
private String email;
private Set<String> roles;

// Custom method to match the expected getUserId() in your other code
public String getUserId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tech.ebp.oqm.plugin.alertMessenger.repositories;

import io.quarkus.hibernate.orm.panache.PanacheRepository;
import jakarta.enterprise.context.ApplicationScoped;
import tech.ebp.oqm.plugin.alertMessenger.model.UserInfo;

@ApplicationScoped
public class UserRepository implements PanacheRepository<UserInfo> {

public UserInfo findByUserId(String userId) {
return find("userId", userId).firstResult();
}

public UserInfo findByEmail(String email) {
return find("email", email).firstResult();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package tech.ebp.oqm.plugin.alertMessenger.utils;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import lombok.extern.slf4j.Slf4j;
import tech.ebp.oqm.plugin.alertMessenger.model.UserInfo;
import tech.ebp.oqm.plugin.alertMessenger.repositories.UserRepository;

import java.util.Optional;
import java.util.Set;

@ApplicationScoped
@Slf4j
public class UserUtils {

@Inject
UserRepository userRepository;

/**
* Retrieve a UserInfo object from the database based on the user ID.
*
* @param userId the unique user ID
* @return UserInfo object if found, Optional.empty otherwise
*/
public Optional<UserInfo> getUserInfo(String userId) {
return Optional.ofNullable(userRepository.findByUserId(userId));
}

/**
* Retrieve the user's unique ID.
*
* @param userId the unique user ID
* @return the user ID, or null if the user is not found
*/
public String getId(String userId) {
return getUserInfo(userId)
.map(UserInfo::getUserId)
.orElse(null);
}

/**
* Retrieve the user's name.
*
* @param userId the unique user ID
* @return the user's name, or null if the user is not found
*/
public String getName(String userId) {
return getUserInfo(userId)
.map(UserInfo::getName)
.orElse(null);
}

/**
* Retrieve the user's email.
*
* @param userId the unique user ID
* @return the user's email, or null if the user is not found
*/
public String getEmail(String userId) {
return getUserInfo(userId)
.map(UserInfo::getEmail)
.orElse(null);
}

/**
* Retrieve the user's username.
*
* @param userId the unique user ID
* @return the user's username, or null if the user is not found
*/
public String getUserName(String userId) {
return getUserInfo(userId)
.map(UserInfo::getUsername)
.orElse(null);
}

/**
* Retrieve the user's roles.
*
* @param userId the unique user ID
* @return a set of roles, or null if the user is not found
*/
public Set<String> getRoles(String userId) {
return getUserInfo(userId)
.map(UserInfo::getRoles)
.orElse(null);
}
}

0 comments on commit 7ea4180

Please sign in to comment.