Search in sources :

Example 66 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class CustomerAccountController method customerInformation.

@RequestMapping(value = "/accountSummary.json", method = RequestMethod.GET)
@ResponseBody
public ReadableCustomer customerInformation(@RequestParam String userName, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    MerchantStore store = getSessionAttribute(Constants.MERCHANT_STORE, request);
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Customer customer = null;
    if (auth != null && request.isUserInRole("AUTH_CUSTOMER")) {
        customer = customerFacade.getCustomerByUserName(auth.getName(), store);
    } else {
        response.sendError(401, "Customer not authenticated");
        return null;
    }
    if (StringUtils.isBlank(userName)) {
        response.sendError(403, "Customer name required");
        return null;
    }
    if (customer == null) {
        response.sendError(401, "Customer not authenticated");
        return null;
    }
    if (!customer.getNick().equals(userName)) {
        response.sendError(401, "Customer not authenticated");
        return null;
    }
    ReadableCustomer readableCustomer = new ReadableCustomer();
    Language lang = languageUtils.getRequestLanguage(request, response);
    ReadableCustomerPopulator readableCustomerPopulator = new ReadableCustomerPopulator();
    readableCustomerPopulator.populate(customer, readableCustomer, store, lang);
    return readableCustomer;
}
Also used : Language(com.salesmanager.core.model.reference.language.Language) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Customer(com.salesmanager.core.model.customer.Customer) Authentication(org.springframework.security.core.Authentication) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ReadableCustomerPopulator(com.salesmanager.shop.populator.customer.ReadableCustomerPopulator) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 67 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class CustomerAccountController method editAddress.

@PreAuthorize("hasRole('AUTH_CUSTOMER')")
@RequestMapping(value = "/editAddress.html", method = { RequestMethod.GET, RequestMethod.POST })
public String editAddress(final Model model, final HttpServletRequest request, @RequestParam(value = "billingAddress", required = false) Boolean billingAddress) throws Exception {
    MerchantStore store = getSessionAttribute(Constants.MERCHANT_STORE, request);
    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;
    }
    Address address = customerFacade.getAddress(customer.getId(), store, billingAddress);
    model.addAttribute("address", address);
    model.addAttribute("customerId", customer.getId());
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.EditAddress).append(".").append(store.getStoreTemplate());
    return template.toString();
}
Also used : Address(com.salesmanager.shop.model.customer.address.Address) ReadableCustomer(com.salesmanager.shop.model.customer.ReadableCustomer) Customer(com.salesmanager.core.model.customer.Customer) Authentication(org.springframework.security.core.Authentication) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 68 with Customer

use of com.salesmanager.core.model.customer.Customer 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)

Example 69 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class CustomerOrdersController method listOrders.

@PreAuthorize("hasRole('AUTH_CUSTOMER')")
@RequestMapping(value = "/orders.html", method = { RequestMethod.GET, RequestMethod.POST })
public String listOrders(Model model, @RequestParam(value = "page", defaultValue = "1") final int page, HttpServletRequest request, HttpServletResponse response) throws Exception {
    LOGGER.info("Fetching orders for current customer");
    MerchantStore store = getSessionAttribute(Constants.MERCHANT_STORE, request);
    Language language = getSessionAttribute(Constants.LANGUAGE, request);
    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;
    }
    PaginationData paginaionData = createPaginaionData(page, Constants.MAX_ORDERS_PAGE);
    ReadableOrderList readable = orderFacade.getReadableOrderList(store, customer, (paginaionData.getOffset() - 1), paginaionData.getPageSize(), language);
    model.addAttribute("customerOrders", readable);
    if (readable != null) {
        model.addAttribute("paginationData", calculatePaginaionData(paginaionData, Constants.MAX_ORDERS_PAGE, readable.getNumber()));
    } else {
        model.addAttribute("paginationData", null);
    }
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.customerOrders).append(".").append(store.getStoreTemplate());
    return template.toString();
}
Also used : ReadableOrderList(com.salesmanager.shop.model.order.v0.ReadableOrderList) PaginationData(com.salesmanager.shop.store.model.paging.PaginationData) Language(com.salesmanager.core.model.reference.language.Language) Customer(com.salesmanager.core.model.customer.Customer) Authentication(org.springframework.security.core.Authentication) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 70 with Customer

use of com.salesmanager.core.model.customer.Customer in project shopizer by shopizer-ecommerce.

the class CustomerRegistrationController method registerCustomer.

@RequestMapping(value = "/register.html", method = RequestMethod.POST)
public String registerCustomer(@Valid @ModelAttribute("customer") SecuredShopPersistableCustomer customer, BindingResult bindingResult, Model model, HttpServletRequest request, HttpServletResponse response, final Locale locale) throws Exception {
    MerchantStore merchantStore = (MerchantStore) request.getAttribute(Constants.MERCHANT_STORE);
    Language language = super.getLanguage(request);
    String userName = null;
    String password = null;
    model.addAttribute("recapatcha_public_key", siteKeyKey);
    if (!StringUtils.isBlank(request.getParameter("g-recaptcha-response"))) {
        boolean validateCaptcha = captchaRequestUtils.checkCaptcha(request.getParameter("g-recaptcha-response"));
        if (!validateCaptcha) {
            LOGGER.debug("Captcha response does not matched");
            FieldError error = new FieldError("captchaChallengeField", "captchaChallengeField", messages.getMessage("validaion.recaptcha.not.matched", locale));
            bindingResult.addError(error);
        }
    }
    if (StringUtils.isNotBlank(customer.getUserName())) {
        if (customerFacade.checkIfUserExists(customer.getUserName(), merchantStore)) {
            LOGGER.debug("Customer with username {} already exists for this store ", customer.getUserName());
            FieldError error = new FieldError("userName", "userName", messages.getMessage("registration.username.already.exists", locale));
            bindingResult.addError(error);
        }
        userName = customer.getUserName();
    }
    if (StringUtils.isNotBlank(customer.getPassword()) && StringUtils.isNotBlank(customer.getCheckPassword())) {
        if (!customer.getPassword().equals(customer.getCheckPassword())) {
            FieldError error = new FieldError("password", "password", messages.getMessage("message.password.checkpassword.identical", locale));
            bindingResult.addError(error);
        }
        password = customer.getPassword();
    }
    if (bindingResult.hasErrors()) {
        LOGGER.debug("found {} validation error while validating in customer registration ", bindingResult.getErrorCount());
        StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.register).append(".").append(merchantStore.getStoreTemplate());
        return template.toString();
    }
    @SuppressWarnings("unused") CustomerEntity customerData = null;
    try {
        // set user clear password
        customer.setPassword(password);
        customerData = customerFacade.registerCustomer(customer, merchantStore, language);
    } catch (Exception e) {
        LOGGER.error("Error while registering customer.. ", e);
        ObjectError error = new ObjectError("registration", messages.getMessage("registration.failed", locale));
        bindingResult.addError(error);
        StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.register).append(".").append(merchantStore.getStoreTemplate());
        return template.toString();
    }
    try {
        /**
         * Send registration email
         */
        emailTemplatesUtils.sendRegistrationEmail(customer, merchantStore, locale, request.getContextPath());
    } catch (Exception e) {
        LOGGER.error("Cannot send email to customer ", e);
    }
    try {
        // refresh customer
        Customer c = customerFacade.getCustomerByUserName(customer.getUserName(), merchantStore);
        // authenticate
        customerFacade.authenticate(c, userName, password);
        super.setSessionAttribute(Constants.CUSTOMER, c, request);
        StringBuilder cookieValue = new StringBuilder();
        cookieValue.append(merchantStore.getCode()).append("_").append(c.getNick());
        // set username in the cookie
        Cookie cookie = new Cookie(Constants.COOKIE_NAME_USER, cookieValue.toString());
        cookie.setMaxAge(60 * 24 * 3600);
        cookie.setPath(Constants.SLASH);
        response.addCookie(cookie);
        String sessionShoppingCartCode = (String) request.getSession().getAttribute(Constants.SHOPPING_CART);
        if (!StringUtils.isBlank(sessionShoppingCartCode)) {
            ShoppingCart shoppingCart = customerFacade.mergeCart(c, sessionShoppingCartCode, merchantStore, language);
            ShoppingCartData shoppingCartData = this.populateShoppingCartData(shoppingCart, merchantStore, language);
            if (shoppingCartData != null) {
                request.getSession().setAttribute(Constants.SHOPPING_CART, shoppingCartData.getCode());
            }
            // set username in the cookie
            Cookie c1 = new Cookie(Constants.COOKIE_NAME_CART, shoppingCartData.getCode());
            c1.setMaxAge(60 * 24 * 3600);
            c1.setPath(Constants.SLASH);
            response.addCookie(c1);
        }
        return "redirect:/shop/customer/dashboard.html";
    } catch (Exception e) {
        LOGGER.error("Cannot authenticate user ", e);
        ObjectError error = new ObjectError("registration", messages.getMessage("registration.failed", locale));
        bindingResult.addError(error);
    }
    StringBuilder template = new StringBuilder().append(ControllerConstants.Tiles.Customer.register).append(".").append(merchantStore.getStoreTemplate());
    return template.toString();
}
Also used : Cookie(javax.servlet.http.Cookie) AnonymousCustomer(com.salesmanager.shop.model.customer.AnonymousCustomer) SecuredShopPersistableCustomer(com.salesmanager.shop.model.customer.SecuredShopPersistableCustomer) Customer(com.salesmanager.core.model.customer.Customer) FieldError(org.springframework.validation.FieldError) ShoppingCartData(com.salesmanager.shop.model.shoppingcart.ShoppingCartData) ServiceException(com.salesmanager.core.business.exception.ServiceException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ObjectError(org.springframework.validation.ObjectError) Language(com.salesmanager.core.model.reference.language.Language) ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) CustomerEntity(com.salesmanager.shop.model.customer.CustomerEntity) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Customer (com.salesmanager.core.model.customer.Customer)71 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)33 ReadableCustomer (com.salesmanager.shop.model.customer.ReadableCustomer)32 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)31 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)30 Language (com.salesmanager.core.model.reference.language.Language)26 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)17 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)17 ConversionException (com.salesmanager.core.business.exception.ConversionException)16 ServiceException (com.salesmanager.core.business.exception.ServiceException)16 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)16 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)12 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)12 Authentication (org.springframework.security.core.Authentication)12 Date (java.util.Date)11 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)10 ArrayList (java.util.ArrayList)10 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)10 Product (com.salesmanager.core.model.catalog.product.Product)9 Country (com.salesmanager.core.model.reference.country.Country)9