SEC-272: More group manager method implementations.

This commit is contained in:
Luke Taylor 2008-01-14 11:33:05 +00:00
parent bad58fe96a
commit f27ea98217
3 changed files with 128 additions and 10 deletions

View File

@ -15,12 +15,12 @@ public interface GroupsManager {
List findUsersInGroup(String groupName); List findUsersInGroup(String groupName);
void createGroup(String groupName, GrantedAuthority[] authorities); void createGroup(String groupName, GrantedAuthority[] authorities);
//
// void deleteGroup(String groupName); void deleteGroup(String groupName);
//
// void renameGroup(String oldName, String newName); void renameGroup(String oldName, String newName);
//
// void addUserToGroup(String username, String group); void addUserToGroup(String username, String group);
// //
// void removeUserFromGroup(String username, String groupName); // void removeUserFromGroup(String username, String groupName);
// //

View File

@ -67,6 +67,17 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
"select id from groups where group_name = ?"; "select id from groups where group_name = ?";
public static final String DEF_INSERT_GROUP_AUTHORITY_SQL = public static final String DEF_INSERT_GROUP_AUTHORITY_SQL =
"insert into group_authorities (group_id, authority) values (?,?)"; "insert into group_authorities (group_id, authority) values (?,?)";
public static final String DEF_DELETE_GROUP_SQL =
"delete from groups where id = ?";
public static final String DEF_DELETE_GROUP_AUTHORITIES_SQL =
"delete from group_authorities where group_id = ?";
public static final String DEF_DELETE_GROUP_MEMBERS_SQL =
"delete from group_members where group_id = ?";
public static final String DEF_RENAME_GROUP_SQL =
"update groups set group_name = ? where group_name = ?";
public static final String DEF_INSERT_GROUP_MEMBER_SQL =
"insert into group_members (group_id, username) values (?,?)";
//~ Instance fields ================================================================================================ //~ Instance fields ================================================================================================
@ -85,6 +96,11 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
private String insertGroupSql = DEF_INSERT_GROUP_SQL; private String insertGroupSql = DEF_INSERT_GROUP_SQL;
private String findGroupIdSql = DEF_FIND_GROUP_ID_SQL; private String findGroupIdSql = DEF_FIND_GROUP_ID_SQL;
private String insertGroupAuthoritySql = DEF_INSERT_GROUP_AUTHORITY_SQL; private String insertGroupAuthoritySql = DEF_INSERT_GROUP_AUTHORITY_SQL;
private String deleteGroupSql = DEF_DELETE_GROUP_SQL;
private String deleteGroupAuthoritiesSql = DEF_DELETE_GROUP_AUTHORITIES_SQL;
private String deleteGroupMembersSql = DEF_DELETE_GROUP_MEMBERS_SQL;
private String renameGroupSql = DEF_RENAME_GROUP_SQL;
private String insertGroupMemberSql = DEF_INSERT_GROUP_MEMBER_SQL;
protected SqlUpdate insertUser; protected SqlUpdate insertUser;
protected SqlUpdate deleteUser; protected SqlUpdate deleteUser;
@ -99,6 +115,11 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
protected SqlUpdate insertGroup; protected SqlUpdate insertGroup;
protected SqlQuery findGroupIdQuery; protected SqlQuery findGroupIdQuery;
protected SqlUpdate insertGroupAuthority; protected SqlUpdate insertGroupAuthority;
protected SqlUpdate deleteGroup;
protected SqlUpdate deleteGroupMembers;
protected SqlUpdate deleteGroupAuthorities;
protected SqlUpdate renameGroup;
protected SqlUpdate insertGroupMember;
private AuthenticationManager authenticationManager; private AuthenticationManager authenticationManager;
@ -125,6 +146,11 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
insertGroup = new InsertGroup(getDataSource()); insertGroup = new InsertGroup(getDataSource());
findGroupIdQuery = new FindGroupIdQuery(getDataSource()); findGroupIdQuery = new FindGroupIdQuery(getDataSource());
insertGroupAuthority = new InsertGroupAuthority(getDataSource()); insertGroupAuthority = new InsertGroupAuthority(getDataSource());
deleteGroup = new DeleteGroup(getDataSource());
deleteGroupAuthorities = new DeleteGroupAuthorities(getDataSource());
deleteGroupMembers = new DeleteGroupMembers(getDataSource());
renameGroup = new RenameGroup(getDataSource());
insertGroupMember = new InsertGroupMember(getDataSource());
super.initDao(); super.initDao();
} }
@ -228,6 +254,31 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
} }
} }
public void deleteGroup(String groupName) {
Assert.hasText(groupName);
int id = ((Integer) findGroupIdQuery.findObject(groupName)).intValue();
deleteGroupMembers.update(id);
deleteGroupAuthorities.update(id);
deleteGroup.update(id);
}
public void renameGroup(String oldName, String newName) {
Assert.hasText(oldName);
Assert.hasText(newName);
renameGroup.update(newName, oldName);
}
public void addUserToGroup(String username, String groupName) {
Assert.hasText(username);
Assert.hasText(groupName);
Integer key = (Integer) findGroupIdQuery.findObject(groupName);
insertGroupMember.update(new Object[] {key, username});
}
public void setAuthenticationManager(AuthenticationManager authenticationManager) { public void setAuthenticationManager(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager; this.authenticationManager = authenticationManager;
} }
@ -403,4 +454,47 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
compile(); compile();
} }
} }
protected class DeleteGroup extends SqlUpdate {
public DeleteGroup(DataSource ds) {
super(ds, deleteGroupSql);
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
}
protected class DeleteGroupMembers extends SqlUpdate {
public DeleteGroupMembers(DataSource ds) {
super(ds, deleteGroupMembersSql);
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
}
protected class DeleteGroupAuthorities extends SqlUpdate {
public DeleteGroupAuthorities(DataSource ds) {
super(ds, deleteGroupAuthoritiesSql);
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
}
protected class RenameGroup extends SqlUpdate {
public RenameGroup(DataSource ds) {
super(ds, renameGroupSql);
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
}
protected class InsertGroupMember extends SqlUpdate {
public InsertGroupMember(DataSource ds) {
super(ds, insertGroupMemberSql);
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
}
} }

View File

@ -5,8 +5,6 @@ import org.springframework.security.Authentication;
import org.springframework.security.BadCredentialsException; import org.springframework.security.BadCredentialsException;
import org.springframework.security.MockAuthenticationManager; import org.springframework.security.MockAuthenticationManager;
import org.springframework.security.PopulatedDatabase; import org.springframework.security.PopulatedDatabase;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.context.SecurityContextHolder; import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken; import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
import org.springframework.security.providers.dao.UserCache; import org.springframework.security.providers.dao.UserCache;
@ -214,12 +212,38 @@ public class JdbcUserDetailsManagerTests {
List roles = template.queryForList( List roles = template.queryForList(
"select ga.authority from groups g, group_authorities ga " + "select ga.authority from groups g, group_authorities ga " +
"where ga.group_id = g.id" + "where ga.group_id = g.id " +
" and g.group_name = 'TEST_GROUP'"); "and g.group_name = 'TEST_GROUP'");
assertEquals(2, roles.size()); assertEquals(2, roles.size());
} }
@Test
public void deleteGroupRemovesData() throws Exception {
manager.deleteGroup("GROUP_0");
manager.deleteGroup("GROUP_1");
manager.deleteGroup("GROUP_2");
manager.deleteGroup("GROUP_3");
assertEquals(0, template.queryForList("select * from group_authorities").size());
assertEquals(0, template.queryForList("select * from group_members").size());
assertEquals(0, template.queryForList("select id from groups").size());
}
@Test
public void renameGroupIsSuccessful() throws Exception {
manager.renameGroup("GROUP_0", "GROUP_X");
assertEquals(0, template.queryForInt("select id from groups where group_name = 'GROUP_X'"));
}
@Test
public void addingGroupUserSetsCorrectData() throws Exception {
manager.addUserToGroup("tom", "GROUP_0");
assertEquals(2, template.queryForList("select username from group_members where group_id = 0").size());
}
private Authentication authenticateJoe() { private Authentication authenticateJoe() {
UsernamePasswordAuthenticationToken auth = UsernamePasswordAuthenticationToken auth =
new UsernamePasswordAuthenticationToken("joe","password", joe.getAuthorities()); new UsernamePasswordAuthenticationToken("joe","password", joe.getAuthorities());