Base solution for your next web application
Open Closed

How to customize Audit fields values? #2127


User avatar
0
pradippatil created

Hi,

Currently, in ABP, in case of impersonation, audit fields are getting updated by the User Id of the user and not of the impersonated user. i.e. If host logs in as tenant then Tenant user id is updated in audit fields while performing any CRUD operation.

I want to change it like if host logs in as tenant and performs any CRUD operation then audit fields should get updated by the host user id instead of tenant user id. Is there any quick way to do it?

I have investigated a bit and got to know that I need to change in methods like "SetCreationAuditProperties", but this method is also not available for override. Any solution will be helpful.

Thanks


6 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Currently AuditLog contains ImpersonatorUserId and ImpersonatorTenantId, see <a class="postlink" href="https://github.com/aspnetboilerplate/module-zero/blob/09cb578f09ee0318b479aa31dd0ceff56a5d218d/src/Abp.Zero/Auditing/AuditLog.cs">https://github.com/aspnetboilerplate/mo ... uditLog.cs</a>.

    If you want to change current behaviour, you can create a custom AuditInfoProvider which implements IAuditInfoProvider and replace current AuditInfoProvider with your version. In this way, you can write the audit logs in the way you want.

    In order to do that, see these forum topics #1966@9109b9cf-d2db-49b7-a4ea-3782fbbc599c #1180@a4d5b66c-9f13-4b32-90ef-fb880f23eeb5

  • User Avatar
    0
    pradippatil created

    Hi,

    I do not want to update AuditLogs.

    I want to change the values which are saved in auditable entities fields like "CreatorUserId", "LastModifiedUserId", and "DeletedUserId". I think, currently, AbpSession.UserId is being saved in these fields for each auditable entity (for example...AbpRoles), instead I want to save AbpSession.ImpersonatorUserId value in these fields in case host logs in as tenant.

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    This action does not update audit logs, it just changes audit log information before you save it. Here is the place where we create an AuditInfo <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/a706b7e8f497a4afe2bb746892fd6c010b0875fc/src/Abp/Auditing/AuditingHelper.cs#L100">https://github.com/aspnetboilerplate/as ... er.cs#L100</a>

    If you create your own AuditInfoProvider, and use ReplaceService to replace current AuditInfoProvider with your version, your AuditInfoProvider's fill method will be called here <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/a706b7e8f497a4afe2bb746892fd6c010b0875fc/src/Abp/Auditing/AuditingHelper.cs#L116">https://github.com/aspnetboilerplate/as ... er.cs#L116</a>.

    In this way, you can change TenantId, UserId, ImpersonatorUserId, ImpersonatorTenantId as you like.

    You can create a similar class like this one <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/a706b7e8f497a4afe2bb746892fd6c010b0875fc/src/Abp/Auditing/NullAuditInfoProvider.cs">https://github.com/aspnetboilerplate/as ... rovider.cs</a>.

  • User Avatar
    0
    pradippatil created

    Hi,

    I think you still didn't get me.

    I do not want to update the existing functionality of the AbpAuditLogs table.

    I want to change the values of entities fields like "CreatorUserId", "LastModifiedUserId", and "DeletedUserId". These are the fields of all auditable entities (like AbpUsers, AbpRoles etc) and not the AbpAuditLogs table.

    Currently, irrespective of who is performing operation i.e. whether actual user or impersonator, these fields are updated by AbpSession.UserId (which always contains the actual user id, not the impersonator use id).

    Hence, I want to save Impersonator User Id value in fields like "CreatorUserId", "LastModifiedUserId", and "DeletedUserId" for each entity, so that I can get who has performed that particular operation on any entity without looking into AbpAuditLogs.

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I finally get it :), you can overide GetAuditUserId method of your DbContext and return the userId you like. See <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/f83c3729a235425ba098b0f76728c03f3b4a36a6/src/Abp.EntityFramework/EntityFramework/AbpDbContext.cs#L504">https://github.com/aspnetboilerplate/as ... xt.cs#L504</a>.

  • User Avatar
    0
    pradippatil created

    Thanks!

    This is what I wanted.

    Thanks again!