Skip to content

Commit

Permalink
Remove custom attributes when deleting an entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 committed Jan 30, 2024
1 parent eda8fbb commit 1a7342f
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/Logitar.Identity.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ public static async Task Main(string[] args)
application.Run();
}
}

// TODO(fpion): CustomAttributes are not deleted when an entity is deleted
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ public CustomAttributeService(IdentityContext context)
Context = context;
}

public virtual async Task RemoveAsync(string entityType, int entityId, CancellationToken cancellationToken)
{
CustomAttributeEntity[] entities = await Context.CustomAttributes
.Where(x => x.EntityType == entityType && x.EntityId == entityId)
.ToArrayAsync(cancellationToken);
Context.CustomAttributes.RemoveRange(entities);
}

public virtual async Task SynchronizeAsync(string entityType, int entityId, Dictionary<string, string?> customAttributes, CancellationToken cancellationToken)
{
if (customAttributes.Count == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public interface ICustomAttributeService
{
Task RemoveAsync(string entityType, int entityId, CancellationToken cancellationToken = default);
Task SynchronizeAsync(string entityType, int entityId, Dictionary<string, string?> customAttributes, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public virtual async Task HandleAsync(ApiKeyDeletedEvent @event, CancellationTok
Context.ApiKeys.Remove(apiKey);

await DeleteActorAsync(apiKey, cancellationToken);
await CustomAttributes.RemoveAsync(EntityType, apiKey.ApiKeyId, cancellationToken);
await Context.SaveChangesAsync(cancellationToken);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public virtual async Task HandleAsync(OneTimePasswordDeletedEvent @event, Cancel
{
Context.OneTimePasswords.Remove(oneTimePassword);

await CustomAttributes.RemoveAsync(EntityType, oneTimePassword.OneTimePasswordId, cancellationToken);
await Context.SaveChangesAsync(cancellationToken);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public virtual async Task HandleAsync(RoleDeletedEvent @event, CancellationToken
{
Context.Roles.Remove(role);

await CustomAttributes.RemoveAsync(EntityType, role.RoleId, cancellationToken);
await Context.SaveChangesAsync(cancellationToken);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public virtual async Task HandleAsync(SessionDeletedEvent @event, CancellationTo
if (session != null)
{
Context.Sessions.Remove(session);

await CustomAttributes.RemoveAsync(EntityType, session.SessionId, cancellationToken);
await Context.SaveChangesAsync(cancellationToken);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public virtual async Task HandleAsync(UserDeletedEvent @event, CancellationToken
Context.Users.Remove(user);

await DeleteActorAsync(user, cancellationToken);
await CustomAttributes.RemoveAsync(EntityType, user.UserId, cancellationToken);
await Context.SaveChangesAsync(cancellationToken);
}
}
Expand Down Expand Up @@ -90,33 +91,34 @@ public virtual async Task HandleAsync(UserEmailChangedEvent @event, Cancellation
}
}

public virtual async Task HandleAsync(UserIdentifierChangedEvent @event, CancellationToken cancellationToken)
public virtual async Task HandleAsync(UserEnabledEvent @event, CancellationToken cancellationToken)
{
UserEntity? user = await TryLoadAsync(@event.AggregateId, cancellationToken);
if (user != null)
{
user.SetCustomIdentifier(@event);
user.Enable(@event);

await Context.SaveChangesAsync(cancellationToken);
}
}
public virtual async Task HandleAsync(UserIdentifierRemovedEvent @event, CancellationToken cancellationToken)

public virtual async Task HandleAsync(UserIdentifierChangedEvent @event, CancellationToken cancellationToken)
{
UserEntity? user = await TryLoadAsync(@event.AggregateId, cancellationToken);
if (user != null)
{
user.RemoveCustomIdentifier(@event);
user.SetCustomIdentifier(@event);

await Context.SaveChangesAsync(cancellationToken);
}
}

public virtual async Task HandleAsync(UserEnabledEvent @event, CancellationToken cancellationToken)
public virtual async Task HandleAsync(UserIdentifierRemovedEvent @event, CancellationToken cancellationToken)
{
UserEntity? user = await TryLoadAsync(@event.AggregateId, cancellationToken);
if (user != null)
{
user.Enable(@event);
user.RemoveCustomIdentifier(@event);

await Context.SaveChangesAsync(cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/Logitar/Identity</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<AssemblyVersion>0.11.2.0</AssemblyVersion>
<AssemblyVersion>0.11.3.0</AssemblyVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<Version>0.11.2</Version>
<Version>0.11.3</Version>
<NeutralLanguage>en-CA</NeutralLanguage>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
<PackageReleaseNotes>Implemented CustomAttributeService and SessionRepository.LoadActiveAsync.</PackageReleaseNotes>
<PackageReleaseNotes>Remove custom attributes when deleting an entity.</PackageReleaseNotes>
<PackageTags>logitar;net;framework;identity;entityframeworkcore;relational</PackageTags>
<PackageProjectUrl>https://github.com/Logitar/Identity/tree/main/src/Logitar.Identity.EntityFrameworkCore.Relational</PackageProjectUrl>
</PropertyGroup>
Expand Down

0 comments on commit 1a7342f

Please sign in to comment.