Search in sources :

Example 41 with Customer

use of org.broadleafcommerce.profile.core.domain.Customer in project BroadleafCommerce by BroadleafCommerce.

the class CustomerServiceImpl method sendForgotPasswordNotification.

@Override
@Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER)
public GenericResponse sendForgotPasswordNotification(String username, String resetPasswordUrl) {
    GenericResponse response = new GenericResponse();
    Customer customer = null;
    if (username != null) {
        customer = customerDao.readCustomerByUsername(username);
    }
    checkCustomer(customer, response);
    if (!response.getHasErrors()) {
        String token = PasswordUtils.generateSecurePassword(getPasswordTokenLength());
        token = token.toLowerCase();
        Object salt = getSalt(customer, token);
        String saltString = null;
        if (salt != null) {
            saltString = Hex.encodeHexString(salt.toString().getBytes());
        }
        CustomerForgotPasswordSecurityToken fpst = new CustomerForgotPasswordSecurityTokenImpl();
        fpst.setCustomerId(customer.getId());
        fpst.setToken(encodePass(token, saltString));
        fpst.setCreateDate(SystemTime.asDate());
        customerForgotPasswordSecurityTokenDao.saveToken(fpst);
        if (usingDeprecatedPasswordEncoder() && saltString != null) {
            token = token + '-' + saltString;
        }
        HashMap<String, Object> vars = new HashMap<String, Object>();
        vars.put("token", token);
        if (!StringUtils.isEmpty(resetPasswordUrl)) {
            if (resetPasswordUrl.contains("?")) {
                resetPasswordUrl = resetPasswordUrl + "&token=" + token;
            } else {
                resetPasswordUrl = resetPasswordUrl + "?token=" + token;
            }
        }
        vars.put("resetPasswordUrl", resetPasswordUrl);
        sendEmail(customer.getEmailAddress(), getForgotPasswordEmailInfo(), vars);
    }
    return response;
}
Also used : CustomerForgotPasswordSecurityToken(org.broadleafcommerce.profile.core.domain.CustomerForgotPasswordSecurityToken) GenericResponse(org.broadleafcommerce.common.service.GenericResponse) Customer(org.broadleafcommerce.profile.core.domain.Customer) HashMap(java.util.HashMap) CustomerForgotPasswordSecurityTokenImpl(org.broadleafcommerce.profile.core.domain.CustomerForgotPasswordSecurityTokenImpl) Transactional(org.springframework.transaction.annotation.Transactional)

Example 42 with Customer

use of org.broadleafcommerce.profile.core.domain.Customer in project BroadleafCommerce by BroadleafCommerce.

the class CustomerServiceImpl method registerCustomer.

@Override
@Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER)
public Customer registerCustomer(Customer customer, String password, String passwordConfirm) {
    customer.setRegistered(true);
    // When unencodedPassword is set the save() will encode it
    if (customer.getId() == null) {
        customer.setId(findNextCustomerId());
    }
    customer.setUnencodedPassword(password);
    Customer retCustomer = saveCustomer(customer);
    createRegisteredCustomerRoles(retCustomer);
    HashMap<String, Object> vars = new HashMap<String, Object>();
    vars.put("customer", retCustomer);
    sendEmail(customer.getEmailAddress(), getRegistrationEmailInfo(), vars);
    notifyPostRegisterListeners(retCustomer);
    return retCustomer;
}
Also used : Customer(org.broadleafcommerce.profile.core.domain.Customer) HashMap(java.util.HashMap) Transactional(org.springframework.transaction.annotation.Transactional)

Example 43 with Customer

use of org.broadleafcommerce.profile.core.domain.Customer in project BroadleafCommerce by BroadleafCommerce.

the class UserDetailsServiceImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    Customer customer = customerService.readCustomerByUsername(username, false);
    if (customer == null) {
        throw new UsernameNotFoundException("The customer was not found");
    }
    List<GrantedAuthority> grantedAuthorities = createGrantedAuthorities(roleService.findCustomerRolesByCustomerId(customer.getId()));
    return new CustomerUserDetails(customer.getId(), username, customer.getPassword(), !customer.isDeactivated(), true, !customer.isPasswordChangeRequired(), true, grantedAuthorities);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Customer(org.broadleafcommerce.profile.core.domain.Customer) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority)

Example 44 with Customer

use of org.broadleafcommerce.profile.core.domain.Customer in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafRegisterController method processRegister.

