/// <summary> /// Gets the random identity verification code. /// </summary> /// <returns></returns> public static IdentityVerificationCode GetRandomIdentityVerificationCode() { lock ( _obj ) { using (var rockContext = new RockContext()) { IdentityVerificationCode verificationCode = null; rockContext.WrapTransaction(() => { var identityVerificationService = new IdentityVerificationCodeService(rockContext); var availableCodeCount = identityVerificationService .Queryable() .Where(pvc => pvc.LastIssueDateTime < RockDateTime.Today || pvc.LastIssueDateTime == null) .Count(); var hasUsableRecords = availableCodeCount > 0; if (!hasUsableRecords) { RockLogger.Log.Warning(RockLogDomains.Core, "All of the available phone verification codes have been used."); ExceptionLogService.LogException("All of the available phone verification codes have been used."); availableCodeCount = identityVerificationService .Queryable() .Count(); } var itemIndex = _random.Next(availableCodeCount); verificationCode = identityVerificationService .Queryable() .OrderBy(pvc => pvc.LastIssueDateTime) .Where(pvc => !hasUsableRecords || pvc.LastIssueDateTime < RockDateTime.Today || pvc.LastIssueDateTime == null) .Skip(itemIndex) .FirstOrDefault(); if (verificationCode != null) { verificationCode.LastIssueDateTime = RockDateTime.Now; rockContext.SaveChanges(); } }); return(verificationCode); } } }
/// <summary> /// Creates the identity verification record. /// </summary> /// <param name="ipAddress">The ip address.</param> /// <param name="ipLimit">The ip limit.</param> /// <param name="phoneNumber">The phone number.</param> /// <returns></returns> public IdentityVerification CreateIdentityVerificationRecord(string ipAddress, int ipLimit, string phoneNumber) { ValidateIpCountWithinLimits(ipAddress, ipLimit); var verificationCode = IdentityVerificationCodeService.GetRandomIdentityVerificationCode(); if (verificationCode != null) { var identityVerification = new IdentityVerification { ReferenceNumber = phoneNumber, IssueDateTime = RockDateTime.Now, IdentityVerificationCode = verificationCode, RequestIpAddress = ipAddress }; Add(identityVerification); (( RockContext )Context).IdentityVerificationCodes.Attach(verificationCode); Context.SaveChanges(); return(identityVerification); } return(null); }