public IHttpActionResult GetDonations(string donationYear = null,
                                              int?limit           = null,
                                              [FromUri(Name = "softCredit")] bool?softCredit = null,
                                              [FromUri(Name = "impersonateDonorId")] int?impersonateDonorId = null,
                                              bool?includeRecurring = true)
        {
            return(Authorized(token =>
            {
                var impersonateUserId = impersonateDonorId == null ? string.Empty : _mpDonorService.GetEmailViaDonorId(impersonateDonorId.Value).Email;
                try
                {
                    var donations = (impersonateDonorId != null)
                        ? _impersonationService.WithImpersonation(token,
                                                                  impersonateUserId,
                                                                  () =>
                                                                  _gatewayDonationService.GetDonationsForAuthenticatedUser(token, donationYear, limit, softCredit, includeRecurring))
                        : _gatewayDonationService.GetDonationsForAuthenticatedUser(token, donationYear, limit, softCredit, includeRecurring);
                    if (donations == null || !donations.HasDonations)
                    {
                        return (RestHttpActionResult <ApiErrorDto> .WithStatus(HttpStatusCode.NotFound, new ApiErrorDto("No matching donations found")));
                    }

                    return (Ok(donations));
                }
                catch (UserImpersonationException e)
                {
                    return (e.GetRestHttpActionResult());
                }
            }));
        }
Exemple #2
0
        protected async Task <IHttpActionResult> Authorized(Func <string, IHttpActionResult> actionWhenAuthorized, Func <IHttpActionResult> actionWhenNotAuthorized)
        {
            try
            {
                IEnumerable <string> refreshTokens;
                IEnumerable <string> impersonateUserIds;
                bool impersonate = false;
                var  authorized  = "";

                if (Request.Headers.TryGetValues("ImpersonateUserId", out impersonateUserIds) && impersonateUserIds.Any())
                {
                    impersonate = true;
                }

                if (Request.Headers.TryGetValues("RefreshToken", out refreshTokens) && refreshTokens.Any())
                {
                    var authData = AuthenticationRepository.RefreshToken(refreshTokens.FirstOrDefault());
                    if (authData != null)
                    {
                        authorized = authData.AccessToken;
                        var refreshToken         = authData.RefreshToken;
                        IHttpActionResult result = null;
                        if (impersonate)
                        {
                            result =
                                new HttpAuthResult(
                                    _userImpersonationService.WithImpersonation(authorized, impersonateUserIds.FirstOrDefault(), () => actionWhenAuthorized(authorized)),
                                    authorized,
                                    refreshToken);
                        }
                        else
                        {
                            result = new HttpAuthResult(actionWhenAuthorized(authorized), authorized, refreshToken);
                        }
                        return(await Observable.Return(result));
                    }
                }

                authorized = Request.Headers.GetValues("Authorization").FirstOrDefault();
                if (authorized != null && (authorized != "null" || authorized != ""))
                {
                    if (impersonate)
                    {
                        return(await Observable.Return(_userImpersonationService.WithImpersonation(authorized, impersonateUserIds.FirstOrDefault(), () => actionWhenAuthorized(authorized))));
                    }
                    else
                    {
                        return(await Observable.Return(actionWhenAuthorized(authorized)));
                    }
                }
                return(await Observable.Return(actionWhenNotAuthorized()));
            }
            catch (System.InvalidOperationException e)
            {
                return(await Observable.Return(actionWhenNotAuthorized()));
            }
        }
Exemple #3
0
        public void AutoCompleteTasks()
        {
            try
            {
                var apiUserToken    = _apiUserService.GetToken();
                var tasksToComplete = _taskRepository.GetTasksToAutostart();

                _logger.InfoFormat("Number of tasks to autocomplete: {0} ", tasksToComplete.Count);

                foreach (var task in tasksToComplete)
                {
                    _logger.InfoFormat("Inside of tasks to complete Loop");

                    var user = _userService.GetUserByRecordId(task.Assigned_User_ID);

                    _logger.InfoFormat("User Record ID for task to complete: {0}", user.UserRecordId);
                    _logger.InfoFormat("Task ID for task to complete: {0}", task.Task_ID);

                    try
                    {
                        _impersonationService.WithImpersonation(apiUserToken,
                                                                user.UserEmail,
                                                                () =>
                        {
                            _taskRepository.CompleteTask(apiUserToken, task.Task_ID, task.Rejected, "Auto Completed");
                            return(true);
                        });
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorFormat("Auto complete task failed for Task {0} Detail: {1}", task.Task_ID, ex);
                    }
                }
            }
            catch (Exception outerException)
            {
                _logger.ErrorFormat("Could not process tasks for autocomplete, Detail: {0}", outerException);
            }
        }
 public IHttpActionResult GetProfile([FromUri(Name = "impersonateDonorId")] int?impersonateDonorId = null)
 {
     return(Authorized(token =>
     {
         var impersonateUserId = impersonateDonorId == null ? string.Empty : _donorService.GetContactDonorForDonorId(impersonateDonorId.Value).Email;
         try
         {
             var person = (impersonateDonorId != null)
                 ? _impersonationService.WithImpersonation(token, impersonateUserId, () => _personService.GetLoggedInUserProfile(token))
                 : _personService.GetLoggedInUserProfile(token);
             if (person == null)
             {
                 return Unauthorized();
             }
             return Ok(person);
         }
         catch (UserImpersonationException e)
         {
             return (e.GetRestHttpActionResult());
         }
     }));
 }
