diff --git a/src/main/java/org/example/kickoff/business/service/LoginTokenService.java b/src/main/java/org/example/kickoff/business/service/LoginTokenService.java index 5e24f65..84a6373 100644 --- a/src/main/java/org/example/kickoff/business/service/LoginTokenService.java +++ b/src/main/java/org/example/kickoff/business/service/LoginTokenService.java @@ -45,13 +45,13 @@ public String generate(String email, String ipAddress, String description, Token } public void remove(String loginToken) { - createNamedQuery("LoginToken.remove") + createNamedQuery(LoginToken.REMOVE) .setParameter("tokenHash", digest(loginToken, MESSAGE_DIGEST_ALGORITHM)) .executeUpdate(); } public void removeExpired() { - createNamedQuery("LoginToken.removeExpired") + createNamedQuery(LoginToken.REMOVE_EXPIRED) .executeUpdate(); } diff --git a/src/main/java/org/example/kickoff/business/service/PersonService.java b/src/main/java/org/example/kickoff/business/service/PersonService.java index 62b56c5..00e9b81 100644 --- a/src/main/java/org/example/kickoff/business/service/PersonService.java +++ b/src/main/java/org/example/kickoff/business/service/PersonService.java @@ -111,12 +111,12 @@ public void requestResetPassword(String email, String ipAddress, String callback } public Optional findByEmail(String email) { - return getOptionalSingleResult(createNamedTypedQuery("Person.getByEmail") + return getOptionalSingleResult(createNamedTypedQuery(Person.BY_EMAIL) .setParameter("email", email)); } public Optional findByLoginToken(String loginToken, TokenType type) { - return getOptionalSingleResult(createNamedTypedQuery("Person.getByLoginToken") + return getOptionalSingleResult(createNamedTypedQuery(Person.BY_LOGIN_TOKEN) .setParameter("tokenHash", digest(loginToken, "SHA-256")) .setParameter("tokenType", type)); } diff --git a/src/main/java/org/example/kickoff/model/LoginToken.java b/src/main/java/org/example/kickoff/model/LoginToken.java index 96d4236..415ebe2 100644 --- a/src/main/java/org/example/kickoff/model/LoginToken.java +++ b/src/main/java/org/example/kickoff/model/LoginToken.java @@ -9,14 +9,31 @@ import jakarta.persistence.Entity; import jakarta.persistence.Enumerated; import jakarta.persistence.ManyToOne; +import jakarta.persistence.NamedQuery; import jakarta.persistence.PrePersist; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; import org.omnifaces.persistence.model.GeneratedIdEntity; +@NamedQuery(name = LoginToken.REMOVE, query = + "DELETE" ++ " FROM" ++ " LoginToken _loginToken" ++ " WHERE" ++ " _loginToken.tokenHash = :tokenHash" +) +@NamedQuery(name = LoginToken.REMOVE_EXPIRED, query = + "DELETE" ++ " FROM" ++ " LoginToken _loginToken" ++ " WHERE" ++ " _loginToken.expiration < CURRENT_TIMESTAMP" +) @Entity public class LoginToken extends GeneratedIdEntity { + public final static String REMOVE = "LoginToken.remove"; + public final static String REMOVE_EXPIRED = "LoginToken.removeExpired"; private static final long serialVersionUID = 1L; @@ -116,4 +133,4 @@ public void setTimestamps() { } } -} \ No newline at end of file +} diff --git a/src/main/java/org/example/kickoff/model/Person.java b/src/main/java/org/example/kickoff/model/Person.java index 836e4e6..e14880d 100644 --- a/src/main/java/org/example/kickoff/model/Person.java +++ b/src/main/java/org/example/kickoff/model/Person.java @@ -16,6 +16,7 @@ import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; import jakarta.persistence.Enumerated; +import jakarta.persistence.NamedQuery; import jakarta.persistence.OneToMany; import jakarta.persistence.OneToOne; import jakarta.persistence.Transient; @@ -25,8 +26,32 @@ import org.example.kickoff.model.validator.Email; import org.omnifaces.persistence.model.TimestampedEntity; +@NamedQuery(name = Person.BY_EMAIL, query = + "SELECT" ++ " _person" ++ " FROM" ++ " Person _person" ++ " WHERE" ++ " _person.email = :email" +) +@NamedQuery(name = Person.BY_LOGIN_TOKEN, query = + "SELECT" ++ " _person" ++ " FROM" ++ " Person _person" ++ " JOIN" ++ " _person.loginTokens _loginToken" ++ " JOIN FETCH" ++ " _person.loginTokens" ++ " WHERE" ++ " _loginToken.tokenHash = :tokenHash AND" ++ " _loginToken.type = :tokenType AND" ++ " _loginToken.expiration > CURRENT_TIMESTAMP" +) @Entity public class Person extends TimestampedEntity { + public final static String BY_LOGIN_TOKEN = "Person.getByLoginToken"; + public final static String BY_EMAIL = "Person.getByEmail"; private static final long serialVersionUID = 1L; @@ -136,4 +161,4 @@ public Set getRolesAsStrings() { return getRoles().stream().map(Role::name).collect(toSet()); } -} \ No newline at end of file +} diff --git a/src/main/resources/META-INF/LoginToken.xml b/src/main/resources/META-INF/LoginToken.xml deleted file mode 100644 index 2e3599b..0000000 --- a/src/main/resources/META-INF/LoginToken.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - DELETE - FROM - LoginToken _loginToken - WHERE - _loginToken.tokenHash = :tokenHash - - - - - - DELETE - FROM - LoginToken _loginToken - WHERE - _loginToken.expiration < CURRENT_TIMESTAMP - - - \ No newline at end of file diff --git a/src/main/resources/META-INF/Person.xml b/src/main/resources/META-INF/Person.xml deleted file mode 100644 index 3d102a5..0000000 --- a/src/main/resources/META-INF/Person.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - SELECT - _person - FROM - Person _person - WHERE - _person.email = :email - - - - - - SELECT - _person - FROM - Person _person - JOIN - _person.loginTokens _loginToken - JOIN FETCH - _person.loginTokens - WHERE - _loginToken.tokenHash = :tokenHash AND - _loginToken.type = :tokenType AND - _loginToken.expiration > CURRENT_TIMESTAMP - - - \ No newline at end of file diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml index 6f28268..b6f847d 100644 --- a/src/main/resources/META-INF/persistence.xml +++ b/src/main/resources/META-INF/persistence.xml @@ -8,9 +8,6 @@ java:app/kickoff/DataSource - META-INF/LoginToken.xml - META-INF/Person.xml -