JonPSmith / AuthPermissions.AspNetCore

This library provides extra authorization and multi-tenant features to an ASP.NET Core application.
https://www.thereformedprogrammer.net/finally-a-library-that-improves-role-authorization-in-asp-net-core/
MIT License
806 stars 161 forks source link

Enable Audit functionality on Tenant #89

Closed ccnicholls99 closed 1 year ago

ccnicholls99 commented 1 year ago

I want to be able to add Audit functionality on Tenants and related dependent entities that are created in ITenantChangeService (i.e. CreatedBy, CreatedOn, UpdatedBy, UpdatedOn, etc). In my implementation, for example, I have a Company entity associated with the tenant. When the Tenant is added or modified, I want to record audit information about when it was modified and more importantly, by whom. Currently, this is not possible without some workaround code after the tenant is created and the TenantChangeService has been executed.

One solution might be to alter ITenantChangeService methods to accept an extra parameter of AuthUserId so the editing User can be recorded on the Tenant and any dependent entities updated. For example....

ITenantChangeService.CreateNewTenantAsync(Tenant tenant, string? UserId = null);

AuthPermissions.SupportCode.AddUsersServices.ISignInAndCreateTenant methods could then supply the relevant UserIID during Tenant change.

JonPSmith commented 1 year ago

Hi @ccnicholls99,

If your think this though you will see that you can inject the users ID via the constructor of your implementation of your ITenantChangeService.

ccnicholls99 commented 1 year ago

Hi @ccnicholls99,

If your think this though you will see that you can inject the users ID via the constructor of your implementation of your ITenantChangeService.

That was the first thing I tried. Unfortunately, at the point where the Tenant is created, you don't yet have access to the User for whom you are creating the Tenant.

See AuthPermissions.SupportCode.AddUsersServices.SignInAndCreateTenant.SignUpNewTenantWithVersionAsync()...

...so injecting UserId yields null (or Non-Tenanted Admin if tenant is being created by an admin).

At the moment, I am working around this by updating any created assets manually, after the tenant is created. Not a deal breaker but another code dependency where I have to make sure the code gets updated in two places instead of one.

IMHO, it would be preferable if SignUpNewTenantWithVersionAsync deferred the call to the TenantChangeService until AFTER both the tenant and owning Auth User are created.

JonPSmith commented 1 year ago

Hi @ccnicholls99,

OK, I can think of two ways to do this with the ISignInAndCreateTenant service:

  1. The simplest approach is to log the user's Email and and the Tenant's name via the two DTO classes once the SignUpNewTenantWithVersionAsync have finished successfully.
  2. If you need more info, such as the user's ID, then after the SignUpNewTenantWithVersionAsync has finished successfully, then the new user has been registered. You can get user's email from the AddNewUserDto class. You can get that user via the IAuthUsersAdminService's FindAuthUserByEmailAsync method and the tenant name via the AddNewTenantDto and IAuthTenantAdminService's QueryTenants, which returns a IQueryable<Tenant> result.

Remember, you can always wrap a service with your own service, or build your own version of a service and use that instead (but in the case of the SignInAndCreateTenant I don't suggest building your code!)

PS. The ISignInAndCreateTenant service doesn't setup the user until the tenant has been successfully created. This is done so that if there is a problem, then the user can try again. NOTE: The SignInAndCreateTenant class has LOT of code to undo things if things goes wrong, especially with sharding multi-tenant app, but even then there can problems.

JonPSmith commented 10 months ago

Hi @ccnicholls99

Version 6.2.0 provides a new implementation of the SignInAndCreateTenant service. This was triggered by a user having problems when an Exception came in (I think in his implementation of the ITenantChangeService). At the same time I made much simpler - see this section of what I did. The Sign up for a new tenant, with versioning documentation has been updated too.