Exemple #5
0
        public IHttpActionResult CreateRecurringGift([FromBody] RecurringGiftDto recurringGiftDto, [FromUri(Name = "impersonateDonorId")] int?impersonateDonorId = null)
        {
            return(Authorized(token =>
            {
                var impersonateUserId = impersonateDonorId == null ? string.Empty : _mpDonorService.GetEmailViaDonorId(impersonateDonorId.Value).Email;

                try
                {
                    var contactDonor = (impersonateDonorId != null)
                        ? _impersonationService.WithImpersonation(token,
                                                                  impersonateUserId,
                                                                  () =>
                                                                  _donorService.GetContactDonorForAuthenticatedUser(token))
                        : _donorService.GetContactDonorForAuthenticatedUser(token);
                    var donor = _donorService.CreateOrUpdateContactDonor(contactDonor, string.Empty, string.Empty, string.Empty, string.Empty);
                    var recurringGift = !string.IsNullOrWhiteSpace(impersonateUserId)
                        ? _impersonationService.WithImpersonation(token,
                                                                  impersonateUserId,
                                                                  () =>
                                                                  _donorService.CreateRecurringGift(token, recurringGiftDto, donor))
                        : _donorService.CreateRecurringGift(token, recurringGiftDto, donor);

                    recurringGiftDto.EmailAddress = donor.Email;
                    recurringGiftDto.RecurringGiftId = recurringGift;

                    _analyticsService.Track(donor.ContactId.ToString(), "PaymentSucceededServerSide", new EventProperties()
                    {
                        { "Url", recurringGiftDto.SourceUrl }, { "FundingMethod", recurringGiftDto.Source }, { "Email", "" }, { "CheckoutType", "Registered" }, { "Amount", recurringGiftDto.PlanAmount }
                    });

                    return Ok(recurringGiftDto);
                }
                catch (PaymentProcessorException stripeException)
                {
                    return (stripeException.GetStripeResult());
                }
                catch (ApplicationException applicationException)
                {
                    var apiError = new ApiErrorDto("Error calling Ministry Platform " + applicationException.Message, applicationException);
                    throw new HttpResponseException(apiError.HttpResponseMessage);
                }
            }));
        }
Exemple #6
0
        public IHttpActionResult CreateRecurringGift([FromBody] RecurringGiftDto recurringGiftDto, [FromUri(Name = "impersonateDonorId")] int?impersonateDonorId = null)
        {
            return(Authorized(token =>
            {
                var impersonateUserId = impersonateDonorId == null ? string.Empty : _mpDonorService.GetEmailViaDonorId(impersonateDonorId.Value).Email;

                try
                {
                    var contactDonor = (impersonateDonorId != null)
                        ? _impersonationService.WithImpersonation(token,
                                                                  impersonateUserId,
                                                                  () =>
                                                                  _donorService.GetContactDonorForAuthenticatedUser(token))
                        : _donorService.GetContactDonorForAuthenticatedUser(token);
                    var donor = _donorService.CreateOrUpdateContactDonor(contactDonor, string.Empty, string.Empty, string.Empty, string.Empty);
                    var recurringGift = !string.IsNullOrWhiteSpace(impersonateUserId)
                        ? _impersonationService.WithImpersonation(token,
                                                                  impersonateUserId,
                                                                  () =>
                                                                  _donorService.CreateRecurringGift(token, recurringGiftDto, donor))
                        : _donorService.CreateRecurringGift(token, recurringGiftDto, donor);

                    recurringGiftDto.EmailAddress = donor.Email;
                    recurringGiftDto.RecurringGiftId = recurringGift;
                    return Ok(recurringGiftDto);
                }
                catch (PaymentProcessorException stripeException)
                {
                    return (stripeException.GetStripeResult());
                }
                catch (ApplicationException applicationException)
                {
                    var apiError = new ApiErrorDto("Error calling Ministry Platform " + applicationException.Message, applicationException);
                    throw new HttpResponseException(apiError.HttpResponseMessage);
                }
            }));
        }