Open Closed

Add a new role to existing tenants #8159


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


8 Answer(s)
  • 0
    demirmusa created

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

  • 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

  • 0
    kasem created

    any news?

  • 0
    maliming created

    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;
    }
    
  • 0
    demirmusa created

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

    You can use @maliming solution.

  • 0
    kasem created

    Thanks @maliming and @demirmusa

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

  • 0
    demirmusa created

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

  • 0
    ismcagdas created

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