Esempio n. 1
0
 /// <summary>
 /// Clones this IdentityVerificationCode object to a new IdentityVerificationCode object
 /// </summary>
 /// <param name="source">The source.</param>
 /// <param name="deepCopy">if set to <c>true</c> a deep copy is made. If false, only the basic entity properties are copied.</param>
 /// <returns></returns>
 public static IdentityVerificationCode Clone(this IdentityVerificationCode source, bool deepCopy)
 {
     if (deepCopy)
     {
         return(source.Clone() as IdentityVerificationCode);
     }
     else
     {
         var target = new IdentityVerificationCode();
         target.CopyPropertiesFrom(source);
         return(target);
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Copies the properties from another IdentityVerificationCode object to this IdentityVerificationCode object
 /// </summary>
 /// <param name="target">The target.</param>
 /// <param name="source">The source.</param>
 public static void CopyPropertiesFrom(this IdentityVerificationCode target, IdentityVerificationCode source)
 {
     target.Id                      = source.Id;
     target.Code                    = source.Code;
     target.ForeignGuid             = source.ForeignGuid;
     target.ForeignKey              = source.ForeignKey;
     target.LastIssueDateTime       = source.LastIssueDateTime;
     target.CreatedDateTime         = source.CreatedDateTime;
     target.ModifiedDateTime        = source.ModifiedDateTime;
     target.CreatedByPersonAliasId  = source.CreatedByPersonAliasId;
     target.ModifiedByPersonAliasId = source.ModifiedByPersonAliasId;
     target.Guid                    = source.Guid;
     target.ForeignId               = source.ForeignId;
 }
        /// <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);
                }
            }
        }