Search in sources :

Example 1 with CustomerPassword

use of com.salesmanager.shop.model.customer.CustomerPassword in project shopizer by shopizer-ecommerce.

the class CustomerAccountController method displayCustomerChangePassword.

@PreAuthorize("hasRole('AUTH_CUSTOMER')")
@RequestMapping(value = "/password.html", method = RequestMethod.GET)
public String displayCustomerChangePassword(Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    MerchantStore store = getSessionAttribute(Constants.MERCHANT_STORE, request);
    CustomerPassword customerPassword = new CustomerPassword();
    model.addAttribute("password", customerPassword);
    /**
     * template *
     */
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.changePassword).append(".").append(store.getStoreTemplate());
    return template.toString();
}
Also used : CustomerPassword(com.salesmanager.shop.model.customer.CustomerPassword) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with CustomerPassword

use of com.salesmanager.shop.model.customer.CustomerPassword in project shopizer by shopizer-ecommerce.

the class CustomerAccountController method changePassword.

@PreAuthorize("hasRole('AUTH_CUSTOMER')")
@RequestMapping(value = "/changePassword.html", method = RequestMethod.POST)
public String changePassword(@Valid @ModelAttribute(value = "password") CustomerPassword password, BindingResult bindingResult, Model model, HttpServletRequest request, HttpServletResponse response, Locale locale) throws Exception {
    MerchantStore store = getSessionAttribute(Constants.MERCHANT_STORE, request);
    /**
     * template *
     */
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.changePassword).append(".").append(store.getStoreTemplate());
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Customer customer = null;
    if (auth != null && request.isUserInRole("AUTH_CUSTOMER")) {
        customer = customerFacade.getCustomerByUserName(auth.getName(), store);
    }
    if (customer == null) {
        return "redirect:/" + Constants.SHOP_URI;
    }
    String currentPassword = password.getCurrentPassword();
    BCryptPasswordEncoder encoder = (BCryptPasswordEncoder) passwordEncoder;
    if (!encoder.matches(currentPassword, customer.getPassword())) {
        FieldError error = new FieldError("password", "password", messages.getMessage("message.invalidpassword", locale));
        bindingResult.addError(error);
    }
    if (bindingResult.hasErrors()) {
        LOGGER.info("found {} validation error while validating customer password", bindingResult.getErrorCount());
        return template.toString();
    }
    CustomerPassword customerPassword = new CustomerPassword();
    model.addAttribute("password", customerPassword);
    String newPassword = password.getPassword();
    String encodedPassword = passwordEncoder.encode(newPassword);
    customer.setPassword(encodedPassword);
    customerService.saveOrUpdate(customer);
    emailTemplatesUtils.changePasswordNotificationEmail(customer, store, LocaleUtils.getLocale(customer.getDefaultLanguage()), request.getContextPath());
    model.addAttribute("success", "success");
    return template.toString();
}
Also used : ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Customer(com.salesmanager.core.model.customer.Customer) Authentication(org.springframework.security.core.Authentication) FieldError(org.springframework.validation.FieldError) CustomerPassword(com.salesmanager.shop.model.customer.CustomerPassword) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)2 CustomerPassword (com.salesmanager.shop.model.customer.CustomerPassword)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Customer (com.salesmanager.core.model.customer.Customer)1 ReadableCustomer (com.salesmanager.shop.model.customer.ReadableCustomer)1 Authentication (org.springframework.security.core.Authentication)1 BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)1 FieldError (org.springframework.validation.FieldError)1