Ejemplo n.º 1
0
        public override string GenerateResetPasswordToken(string email, Guid storeCode, string urlBack = "", int emailTemplateCode = 0)
        {
            ApplicationStore appSto;
            CustomerImport   customerImport;
            string           _tokenCode = string.Empty;

            using (var transaction = Connection.BeginTransaction())
            {
                try
                {
                    var application = applicationRepository.Get("EC-Loja", true);

                    appSto = applicationStoreRepository.Get(application.Code.Value, storeCode, true);

                    customerImport = customerImportService.Get(email, storeCode);

                    if (customerImport != null)
                    {
                        var token = resetPasswordTokenService.GenerateResetPasswordToken(customerImport, appSto, urlBack);

                        _tokenCode = token.Code.EncodeURIComponent();

                        customerImport.HandleCustomer();
                        svcEmail.SendPasswordRecoveryEmailAsync(customerImport, appSto.Store, _tokenCode, token.UrlBack, emailTemplateCode);
                    }

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

            if (!appSto.IsNull() && !customerImport.IsNull())
            {
                using (var transaction = Connection.BeginTransaction())
                {
                    try
                    {
                        passwordLogRepository.Save(new PasswordLog(customerImport.AccountCode, PasswordEventLog.ResquetRecoryCustomerImport, appSto.Store.Code));
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                    }
                }
            }

            return(_tokenCode);
        }
Ejemplo n.º 2
0
        public override string GenerateResetPasswordToken(string email, Guid clientId, string urlBack = "", int emailTemplateCode = 0)
        {
            if (email.IsNullOrWhiteSpace())
            {
                throw new ArgumentException("E-mail não informado");
            }

            ApplicationStore appSto;
            Account          account;
            CustomerImport   customerImport = null;
            string           _tokenCode     = string.Empty;

            using (var transaction = Connection.BeginTransaction())
            {
                try
                {
                    appSto = applicationStoreRepository.GetByClientId(clientId);
                    var accounts = accountRepository.Get(email, appSto, true);

                    accountService.lockedUpMemberPolicy = lockedUpMemberPolicy;
                    accountService.lockMemberPolicy     = lockMemberPolicy;
                    accountService.passwordPolicy       = passwordPolicy;

                    account = accountService.Authenticate(accounts, appSto, false);

                    if (account != null)
                    {
                        var resetPasswordTokenService = resetPasswordTokenFactory.GetResetPasswordTokenService(account);

                        resetPasswordTokenService.lockedUpMemberPolicy = lockedUpMemberPolicy;

                        var token = resetPasswordTokenService.GenerateResetPasswordToken(account, appSto, urlBack);

                        _tokenCode = token.Code.EncodeURIComponent();

                        svcEmail.SendPasswordRecoveryEmailAsync(account, appSto.Store, _tokenCode, token.UrlBack, emailTemplateCode);
                    }
                    else
                    {
                        customerImport = customerImportService.Get(email, appSto.Store.Code);

                        if (customerImport != null)
                        {
                            var resetPasswordTokenService = resetPasswordTokenFactory.GetResetPasswordTokenService(customerImport);

                            var token = resetPasswordTokenService.GenerateResetPasswordToken(customerImport, appSto, urlBack);

                            _tokenCode = token.Code.EncodeURIComponent();

                            customerImport.HandleCustomer();
                            svcEmail.SendPasswordRecoveryEmailAsync(customerImport, appSto.Store, _tokenCode, urlBack, emailTemplateCode);
                        }
                    }

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

            if (!appSto.IsNull() && (!account.IsNull() || !customerImport.IsNull()))
            {
                using (var transaction = Connection.BeginTransaction())
                {
                    try
                    {
                        var code = !account.IsNull() ? account.Code : customerImport.AccountCode;

                        passwordLogRepository.Save(new PasswordLog(code, PasswordEventLog.RequestRecovery, appSto.Store.Code));

                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                    }
                }
            }

            return(_tokenCode);
        }
Ejemplo n.º 3
0
        public Account Authenticate(string login, string password, ApplicationStore applicationStore, out bool requirePasswordChange, string urlBack = "")
        {
            requirePasswordChange = false;
            Account account = null;

            using (var transaction = Connection.BeginTransaction())
            {
                var accounts = accountRepository.Get(login, applicationStore, true);

                if (accounts.Count() > 0)
                {
                    var matchedAccounts = accountService.MatchPassword(accounts.ToList(), password);

                    if (matchedAccounts.IsNull() || matchedAccounts.Count() == 0)
                    {
                        if (applicationStore.Application.MemberType == MemberType.Consumer)
                        {
                            if (accounts.Any(a => !a.Customer.IsNull() && a.Customer.Code.IsEmpty()))
                            {
                                accounts.ForEach(a => a.WrongLoginAttempt(lockedUpMemberPolicy, lockMemberPolicy));
                            }
                        }
                        else
                        {
                            var _accounts = accounts.Where(a =>
                                                           a.AccountRoles.Any(ar =>
                                                                              ar.Role.Status == true &&
                                                                              ar.Role.Store.Status == true &&
                                                                              ar.Role.StoreCode == applicationStore.StoreCode &&
                                                                              ar.Role.Permissions.Any(p =>
                                                                                                      p.Status == true &&
                                                                                                      p.Resource.Application.Status == true &&
                                                                                                      p.Resource.ApplicationCode == applicationStore.ApplicationCode)));

                            if (!_accounts.IsNull() && _accounts.Count() > 0)
                            {
                                _accounts.ForEach(a => a.WrongLoginAttempt(lockedUpMemberPolicy, lockMemberPolicy));
                            }
                            else
                            {
                                accounts.ForEach(a => a.WrongLoginAttempt(lockedUpMemberPolicy, lockMemberPolicy));
                            }
                        }

                        transaction.Commit();

                        throw new ArgumentException("User and password not found");
                    }
                    else
                    {
                        accountService.lockedUpMemberPolicy = lockedUpMemberPolicy;
                        accountService.lockMemberPolicy     = lockMemberPolicy;
                        accountService.passwordPolicy       = passwordPolicy;

                        account = accountService.Authenticate(accounts, applicationStore);

                        if (passwordPolicy != null)
                        {
                            requirePasswordChange = !passwordPolicy.Validate(account.Email, password, false);
                        }

                        transaction.Commit();
                        return(account);
                    }
                }
                else
                {
                    if (applicationStore.Application.MemberType == MemberType.Consumer)
                    {
                        var customerImport = customerImportService.Get(login, applicationStore.Store.Code);

                        if (customerImport != null)
                        {
                            var token = resetPasswordTokenService.GenerateResetPasswordToken(customerImport, applicationStore, urlBack);

                            var _tokenCode = token.Code.EncodeURIComponent();

                            customerImport.HandleCustomer();
                            svcEmail.SendPasswordRecoveryEmailAsync(customerImport, applicationStore.Store, _tokenCode, urlBack);

                            transaction.Commit();

                            using (var transactionLog = Connection.BeginTransaction())
                            {
                                try
                                {
                                    passwordLogRepository.Save(new PasswordLog(customerImport.AccountCode, PasswordEventLog.ResquetRecoryCustomerImport, applicationStore.Store.Code));
                                    transactionLog.Commit();
                                }
                                catch
                                {
                                    transactionLog.Rollback();
                                }
                            }

                            throw new ArgumentException("create_password_is_needed");
                        }
                    }
                }
            }

            return(account);
        }