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 @@
*/
package org.genesys.blocks.security.service;
import java.util.List;
import java.util.Set;
import org.genesys.blocks.security.NoUserFoundException;
......@@ -45,7 +46,7 @@ public interface BasicUserService<R extends GrantedAuthority, T extends BasicUse
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
......@@ -59,7 +60,8 @@ public interface BasicUserService<R extends GrantedAuthority, T extends BasicUse
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
......@@ -104,5 +106,8 @@ public interface BasicUserService<R extends GrantedAuthority, T extends BasicUse
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;
......@@ -59,6 +60,12 @@ public abstract class BasicUserServiceImpl<R extends GrantedAuthority, T extends
this.accountLockoutTime = accountLockoutTime;
}
@Override
public abstract R getDefaultUserRole();
@Override
public abstract List<R> listAvailableRoles();
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return getUserByEmail(username);
......@@ -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;
......@@ -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