We will enhance it in the next version
Hi,
Try await serviceScope.ServiceProvider.GetRequiredService<MRSDbMigrationService>().MigrateAsync();
Hi,
You need to use the data seeding system to initial data: https://docs.abp.io/en/abp/latest/Data-Seeding
Hi,
I will check it
Hi,
Yes, it's possible, but it has a lot of work to do and breaks the existing framework design.
disabled property.MyRolePermissionManagementProvider
[ExposeServices(typeof(RolePermissionManagementProvider))]
public class MyRolePermissionManagementProvider : RolePermissionManagementProvider
{
public MyRolePermissionManagementProvider(
IPermissionGrantRepository permissionGrantRepository,
IGuidGenerator guidGenerator,
ICurrentTenant currentTenant,
IUserRoleFinder userRoleFinder) : base(permissionGrantRepository, guidGenerator, currentTenant, userRoleFinder)
{
}
public override async Task<MultiplePermissionValueProviderGrantInfo> CheckAsync(string[] names, string providerName, string providerKey)
{
var multiplePermissionValueProviderGrantInfo = await base.CheckAsync(names, providerName, providerKey);
if (providerName != UserPermissionValueProvider.ProviderName)
{
return multiplePermissionValueProviderGrantInfo;
}
var permissionNotGrants = await PermissionGrantRepository.GetListAsync(names, providerName, $"{providerKey},False");
foreach (var permissionNotGrant in permissionNotGrants)
{
if (multiplePermissionValueProviderGrantInfo.Result.ContainsKey(permissionNotGrant.Name))
{
multiplePermissionValueProviderGrantInfo.Result[permissionNotGrant.Name] = new PermissionValueProviderGrantInfo(false, providerKey);
}
}
return multiplePermissionValueProviderGrantInfo;
}
}
It will check the user permissions again and change the result if the permission is not granted to the user.
MyUserPermissionManagementProvider
[ExposeServices(typeof(UserPermissionManagementProvider))]
public class MyUserPermissionManagementProvider : UserPermissionManagementProvider
{
protected IUserRoleFinder UserRoleFinder { get; }
public MyUserPermissionManagementProvider(IPermissionGrantRepository permissionGrantRepository, IGuidGenerator guidGenerator, ICurrentTenant currentTenant, IUserRoleFinder userRoleFinder) : base(permissionGrantRepository, guidGenerator, currentTenant)
{
UserRoleFinder = userRoleFinder;
}
public override Task<MultiplePermissionValueProviderGrantInfo> CheckAsync(string[] names, string providerName, string providerKey)
{
var key = $"{providerKey},True";
return base.CheckAsync(names, providerName, key);
}
public override async Task SetAsync(string name, string providerKey, bool isGranted)
{
if (isGranted)
{
await RevokeAsync(name, $"{providerKey},False");
if(await ShouldAddPermissionAsync(name, providerKey))
{
await AddIfNotExistsAsync(name, providerKey, isGranted);
}
return;
}
await RevokeAsync(name, $"{providerKey},True");
await AddIfNotExistsAsync(name, providerKey, isGranted);
}
private async Task<bool> ShouldAddPermissionAsync(string name, string providerKey)
{
var userId = Guid.Parse(providerKey);
var roleNames = await UserRoleFinder.GetRolesAsync(userId);
foreach (var roleName in roleNames)
{
var permission = await PermissionGrantRepository.FindAsync(name,RolePermissionValueProvider.ProviderName, roleName);
if(permission != null)
{
return false;
}
}
return true;
}
private async Task AddIfNotExistsAsync(string name, string providerKey, bool isGranted)
{
var key = $"{providerKey},{isGranted}";
if (await PermissionGrantRepository.FindAsync(name, Name, key) != null)
{
return;
}
await PermissionGrantRepository.InsertAsync(
new PermissionGrant(
GuidGenerator.Create(),
name,
Name,
key,
CurrentTenant.Id
)
);
}
}
It will add suffixes True and False to the provider key.
It should work for you, but I didn't consider all scenarios. you should do the full test to check it and enhance it
Hi,
Please add these module dependencies to MRSModule
// Saas Management module packages
typeof(SaasHostWebModule),
typeof(SaasHostHttpApiModule),
typeof(SaasHostApplicationModule),
typeof(SaasEntityFrameworkCoreModule)
BTW, I guess you don't want to include the SaasModule in your project. if so, you don't need to add module dependencies. but you should remove all ITenantRepository uses.
Update the MRSDbMigrationService class.
public class MRSDbMigrationService : ITransientDependency
{
public ILogger<MRSDbMigrationService> Logger { get; set; }
private readonly IDataSeeder _dataSeeder;
private readonly MRSEFCoreDbSchemaMigrator _dbSchemaMigrator;
public MRSDbMigrationService(
IDataSeeder dataSeeder,
MRSEFCoreDbSchemaMigrator dbSchemaMigrator)
{
_dataSeeder = dataSeeder;
_dbSchemaMigrator = dbSchemaMigrator;
Logger = NullLogger<MRSDbMigrationService>.Instance;
}
public async Task MigrateAsync()
{
var initialMigrationAdded = AddInitialMigrationIfNotExist();
if (initialMigrationAdded)
{
return;
}
Logger.LogInformation("Started database migrations...");
await MigrateDatabaseSchemaAsync();
await SeedDataAsync();
Logger.LogInformation($"Successfully completed host database migrations.");
Logger.LogInformation("You can safely end this process...");
}
.........
}
Add the following code to Program class
if (IsMigrateDatabase(args))
{
builder.Services.AddDataMigrationEnvironment();
}
Hi,
It's better you create different roles to manage the permissions. https://en.wikipedia.org/wiki/Role-based_access_control Or you can set a role without any permission and manage the permissions on the user side.