Search in sources :

Example 1 with PersistableGroup

use of com.salesmanager.shop.model.security.PersistableGroup in project shopizer by shopizer-ecommerce.

the class PersistableUserPopulator method populate.

@Override
public User populate(PersistableUser source, User target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(source, "PersistableUser cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    if (target == null) {
        target = new User();
    }
    target.setFirstName(source.getFirstName());
    target.setLastName(source.getLastName());
    target.setAdminEmail(source.getEmailAddress());
    target.setAdminName(source.getUserName());
    if (!StringUtils.isBlank(source.getPassword())) {
        target.setAdminPassword(passwordEncoder.encode(source.getPassword()));
    }
    if (!StringUtils.isBlank(source.getStore())) {
        try {
            MerchantStore userStore = merchantStoreService.getByCode(source.getStore());
            target.setMerchantStore(userStore);
        } catch (ServiceException e) {
            throw new ConversionException("Error while reading MerchantStore store [" + source.getStore() + "]", e);
        }
    } else {
        target.setMerchantStore(store);
    }
    target.setActive(source.isActive());
    Language lang = null;
    try {
        lang = languageService.getByCode(source.getDefaultLanguage());
    } catch (Exception e) {
        throw new ConversionException("Cannot get language [" + source.getDefaultLanguage() + "]", e);
    }
    // set default language
    target.setDefaultLanguage(lang);
    List<Group> userGroups = new ArrayList<Group>();
    List<String> names = new ArrayList<String>();
    for (PersistableGroup group : source.getGroups()) {
        names.add(group.getName());
    }
    try {
        List<Group> groups = groupService.listGroupByNames(names);
        for (Group g : groups) {
            userGroups.add(g);
        }
    } catch (Exception e1) {
        throw new ConversionException("Error while getting user groups", e1);
    }
    target.setGroups(userGroups);
    return target;
}
Also used : ConversionException(com.salesmanager.core.business.exception.ConversionException) Group(com.salesmanager.core.model.user.Group) PersistableGroup(com.salesmanager.shop.model.security.PersistableGroup) PersistableGroup(com.salesmanager.shop.model.security.PersistableGroup) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) User(com.salesmanager.core.model.user.User) ServiceException(com.salesmanager.core.business.exception.ServiceException) Language(com.salesmanager.core.model.reference.language.Language) ArrayList(java.util.ArrayList) MerchantStore(com.salesmanager.core.model.merchant.MerchantStore) ServiceException(com.salesmanager.core.business.exception.ServiceException) ConversionException(com.salesmanager.core.business.exception.ConversionException)

Example 2 with PersistableGroup

use of com.salesmanager.shop.model.security.PersistableGroup in project shopizer by shopizer-ecommerce.

the class UserFacadeImpl method authorizedGroups.

@Override
public void authorizedGroups(String authenticatedUser, PersistableUser user) {
    Validate.notNull(authenticatedUser, "Required authenticated user");
    Validate.notNull(user, "Required persistable user");
    try {
        User currentUser = userService.getByUserName(authenticatedUser);
        boolean isSuperAdmin = false;
        for (Group g : currentUser.getGroups()) {
            if (g.getGroupName().equals("SUPERADMIN")) {
                isSuperAdmin = true;
                break;
            }
        }
        for (PersistableGroup g : user.getGroups()) {
            if (g.getName().equals("SUPERADMIN")) {
                if (!isSuperAdmin) {
                    throw new UnauthorizedException("Superadmin group not allowed");
                }
            }
        }
    } catch (ServiceException e) {
        throw new ServiceRuntimeException("Error while looking for authorization", e);
    }
}
Also used : ReadableGroup(com.salesmanager.shop.model.security.ReadableGroup) PersistableGroup(com.salesmanager.shop.model.security.PersistableGroup) Group(com.salesmanager.core.model.user.Group) PersistableGroup(com.salesmanager.shop.model.security.PersistableGroup) ReadableUser(com.salesmanager.shop.model.user.ReadableUser) User(com.salesmanager.core.model.user.User) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) ServiceException(com.salesmanager.core.business.exception.ServiceException) UnauthorizedException(com.salesmanager.shop.store.api.exception.UnauthorizedException) ServiceRuntimeException(com.salesmanager.shop.store.api.exception.ServiceRuntimeException)

Example 3 with PersistableGroup

use of com.salesmanager.shop.model.security.PersistableGroup in project shopizer by shopizer-ecommerce.

the class UserApiIntegrationTest method createUserChangePassword.

@Test
public void createUserChangePassword() throws Exception {
    PersistableUser newUser = new PersistableUser();
    newUser.setDefaultLanguage("en");
    newUser.setEmailAddress("test@test.com");
    newUser.setFirstName("Test");
    newUser.setLastName("User");
    newUser.setUserName("test@test.com");
    newUser.setPassword(CREATED_PASSWORD);
    newUser.setRepeatPassword(CREATED_PASSWORD);
    PersistableGroup g = new PersistableGroup();
    g.setName("ADMIN");
    newUser.getGroups().add(g);
    final HttpEntity<PersistableUser> persistableUser = new HttpEntity<PersistableUser>(newUser, getHeader());
    ReadableUser user = null;
    final ResponseEntity<ReadableUser> response = testRestTemplate.exchange(String.format("/api/v1/private/user/"), HttpMethod.POST, persistableUser, ReadableUser.class);
    if (response.getStatusCode() != HttpStatus.OK) {
        throw new Exception(response.toString());
    } else {
        user = response.getBody();
        assertNotNull(user);
    }
    String oldPassword = CREATED_PASSWORD;
    String newPassword = NEW_CREATED_PASSWORD;
    String repeatPassword = newPassword;
    UserPassword userPassword = new UserPassword();
    userPassword.setPassword(oldPassword);
    userPassword.setChangePassword(newPassword);
    final HttpEntity<UserPassword> changePasswordEntity = new HttpEntity<UserPassword>(userPassword, getHeader());
    final ResponseEntity<Void> changePassword = testRestTemplate.exchange(String.format("/api/v1/private/user/" + user.getId() + "/password"), HttpMethod.PATCH, changePasswordEntity, Void.class);
    if (changePassword.getStatusCode() != HttpStatus.OK) {
        throw new Exception(response.toString());
    } else {
        assertNotNull("Password changed");
    }
}
Also used : PersistableGroup(com.salesmanager.shop.model.security.PersistableGroup) ReadableUser(com.salesmanager.shop.model.user.ReadableUser) PersistableUser(com.salesmanager.shop.model.user.PersistableUser) HttpEntity(org.springframework.http.HttpEntity) UserPassword(com.salesmanager.shop.model.user.UserPassword) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

PersistableGroup (com.salesmanager.shop.model.security.PersistableGroup)3 PersistableUser (com.salesmanager.shop.model.user.PersistableUser)3 ServiceException (com.salesmanager.core.business.exception.ServiceException)2 Group (com.salesmanager.core.model.user.Group)2 User (com.salesmanager.core.model.user.User)2 ReadableUser (com.salesmanager.shop.model.user.ReadableUser)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 MerchantStore (com.salesmanager.core.model.merchant.MerchantStore)1 Language (com.salesmanager.core.model.reference.language.Language)1 ReadableGroup (com.salesmanager.shop.model.security.ReadableGroup)1 UserPassword (com.salesmanager.shop.model.user.UserPassword)1 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)1 UnauthorizedException (com.salesmanager.shop.store.api.exception.UnauthorizedException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 HttpEntity (org.springframework.http.HttpEntity)1