Base solution for your next web application
Open Closed

Add a new role to existing tenants #8159


User avatar
0
kasem created

Hi

New roles are applied to new created tenants only. Do you have an option to create a new role for existing tenants as well? I sometimes have migrations that not only envolve schema updates but also data (Seed data for all tenants) and I'm not sure if you have guidelines that help managing this

Thanks

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

8 Answer(s)
  • User Avatar
    0
    musa.demir created

    What do you mean roles in tenant? Roles are defined on users.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    kasem created

    Yes. But every tenant has roles. When I configure static roles in the system they aren't added to existing tenants but new tenants only. I need them to be added to existing tenants as well

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    kasem created

    any news?

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    maliming created
    Support Team

    hi @kasem

    Try the following code for the tenant to create the missing static role.

    public virtual async Task<IdentityResult> CreateTenantStaticRoles(int tenantId)
    {
    	var staticRoleDefinitions = _roleManagementConfig.StaticRoles.Where(sr => sr.Side == MultiTenancySides.Tenant);
    
    	using (_unitOfWorkManager.Current.SetTenantId(tenantId))
    	{
    		foreach (var staticRoleDefinition in staticRoleDefinitions)
    		{
    			if (await _roleManager.FindByNameAsync(staticRoleDefinition.RoleName) == null)
    			{
    				var role = new Role
    				{
    					TenantId = tenantId,
    					Name = staticRoleDefinition.RoleName,
    					DisplayName = staticRoleDefinition.RoleName,
    					IsStatic = true
    				};
    
    				var identityResult = await _roleManager.CreateAsync(role);
    				if (!identityResult.Succeeded)
    				{
    					return identityResult;
    				}
    			}
    		}
    	}
    
    	return IdentityResult.Success;
    }
    
    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    musa.demir created

    Hi @kasem. It is designed this way from the start. So it is an expected scenario.

    You can use @maliming solution.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    kasem created

    Thanks @maliming and @demirmusa

    @demirmusa, what do you mean by "It is designed this way from the start"?

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    musa.demir created

    If you add a new static role you should handle adding it to all previous tenants.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    ismcagdas created
    Support Team

    This issue is closed because it has not had recent activity for a long time.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)