Commit eb20bd20 authored by Matija Obreza's avatar Matija Obreza
Browse files

BasicUserService exposes #listAvailableRoles and #getDefaultUserRole methods

parent d0dbc1bd
......@@ -15,6 +15,7 @@
*/
package org.genesys.blocks.security.service;
import java.util.List;
import java.util.Set;
import org.genesys.blocks.security.NoUserFoundException;
......@@ -28,24 +29,24 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
public interface BasicUserService<R extends GrantedAuthority, T extends BasicUser<R>> extends UserDetailsService {
/**
* Get User by id
*
* @param id
* @return the user or <code>null</code>
*/
T getUser(long id) throws NoUserFoundException;
/**
* Get User by id
*
* @param id
* @return the user or <code>null</code>
*/
T getUser(long id) throws NoUserFoundException;
/**
* Get User by email
*
* @param email
* @return the user
*/
T getUserByEmail(String email) throws UsernameNotFoundException;
/**
* Get User by email
*
* @param email
* @return the user
*/
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 fullName Full name
......@@ -56,53 +57,57 @@ public interface BasicUserService<R extends GrantedAuthority, T extends BasicUse
* @throws UserException any other exception
* @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.
*
* @param user
* @param roles
* @return the updated user
*/
T setRoles(T user, Set<R> roles);
/**
* Grant specified roles to user. The {@link #getDefaultUserRole()} will be
* added if missing.
*
* @param user
* @param roles
* @return the updated user
*/
T setRoles(T user, Set<R> roles);
/**
* Update user information
*
* @param user the user
* @param email new email address
* @param fullName new fullName
* @return
*/
T updateUser(T user, String email, String fullName) throws NotUniqueUserException;
/**
* Update user information
*
* @param user the user
* @param email new email address
* @param fullName new fullName
* @return
*/
T updateUser(T user, String email, String fullName) throws NotUniqueUserException;
/**
* Change password
*
* @param user the user
* @param password new password
* @throws PasswordPolicyException
* @return
*/
T changePassword(T user, String password) throws PasswordPolicyException;
/**
* Change password
*
* @param user the user
* @param password new password
* @throws PasswordPolicyException
* @return
*/
T changePassword(T user, String password) throws PasswordPolicyException;
/**
* Try to delete user.
*
* @param user user to delete
*/
void deleteUser(T user);
/**
* Try to delete user.
*
* @param user user to delete
*/
void deleteUser(T user);
/**
* Lock user account
*
* @param userID User ID
* @param locked Is account locked
* @throws NoUserFoundException
*/
/**
* Lock user account
*
* @param userID User ID
* @param locked Is account locked
* @throws NoUserFoundException
*/
void setAccountLockLocal(long userId, boolean locked) throws NoUserFoundException;
void setAccountLock(long userId, boolean locked) throws NoUserFoundException;
}
List<R> listAvailableRoles();
R getDefaultUserRole();
}
......@@ -16,6 +16,7 @@
package org.genesys.blocks.security.service.impl;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.genesys.blocks.security.NoUserFoundException;
......@@ -58,6 +59,12 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
public void setAccountLockoutTime(long accountLockoutTime) {
this.accountLockoutTime = accountLockoutTime;
}
@Override
public abstract R getDefaultUserRole();
@Override
public abstract List<R> listAvailableRoles();
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
......@@ -101,6 +108,7 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
user.getRoles().clear();
user.getRoles().addAll(newRoles);
user.getRoles().add(getDefaultUserRole());
LOG.info("Setting roles for user {} to {}", user.getEmail(), user.getRoles());
return userRepository.save(user);
}
......
......@@ -16,6 +16,10 @@
package org.genesys.blocks.security.config;
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.security.NotUniqueUserException;
......@@ -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" })
public class ApplicationConfig {
@Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
@Bean
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();
propertyPlaceholderConfigurer.setIgnoreResourceNotFound(true);
propertyPlaceholderConfigurer.setFileEncoding("utf-8");
propertyPlaceholderConfigurer.setLocations(new ClassPathResource("application.properties"));
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
return cacheManager;
}
return propertyPlaceholderConfigurer;
}
@Bean
public OAuthServiceImpl oauthService() {
return new OAuthServiceImpl();
}
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
return cacheManager;
}
@Bean
public BasicUserService<UserRole, TestUser> testUserService() {
return new BasicUserServiceImpl<UserRole, TestUser>() {
@Bean
public OAuthServiceImpl oauthService() {
return new OAuthServiceImpl();
}
@Bean
public BasicUserService<UserRole, TestUser> testUserService() {
return new BasicUserServiceImpl<UserRole, TestUser>() {
@Autowired
private TestUserPersistence testUserRepository;
@Autowired
private TestUserPersistence testUserRepository;
@Override
@Override
public TestUser getUserByEmail(String email) throws UsernameNotFoundException {
return testUserRepository.findByEmail(email);
}
@Override
@Transactional
public TestUser createUser(String email, String fullName, String password, AccountType accountType) throws NotUniqueUserException, PasswordPolicyException {
......@@ -91,9 +95,20 @@ public class ApplicationConfig {
user.setEmail(email);
user.setFullName(fullName);
user.setAccountType(accountType);
user.setRoles(Sets.newHashSet(getDefaultUserRole()));
setPassword(user, password);
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;
import org.springframework.security.core.GrantedAuthority;
public enum UserRole implements GrantedAuthority {
USER("User"), ADMINISTRATOR("Administrator");
USER("User"), ADMINISTRATOR("Administrator"), EXTRAROLE("Extra");
String label;
......
......@@ -18,9 +18,13 @@ package org.genesys.blocks.security.tests;
import static org.hamcrest.Matchers.*;
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.model.BasicUser.AccountType;
import org.genesys.blocks.security.model.TestUser;
import org.genesys.blocks.security.model.UserRole;
import org.genesys.blocks.security.service.PasswordPolicy.PasswordPolicyException;
import org.junit.Test;
......@@ -50,4 +54,24 @@ public class BasicUserServiceTest extends ServiceTest {
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