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
770 stars 158 forks source link

Improvement to the multi-tenant ITenantChangeService to better support sharding #14

Closed JonPSmith closed 2 years ago

JonPSmith commented 2 years ago

An application using AuthP's multi-tenant feature requires a service that implements the ITenantChangeService which is called when a tenant is created, updated, moved or deleted. The current implementation creates a transaction that covers both the AuthP DbContext and the application's DbContext.

This works, but has the following limitations:

Possible solution

Looking at the code it could be changed to a transaction on just the AuthP DbContext, and within that transaction a call to the (modified) ITenantChangeService. If the ChangeService fails / returns an error it can roll back the AuthP changes.

For some changes, like delete and move, the ITenantChangeService would need a transaction too. In this case it gets complex.

Things to look at

FELS-Zak commented 2 years ago

Agree :-) Just found this project - Does it have to use SQL server ?

JonPSmith commented 2 years ago

Have a look at the Roadmap discussions for a roadmap of what I'm planning to do - that includes supporting PostgreSQL, but no other database types.

JonPSmith commented 2 years ago

PS. @FELS-Zak, why were you interested with this feature?

JonPSmith commented 2 years ago

In AuthP versions before version 3 the aim was to get all the multi-tenant data within one database. In this case AuthP used a external DbTransactions approach. This provides one transaction that covers two DbContexts at the same time, but the code is rather complex as couldn't get an instance of the tenant's database DbContext via DI, but you needed to add extra code to manually create the tenant's database DbContext

With the addition in version 3 of the AuthP library of database sharding I decided to simplify the transaction into two separate transactions, with the second transaction run started from within the first transaction. This means that both transactions have to be successful for the database to be updated.

This does mean that the ITenantChangeService has changed a lot. Please see the Update To Version 3 file for how update your version 2 code to version 3.

akema-trebla commented 2 years ago

this seems to be broken

akema-trebla commented 2 years ago

Have a look at the Roadmap discussions for a roadmap of what I'm planning to do - that includes supporting PostgreSQL, but no other database types.

Is there a reason you're not considering other database types like MySQL, Oracle?

JonPSmith commented 2 years ago

Hi @akema-trebla,

this seems to be broken

Yep, that won't be out until version 3.

Is there a reason you're not considering other database types like MySQL, Oracle?

This library is really complex and is taking a lot of work, which I do for free. The more database types a support requires extra work, but the real reason I want to support lots of databases is the yearly update to the library to the changes in EF Core for each database type.

JonPSmith commented 2 years ago

Implemented in version 3