From ac01608be97120f82c47a54d2d54a2c04861fb11 Mon Sep 17 00:00:00 2001 From: BrendanAndrews <113378507+BrendanAndrews@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:04:31 -0500 Subject: [PATCH] Database connection works --- software/plugins/alert-messenger/build.gradle | 5 +- .../alertMessenger/interfaces/ui/Home.java | 63 +++++++++++++++++++ .../plugin/alertMessenger/model/UserInfo.java | 43 +++++++++++-- .../alertMessenger/repositories/Query.java | 5 ++ .../repositories/UserRepository.java | 26 +++++++- .../plugin/alertMessenger/utils/JwtUtils.java | 35 ++++++----- .../alertMessenger/utils/UserUtils.java | 15 ++--- .../src/main/resources/application.yaml | 10 +++ 8 files changed, 172 insertions(+), 30 deletions(-) create mode 100644 software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/repositories/Query.java diff --git a/software/plugins/alert-messenger/build.gradle b/software/plugins/alert-messenger/build.gradle index d7ac0ef7d..1d5cb87c6 100644 --- a/software/plugins/alert-messenger/build.gradle +++ b/software/plugins/alert-messenger/build.gradle @@ -22,8 +22,11 @@ dependencies { implementation 'io.quarkus:quarkus-qute' implementation 'io.quarkus:quarkus-mailer' implementation 'io.quarkus:quarkus-hibernate-orm-panache' + implementation 'io.quarkus:quarkus-jdbc-postgresql' + + implementation 'jakarta.persistence:jakarta.persistence-api:3.1.0' + - // Add Quarkus REST Client dependency implementation 'io.quarkus:quarkus-rest-client' diff --git a/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/interfaces/ui/Home.java b/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/interfaces/ui/Home.java index b5961d652..dce8cb58e 100644 --- a/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/interfaces/ui/Home.java +++ b/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/interfaces/ui/Home.java @@ -2,6 +2,7 @@ import io.quarkus.qute.Location; import io.quarkus.qute.Template; +import io.quarkus.security.identity.SecurityIdentity; import jakarta.annotation.security.PermitAll; import jakarta.annotation.security.RolesAllowed; import jakarta.enterprise.context.RequestScoped; @@ -11,8 +12,20 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; +import jakarta.transaction.Transactional; import lombok.Getter; import lombok.extern.slf4j.Slf4j; +import tech.ebp.oqm.plugin.alertMessenger.repositories.UserRepository; +import tech.ebp.oqm.plugin.alertMessenger.model.UserInfo; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import io.quarkus.hibernate.orm.panache.PanacheRepository; + +import java.util.Set; +import java.util.UUID; + +import org.eclipse.microprofile.jwt.JsonWebToken; import org.eclipse.microprofile.openapi.annotations.tags.Tag; import org.eclipse.microprofile.openapi.annotations.tags.Tags; @@ -28,11 +41,61 @@ public class Home extends UiInterface { @Location("webui/pages/index") Template pageTemplate; + // EVERYTHING BELOW THIS HAS JUST BEEN ADDED BY CHATGPT + @Inject + JsonWebToken jwt; // Extracts claims from the JWT + + @Inject + UserRepository userRepository; // Repository for database operations + + @Inject + SecurityIdentity identity; // Provides user and role info + @GET @RolesAllowed("inventoryView") @Produces(MediaType.TEXT_HTML) + @Transactional // Ensures a transaction is active public Response index() { log.info("Got index page"); + + // Extract user details from the JWT + String idString = jwt.getClaim("sub"); // Extract the ID as a string + UUID id; + try { + id = UUID.fromString(idString); // Convert String to UUID + } catch (IllegalArgumentException e) { + log.error("Invalid UUID format in 'sub' claim: {}", idString); + return Response.status(Response.Status.BAD_REQUEST).entity("Invalid user ID format").build(); + } + + String name = jwt.getClaim("name"); + String username = jwt.getClaim("preferred_username"); + String email = jwt.getClaim("email"); + Set roles = identity.getRoles(); // Retrieves user roles + + // Ensure the user is saved or updated in the database + UserInfo existingUser = userRepository.findById(id); // Find by unique ID + if (existingUser == null) { + log.info("New user detected, saving to database: {}", username); + UserInfo newUser = UserInfo.builder() + .id(id) + .name(name) + .username(username) + .email(email) + .roles(roles) + .build(); + userRepository.persist(newUser); // Save new user + } else { + log.info("Existing user found: {}, updating details", username); + existingUser.setName(name); + existingUser.setUsername(username); + existingUser.setEmail(email); + existingUser.setRoles(roles); + userRepository.merge(existingUser); // Merge the detached entity + } + + // Render the UI page + return Response.ok( this.setupPageTemplate(this.pageTemplate) ).build(); diff --git a/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/model/UserInfo.java b/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/model/UserInfo.java index 989b63f2f..5771c5f60 100644 --- a/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/model/UserInfo.java +++ b/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/model/UserInfo.java @@ -1,18 +1,51 @@ package tech.ebp.oqm.plugin.alertMessenger.model; -import lombok.AllArgsConstructor; +/*import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; -import lombok.NoArgsConstructor; - +import lombok.NoArgsConstructor;*/ import java.util.Set; +import java.util.UUID; + +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Table(name = "users") +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +public class UserInfo { + @Id + private UUID id; + + private String name; + + private String username; -@Data + private String email; + + @ElementCollection + private Set roles; + + // Custom method to match the expected getUserId() in your other code + public String getUserId() { + return id.toString(); + } +} + +/*@Data @Builder @NoArgsConstructor @AllArgsConstructor +@Entity // Makes this class a database entity +@Table(name = "users") public class UserInfo { + @Id // This field acts as the primary key private String id; + private String name; private String username; private String email; @@ -22,4 +55,4 @@ public class UserInfo { public String getUserId() { return id; } -} +}*/ diff --git a/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/repositories/Query.java b/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/repositories/Query.java new file mode 100644 index 000000000..751f83282 --- /dev/null +++ b/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/repositories/Query.java @@ -0,0 +1,5 @@ +package tech.ebp.oqm.plugin.alertMessenger.repositories; + +public @interface Query { + +} diff --git a/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/repositories/UserRepository.java b/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/repositories/UserRepository.java index ca76047a7..b4e31b8b8 100644 --- a/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/repositories/UserRepository.java +++ b/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/repositories/UserRepository.java @@ -2,16 +2,38 @@ import io.quarkus.hibernate.orm.panache.PanacheRepository; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.persistence.EntityManager; import tech.ebp.oqm.plugin.alertMessenger.model.UserInfo; +import java.util.UUID; @ApplicationScoped public class UserRepository implements PanacheRepository { - public UserInfo findByUserId(String userId) { - return find("userId", userId).firstResult(); + /** + * Find a user by their unique ID. + * + * @param id the unique user ID + * @return the UserInfo object, or null if not found + */ + public UserInfo findById(UUID id) { + return find("id", id).firstResult(); } + /** + * Find a user by their email. + * + * @param email the user's email address + * @return the UserInfo object, or null if not found + */ public UserInfo findByEmail(String email) { return find("email", email).firstResult(); } + + @Inject + EntityManager entityManager; // Inject EntityManager for persistence operations + + public UserInfo merge(UserInfo userInfo) { + return entityManager.merge(userInfo); // Use EntityManager to merge detached entities + } } \ No newline at end of file diff --git a/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/utils/JwtUtils.java b/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/utils/JwtUtils.java index 516b5333c..5c5a96809 100644 --- a/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/utils/JwtUtils.java +++ b/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/utils/JwtUtils.java @@ -1,5 +1,6 @@ package tech.ebp.oqm.plugin.alertMessenger.utils; +import java.util.UUID; import org.eclipse.microprofile.jwt.Claims; import org.eclipse.microprofile.jwt.JsonWebToken; import tech.ebp.oqm.plugin.alertMessenger.model.UserInfo; @@ -7,30 +8,34 @@ import java.util.Set; public class JwtUtils { - - public static String getId(JsonWebToken jwt){ - return jwt.getClaim(Claims.sub); + + public static UUID getId(JsonWebToken jwt) { + return UUID.fromString(jwt.getClaim(Claims.sub)); } - public static String getName(JsonWebToken jwt){ + + public static String getName(JsonWebToken jwt) { return jwt.getClaim("name"); } - public static String getEmail(JsonWebToken jwt){ + + public static String getEmail(JsonWebToken jwt) { return jwt.getClaim(Claims.email); } - public static String getUserName(JsonWebToken jwt){ + + public static String getUserName(JsonWebToken jwt) { return jwt.getClaim(Claims.preferred_username); } - public static Set getRoles(JsonWebToken jwt){ + + public static Set getRoles(JsonWebToken jwt) { return jwt.getGroups(); } - - public static UserInfo getUserInfo(JsonWebToken jwt){ + + public static UserInfo getUserInfo(JsonWebToken jwt) { return UserInfo.builder() - .id(getId(jwt)) - .name(getName(jwt)) - .username(getUserName(jwt)) - .email(getEmail(jwt)) - .roles(getRoles(jwt)) - .build(); + .id(getId(jwt)) + .name(getName(jwt)) + .username(getUserName(jwt)) + .email(getEmail(jwt)) + .roles(getRoles(jwt)) + .build(); } } diff --git a/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/utils/UserUtils.java b/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/utils/UserUtils.java index f41d805a6..786b4c6e5 100644 --- a/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/utils/UserUtils.java +++ b/software/plugins/alert-messenger/src/main/java/tech/ebp/oqm/plugin/alertMessenger/utils/UserUtils.java @@ -8,6 +8,7 @@ import java.util.Optional; import java.util.Set; +import java.util.UUID; @ApplicationScoped @Slf4j @@ -22,8 +23,8 @@ public class UserUtils { * @param userId the unique user ID * @return UserInfo object if found, Optional.empty otherwise */ - public Optional getUserInfo(String userId) { - return Optional.ofNullable(userRepository.findByUserId(userId)); + public Optional getUserInfo(UUID userId) { + return Optional.ofNullable(userRepository.findById(userId)); } /** @@ -32,7 +33,7 @@ public Optional getUserInfo(String userId) { * @param userId the unique user ID * @return the user ID, or null if the user is not found */ - public String getId(String userId) { + public String getId(UUID userId) { return getUserInfo(userId) .map(UserInfo::getUserId) .orElse(null); @@ -44,7 +45,7 @@ public String getId(String userId) { * @param userId the unique user ID * @return the user's name, or null if the user is not found */ - public String getName(String userId) { + public String getName(UUID userId) { return getUserInfo(userId) .map(UserInfo::getName) .orElse(null); @@ -56,7 +57,7 @@ public String getName(String userId) { * @param userId the unique user ID * @return the user's email, or null if the user is not found */ - public String getEmail(String userId) { + public String getEmail(UUID userId) { return getUserInfo(userId) .map(UserInfo::getEmail) .orElse(null); @@ -68,7 +69,7 @@ public String getEmail(String userId) { * @param userId the unique user ID * @return the user's username, or null if the user is not found */ - public String getUserName(String userId) { + public String getUserName(UUID userId) { return getUserInfo(userId) .map(UserInfo::getUsername) .orElse(null); @@ -80,7 +81,7 @@ public String getUserName(String userId) { * @param userId the unique user ID * @return a set of roles, or null if the user is not found */ - public Set getRoles(String userId) { + public Set getRoles(UUID userId) { return getUserInfo(userId) .map(UserInfo::getRoles) .orElse(null); diff --git a/software/plugins/alert-messenger/src/main/resources/application.yaml b/software/plugins/alert-messenger/src/main/resources/application.yaml index dc71bea73..573a20242 100644 --- a/software/plugins/alert-messenger/src/main/resources/application.yaml +++ b/software/plugins/alert-messenger/src/main/resources/application.yaml @@ -39,6 +39,16 @@ quarkus: username: "oqm.alert.messenger@gmail.com" password: "${SMTP_PASSWORD}" mock: false + # Database configuration below (for user credentials). + datasource: + db-kind: postgresql + jdbc: + url: jdbc:postgresql://localhost:5432/oqm_db + username: Brendan + password: Password123 + hibernate-orm: + database: + generation: update mp: messaging: