Ejemplo n.º 1
0
        public void DeleteUser(string userName)
        {
            using (var unitOfWork = _unitOfWorkFactory.CreateAccountUnitOfWork())
            {
                using (var transaction = unitOfWork.BeginTransaction())
                {
                    try
                    {
                        var userManager = CreateUserManager(unitOfWork.CurrentContext);
                        var user        = userManager.FindByName(userName);
                        if (user == null)
                        {
                            throw new InvalidOperationException(string.Format("Cannot delete user {0}: User not found ", userName));
                        }

                        var result = userManager.Delete(user);
                        if (!result.Succeeded)
                        {
                            transaction.Rollback();
                            throw new Exception("Error removing user account from identity store");
                        }

                        var userProfile = unitOfWork.UserProfileRepository.GetUserByUserName(userName);
                        unitOfWork.UserProfileRepository.Delete(userProfile);
                        unitOfWork.SaveChanges();

                        transaction.Commit();

                        using (var systemUnitOfWork = _unitOfWorkFactory.CreateSystemUnitOfWork())
                        {
                            systemUnitOfWork.NotificationRepository.DeleteNotificationsByUser(userName);
                            systemUnitOfWork.SaveChanges();
                        }
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }