Search in sources :

Example 1 with CredentialsReset

use of com.salesmanager.core.model.common.CredentialsReset in project shopizer by shopizer-ecommerce.

the class CustomerFacadeImpl method requestPasswordReset.

@Override
public void requestPasswordReset(String customerName, String customerContextPath, MerchantStore store, Language language) {
    try {
        // get customer by user name
        Customer customer = customerService.getByNick(customerName, store.getId());
        if (customer == null) {
            throw new ResourceNotFoundException("Customer [" + customerName + "] not found for store [" + store.getCode() + "]");
        }
        // generates unique token
        String token = UUID.randomUUID().toString();
        Date expiry = DateUtil.addDaysToCurrentDate(2);
        CredentialsReset credsRequest = new CredentialsReset();
        credsRequest.setCredentialsRequest(token);
        credsRequest.setCredentialsRequestExpiry(expiry);
        customer.setCredentialsResetRequest(credsRequest);
        customerService.saveOrUpdate(customer);
        // reset password link
        // this will build http | https ://domain/contextPath
        String baseUrl = filePathUtils.buildBaseUrl(customerContextPath, store);
        // need to add link to controller receiving user reset password
        // request
        String customerResetLink = new StringBuilder().append(baseUrl).append(String.format(resetCustomerLink, store.getCode(), token)).toString();
        resetPasswordRequest(customer, customerResetLink, store, lamguageService.toLocale(language, store));
    } catch (Exception e) {
        throw new ServiceRuntimeException("Error while executing resetPassword request", e);
    }
/**
 * User sends username (unique in the system)
 *
 * UserNameEntity will be the following { userName: "test@test.com" }
 *
 * The system retrieves user using userName (username is unique) if user
 * exists, system sends an email with reset password link
 *
 * How to retrieve a User from userName
 *
 * userFacade.findByUserName
 *
 * How to send an email
 *
 * How to generate a token
 *
 * Generate random token
 *
 * Calculate token expiration date
 *
 * Now + 48 hours
 *
 * Update User in the database with token
 *
 * Send reset token email
 */
}
Also used : Customer(com.salesmanager.core.model.customer.Customer) CredentialsReset(com.salesmanager.core.model.common.CredentialsReset) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Date(java.util.Date) ServiceException(com.salesmanager.core.business.exception.ServiceException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 2 with CredentialsReset

use of com.salesmanager.core.model.common.CredentialsReset in project shopizer by shopizer-ecommerce.

the class UserFacadeImpl method requestPasswordReset.

@Override
public void requestPasswordReset(String userName, String userContextPath, MerchantStore store, Language language) {
    Validate.notNull(userName, "Username cannot be empty");
    Validate.notNull(userContextPath, "Return url cannot be empty");
    try {
        // get user by user name
        User user = userService.getByUserName(userName, store.getCode());
        if (user == null) {
            throw new ResourceNotFoundException("User [" + userName + "] not found for store [" + store.getCode() + "]");
        }
        // generates unique token
        String token = UUID.randomUUID().toString();
        Date expiry = DateUtil.addDaysToCurrentDate(2);
        CredentialsReset credsRequest = new CredentialsReset();
        credsRequest.setCredentialsRequest(token);
        credsRequest.setCredentialsRequestExpiry(expiry);
        user.setCredentialsResetRequest(credsRequest);
        userService.saveOrUpdate(user);
        // reset password link
        // this will build http | https ://domain/contextPath
        String baseUrl = userContextPath;
        if (!filePathUtils.isValidURL(baseUrl)) {
            throw new ServiceRuntimeException("Request url [" + baseUrl + "] is invalid");
        }
        // need to add link to controller receiving user reset password
        // request
        String customerResetLink = new StringBuilder().append(baseUrl).append(Constants.SLASH).append(String.format(resetUserLink, store.getCode(), token)).toString();
        resetPasswordRequest(user, customerResetLink, store, lamguageService.toLocale(language, store));
    } catch (Exception e) {
        throw new ServiceRuntimeException("Error while executing resetPassword request", e);
    }
}
Also used : ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) CredentialsReset(com.salesmanager.core.model.common.CredentialsReset) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) Date(java.util.Date) ServiceException(com.salesmanager.core.business.exception.ServiceException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException) ConversionRuntimeException(com.salesmanager.shop.store.api.exception.ConversionRuntimeException) ResourceNotFoundException(com.salesmanager.shop.store.api.exception.ResourceNotFoundException) OperationNotAllowedException(com.salesmanager.shop.store.api.exception.OperationNotAllowedException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ConversionException(com.salesmanager.core.business.exception.ConversionException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Aggregations

ServiceException (com.salesmanager.core.business.exception.ServiceException)2 CredentialsReset (com.salesmanager.core.model.common.CredentialsReset)2 GenericRuntimeException (com.salesmanager.shop.store.api.exception.GenericRuntimeException)2 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)2 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)2 UnauthorizedException (com.salesmanager.shop.store.api.exception.UnauthorizedException)2 Date (java.util.Date)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 Customer (com.salesmanager.core.model.customer.Customer)1 User (com.salesmanager.core.model.user.User)1 PersistableUser (com.salesmanager.shop.model.user.PersistableUser)1 ReadableUser (com.salesmanager.shop.model.user.ReadableUser)1 ConversionRuntimeException (com.salesmanager.shop.store.api.exception.ConversionRuntimeException)1 OperationNotAllowedException (com.salesmanager.shop.store.api.exception.OperationNotAllowedException)1