Search in sources :

Example 1 with Membership

use of com.yahoo.athenz.zms.Membership in project athenz by yahoo.

the class JDBCConnectionTest method testGetRoleMemberNo.

@Test
public void testGetRoleMemberNo() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
    Mockito.when(mockResultSet.next()).thenReturn(// domain id
    true).thenReturn(// rold id
    true).thenReturn(// not a member
    false);
    Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
    5).thenReturn(// role id
    7);
    Membership membership = jdbcConn.getRoleMember("my-domain", "role1", "user.user1");
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
    Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
    Mockito.verify(mockPrepStmt, times(1)).setString(2, "role1");
    Mockito.verify(mockPrepStmt, times(1)).setInt(1, 7);
    Mockito.verify(mockPrepStmt, times(1)).setString(2, "user.user1");
    assertEquals(membership.getMemberName(), "user.user1");
    assertEquals(membership.getRoleName(), "my-domain:role.role1");
    assertFalse(membership.getIsMember());
    jdbcConn.close();
}
Also used : Membership(com.yahoo.athenz.zms.Membership) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 2 with Membership

use of com.yahoo.athenz.zms.Membership in project athenz by yahoo.

the class JDBCConnectionTest method testGetRoleMemberYes.

@Test
public void testGetRoleMemberYes() throws Exception {
    JDBCConnection jdbcConn = new JDBCConnection(mockConn, true);
    Mockito.doReturn(1).when(mockPrepStmt).executeUpdate();
    // yes a member
    Mockito.when(mockResultSet.next()).thenReturn(true);
    Mockito.when(mockResultSet.getInt(1)).thenReturn(// domain id
    5).thenReturn(// role id
    7);
    Membership membership = jdbcConn.getRoleMember("my-domain", "role1", "user.user1");
    Mockito.verify(mockPrepStmt, times(1)).setString(1, "my-domain");
    Mockito.verify(mockPrepStmt, times(1)).setInt(1, 5);
    Mockito.verify(mockPrepStmt, times(1)).setString(2, "role1");
    Mockito.verify(mockPrepStmt, times(1)).setInt(1, 7);
    Mockito.verify(mockPrepStmt, times(1)).setString(2, "user.user1");
    assertEquals(membership.getMemberName(), "user.user1");
    assertEquals(membership.getRoleName(), "my-domain:role.role1");
    assertTrue(membership.getIsMember());
    jdbcConn.close();
}
Also used : Membership(com.yahoo.athenz.zms.Membership) JDBCConnection(com.yahoo.athenz.zms.store.jdbc.JDBCConnection) Test(org.testng.annotations.Test)

Example 3 with Membership

use of com.yahoo.athenz.zms.Membership in project athenz by yahoo.

the class FileConnection method getRoleMember.

@Override
public Membership getRoleMember(String domainName, String roleName, String principal) {
    DomainStruct domainStruct = getDomainStruct(domainName);
    if (domainStruct == null) {
        throw ZMSUtils.error(ResourceException.NOT_FOUND, "domain not found", "getRoleMember");
    }
    Role role = getRoleObject(domainStruct, roleName);
    if (role == null) {
        throw ZMSUtils.error(ResourceException.NOT_FOUND, "role not found", "getRoleMember");
    }
    Membership membership = new Membership().setMemberName(principal).setRoleName(ZMSUtils.roleResourceName(domainName, roleName)).setIsMember(false);
    if (role.getRoleMembers() != null) {
        Set<RoleMember> members = new HashSet<>(role.getRoleMembers());
        for (RoleMember member : members) {
            if (member.getMemberName().equalsIgnoreCase(principal)) {
                membership.setIsMember(true);
                membership.setExpiration(member.getExpiration());
                break;
            }
        }
    }
    return membership;
}
Also used : Role(com.yahoo.athenz.zms.Role) PrincipalRole(com.yahoo.athenz.zms.PrincipalRole) Membership(com.yahoo.athenz.zms.Membership) RoleMember(com.yahoo.athenz.zms.RoleMember) HashSet(java.util.HashSet)

Example 4 with Membership

use of com.yahoo.athenz.zms.Membership in project athenz by yahoo.

the class JDBCConnection method getRoleMember.

@Override
public Membership getRoleMember(String domainName, String roleName, String member) {
    final String caller = "getRoleMember";
    int domainId = getDomainId(domainName);
    if (domainId == 0) {
        throw notFoundError(caller, ZMSConsts.OBJECT_DOMAIN, domainName);
    }
    int roleId = getRoleId(domainId, roleName);
    if (roleId == 0) {
        throw notFoundError(caller, ZMSConsts.OBJECT_ROLE, ZMSUtils.roleResourceName(domainName, roleName));
    }
    Membership membership = new Membership().setMemberName(member).setRoleName(ZMSUtils.roleResourceName(domainName, roleName)).setIsMember(false);
    try (PreparedStatement ps = con.prepareStatement(SQL_GET_ROLE_MEMBER)) {
        ps.setInt(1, roleId);
        ps.setString(2, member);
        try (ResultSet rs = executeQuery(ps, caller)) {
            if (rs.next()) {
                membership.setIsMember(true);
                java.sql.Timestamp expiration = rs.getTimestamp(2);
                if (expiration != null) {
                    membership.setExpiration(Timestamp.fromMillis(expiration.getTime()));
                }
            }
        }
    } catch (SQLException ex) {
        throw sqlError(ex, caller);
    }
    return membership;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Membership(com.yahoo.athenz.zms.Membership) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Membership (com.yahoo.athenz.zms.Membership)4 JDBCConnection (com.yahoo.athenz.zms.store.jdbc.JDBCConnection)2 Test (org.testng.annotations.Test)2 PrincipalRole (com.yahoo.athenz.zms.PrincipalRole)1 Role (com.yahoo.athenz.zms.Role)1 RoleMember (com.yahoo.athenz.zms.RoleMember)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1