public async Task <bool> Handle(CheckUserAccess request, CancellationToken cancellationToken)
        {
            var userRoles = await _identityService.GetUserRoles(request.Username);

            var check1 = await _context.AccessControls.AsNoTracking().OfType <UserAccessControl>().
                         Where(x => x.ResourceId == request.ResourceId && x.Username == request.Username)
                         .Select(x => x.Permission).SingleOrDefaultAsync();

            switch (check1)
            {
            case Permission.Deny:
                return(false);

            case Permission.Allow:
                return(true);
            }

            var check2 = await _context.AccessControls.AsNoTracking().OfType <RoleAccessControl>().
                         Where(x => x.ResourceId == request.ResourceId && userRoles.Contains(x.RoleName))
                         .Select(x => x.Permission).SingleOrDefaultAsync();

            switch (check2)
            {
            case Permission.Deny:
                return(false);

            case Permission.Allow:
                return(true);

            default:
                return(false);
            }
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <Resource> > Handle(GetAccessibleResourceByUsername request, CancellationToken cancellationToken)
        {
            var userRoles = await _identityService.GetUserRoles(request.Username);

            var result = new List <Resource>();

            switch (request.ResourceType)
            {
            case ResourceType.UnKnown:
                return(Enumerable.Empty <Resource>());

            case ResourceType.Api:
                result.AddRange(await _context.AccessControls.OfType <UserAccessControl>()
                                .Include(x => x.ApiResource)
                                .Where(x => x.Username == request.Username)
                                .Where(x => x.Permission == Permission.Allow)
                                .Select(x => x.ApiResource)
                                .ToListAsync(cancellationToken: cancellationToken));

                result.AddRange(await _context.AccessControls.OfType <RoleAccessControl>()
                                .Include(x => x.ApiResource)
                                .Where(x => userRoles.Contains(request.Username))
                                .Where(x => x.Permission == Permission.Allow)
                                .Select(x => x.ApiResource)
                                .ToListAsync(cancellationToken: cancellationToken));


                break;

            case ResourceType.Page:
                result.AddRange(await _context.AccessControls.OfType <UserAccessControl>()
                                .Include(x => x.PageResource)
                                .Where(x => x.Username == request.Username)
                                .Where(x => x.Permission == Permission.Allow)
                                .Select(x => x.PageResource)
                                .ToListAsync(cancellationToken: cancellationToken));

                result.AddRange(await _context.AccessControls.OfType <RoleAccessControl>()
                                .Include(x => x.PageResource)
                                .Where(x => userRoles.Contains(request.Username))
                                .Where(x => x.Permission == Permission.Allow)
                                .Select(x => x.PageResource)
                                .ToListAsync(cancellationToken: cancellationToken));
                break;

            case ResourceType.Menu:
                result.AddRange(await _context.AccessControls.OfType <UserAccessControl>()
                                .Include(x => x.MenuResource)
                                .Where(x => x.Username == request.Username)
                                .Where(x => x.Permission == Permission.Allow)
                                .Select(x => x.MenuResource)
                                .ToListAsync(cancellationToken: cancellationToken));

                result.AddRange(await _context.AccessControls.OfType <RoleAccessControl>()
                                .Include(x => x.MenuResource)
                                .Where(x => userRoles.Contains(request.Username))
                                .Where(x => x.Permission == Permission.Allow)
                                .Select(x => x.MenuResource)
                                .ToListAsync(cancellationToken: cancellationToken));
                break;

            case ResourceType.Element:
                result.AddRange(await _context.AccessControls.OfType <UserAccessControl>()
                                .Include(x => x.ElementResource)
                                .Where(x => x.Username == request.Username)
                                .Where(x => x.Permission == Permission.Allow)
                                .Select(x => x.ElementResource)
                                .ToListAsync(cancellationToken: cancellationToken));

                result.AddRange(await _context.AccessControls.OfType <RoleAccessControl>()
                                .Include(x => x.ElementResource)
                                .Where(x => userRoles.Contains(request.Username))
                                .Where(x => x.Permission == Permission.Allow)
                                .Select(x => x.ElementResource)
                                .ToListAsync(cancellationToken: cancellationToken));
                break;

            default:
                break;
            }

            return(result.GroupBy(x => x.Id).Select(x => x.First()).ToList());
        }