abp icon indicating copy to clipboard operation
abp copied to clipboard

FullAuditedEntity.DeleterId not set when deleted by host admin

Open duard-sf opened this issue 6 years ago • 8 comments

When removing a FullAuditedEntity, IMultiTenant entity - that has a tenant - as the host admin - that does not have a tenant - the DeleterId property is not set and remains null.

I have not confirmed, but I would suspect this would be the case when deleting any entity that has a tenant different from the deleting user's.

Even manually setting the DeleterId and calling DbContext.SaveChanges does not work.

Could you please advise how to soft delete entities of different tenants as the host admin, while retaining the DeleterId?

duard-sf avatar Jan 29 '20 08:01 duard-sf

hi @duard-sf

Look at the logic of the SetDeleterId method.

The data between the tenant and the tenant and the host is isolated.

https://github.com/abpframework/abp/blob/42f37c5ff01ad853a5425d15539d4222cd0dab69/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditPropertySetter.cs#L151-L181

maliming avatar Jan 29 '20 12:01 maliming

From lines 158 - 161 I would assume that if I set the DeleterId manually , it would be saved as such in the database? Why is that not happening then?

duard-sf avatar Jan 29 '20 15:01 duard-sf

Please show your code.

acjh avatar Jan 29 '20 15:01 acjh

Here is what I initially tried:

HubRepository

public override async Task DeleteAsync(Hub entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
    entity.DeleterId = _currentUser.Id;
    await base.DeleteAsync(entity, autoSave, cancellationToken);
}

I also looked at what base.DeleteAsync does, and implemented that manually, but it also did not work.

duard-sf avatar Jan 29 '20 15:01 duard-sf

Where is the DbContext.SaveChanges call that you claim?

acjh avatar Jan 29 '20 15:01 acjh

So this would be how I tried it the last time:

public override async Task DeleteAsync(Hub entity, bool autoSave = false, CancellationToken cancellationToken = default)
{
    entity.DeleterId = _currentUser.Id;
    DbSet.Remove(entity);

    await DbContext.SaveChangesAsync(cancellationToken);
}

duard-sf avatar Jan 29 '20 15:01 duard-sf

I believe it is because CancelDeletionForSoftDelete reloads the entity entry:

https://github.com/abpframework/abp/blob/6e0c55d4b67b0b9e52a95c2876067fc7bf95b175/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs#L256-L266

Try to override that to:

protected override void CancelDeletionForSoftDelete(EntityEntry entry)
{
    if (!(entry.Entity is ISoftDelete))
    {
        return;
    }

    if (entry.Entity is IDeletionAuditedObject entity)
    {
        var deleterId = entity.DeleterId;
        entry.Reload();
        entry.Entity.As<IDeletionAuditedObject>().DeleterId = deleterId;
    }
    else
    {
        entry.Reload();
    }

    entry.State = EntityState.Modified;
    entry.Entity.As<ISoftDelete>().IsDeleted = true;
}

acjh avatar Jan 29 '20 16:01 acjh

That solved the issue for me, thanks!

duard-sf avatar Jan 30 '20 04:01 duard-sf