Ejemplo n.º 1
0
        public async ValueTask <OperationResult> ValidateIdsAsync(
            IReadOnlyCollection <int> permissionIds,
            CancellationToken cancellationToken)
        {
            PermissionsLogMessages.PermissionIdsValidating(_logger, permissionIds);

            if (!permissionIds.Any())
            {
                PermissionsLogMessages.PermissionIdsValidationSucceeded(_logger, permissionIds);
                return(OperationResult.Success);
            }

            var invalidPermissionIds = permissionIds
                                       .Except((await GetIdentitiesAsync(cancellationToken))
                                               .Select(x => x.Id)).ToArray();

            if (invalidPermissionIds.Length == 0)
            {
                PermissionsLogMessages.PermissionIdsValidationSucceeded(_logger, permissionIds);
                return(OperationResult.Success);
            }
            else if (invalidPermissionIds.Length == 1)
            {
                PermissionsLogMessages.PermissionIdsValidationFailed(_logger, invalidPermissionIds);
                return(new DataNotFoundError($"Permission ID {invalidPermissionIds.First()}"));
            }
            else
            {
                PermissionsLogMessages.PermissionIdsValidationFailed(_logger, invalidPermissionIds);
                return(new DataNotFoundError($"Permission IDs {string.Join(", ", invalidPermissionIds)}"));
            }
        }
Ejemplo n.º 2
0
        public ValueTask <IReadOnlyCollection <PermissionIdentityViewModel> > GetIdentitiesAsync(
            CancellationToken cancellationToken)
        => _memoryCache.OptimisticGetOrCreateAsync <IReadOnlyCollection <PermissionIdentityViewModel> >(_getIdentitiesCacheKey, async entry =>
        {
            PermissionsLogMessages.PermissionIdentitiesFetching(_logger);

            entry.Priority = CacheItemPriority.NeverRemove;

            var identities = await _permissionsRepository.AsyncEnumerateIdentities()
                             .ToArrayAsync(cancellationToken);
            PermissionsLogMessages.PermissionIdentitiesFetched(_logger);

            return(identities);
        });