use of com.yahoo.athenz.zms.PrincipalRole in project athenz by yahoo.
the class FileConnection method listPrincipalRoles.
@Override
public List<PrincipalRole> listPrincipalRoles(String principalName) {
// we're going to go through all domains
String[] fnames = rootDir.list();
List<PrincipalRole> roles = new ArrayList<>();
for (String fname : fnames) {
File f = new File(rootDir, fname);
DomainStruct domainStruct = null;
try {
Path path = Paths.get(f.toURI());
domainStruct = JSON.fromBytes(Files.readAllBytes(path), DomainStruct.class);
} catch (IOException e) {
}
if (domainStruct == null) {
continue;
}
for (Role role : domainStruct.getRoles().values()) {
List<RoleMember> roleMembers = role.getRoleMembers();
if (roleMembers == null) {
continue;
}
for (int idx = 0; idx < roleMembers.size(); idx++) {
final String memberName = roleMembers.get(idx).getMemberName();
if (memberName.equals(principalName)) {
PrincipalRole pRole = new PrincipalRole();
pRole.setDomainName(fname);
pRole.setRoleName(extractRoleName(fname, role.getName()));
roles.add(pRole);
}
}
}
}
return roles;
}
use of com.yahoo.athenz.zms.PrincipalRole in project athenz by yahoo.
the class JDBCConnectionTest method testListPrincipalRoles.
@Test
public void testListPrincipalRoles() throws Exception {
JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
Mockito.when(mockResultSet.getInt(1)).thenReturn(// principal id
5);
// principal roles
Mockito.when(mockResultSet.next()).thenReturn(// get principal id
true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_NAME)).thenReturn("coretech").thenReturn("sports").thenReturn("sports").thenReturn("weather");
Mockito.when(mockResultSet.getString(ZMSConsts.DB_COLUMN_ROLE_NAME)).thenReturn("admin").thenReturn("reader").thenReturn("writer").thenReturn("reader");
List<PrincipalRole> roles = jdbcConn.listPrincipalRoles("user.joe");
assertEquals(4, roles.size());
// get principal id
Mockito.verify(mockPrepStmt, times(1)).setString(1, "user.joe");
// get role list
Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
boolean coretech_admin = false;
boolean sports_reader = false;
boolean sports_writer = false;
boolean weather_reader = false;
for (PrincipalRole role : roles) {
if (role.getDomainName().equals("coretech") && role.getRoleName().equals("admin")) {
coretech_admin = true;
} else if (role.getDomainName().equals("sports") && role.getRoleName().equals("reader")) {
sports_reader = true;
} else if (role.getDomainName().equals("sports") && role.getRoleName().equals("writer")) {
sports_writer = true;
} else if (role.getDomainName().equals("weather") && role.getRoleName().equals("reader")) {
weather_reader = true;
}
}
assertTrue(coretech_admin);
assertTrue(sports_reader);
assertTrue(sports_writer);
assertTrue(weather_reader);
jdbcConn.close();
}
use of com.yahoo.athenz.zms.PrincipalRole in project athenz by yahoo.
the class JDBCConnection method listPrincipalRoles.
@Override
public List<PrincipalRole> listPrincipalRoles(String principalName) {
final String caller = "listPrincipalRoles";
int principalId = getPrincipalId(principalName);
if (principalId == 0) {
throw notFoundError(caller, ZMSConsts.OBJECT_PRINCIPAL, principalName);
}
List<PrincipalRole> roles = new ArrayList<>();
try (PreparedStatement ps = con.prepareStatement(SQL_LIST_PRINCIPAL_ROLES)) {
ps.setInt(1, principalId);
try (ResultSet rs = executeQuery(ps, caller)) {
while (rs.next()) {
PrincipalRole role = new PrincipalRole();
role.setDomainName(rs.getString(ZMSConsts.DB_COLUMN_NAME));
role.setRoleName(rs.getString(ZMSConsts.DB_COLUMN_ROLE_NAME));
roles.add(role);
}
}
} catch (SQLException ex) {
throw sqlError(ex, caller);
}
return roles;
}
Aggregations