Base solution for your next web application
Open Closed

Required permissions are not granted #3568


User avatar
0
avanekar02 created

Hello,

i generated the app and ran it , it throws the following error

{"result":null,"targetUrl":null,"success":false,"error":{"code":0,"message":"Required permissions are not granted. At least one of these permissions must be granted: Users","details":null,"validationErrors":null},"unAuthorizedRequest":true,"__abp":true}

Regards Anwar


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

    hi

    it seems that the logon user doesn't have permission on UserAppService GetAll() method. Can you grant all permissions for that user from related role.

  • User Avatar
    0
    avanekar02 created

    Any link i give it throws the internal server error, i clicked on roles it throws same error.

    Regards Anwar

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @avanekar02,

    Can you share RoleAppService class in your project ?

    Thanks.

  • User Avatar
    0
    avanekar02 created
    using System.Collections.Generic;
    using System.Threading.Tasks;
    using Abp.Application.Services;
    using Abp.Application.Services.Dto;
    using Abp.Domain.Repositories;
    using System.Linq;
    using ACMEPROJECT.Authorization.Roles;
    using ACMEPROJECT.Roles.Dto;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.EntityFrameworkCore;
    using Abp.IdentityFramework;
    using ACMEPROJECT.Authorization.Users;
    using ACMEPROJECT.Authorization;
    using Abp.UI;
    
    namespace ACMEPROJECT.Roles
    {
        public class RoleAppService : AsyncCrudAppService<Role, RoleDto, int, PagedResultRequestDto, CreateRoleDto, RoleDto>, IRoleAppService
        {
            private readonly RoleManager _roleManager;
            private readonly UserManager _userManager;
    
            public RoleAppService(IRepository<Role> repository, RoleManager roleManager, UserManager userManager)
                : base(repository)
            {
                _roleManager = roleManager;
                _userManager = userManager;
    
                CreatePermissionName
                = GetAllPermissionName
                = GetPermissionName
                = UpdatePermissionName
                = DeletePermissionName
                = PermissionNames.Pages_Roles;
            }
    
            public override async Task<RoleDto> Create(CreateRoleDto input)
            {
                CheckCreatePermission();
    
                var role = ObjectMapper.Map<Role>(input);
                role.SetNormalizedName();
    
                CheckErrors(await _roleManager.CreateAsync(role));
    
                var grantedPermissions = PermissionManager
                    .GetAllPermissions()
                    .Where(p => input.Permissions.Contains(p.Name))
                    .ToList();
    
                await _roleManager.SetGrantedPermissionsAsync(role, grantedPermissions);
    
                return MapToEntityDto(role);
            }
    
            public override async Task<RoleDto> Update(RoleDto input)
            {
                CheckUpdatePermission();
    
                var role = await _roleManager.GetRoleByIdAsync(input.Id);
    
                ObjectMapper.Map(input, role);
    
                CheckErrors(await _roleManager.UpdateAsync(role));
    
                var grantedPermissions = PermissionManager
                    .GetAllPermissions()
                    .Where(p => input.Permissions.Contains(p.Name))
                    .ToList();
    
                await _roleManager.SetGrantedPermissionsAsync(role, grantedPermissions);
    
                return MapToEntityDto(role);
            }
    
            public override async Task Delete(EntityDto<int> input)
            {
                CheckDeletePermission();
    
                var role = await _roleManager.FindByIdAsync(input.Id.ToString());
                if (role.IsStatic)
                {
                    throw new UserFriendlyException("CannotDeleteAStaticRole");
                }
    
                var users = await _userManager.GetUsersInRoleAsync(role.NormalizedName);
    
                foreach (var user in users)
                {
                    CheckErrors(await _userManager.RemoveFromRoleAsync(user, role.NormalizedName));
                }
    
                CheckErrors(await _roleManager.DeleteAsync(role));
            }
    
            public Task<ListResultDto<PermissionDto>> GetAllPermissions()
            {
                var permissions = PermissionManager.GetAllPermissions();
    
                return Task.FromResult(new ListResultDto<PermissionDto>(
                    ObjectMapper.Map<List<PermissionDto>>(permissions)
                ));
            }
    
            protected override IQueryable<Role> CreateFilteredQuery(PagedResultRequestDto input)
            {
                return Repository.GetAllIncluding(x => x.Permissions);
            }
    
            protected override async Task<Role> GetEntityByIdAsync(int id)
            {
                return await Repository.GetAllIncluding(x => x.Permissions).FirstOrDefaultAsync(x => x.Id == id);
            }
    
            protected override IQueryable<Role> ApplySorting(IQueryable<Role> query, PagedResultRequestDto input)
            {
                return query.OrderBy(r => r.DisplayName);
            }
    
            protected virtual void CheckErrors(IdentityResult identityResult)
            {
                identityResult.CheckErrors(LocalizationManager);
            }
        }
    }
    
  • User Avatar
    0
    avanekar02 created

    I put a trace on debug and found this

  • User Avatar
    0
    avanekar02 created

    Please respond to this, appreciate a response please

    Regards Anwar

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I'm not sure if the problem is related to that or not. Can you lastly clean the cookies in your browser and try to login again ?

    Thanks.