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;
}
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);
}
}
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");
}
}
Aggregations