Skip to content
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

Add @CreatedBy and @UpdatedBy annotation #1528

Closed
wants to merge 1 commit into from

Conversation

sdelamo
Copy link
Contributor

@sdelamo sdelamo commented Dec 8, 2023

See Entity Events

This PR adds the @CreatedBy and @UpdatedBy annotations to populate some entity fields automatically:

@MappedEntity
class Message {
    @Id
    @GeneratedValue
    @Nullable
    Long id

    @NotBlank
    String title

    @CreatedBy
    @Nullable
    String createdBy
}

@sdelamo sdelamo added the type: enhancement New feature or request label Dec 8, 2023
@sdelamo
Copy link
Contributor Author

sdelamo commented Dec 8, 2023

@jeremyg484 I have started the PR, please pick it up from here and ping me when it is ready for review.

Copy link

sonarqubecloud bot commented Dec 8, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sdelamo
Copy link
Contributor Author

sdelamo commented Dec 12, 2023

We will need to use the BeanIntrospection API.

@sdelamo
Copy link
Contributor Author

sdelamo commented Dec 13, 2023

@jeremyg484

One pending thought I have - should we ensure there is a means of mapping from/to full Principal objects of some sort the way that Spring Data does? (See https://docs.spring.io/spring-data/jpa/reference/auditing.html#auditing.annotations)

I think for now it is fine if we only allow a String as the type annotated with @CreatedBy @UpdatedBy. What the user wants to persist is a serialized representation of the user unique id. Mapping to a fully fledged Authentication may mean several queries to a database or a remote service.

Maybe what we need is an API to allow developers to calculate the user unique id in case they want to use other than Principal::getName

@FunctionalInterface
interface UserUniqueIdentifier {
@NonNull String uniqueIdentifier(@NonNull Principal principal);
}

and provide a default implementation.

@Secondary
@Singleton
interface DefaultUserUniqueIdentifier {
@NonNull 
String uniqueIdentifier(@NonNull  Principal principal) {
return principal.getName();
}
}

@sdelamo sdelamo closed this Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants