-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[#14396] Create MagicLink entity #14401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TobyCyan
wants to merge
9
commits into
TEAMMATES:master
Choose a base branch
from
TobyCyan:magic-link-entity
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+147
−0
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
16eef35
Create magic link entity
TobyCyan 1a09a72
Change validity period
TobyCyan bd2343a
Remove fields
TobyCyan 93b129b
Merge branch 'master' into magic-link-entity
TobyCyan 137972d
Create migration file
TobyCyan 28ef972
Change expiry time
TobyCyan 3f6dfe4
Remove naturalId
TobyCyan 42ea8dc
Remove updatedAt and email unique constraint
TobyCyan f4f2b91
Merge branch 'master' into magic-link-entity
TobyCyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,138 @@ | ||
| package teammates.storage.entity; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
|
|
||
| import org.hibernate.annotations.UpdateTimestamp; | ||
|
|
||
| import teammates.common.util.Const; | ||
| import teammates.common.util.FieldValidator; | ||
| import teammates.common.util.SanitizationHelper; | ||
|
|
||
| /** | ||
| * Represents a passwordless login magic link. | ||
| */ | ||
| @Entity | ||
| @Table(name = "MagicLinks") | ||
| public class MagicLink extends BaseEntity { | ||
|
|
||
| @Id | ||
| private UUID id; | ||
|
|
||
| @Column(nullable = false, unique = true) | ||
| private String email; | ||
|
|
||
| @Column(nullable = false, unique = true) | ||
| private String tokenHash; | ||
|
|
||
| @Column(nullable = false) | ||
| private Instant expiresAt; | ||
|
|
||
| @UpdateTimestamp | ||
| private Instant updatedAt; | ||
|
TobyCyan marked this conversation as resolved.
Outdated
|
||
|
|
||
| protected MagicLink() { | ||
| // required by Hibernate | ||
| } | ||
|
|
||
| public MagicLink(String email, String tokenHash, Instant now) { | ||
| this.setId(UUID.randomUUID()); | ||
| this.setEmail(email); | ||
| this.setTokenHash(tokenHash); | ||
| this.setExpiresAt(now.plus(Const.MAGIC_LINK_VALIDITY_PERIOD)); | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getInvalidityInfo() { | ||
| List<String> errors = new ArrayList<>(); | ||
|
|
||
| addNonEmptyError(FieldValidator.getInvalidityInfoForEmail(email), errors); | ||
|
|
||
| return errors; | ||
| } | ||
|
|
||
| public UUID getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(UUID id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| public String getEmail() { | ||
| return email; | ||
| } | ||
|
|
||
| public void setEmail(String email) { | ||
| this.email = SanitizationHelper.sanitizeEmail(email); | ||
| } | ||
|
|
||
| public String getTokenHash() { | ||
| return tokenHash; | ||
| } | ||
|
|
||
| public void setTokenHash(String tokenHash) { | ||
| this.tokenHash = SanitizationHelper.sanitizeTextField(tokenHash); | ||
| } | ||
|
|
||
| public Instant getExpiresAt() { | ||
| return expiresAt; | ||
| } | ||
|
|
||
| public void setExpiresAt(Instant expiresAt) { | ||
| this.expiresAt = expiresAt; | ||
| } | ||
|
|
||
| public Instant getUpdatedAt() { | ||
| return updatedAt; | ||
| } | ||
|
|
||
| public void setUpdatedAt(Instant updatedAt) { | ||
| this.updatedAt = updatedAt; | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the magic link expires at or before {@code now}. | ||
| */ | ||
| public boolean isExpired(Instant now) { | ||
| return !expiresAt.isAfter(now); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the magic link can still be used. | ||
| */ | ||
| public boolean isUsable(Instant now) { | ||
| return !isExpired(now); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
|
|
||
| if (!(o instanceof MagicLink other)) { | ||
| return false; | ||
| } | ||
|
|
||
| return getId() != null && getId().equals(other.getId()); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return getClass().hashCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "MagicLink [id=" + id + ", email=" + email + ", expiresAt=" + expiresAt | ||
| + ", createdAt=" + getCreatedAt() + ", updatedAt=" + updatedAt + "]"; | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
src/main/resources/db/changelog/migrations/20260715-create-magic-link.xml
This file contains hidden or 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,23 @@ | ||
| <?xml version="1.1" encoding="UTF-8" standalone="no"?> | ||
| <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd"> | ||
| <changeSet author="TobyCyan" id="1784084184512-1"> | ||
| <createTable tableName="magic_links"> | ||
| <column name="id" type="UUID"> | ||
| <constraints nullable="false" primaryKey="true" primaryKeyName="magic_linksPK"/> | ||
| </column> | ||
| <column name="created_at" type="TIMESTAMP(6) WITH TIME ZONE"/> | ||
| <column name="email" type="VARCHAR(255)"> | ||
| <constraints nullable="false"/> | ||
| </column> | ||
| <column name="expires_at" type="TIMESTAMP(6) WITH TIME ZONE"> | ||
| <constraints nullable="false"/> | ||
| </column> | ||
| <column name="token_hash" type="VARCHAR(255)"> | ||
| <constraints nullable="false"/> | ||
| </column> | ||
| <column name="updated_at" type="TIMESTAMP(6) WITH TIME ZONE"/> | ||
| </createTable> | ||
| <addUniqueConstraint columnNames="token_hash" constraintName="UC_MAGIC_LINKSTOKEN_HASH_COL" tableName="magic_links"/> | ||
| <addUniqueConstraint columnNames="email" constraintName="UK6gxypheo8erwn0q8svo6osc1w" tableName="magic_links"/> | ||
| </changeSet> | ||
| </databaseChangeLog> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.