Beispiel #1
0
        /// <summary>
        ///     Search tokens with specific conditions.
        /// </summary>
        /// <param name="tokens"></param>
        /// <param name="conditions"></param>
        /// <returns></returns>
        public IQueryable <Token> Search(IQueryable <Token> tokens, FindTokensViewModel conditions)
        {
            // Owner index are specified.
            if (conditions.OwnerIndexes != null)
            {
                tokens = tokens.Where(x => conditions.OwnerIndexes.Contains(x.OwnerIndex));
            }

            // Types are specified.
            if (conditions.Types != null)
            {
                tokens = tokens.Where(x => conditions.Types.Contains(x.Type));
            }

            // Code has been specified.
            if (conditions.Code != null && !string.IsNullOrEmpty(conditions.Code.Value))
            {
                var szCode = conditions.Code;
                switch (szCode.Mode)
                {
                case TextSearchMode.Contain:
                    tokens = tokens.Where(x => x.Code.Contains(szCode.Value));
                    break;

                case TextSearchMode.Equal:
                    tokens = tokens.Where(x => x.Code.Equals(szCode.Value));
                    break;

                case TextSearchMode.EqualIgnoreCase:
                    tokens =
                        tokens.Where(x => x.Code.Equals(szCode.Value, StringComparison.CurrentCultureIgnoreCase));
                    break;

                case TextSearchMode.StartsWith:
                    tokens = tokens.Where(x => x.Code.StartsWith(szCode.Value));
                    break;

                case TextSearchMode.StartsWithIgnoreCase:
                    tokens =
                        tokens.Where(
                            x => x.Code.StartsWith(szCode.Value, StringComparison.CurrentCultureIgnoreCase));
                    break;

                case TextSearchMode.EndsWith:
                    tokens = tokens.Where(x => x.Code.EndsWith(szCode.Value));
                    break;

                case TextSearchMode.EndsWithIgnoreCase:
                    tokens =
                        tokens.Where(
                            x => x.Code.EndsWith(szCode.Value, StringComparison.CurrentCultureIgnoreCase));
                    break;

                default:
                    tokens = tokens.Where(x => x.Code.ToLower().Contains(szCode.Value.ToLower()));
                    break;
                }
            }

            // Issued range is specified.
            if (conditions.Issued != null)
            {
                var issuedRange = conditions.Issued;
                if (issuedRange.From != null)
                {
                    tokens = tokens.Where(x => x.Issued >= issuedRange.From.Value);
                }
                if (issuedRange.To != null)
                {
                    tokens = tokens.Where(x => x.Issued <= issuedRange.To.Value);
                }
            }

            // Expired range is specified.
            if (conditions.Expired != null)
            {
                var expiredRange = conditions.Expired;
                if (expiredRange.From != null)
                {
                    tokens = tokens.Where(x => x.Expired >= expiredRange.From.Value);
                }
                if (expiredRange.To != null)
                {
                    tokens = tokens.Where(x => x.Expired <= expiredRange.To.Value);
                }
            }


            return(tokens);
        }
        public async Task <HttpResponseMessage> SubmitAlternativePassword([FromBody] ResetPasswordViewModel parameters)
        {
            try
            {
                #region Parameters validation

                // Parameters haven't been initialized.
                if (parameters == null)
                {
                    parameters = new ResetPasswordViewModel();
                    Validate(parameters);
                }

                // Parameters are invalid.
                if (!ModelState.IsValid)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest,
                                                  FindValidationMessage(ModelState, nameof(parameters))));
                }

                #endregion

                #region Information search

                // Account search condition construction.
                var searchAccountCondition = new SearchAccountViewModel();
                searchAccountCondition.Email       = new TextSearch();
                searchAccountCondition.Email.Value = parameters.Email;
                searchAccountCondition.Email.Mode  = TextComparision.Equal;

                // Search all accounts in database.
                var accounts = UnitOfWork.RepositoryAccounts.Search();
                accounts = UnitOfWork.RepositoryAccounts.Search(accounts, searchAccountCondition);

                // Token search.
                var conditions = new FindTokensViewModel();
                conditions.Code = new TextSearch
                {
                    Mode  = TextComparision.Equal,
                    Value = parameters.Token
                };
                conditions.Types = new[] { TokenType.Forgot };

                // Search all tokens from database.
                var tokens = UnitOfWork.RepositoryTokens.Search();
                tokens = UnitOfWork.RepositoryTokens.Search(tokens, conditions);

                // Information join & search.
                var findResult = await(from token in tokens
                                       from account in accounts
                                       where account.Id == token.OwnerIndex
                                       select account).FirstOrDefaultAsync();

                // No result has been found.
                if (findResult == null)
                {
                    _log.Error("Token doesn't belong to any accounts or it doesn't exist.");
                    return(Request.CreateResponse(HttpStatusCode.NotFound, HttpMessages.TokenNotFound));
                }

                #endregion

                #region Information handling

                // Update password
                findResult.Password = _encryptionService.Md5Hash(parameters.NewPassword);

                // Remove tokens.
                UnitOfWork.RepositoryTokens.Remove(tokens);

                // Save changes into database.
                await UnitOfWork.CommitAsync();

                #endregion

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception exception)
            {
                _log.Error(exception.Message, exception);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }