Ends in:
7 DAYS
13 HRS
20 MIN
3 SEC
Ends in:
7 D
13 H
20 M
3 S
Open Closed

Granting module permissions to anonymous user #621


User avatar
0
jonas.widenberg created

Hello! We are in the beginning of our first project using ABP. Really like it!

I want to take advantage of the Language Management Module, exposing system texts in a front end app (built in vue.js).

I've created a custom API controller as an endpoint for the app to fetch texts from. I've also created a AppService where to create my own DTO (by requirement from the vue app localization plugin I'm using). The trouble is that certain texts should be visible to all users, regardless if they are authenticated or not. When I (anonymously) call LanguageTextAppService I get an exception, Volo.Abp.Authorization.AbpAuthorizationException: Authorization failed! Given policy has not granted.

Digging into the code I can see that the user needs to have the LanguageManagementPermissions.LanguageTexts.Default permission.

So, is there a way giving all users (anonymous) that permission OR is there a way of bypassing the permissions demanded by the LanguageTextAppService OR any other way?

Any advice is must appreciated.

Thanks! <br>

  • ABP Framework version: v3.3.1
  • UI type: MVC
  • Tiered (MVC) or Identity Server Seperated (Angular): no

1 Answer(s)
  • User Avatar
    0
    alper created
    Support Team Director

    Hi,

    Use the below service to fetch the texts, Now you can fetch without any permission.

    
     public class MyLanguageAppService : LanguageAppServiceBase, IApplicationService
        {
            protected ILanguageTextRepository LanguageTextRepository { get; }
            protected IStringLocalizerFactory LocalizerFactory { get; }
            protected AbpLocalizationOptions AbpLocalizationOptions { get; }
    
            public MyLanguageAppService(
                ILanguageTextRepository languageTextRepository,
                IOptions<AbpLocalizationOptions> abpLocalizationOptions,
                IStringLocalizerFactory localizerFactory)
            {
                LanguageTextRepository = languageTextRepository;
                LocalizerFactory = localizerFactory;
                AbpLocalizationOptions = abpLocalizationOptions.Value;
            }
    
            public Task<PagedResultDto<LanguageTextDto>> GetListAsync(GetLanguagesTextsInput input)
            {
                var languageTexts = new List<LanguageTextDto>();
    
                foreach (var resourceName in GetResourceNames(input))
                {
                    languageTexts.AddRange(GetLocalizationsFromResource(input, resourceName));
                }
    
                var filteredQuery = languageTexts
                    .AsQueryable()
                    .WhereIf(
                        !input.Filter.IsNullOrWhiteSpace(),
                        l =>
                            (l.Name != null && l.Name.IndexOf(input.Filter, StringComparison.OrdinalIgnoreCase) >= 0) ||
                            (l.BaseValue != null && l.BaseValue.IndexOf(input.Filter, StringComparison.OrdinalIgnoreCase) >= 0) ||
                            (!input.GetOnlyEmptyValues && l.Value != null && l.Value.IndexOf(input.Filter, StringComparison.InvariantCultureIgnoreCase) >= 0)
                    );
    
                var languagesTextDtos = filteredQuery
                    .OrderBy(input.Sorting ?? $"{nameof(LanguageTextDto.Name)} ASC")
                    .PageBy(input)
                    .ToList();
    
                return Task.FromResult(new PagedResultDto<LanguageTextDto>(
                    filteredQuery.Count(),
                    languagesTextDtos
                ));
            }
    
            protected List<string> GetResourceNames(GetLanguagesTextsInput input)
            {
                var resourceNames = new List<string>();
    
                if (string.IsNullOrWhiteSpace(input.ResourceName))
                {
                    resourceNames.AddRange(
                        AbpLocalizationOptions
                            .Resources
                            .Values
                            .Select(l => l.ResourceName)
                    );
                }
                else
                {
                    resourceNames.Add(input.ResourceName);
                }
    
                return resourceNames;
            }
    
            protected virtual List<LanguageTextDto> GetLocalizationsFromResource(
              GetLanguagesTextsInput input,
              string resourceName)
            {
                var localizer = GetLocalizer(resourceName);
    
                List<LocalizedString> baseLocalizedStrings;
                List<LocalizedString> targetLocalizedStrings;
    
                using (CultureHelper.Use(CultureInfo.GetCultureInfo(input.BaseCultureName)))
                {
                    baseLocalizedStrings = localizer
                        .GetAllStrings(includeParentCultures: true, includeBaseLocalizers: false)
                        .ToList();
                }
    
                using (CultureHelper.Use(CultureInfo.GetCultureInfo(input.TargetCultureName)))
                {
                    targetLocalizedStrings = localizer
                        .GetAllStrings(includeParentCultures: false, includeBaseLocalizers: false)
                        .ToList();
                }
    
                var languageTextDtos = new List<LanguageTextDto>();
    
                foreach (var baseLocalizedString in baseLocalizedStrings)
                {
                    var target = targetLocalizedStrings.FirstOrDefault(l => l.Name == baseLocalizedString.Name);
    
                    if (input.GetOnlyEmptyValues)
                    {
                        if (!string.IsNullOrEmpty(target?.Value))
                        {
                            continue;
                        }
                    }
    
                    languageTextDtos.Add(
                        new LanguageTextDto
                        {
                            BaseCultureName = input.BaseCultureName,
                            CultureName = input.TargetCultureName,
                            Name = baseLocalizedString.Name,
                            BaseValue = baseLocalizedString.Value,
                            ResourceName = resourceName,
                            Value = target?.Value ?? ""
                        }
                    );
                }
    
                return languageTextDtos;
            }
    
            protected IStringLocalizer GetLocalizer(LocalizationResource resource)
            {
                return LocalizerFactory.Create(resource.ResourceType);
            }
    
            protected IStringLocalizer GetLocalizer(string recourseName)
            {
                return GetLocalizer(GetLocalizationResource(recourseName));
            }
    
            protected LocalizationResource GetLocalizationResource(string resourceName)
            {
                var resource = AbpLocalizationOptions.Resources
                    .Values
                    .FirstOrDefault(r => r.ResourceName == resourceName);
    
                if (resource == null)
                {
                    throw new AbpException($"Resource not found: {resourceName}");
                }
    
                return resource;
            }
        }
    
    
Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06