private async Task ExecuteWithChangedFsId( FtpExecutionContext context, ClaimsPrincipal unixUser, FtpCommandExecutionDelegate next) { var userId = ConvertToLong(unixUser.FindFirst(FtpClaimTypes.UserId)?.Value) ?? uint.MaxValue; var groupId = ConvertToLong(unixUser.FindFirst(FtpClaimTypes.GroupId)?.Value) ?? uint.MaxValue; using (var contextThread = new AsyncContextThread()) { await contextThread.Factory.Run( async() => { using (new UnixFileSystemIdChanger( _logger, userId, groupId, _serverUser.UserId, _serverUser.GroupId)) { await next(context).ConfigureAwait(true); } }) .ConfigureAwait(true); await contextThread.JoinAsync().ConfigureAwait(false); } }
/// <inheritdoc /> public Task InvokeAsync(FtpExecutionContext context, FtpCommandExecutionDelegate next) { var connection = context.Connection; var authInfo = connection.Features.Get <IAuthorizationInformationFeature>(); if (!(authInfo.User is IUnixUser unixUser)) { return(next(context)); } var fsInfo = connection.Features.Get <IFileSystemFeature>(); if (!(fsInfo.FileSystem is UnixFileSystem)) { return(next(context)); } return(ExecuteWithChangedFsId(context, unixUser, next)); }
/// <summary> /// Initializes a new instance of the <see cref="DefaultFtpCommandDispatcher"/> class. /// </summary> /// <param name="connection">The FTP connection.</param> /// <param name="loginStateMachine">The login state machine.</param> /// <param name="commandActivator">The command activator.</param> /// <param name="middlewareObjects">The list of middleware objects.</param> /// <param name="logger">The logger.</param> public DefaultFtpCommandDispatcher( IFtpConnection connection, IFtpLoginStateMachine loginStateMachine, IFtpCommandActivator commandActivator, IEnumerable <IFtpCommandMiddleware> middlewareObjects, ILogger <DefaultFtpCommandDispatcher>?logger = null) { _connection = connection; _loginStateMachine = loginStateMachine; _commandActivator = commandActivator; _logger = logger; var nextStep = new FtpCommandExecutionDelegate(ExecuteCommandAsync); foreach (var middleware in middlewareObjects.Reverse()) { var tempStep = nextStep; nextStep = (context) => middleware.InvokeAsync(context, tempStep); } _executionDelegate = nextStep; }
private async Task ExecuteWithChangedFsId( [NotNull] FtpExecutionContext context, [NotNull] IUnixUser unixUser, [NotNull] FtpCommandExecutionDelegate next) { var contextThread = new AsyncContextThread(); await contextThread.Factory.Run( async() => { using (new UnixFileSystemIdChanger( _logger, unixUser.UserId, unixUser.GroupId, _serverUser.UserId, _serverUser.GroupId)) { await next(context).ConfigureAwait(true); } }) .ConfigureAwait(true); await contextThread.JoinAsync().ConfigureAwait(false); }