Commit 43d6e21f authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '18-required-userservice-methods' into 'master'

Resolve "Required userService methods"

Closes #18

See merge request genesys-pgr/application-blocks!17
parents d0dbc1bd eb20bd20
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package org.genesys.blocks.security.service; package org.genesys.blocks.security.service;
import java.util.List;
import java.util.Set; import java.util.Set;
import org.genesys.blocks.security.NoUserFoundException; import org.genesys.blocks.security.NoUserFoundException;
...@@ -28,24 +29,24 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException; ...@@ -28,24 +29,24 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
public interface BasicUserService<R extends GrantedAuthority, T extends BasicUser<R>> extends UserDetailsService { public interface BasicUserService<R extends GrantedAuthority, T extends BasicUser<R>> extends UserDetailsService {
/** /**
* Get User by id * Get User by id
* *
* @param id * @param id
* @return the user or <code>null</code> * @return the user or <code>null</code>
*/ */
T getUser(long id) throws NoUserFoundException; T getUser(long id) throws NoUserFoundException;
/** /**
* Get User by email * Get User by email
* *
* @param email * @param email
* @return the user * @return the user
*/ */
T getUserByEmail(String email) throws UsernameNotFoundException; T getUserByEmail(String email) throws UsernameNotFoundException;
/** /**
* Create a new user account * Create a new user account with {@link #getDefaultUserRole()} assigned.
* *
* @param email unique email address * @param email unique email address
* @param fullName Full name * @param fullName Full name
...@@ -56,53 +57,57 @@ public interface BasicUserService<R extends GrantedAuthority, T extends BasicUse ...@@ -56,53 +57,57 @@ public interface BasicUserService<R extends GrantedAuthority, T extends BasicUse
* @throws UserException any other exception * @throws UserException any other exception
* @return the new user * @return the new user
*/ */
T createUser(String email, String fullName, String password, BasicUser.AccountType accountType) throws NotUniqueUserException, PasswordPolicyException, UserException; T createUser(String email, String fullName, String password, BasicUser.AccountType accountType) throws NotUniqueUserException, PasswordPolicyException, UserException;
/** /**
* Grant specified roles to user. The {@link UserRole#USER} will be added if missing. * Grant specified roles to user. The {@link #getDefaultUserRole()} will be
* * added if missing.
* @param user *
* @param roles * @param user
* @return the updated user * @param roles
*/ * @return the updated user
T setRoles(T user, Set<R> roles); */
T setRoles(T user, Set<R> roles);
/** /**
* Update user information * Update user information
* *
* @param user the user * @param user the user
* @param email new email address * @param email new email address
* @param fullName new fullName * @param fullName new fullName
* @return * @return
*/ */
T updateUser(T user, String email, String fullName) throws NotUniqueUserException; T updateUser(T user, String email, String fullName) throws NotUniqueUserException;
/** /**
* Change password * Change password
* *
* @param user the user * @param user the user
* @param password new password * @param password new password
* @throws PasswordPolicyException * @throws PasswordPolicyException
* @return * @return
*/ */
T changePassword(T user, String password) throws PasswordPolicyException; T changePassword(T user, String password) throws PasswordPolicyException;
/** /**
* Try to delete user. * Try to delete user.
* *
* @param user user to delete * @param user user to delete
*/ */
void deleteUser(T user); void deleteUser(T user);
/** /**
* Lock user account * Lock user account
* *
* @param userID User ID * @param userID User ID
* @param locked Is account locked * @param locked Is account locked
* @throws NoUserFoundException * @throws NoUserFoundException
*/ */
void setAccountLockLocal(long userId, boolean locked) throws NoUserFoundException; void setAccountLockLocal(long userId, boolean locked) throws NoUserFoundException;
void setAccountLock(long userId, boolean locked) throws NoUserFoundException; void setAccountLock(long userId, boolean locked) throws NoUserFoundException;
}
List<R> listAvailableRoles();
R getDefaultUserRole();
}
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.genesys.blocks.security.service.impl; package org.genesys.blocks.security.service.impl;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Set; import java.util.Set;
import org.genesys.blocks.security.NoUserFoundException; import org.genesys.blocks.security.NoUserFoundException;
...@@ -58,6 +59,12 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -58,6 +59,12 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
public void setAccountLockoutTime(long accountLockoutTime) { public void setAccountLockoutTime(long accountLockoutTime) {
this.accountLockoutTime = accountLockoutTime; this.accountLockoutTime = accountLockoutTime;
} }
@Override
public abstract R getDefaultUserRole();
@Override
public abstract List<R> listAvailableRoles();
@Override @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
...@@ -101,6 +108,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends ...@@ -101,6 +108,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
user.getRoles().clear(); user.getRoles().clear();
user.getRoles().addAll(newRoles); user.getRoles().addAll(newRoles);
user.getRoles().add(getDefaultUserRole());
LOG.info("Setting roles for user {} to {}", user.getEmail(), user.getRoles()); LOG.info("Setting roles for user {} to {}", user.getEmail(), user.getRoles());
return userRepository.save(user); return userRepository.save(user);
} }
......
...@@ -16,6 +16,10 @@ ...@@ -16,6 +16,10 @@
package org.genesys.blocks.security.config; package org.genesys.blocks.security.config;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.genesys.blocks.oauth.service.OAuthServiceImpl; import org.genesys.blocks.oauth.service.OAuthServiceImpl;
import org.genesys.blocks.security.NotUniqueUserException; import org.genesys.blocks.security.NotUniqueUserException;
...@@ -49,41 +53,41 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -49,41 +53,41 @@ import org.springframework.transaction.annotation.Transactional;
@ComponentScan(basePackages = { "org.genesys.blocks.oauth.service", "org.genesys.blocks.security.service", "org.genesys.blocks.security.component" }) @ComponentScan(basePackages = { "org.genesys.blocks.oauth.service", "org.genesys.blocks.security.service", "org.genesys.blocks.security.component" })
public class ApplicationConfig { public class ApplicationConfig {
@Bean @Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() { public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
propertyPlaceholderConfigurer.setIgnoreResourceNotFound(true);
propertyPlaceholderConfigurer.setFileEncoding("utf-8");
propertyPlaceholderConfigurer.setLocations(new ClassPathResource("application.properties"));
return propertyPlaceholderConfigurer;
}
final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer(); @Bean
propertyPlaceholderConfigurer.setIgnoreResourceNotFound(true); public CacheManager cacheManager() {
propertyPlaceholderConfigurer.setFileEncoding("utf-8"); SimpleCacheManager cacheManager = new SimpleCacheManager();
propertyPlaceholderConfigurer.setLocations(new ClassPathResource("application.properties")); cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
return cacheManager;
}
return propertyPlaceholderConfigurer; @Bean
} public OAuthServiceImpl oauthService() {
return new OAuthServiceImpl();
}
@Bean @Bean
public CacheManager cacheManager() { public BasicUserService<UserRole, TestUser> testUserService() {
SimpleCacheManager cacheManager = new SimpleCacheManager(); return new BasicUserServiceImpl<UserRole, TestUser>() {
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
return cacheManager;
}
@Bean @Autowired
public OAuthServiceImpl oauthService() { private TestUserPersistence testUserRepository;
return new OAuthServiceImpl();
}
@Bean
public BasicUserService<UserRole, TestUser> testUserService() {
return new BasicUserServiceImpl<UserRole, TestUser>() {
@Autowired @Override
private TestUserPersistence testUserRepository;
@Override
public TestUser getUserByEmail(String email) throws UsernameNotFoundException { public TestUser getUserByEmail(String email) throws UsernameNotFoundException {
return testUserRepository.findByEmail(email); return testUserRepository.findByEmail(email);
} }
@Override @Override
@Transactional @Transactional
public TestUser createUser(String email, String fullName, String password, AccountType accountType) throws NotUniqueUserException, PasswordPolicyException { public TestUser createUser(String email, String fullName, String password, AccountType accountType) throws NotUniqueUserException, PasswordPolicyException {
...@@ -91,9 +95,20 @@ public class ApplicationConfig { ...@@ -91,9 +95,20 @@ public class ApplicationConfig {
user.setEmail(email); user.setEmail(email);
user.setFullName(fullName); user.setFullName(fullName);
user.setAccountType(accountType); user.setAccountType(accountType);
user.setRoles(Sets.newHashSet(getDefaultUserRole()));
setPassword(user, password); setPassword(user, password);
return testUserRepository.save(user); return testUserRepository.save(user);
} }
@Override
public UserRole getDefaultUserRole() {
return UserRole.USER;
}
@Override
public List<UserRole> listAvailableRoles() {
return Lists.asList(UserRole.ADMINISTRATOR, UserRole.USER, null);
}
}; };
} }
} }
...@@ -19,7 +19,7 @@ package org.genesys.blocks.security.model; ...@@ -19,7 +19,7 @@ package org.genesys.blocks.security.model;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
public enum UserRole implements GrantedAuthority { public enum UserRole implements GrantedAuthority {
USER("User"), ADMINISTRATOR("Administrator"); USER("User"), ADMINISTRATOR("Administrator"), EXTRAROLE("Extra");
String label; String label;
......
...@@ -18,9 +18,13 @@ package org.genesys.blocks.security.tests; ...@@ -18,9 +18,13 @@ package org.genesys.blocks.security.tests;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import com.google.common.collect.Sets;
import org.genesys.blocks.security.NotUniqueUserException;
import org.genesys.blocks.security.UserException; import org.genesys.blocks.security.UserException;
import org.genesys.blocks.security.model.BasicUser.AccountType; import org.genesys.blocks.security.model.BasicUser.AccountType;
import org.genesys.blocks.security.model.TestUser; import org.genesys.blocks.security.model.TestUser;
import org.genesys.blocks.security.model.UserRole;
import org.genesys.blocks.security.service.PasswordPolicy.PasswordPolicyException; import org.genesys.blocks.security.service.PasswordPolicy.PasswordPolicyException;
import org.junit.Test; import org.junit.Test;
...@@ -50,4 +54,24 @@ public class BasicUserServiceTest extends ServiceTest { ...@@ -50,4 +54,24 @@ public class BasicUserServiceTest extends ServiceTest {
testUserService.changePassword(user, "newPassword"); testUserService.changePassword(user, "newPassword");
} }
@Test
public void testDefaultUserRole() {
assertThat(testUserService.getDefaultUserRole(), not(nullValue()));
assertThat(testUserService.getDefaultUserRole(), is(UserRole.USER));
}
@Test
public void testDefaultRoleAlwaysAssigned() throws NotUniqueUserException, PasswordPolicyException, UserException {
TestUser user = testUserService.createUser(USER_EMAIL, USER_FULLNAME, "password", AccountType.SYSTEM);
assertThat("Default user role was not assigned automatically", user.getRoles(), contains(UserRole.USER));
user = testUserService.setRoles(user, Sets.newHashSet(UserRole.USER));
assertThat("Default user role was not assigned automatically", user.getRoles(), contains(UserRole.USER));
user = testUserService.setRoles(user, Sets.newHashSet(UserRole.EXTRAROLE));
assertThat("Default user role was not assigned automatically", user.getRoles(), containsInAnyOrder(UserRole.USER, UserRole.EXTRAROLE));
user = testUserService.setRoles(user, Sets.newHashSet(UserRole.ADMINISTRATOR));
assertThat("Default user role was not assigned automatically", user.getRoles(), containsInAnyOrder(UserRole.USER, UserRole.ADMINISTRATOR));
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment