Beispiel #1
0
        public async Task <ActionResult> ApproveResetTwoFactor(AdminResetTwoFactorApproveModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("ResetTwoFactorApproveModal", model));
            }

            var result = await AdminUserWriter.ApproveResetAllTwoFactor(User.Identity.GetUserId(), model);

            if (!ModelState.IsWriterResultValid(result))
            {
                return(View("ResetTwoFactorApproveModal", model));
            }

            return(CloseModalSuccess(result.Message));
        }
        public async Task <IWriterResult> ApproveResetAllTwoFactor(string userId, AdminResetTwoFactorApproveModel model)
        {
            EmailMessageModel emailModel = new EmailMessageModel();

            using (var context = DataContextFactory.CreateContext())
            {
                var approval = await context.ApprovalQueue.FirstOrDefaultNoLockAsync(x => x.Id == model.ApprovalId && x.Type == ApprovalQueueType.ResetAllTwoFactor);

                if (approval == null)
                {
                    return(new WriterResult(false, "Not Found."));
                }

                if (approval.Status != Enums.ApprovalQueueStatus.Pending)
                {
                    return(new WriterResult(false, $"Unable to approve {approval.Type} status is {approval.Status}."));
                }

                if (approval.RequestUserId == userId)
                {
                    return(new WriterResult(false, $"Another admin must approve this {approval.Type}."));
                }

                approval.Status  = model.Status;
                approval.Message = model.Message;

                if (model.Status != ApprovalQueueStatus.Pending)
                {
                    approval.ApproveUserId = userId;
                    approval.Approved      = DateTime.UtcNow;
                }

                if (model.Status == ApprovalQueueStatus.Approved)
                {
                    int randomPin      = ObjectExtensions.GetRandomNumber();
                    var twoFactorItems = await context.UserTwoFactor.Where(x => x.UserId == approval.DataUserId && x.Type != TwoFactorType.None).ToListNoLockAsync();

                    foreach (var twoFactorItem in twoFactorItems)
                    {
                        if (twoFactorItem.Type == TwoFactorType.None)
                        {
                            continue;
                        }

                        twoFactorItem.Type  = Enums.TwoFactorType.PinCode;
                        twoFactorItem.Data  = randomPin.ToString();
                        twoFactorItem.Data2 = string.Empty;
                        twoFactorItem.Data3 = string.Empty;
                        twoFactorItem.Data4 = string.Empty;
                        twoFactorItem.Data5 = string.Empty;
                    }

                    var user = await context.Users.Where(x => x.Id == approval.DataUserId).FirstOrDefaultNoLockAsync();

                    var emailParameters = new List <object> {
                        user.UserName, randomPin
                    };
                    emailModel = new EmailMessageModel
                    {
                        BodyParameters = emailParameters.ToArray(),
                        Destination    = user.Email,
                        EmailType      = EmailTemplateType.TwoFactorReset
                    };
                }
                await context.SaveChangesAsync();
            }

            if (model.Status == ApprovalQueueStatus.Approved)
            {
                await EmailService.SendEmail(emailModel);

                return(new WriterResult(true, $"Action approved, Two factor reset email email has been sent to {emailModel.Destination}"));
            }

            return(new WriterResult(true, "Action Rejected, two factor reset rejected."));
        }