@Test public void testActionHierarchy() throws Exception { String dbName = "db1"; DBWildcardPrivilege dbAll = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "ALL")); DBWildcardPrivilege dbSelect = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "SELECT")); DBWildcardPrivilege dbInsert = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "INSERT")); DBWildcardPrivilege dbAlter = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "ALTER")); DBWildcardPrivilege dbCreate = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "CREATE")); DBWildcardPrivilege dbDrop = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "DROP")); DBWildcardPrivilege dbIndex = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "INDEX")); DBWildcardPrivilege dbLock = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "LOCK")); assertTrue(dbAll.implies(dbSelect)); assertTrue(dbAll.implies(dbInsert)); assertTrue(dbAll.implies(dbAlter)); assertTrue(dbAll.implies(dbCreate)); assertTrue(dbAll.implies(dbDrop)); assertTrue(dbAll.implies(dbIndex)); assertTrue(dbAll.implies(dbLock)); dbAll = create(new KeyValue("server", "server1"), new KeyValue("db", dbName), new KeyValue("action", "*")); assertTrue(dbAll.implies(dbSelect)); assertTrue(dbAll.implies(dbInsert)); assertTrue(dbAll.implies(dbAlter)); assertTrue(dbAll.implies(dbCreate)); assertTrue(dbAll.implies(dbDrop)); assertTrue(dbAll.implies(dbIndex)); assertTrue(dbAll.implies(dbLock)); dbAll = create(new KeyValue("server", "server1"), new KeyValue("db", dbName)); assertTrue(dbAll.implies(dbSelect)); assertTrue(dbAll.implies(dbInsert)); assertTrue(dbAll.implies(dbAlter)); assertTrue(dbAll.implies(dbCreate)); assertTrue(dbAll.implies(dbDrop)); assertTrue(dbAll.implies(dbIndex)); assertTrue(dbAll.implies(dbLock)); }
@Override public boolean implies(Privilege p) { if (!(p instanceof DBWildcardPrivilege)) { return false; } DBWildcardPrivilege wp = (DBWildcardPrivilege) p; List<KeyValue> otherParts = wp.parts; if(equals(wp)) { return true; } int index = 0; for (KeyValue otherPart : otherParts) { if (parts.size() - 1 < index) { return true; } else { KeyValue part = parts.get(index); if (part.getKey().equalsIgnoreCase("action") && !(otherPart.getKey().equalsIgnoreCase("action"))) { continue; } if(!part.getKey().equalsIgnoreCase(otherPart.getKey())) { return false; } if (!impliesKeyValue(part, otherPart)) { return false; } index++; } } for (; index < parts.size(); index++) { KeyValue part = parts.get(index); if (!part.getValue().equals(AccessConstants.ALL)) { return false; } } return true; }
DBWildcardPrivilege implements Privilege { @Override public boolean implies(Privilege p) { if (!(p instanceof DBWildcardPrivilege)) { return false; } DBWildcardPrivilege wp = (DBWildcardPrivilege) p; List<KeyValue> otherParts = wp.parts; if(equals(wp)) { return true; } int index = 0; for (KeyValue otherPart : otherParts) { if (parts.size() - 1 < index) { return true; } else { KeyValue part = parts.get(index); if (part.getKey().equalsIgnoreCase("action") && !(otherPart.getKey().equalsIgnoreCase("action"))) { continue; } if(!part.getKey().equalsIgnoreCase(otherPart.getKey())) { return false; } if (!impliesKeyValue(part, otherPart)) { return false; } index++; } } for (; index < parts.size(); index++) { KeyValue part = parts.get(index); if (!part.getValue().equals(AccessConstants.ALL)) { return false; } } return true; } }
DBWildcardPrivilege implements Privilege { @Override public boolean implies(Privilege p) { if (!(p instanceof DBWildcardPrivilege)) { return false; } DBWildcardPrivilege wp = (DBWildcardPrivilege) p; List<KeyValue> otherParts = wp.parts; if(equals(wp)) { return true; } int index = 0; for (KeyValue otherPart : otherParts) { if (parts.size() - 1 < index) { return true; } else { KeyValue part = parts.get(index); if (part.getKey().equalsIgnoreCase("action") && !(otherPart.getKey().equalsIgnoreCase("action"))) { continue; } if(!part.getKey().equalsIgnoreCase(otherPart.getKey())) { return false; } if (!impliesKeyValue(part, otherPart)) { return false; } index++; } } for (; index < parts.size(); index++) { KeyValue part = parts.get(index); if (!part.getValue().equals(AccessConstants.ALL)) { return false; } } return true; } DBWildcardPrivilege(String wildcardString); }
DBWildcardPrivilege implements Privilege { @Override public boolean implies(Privilege p) { if (!(p instanceof DBWildcardPrivilege)) { return false; } DBWildcardPrivilege wp = (DBWildcardPrivilege) p; List<KeyValue> otherParts = wp.parts; if(equals(wp)) { return true; } int index = 0; for (KeyValue otherPart : otherParts) { if (parts.size() - 1 < index) { return true; } else { KeyValue part = parts.get(index); if (part.getKey().equalsIgnoreCase("action") && !(otherPart.getKey().equalsIgnoreCase("action"))) { continue; } if(!part.getKey().equalsIgnoreCase(otherPart.getKey())) { return false; } if (!impliesKeyValue(part, otherPart)) { return false; } index++; } } for (; index < parts.size(); index++) { KeyValue part = parts.get(index); if (!part.getValue().equals(AccessConstants.ALL)) { return false; } } return true; } DBWildcardPrivilege(String wildcardString); @Override boolean implies(Privilege p); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode(); }
DBWildcardPrivilege implements Privilege { @Override public boolean implies(Privilege p) { if (!(p instanceof DBWildcardPrivilege)) { return false; } DBWildcardPrivilege wp = (DBWildcardPrivilege) p; List<KeyValue> otherParts = wp.parts; if(equals(wp)) { return true; } int index = 0; for (KeyValue otherPart : otherParts) { if (parts.size() - 1 < index) { return true; } else { KeyValue part = parts.get(index); if (part.getKey().equalsIgnoreCase("action") && !(otherPart.getKey().equalsIgnoreCase("action"))) { continue; } if(!part.getKey().equalsIgnoreCase(otherPart.getKey())) { return false; } if (!impliesKeyValue(part, otherPart)) { return false; } index++; } } for (; index < parts.size(); index++) { KeyValue part = parts.get(index); if (!part.getValue().equals(AccessConstants.ALL)) { return false; } } return true; } DBWildcardPrivilege(String wildcardString); @Override boolean implies(Privilege p); @Override String toString(); @Override boolean equals(Object o); @Override int hashCode();  }