コード例 #1
0
        public static async Task <UserDocument> DeleteUserAsync(this IDurableOrchestrationContext orchestrationContext, Guid userId, bool allowUnsafe = false)
        {
            var user = await orchestrationContext
                       .GetUserAsync(userId, true)
                       .ConfigureAwait(true);

            return(await orchestrationContext
                   .DeleteUserAsync(user, allowUnsafe)
                   .ConfigureAwait(true));
        }
コード例 #2
0
        public static async Task RunOrchestration(
            [OrchestrationTrigger] IDurableOrchestrationContext functionContext,
            ILogger log)
        {
            if (functionContext is null)
            {
                throw new ArgumentNullException(nameof(functionContext));
            }

            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            var command       = functionContext.GetInput <OrchestratorTeamCloudUserDeleteCommand>();
            var commandResult = command.CreateResult();
            var user          = command.Payload;

            using (log.BeginCommandScope(command))
            {
                try
                {
                    functionContext.SetCustomStatus($"Deleting user.", log);

                    using (await functionContext.LockAsync <UserDocument>(user.Id.ToString()).ConfigureAwait(true))
                    {
                        await functionContext
                        .DeleteUserAsync(user.Id)
                        .ConfigureAwait(true);
                    }

                    var projects = default(IEnumerable <ProjectDocument>);

                    // TODO: this is totally wrong
                    // only update all projects if user was an admin
                    if (user.IsAdmin())
                    {
                        projects = await functionContext
                                   .ListProjectsAsync()
                                   .ConfigureAwait(true);
                    }
                    else if (user.ProjectMemberships.Any())
                    {
                        projects = await functionContext
                                   .ListProjectsAsync(user.ProjectMemberships.Select(m => m.ProjectId).ToList())
                                   .ConfigureAwait(true);
                    }

                    if (projects?.Any() ?? false)
                    {
                        foreach (var project in projects)
                        {
                            var projectUpdateCommand = new OrchestratorProjectUpdateCommand(command.BaseApi, command.User, project);

                            functionContext.StartNewOrchestration(nameof(OrchestratorProjectUpdateCommandOrchestration), projectUpdateCommand);
                        }
                    }

                    commandResult.Result = user;

                    functionContext.SetCustomStatus($"User deleted.", log);
                }
                catch (Exception ex)
                {
                    functionContext.SetCustomStatus("Failed to delete user.", log, ex);

                    commandResult ??= command.CreateResult();
                    commandResult.Errors.Add(ex);

                    throw;
                }
                finally
                {
                    functionContext.SetOutput(commandResult);
                }
            }
        }
コード例 #3
0
        public static async Task RunOrchestration(
            [OrchestrationTrigger] IDurableOrchestrationContext functionContext,
            ILogger log)
        {
            if (functionContext is null)
            {
                throw new ArgumentNullException(nameof(functionContext));
            }

            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            var command       = functionContext.GetInput <OrchestratorProviderDeleteCommand>();
            var commandResult = command.CreateResult();
            var provider      = command.Payload;

            using (log.BeginCommandScope(command, provider))
            {
                try
                {
                    using (await functionContext.LockContainerDocumentAsync(provider).ConfigureAwait(true))
                    {
                        if (!(provider is null))
                        {
                            await functionContext
                            .DeleteProviderAsync(provider)
                            .ConfigureAwait(true);

                            if (provider.PrincipalId.HasValue)
                            {
                                await functionContext
                                .DeleteUserAsync(provider.PrincipalId.Value.ToString(), allowUnsafe : true)
                                .ConfigureAwait(true);
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                    commandResult ??= command.CreateResult();
                    commandResult.Errors.Add(exc);

                    throw;
                }
                finally
                {
                    var commandException = commandResult.Errors?.ToException();

                    if (commandException is null)
                    {
                        functionContext.SetCustomStatus($"Command succeeded", log);
                    }
                    else
                    {
                        functionContext.SetCustomStatus($"Command failed: {commandException.Message}", log, commandException);
                    }

                    functionContext.SetOutput(commandResult);
                }
            }
        }
        public static async Task RunOrchestration(
            [OrchestrationTrigger] IDurableOrchestrationContext orchestrationContext,
            ILogger log)
        {
            if (orchestrationContext is null)
            {
                throw new ArgumentNullException(nameof(orchestrationContext));
            }

            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            var command       = orchestrationContext.GetInput <OrchestratorTeamCloudUserDeleteCommand>();
            var commandResult = command.CreateResult();
            var user          = command.Payload;

            using (log.BeginCommandScope(command))
            {
                try
                {
                    orchestrationContext.SetCustomStatus($"Deleting user.", log);

                    using (await orchestrationContext.LockAsync <UserDocument>(user.Id.ToString()).ConfigureAwait(true))
                    {
                        await orchestrationContext
                        .DeleteUserAsync(user.Id)
                        .ConfigureAwait(true);
                    }

                    var projects = default(IEnumerable <ProjectDocument>);

                    // TODO: this is totally wrong
                    // only update all projects if user was an admin
                    if (user.IsAdmin())
                    {
                        projects = await orchestrationContext
                                   .ListProjectsAsync()
                                   .ConfigureAwait(true);
                    }
                    else if (user.ProjectMemberships.Any())
                    {
                        projects = await orchestrationContext
                                   .ListProjectsAsync(user.ProjectMemberships.Select(m => m.ProjectId).ToList())
                                   .ConfigureAwait(true);
                    }

                    if (projects?.Any() ?? false)
                    {
                        foreach (var project in projects)
                        {
                            var projectUpdateCommand = new OrchestratorProjectUpdateCommand(command.User, project);

                            orchestrationContext.StartNewOrchestration(nameof(OrchestratorProjectUpdateCommandOrchestration), projectUpdateCommand);
                        }
                    }

                    var providerCommand = new ProviderTeamCloudUserDeleteCommand
                                          (
                        command.User.PopulateExternalModel(),
                        command.Payload.PopulateExternalModel(),
                        command.CommandId
                                          );

                    var providerResult = await orchestrationContext
                                         .SendProviderCommandAsync <ProviderTeamCloudUserDeleteCommand, ProviderTeamCloudUserDeleteCommandResult>(providerCommand)
                                         .ConfigureAwait(true);

                    providerResult.Errors.ToList().ForEach(e => commandResult.Errors.Add(e));

                    commandResult.Result = user;

                    orchestrationContext.SetCustomStatus($"User deleted.", log);
                }
                catch (Exception ex)
                {
                    commandResult ??= command.CreateResult();
                    commandResult.Errors.Add(ex);
                }
                finally
                {
                    var commandException = commandResult.Errors?.ToException();

                    if (commandException is null)
                    {
                        orchestrationContext.SetCustomStatus($"Command succeeded", log);
                    }
                    else
                    {
                        orchestrationContext.SetCustomStatus($"Command failed: {commandException.Message}", log, commandException);
                    }

                    orchestrationContext.SetOutput(commandResult);
                }
            }
        }