DBService implements RolesProvider { void removePrincipalFromAllRoles(ObjectStoreConnection con, String principalName, String adminUser, String auditRef) { List<PrincipalRole> roles; try { roles = con.listPrincipalRoles(null, principalName); } catch (ResourceException ex) { if (ex.getCode() == ResourceException.NOT_FOUND) { return; } else { throw ex; } } for (PrincipalRole role : roles) { final String domainName = role.getDomainName(); final String roleName = role.getRoleName(); if (LOG.isDebugEnabled()) { LOG.debug("removePrincipalFromAllRoles: removing member {} from {}:role.{}", principalName, domainName, roleName); } try { con.deleteRoleMember(domainName, roleName, principalName, adminUser, auditRef); } catch (ResourceException ex) { LOG.error("removePrincipalFromAllRoles: unable to remove {} from {}:role.{} - error {}", principalName, domainName, roleName, ex.getMessage()); } con.updateRoleModTimestamp(domainName, roleName); con.updateDomainModTimestamp(domainName); } } DBService(ObjectStore store, AuditLogger auditLogger, ZMSConfig zmsConfig, AuditReferenceValidator auditReferenceValidator); DomainRoleMembers listOverdueReviewRoleMembers(String domainName); @Override List<Role> getRolesByDomain(String domain); ResourceAccessList getResourceAccessList(String principal, String action); AthenzDomain getAthenzDomain(final String domainName, boolean masterCopy); Quota getQuota(String domainName); void executePutRoleSystemMeta(ResourceContext ctx, final String domainName, final String roleName,
           RoleSystemMeta meta, final String attribute, final String auditRef, final String caller); void executePutGroupSystemMeta(ResourceContext ctx, final String domainName, final String groupName,
                                          GroupSystemMeta meta, final String attribute, final String auditRef); void executePutServiceIdentitySystemMeta(ResourceContext ctx, String domainName, String serviceName,
            ServiceIdentitySystemMeta meta, String attribute, String auditRef, String caller); void executePutRoleMeta(ResourceContext ctx, String domainName, String roleName, Role originalRole,
                                   RoleMeta meta, String auditRef, String caller); void executePutGroupMeta(ResourceContext ctx, final String domainName, final String groupName,
                                    GroupMeta meta, final String auditRef); Set<String> getPendingMembershipApproverRoles(int delayDays); Set<String> getPendingGroupMembershipApproverRoles(int delayDays); Map<String, DomainRoleMember> getRoleExpiryMembers(int delayDays); Map<String, DomainRoleMember> getRoleReviewMembers(int delayDays); Map<String, DomainGroupMember> getGroupExpiryMembers(int delayDays); void processExpiredPendingMembers(int pendingRoleMemberLifespan, final String monitorIdentity); void processExpiredPendingGroupMembers(int pendingGroupMemberLifespan, final String monitorIdentity); static int AUDIT_TYPE_ROLE; static int AUDIT_TYPE_POLICY; static int AUDIT_TYPE_SERVICE; static int AUDIT_TYPE_DOMAIN; static int AUDIT_TYPE_ENTITY; static int AUDIT_TYPE_TENANCY; static int AUDIT_TYPE_TEMPLATE; static int AUDIT_TYPE_GROUP; }
@Test public void testRemovePrincipalFromAllRolesExceptions() { ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class); Mockito.when(conn.listPrincipalRoles(null, "user.joe")) .thenThrow(new ResourceException(404)) .thenThrow(new ResourceException(501)); zms.dbService.removePrincipalFromAllRoles(conn, "user.joe", adminUser, "unittest"); try { zms.dbService.removePrincipalFromAllRoles(conn, "user.joe", adminUser, "unittest"); fail(); } catch (ResourceException ex) { assertEquals(501, ex.getCode()); } }
@Test public void testRemovePrincipalFromAllRolesDeleteUserException() { List<PrincipalRole> roles = new ArrayList<>(); PrincipalRole role1 = new PrincipalRole(); role1.setDomainName("dom1"); role1.setRoleName("role1"); roles.add(role1); PrincipalRole role2 = new PrincipalRole(); role2.setDomainName("dom1"); role2.setRoleName("role2"); roles.add(role2); ObjectStoreConnection conn = Mockito.mock(ObjectStoreConnection.class); Mockito.when(conn.listPrincipalRoles(null, "user.joe")).thenReturn(roles); Mockito.when(conn.deleteRoleMember("dom1", "role1", "user.joe", adminUser, "unittest")) .thenReturn(true); Mockito.when(conn.deleteRoleMember("dom1", "role2", "user.joe", adminUser, "unittest")) .thenThrow(new ResourceException(501)); zms.dbService.removePrincipalFromAllRoles(conn, "user.joe", adminUser, "unittest"); }