public String processRegister(RegisterCustomerForm registerCustomerForm, BindingResult errors, HttpServletRequest request, HttpServletResponse response, Model model) throws ServiceException, PricingException {
    if (useEmailForLogin) {
        Customer customer = registerCustomerForm.getCustomer();
        customer.setUsername(customer.getEmailAddress());
    }
    registerCustomerValidator.validate(registerCustomerForm, errors, useEmailForLogin);
    if (!errors.hasErrors()) {
        Customer newCustomer = customerService.registerCustomer(registerCustomerForm.getCustomer(), registerCustomerForm.getPassword(), registerCustomerForm.getPasswordConfirm());
        assert (newCustomer != null);
        // The next line needs to use the customer from the input form and not the customer returned after registration
        // so that we still have the unencoded password for use by the authentication mechanism.
        loginService.loginCustomer(registerCustomerForm.getCustomer());
        // Need to ensure that the Cart on CartState is owned by the newly registered customer.
        Order cart = CartState.getCart();
        if (cart != null && !(cart instanceof NullOrderImpl) && cart.getEmailAddress() == null) {
            cart.setEmailAddress(newCustomer.getEmailAddress());
            orderService.save(cart, false);
        }
        String redirectUrl = registerCustomerForm.getRedirectUrl();
        if (StringUtils.isNotBlank(redirectUrl) && redirectUrl.contains(":")) {
            redirectUrl = null;
        }
        return StringUtils.isBlank(redirectUrl) ? getRegisterSuccessView() : "redirect:" + redirectUrl;
    } else {
        return getRegisterView();
    }
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) Customer(org.broadleafcommerce.profile.core.domain.Customer) NullOrderImpl(org.broadleafcommerce.core.order.domain.NullOrderImpl)

Example 45 with Customer

use of org.broadleafcommerce.profile.core.domain.Customer in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafUpdateAccountController method processUpdateAccount.

public String processUpdateAccount(HttpServletRequest request, Model model, UpdateAccountForm form, BindingResult result, RedirectAttributes redirectAttributes) throws ServiceException {
    updateAccountValidator.validate(form, result);
    if (result.hasErrors()) {
        return getUpdateAccountView();
    }
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null || !auth.isAuthenticated()) {
        throw new AuthenticationCredentialsNotFoundException("Authentication was null, not authenticated, or not logged in.");
    }
    Customer customer = CustomerState.getCustomer();
    customer.setEmailAddress(form.getEmailAddress());
    customer.setFirstName(form.getFirstName());
    customer.setLastName(form.getLastName());
    if (useEmailForLogin) {
        customer.setUsername(form.getEmailAddress());
    }
    customer = customerService.saveCustomer(customer);
    if (useEmailForLogin) {
        UserDetails principal = userDetailsService.loadUserByUsername(customer.getUsername());
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(principal, principal.getPassword(), auth.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(token);
    }
    redirectAttributes.addFlashAttribute("successMessage", getAccountUpdatedMessage());
    return getAccountRedirectView();
}
Also used : AuthenticationCredentialsNotFoundException(org.springframework.security.authentication.AuthenticationCredentialsNotFoundException) UserDetails(org.springframework.security.core.userdetails.UserDetails) Customer(org.broadleafcommerce.profile.core.domain.Customer) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Aggregations

Customer (org.broadleafcommerce.profile.core.domain.Customer)98 Order (org.broadleafcommerce.core.order.domain.Order)41 Transactional (org.springframework.transaction.annotation.Transactional)34 Test (org.testng.annotations.Test)33 Address (org.broadleafcommerce.profile.core.domain.Address)14 Rollback (org.springframework.test.annotation.Rollback)11 HashMap (java.util.HashMap)9 CustomerAddress (org.broadleafcommerce.profile.core.domain.CustomerAddress)9 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)8 MergeCartResponse (org.broadleafcommerce.core.order.service.call.MergeCartResponse)6 ArrayList (java.util.ArrayList)5 Money (org.broadleafcommerce.common.money.Money)5 Category (org.broadleafcommerce.core.catalog.domain.Category)5 Product (org.broadleafcommerce.core.catalog.domain.Product)5 AddressImpl (org.broadleafcommerce.profile.core.domain.AddressImpl)5 CommonSetupBaseTest (org.broadleafcommerce.test.CommonSetupBaseTest)5 CustomerImpl (org.broadleafcommerce.profile.core.domain.CustomerImpl)4 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 ServiceException (org.broadleafcommerce.common.exception.ServiceException)3 ISOCountry (org.broadleafcommerce.common.i18n.domain.ISOCountry)3