Search in sources :

Example 11 with MotechUser

use of org.motechproject.security.domain.MotechUser in project motech by motech.

the class MotechAccessVoterTest method shouldVoteAffirmativeIfUserHasAccess.

@Test
public void shouldVoteAffirmativeIfUserHasAccess() {
    List<ConfigAttribute> attributes = new ArrayList<ConfigAttribute>();
    attributes.add(new SecurityConfig("access_testuser"));
    attributes.add(new SecurityConfig("access_motechUser"));
    MotechUserProfile userProfile = new MotechUserProfile(new MotechUser("TestUser", "p@ssw0rd", "", "", null, "", Locale.ENGLISH));
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("TestUser", "p@ssw0rd");
    authentication.setDetails(userProfile);
    MotechAccessVoter voter = new MotechAccessVoter();
    assertEquals(1, voter.vote(authentication, null, attributes));
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) MotechUserProfile(org.motechproject.security.domain.MotechUserProfile) ConfigAttribute(org.springframework.security.access.ConfigAttribute) SecurityConfig(org.springframework.security.access.SecurityConfig) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 12 with MotechUser

use of org.motechproject.security.domain.MotechUser in project motech by motech.

the class MotechAccessVoterTest method shouldAbstrainIfNoAccessAttributes.

public void shouldAbstrainIfNoAccessAttributes() {
    List<ConfigAttribute> attributes = new ArrayList<ConfigAttribute>();
    MotechUserProfile userProfile = new MotechUserProfile(new MotechUser("TestUser", "p@ssw0rd", "", "", null, "", Locale.ENGLISH));
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("TestUser", "p@ssw0rd");
    authentication.setDetails(userProfile);
    MotechAccessVoter voter = new MotechAccessVoter();
    assertEquals(0, voter.vote(authentication, null, attributes));
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) MotechUserProfile(org.motechproject.security.domain.MotechUserProfile) ConfigAttribute(org.springframework.security.access.ConfigAttribute) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 13 with MotechUser

use of org.motechproject.security.domain.MotechUser in project motech by motech.

the class MotechAccessVoterTest method shouldVoteNegativeIfUserDoesNotHaveAccess.

public void shouldVoteNegativeIfUserDoesNotHaveAccess() {
    List<ConfigAttribute> attributes = new ArrayList<ConfigAttribute>();
    attributes.add(new SecurityConfig("access_otheruser"));
    attributes.add(new SecurityConfig("access_motechUser"));
    MotechUserProfile userProfile = new MotechUserProfile(new MotechUser("TestUser", "p@ssw0rd", "", "", null, "", Locale.ENGLISH));
    UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("TestUser", "p@ssw0rd");
    authentication.setDetails(userProfile);
    MotechAccessVoter voter = new MotechAccessVoter();
    assertEquals(-1, voter.vote(authentication, null, attributes));
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) MotechUserProfile(org.motechproject.security.domain.MotechUserProfile) ConfigAttribute(org.springframework.security.access.ConfigAttribute) SecurityConfig(org.springframework.security.access.SecurityConfig) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Example 14 with MotechUser

use of org.motechproject.security.domain.MotechUser in project motech by motech.

the class MotechRoleServiceBundleIT method shouldNotDeleteRoleWithUsers.

@Test(expected = RoleHasUserException.class)
public void shouldNotDeleteRoleWithUsers() {
    motechRoleService.createRole(new RoleDto("Role-With-User", asList("permissionA, permissionB"), true));
    RoleDto role = motechRoleService.getRole("Role-With-User");
    assertNotNull(role);
    motechUserService.register("duke", "password", "email", "1234", asList("Role-With-User"), Locale.ENGLISH);
    MotechUser motechUser = usersDataService.findByUserName("duke");
    assertNotNull(motechUser);
    assertTrue(motechUser.hasRole("Role-With-User"));
    motechRoleService.deleteRole(role);
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) MotechUser(org.motechproject.security.domain.MotechUser) Test(org.junit.Test)

Example 15 with MotechUser

use of org.motechproject.security.domain.MotechUser in project motech by motech.

the class MotechUserServiceBundleIT method shouldNotActivateInvalidUser.

@Test
public void shouldNotActivateInvalidUser() {
    motechUserService.register("userName", "password", "1234", "", asList("IT_ADMIN", "DB_ADMIN"), Locale.ENGLISH, UserStatus.BLOCKED, null);
    motechUserService.activateUser("userName1");
    MotechUser motechUser = usersDataService.findByUserName("userName");
    assertFalse(motechUser.isActive());
}
Also used : MotechUser(org.motechproject.security.domain.MotechUser) Test(org.junit.Test)

Aggregations

MotechUser (org.motechproject.security.domain.MotechUser)61 Test (org.junit.Test)27 Transactional (org.springframework.transaction.annotation.Transactional)24 MotechUserProfile (org.motechproject.security.domain.MotechUserProfile)8 ArrayList (java.util.ArrayList)6 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)6 DateTime (org.joda.time.DateTime)4 PasswordRecovery (org.motechproject.security.domain.PasswordRecovery)4 Authentication (org.springframework.security.core.Authentication)4 User (org.springframework.security.core.userdetails.User)4 HttpSession (javax.servlet.http.HttpSession)3 MotechRole (org.motechproject.security.domain.MotechRole)3 RoleDto (org.motechproject.security.model.RoleDto)3 ConfigAttribute (org.springframework.security.access.ConfigAttribute)3 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)3 AuthenticationException (org.springframework.security.core.AuthenticationException)3 UserNotFoundException (org.motechproject.security.exception.UserNotFoundException)2 UserDto (org.motechproject.security.model.UserDto)2 SecurityConfig (org.springframework.security.access.SecurityConfig)2 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)2