Skip to content

Commit

Permalink
#39 : enable to set the authority for user
Browse files Browse the repository at this point in the history
  • Loading branch information
OhadR committed Jul 8, 2014
1 parent 6e91747 commit a6bc630
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,16 @@ public UserDetails loadUserByUsername(String username)
loginAttemptsLeft = new Integer(loginAttemptsLeftObj.toString());
}

String roleName = (String)entity.getProperty(AUTHORITIES_PROP_NAME);
GrantedAuthority userAuth = new SimpleGrantedAuthority(roleName);
String firstName = (String)entity.getProperty(FIRST_NAME_PROP_NAME);
String lastName = (String)entity.getProperty(LAST_NAME_PROP_NAME);

String roleName = (String)entity.getProperty(AUTHORITIES_PROP_NAME);
GrantedAuthority userAuth = new SimpleGrantedAuthority(roleName);
Collection<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
authSet.add(userAuth);

log.debug( "$$$$$ user " + username + ", authSet= " + authSet );

return new InMemoryAuthenticationUserImpl(
username,
(String)entity.getProperty(PASSWORD_PROP_NAME),
Expand Down Expand Up @@ -223,4 +226,24 @@ public void changePassword(String username, String newEncodedPassword)
datastore.put(entity);
}

}
@Override
public void setAuthority(String username, String authority)
{
Key userKey = KeyFactory.createKey(AUTH_FLOWS_USER_DB_KIND, username);
Entity entity;
try
{
entity = datastore.get(userKey);
log.debug("got entity of " + username + ": " + entity);
}
catch (EntityNotFoundException e)
{
log.error("entity of " + username + " not found");
throw new NoSuchElementException(e.getMessage());
}

entity.setProperty(AUTHORITIES_PROP_NAME, authority );
datastore.put( entity );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public class JdbcAuthenticationAccountRepositoryImpl extends AbstractAuthenticat

private static final String DEFAULT_UPDATE_ATTEMPTS_CNTR_STATEMENT = "update " + TABLE_NAME +
" set LOGIN_ATTEMPTS_COUNTER = ? where USERNAME = ?";


private static final String DEFAULT_UPDATE_AUTHORITY_STATEMENT = "update " + TABLE_NAME +
" set authorities = ? where USERNAME = ?";


@Autowired
private DataSource dataSource;
Expand Down Expand Up @@ -217,4 +220,15 @@ public boolean userExists(String username)
return false;
}


@Override
public void setAuthority(String username, String authority)
{
int count = jdbcTemplate.update( DEFAULT_UPDATE_AUTHORITY_STATEMENT, authority, username);
if ( count != 1 )
{
throw new NoSuchElementException("No user with email: " + username);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public interface AuthenticationAccountRepository extends UserDetailsManager

String getEncodedPassword(String username);
Date getPasswordLastChangeDate(String email);

void setAuthority(String username, String authority);

/**
* NOT IMPLEMENTED
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.ohadr.auth_flows.mocks;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import org.springframework.security.acls.model.AlreadyExistsException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

Expand Down Expand Up @@ -165,4 +169,32 @@ public boolean userExists(String username)
return false;
}



@Override
public void setAuthority( String username, String authority )
{
AuthenticationUser storedUser = loadUserByUsername( username );
if( storedUser != null )
{
GrantedAuthority userAuth = new SimpleGrantedAuthority( authority );
Collection<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
authSet.add(userAuth);

AuthenticationUser newUser = new InMemoryAuthenticationUserImpl(
username,
storedUser.getPassword(),
storedUser.isEnabled(),
storedUser.getLoginAttemptsLeft(),
storedUser.getPasswordLastChangeDate(),
storedUser.getFirstName(),
storedUser.getLastName(),
authSet );

//delete old user and set a new one, since iface does not support "setPassword()":
deleteUser(username);
users.put(username, newUser);
}
}

}

0 comments on commit a6bc630

Please sign in to comment.