Skip to content

Commit

Permalink
[SYNCOPE-1823] Polishing and adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Jul 16, 2024
1 parent 5c13f36 commit e716cfb
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 170 deletions.
5 changes: 5 additions & 0 deletions ext/scimv2/logic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ under the License.
<artifactId>antlr4-runtime</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,6 @@ public class SearchCondVisitor extends SCIMFilterBaseVisitor<SearchCond> {
private static final List<String> MULTIVALUE = List.of(
"emails", "phoneNumbers", "ims", "photos", "addresses");

private final Resource resource;

private final SCIMConf conf;

public SearchCondVisitor(final Resource resource, final SCIMConf conf) {
this.resource = resource;
this.conf = conf;
}

@Override
public SearchCond visitScimFilter(final SCIMFilterParser.ScimFilterContext ctx) {
return visit(ctx.expression(0));
}

private static boolean schemaEquals(final Resource resource, final String value, final String schema) {
return resource == null
? value.contains(":")
Expand All @@ -62,99 +48,6 @@ private static boolean schemaEquals(final Resource resource, final String value,
: value.equalsIgnoreCase(schema) || (resource.schema() + ":" + value).equalsIgnoreCase(schema);
}

public AttrCond createAttrCond(final String schema) {
AttrCond attrCond = null;

if (schemaEquals(Resource.User, "userName", schema)) {
attrCond = new AnyCond();
attrCond.setSchema("username");
} else if (resource == Resource.Group && schemaEquals(Resource.Group, "displayName", schema)) {
attrCond = new AnyCond();
attrCond.setSchema("name");
} else if (schemaEquals(null, "meta.created", schema)) {
attrCond = new AnyCond();
attrCond.setSchema("creationDate");
} else if (schemaEquals(null, "meta.lastModified", schema)) {
attrCond = new AnyCond();
attrCond.setSchema("lastChangeDate");
}

switch (resource) {
case User:
if (conf.getUserConf() != null) {
if (conf.getUserConf().getName() != null) {
for (Map.Entry<String, String> entry : conf.getUserConf().getName().asMap().entrySet()) {
if (schemaEquals(Resource.User, "name." + entry.getKey(), schema)) {
attrCond = new AttrCond();
attrCond.setSchema(entry.getValue());
}
}
}

for (Map.Entry<String, String> entry : conf.getUserConf().asMap().entrySet()) {
if (schemaEquals(Resource.User, entry.getKey(), schema)) {
attrCond = new AttrCond();
attrCond.setSchema(entry.getValue());
}
}

for (SCIMUserAddressConf address : conf.getUserConf().getAddresses()) {
for (Map.Entry<String, String> entry : address.asMap().entrySet()) {
if (schemaEquals(Resource.User, "addresses." + entry.getKey(), schema)) {
attrCond = new AttrCond();
attrCond.setSchema(entry.getValue());
}
}
}
}

if (conf.getEnterpriseUserConf() != null) {
for (Map.Entry<String, String> entry : conf.getEnterpriseUserConf().asMap().entrySet()) {
if (schemaEquals(Resource.EnterpriseUser, entry.getKey(), schema)) {
attrCond = new AttrCond();
attrCond.setSchema(entry.getValue());
}
}

if (conf.getEnterpriseUserConf().getManager() != null
&& conf.getEnterpriseUserConf().getManager().getKey() != null) {

attrCond = new AttrCond();
attrCond.setSchema(conf.getEnterpriseUserConf().getManager().getKey());
}
}

if (conf.getExtensionUserConf() != null) {
for (Map.Entry<String, String> entry : conf.getExtensionUserConf().asMap().entrySet()) {
if (schemaEquals(Resource.ExtensionUser, entry.getKey(), schema)) {
attrCond = new AttrCond();
attrCond.setSchema(entry.getValue());
}
}
}
break;

case Group:
if (conf.getGroupConf() != null) {
for (Map.Entry<String, String> entry : conf.getGroupConf().asMap().entrySet()) {
if (schemaEquals(Resource.Group, entry.getKey(), schema)) {
attrCond = new AttrCond();
attrCond.setSchema(entry.getValue());
}
}
}
break;

default:
}

if (attrCond == null) {
throw new IllegalArgumentException("Could not match " + schema + " for " + resource);
}

return attrCond;
}

private static SearchCond setOperator(final AttrCond attrCond, final String operator) {
switch (operator) {
case "eq":
Expand Down Expand Up @@ -203,7 +96,7 @@ private static SearchCond setOperator(final AttrCond attrCond, final String oper
: SearchCond.getLeaf(attrCond);
}

private <E extends Enum<?>> SearchCond complex(
private static <E extends Enum<?>> SearchCond complex(
final String operator, final String left, final String right, final List<SCIMComplexConf<E>> items) {

if (left.endsWith(".type") && "eq".equals(operator)) {
Expand Down Expand Up @@ -232,7 +125,7 @@ private <E extends Enum<?>> SearchCond complex(
return null;
}

private SearchCond addresses(
private static SearchCond addresses(
final String operator, final String left, final String right, final List<SCIMUserAddressConf> items) {

if (left.endsWith(".type") && "eq".equals(operator)) {
Expand Down Expand Up @@ -261,6 +154,113 @@ private SearchCond addresses(
return null;
}

private final Resource resource;

private final SCIMConf conf;

public SearchCondVisitor(final Resource resource, final SCIMConf conf) {
this.resource = resource;
this.conf = conf;
}

@Override
public SearchCond visitScimFilter(final SCIMFilterParser.ScimFilterContext ctx) {
return visit(ctx.expression(0));
}

public AttrCond createAttrCond(final String schema) {
AttrCond attrCond = null;

if (schemaEquals(Resource.User, "userName", schema)) {
attrCond = new AnyCond();
attrCond.setSchema("username");
} else if (resource == Resource.Group && schemaEquals(Resource.Group, "displayName", schema)) {
attrCond = new AnyCond();
attrCond.setSchema("name");
} else if (schemaEquals(null, "meta.created", schema)) {
attrCond = new AnyCond();
attrCond.setSchema("creationDate");
} else if (schemaEquals(null, "meta.lastModified", schema)) {
attrCond = new AnyCond();
attrCond.setSchema("lastChangeDate");
}

switch (resource) {
case User:
if (conf.getUserConf() != null) {
if (conf.getUserConf().getName() != null) {
for (Map.Entry<String, String> entry : conf.getUserConf().getName().asMap().entrySet()) {
if (schemaEquals(Resource.User, "name." + entry.getKey(), schema)) {
attrCond = new AttrCond();
attrCond.setSchema(entry.getValue());
}
}
}

for (Map.Entry<String, String> entry : conf.getUserConf().asMap().entrySet()) {
if (schemaEquals(Resource.User, entry.getKey(), schema)) {
attrCond = new AttrCond();
attrCond.setSchema(entry.getValue());
}
}

for (SCIMUserAddressConf address : conf.getUserConf().getAddresses()) {
for (Map.Entry<String, String> entry : address.asMap().entrySet()) {
if (schemaEquals(Resource.User, "addresses." + entry.getKey(), schema)) {
attrCond = new AttrCond();
attrCond.setSchema(entry.getValue());
}
}
}
}

if (conf.getEnterpriseUserConf() != null) {
for (Map.Entry<String, String> entry : conf.getEnterpriseUserConf().asMap().entrySet()) {
if (schemaEquals(Resource.EnterpriseUser, entry.getKey(), schema)) {
attrCond = new AttrCond();
attrCond.setSchema(entry.getValue());
}
}

if (conf.getEnterpriseUserConf().getManager() != null
&& conf.getEnterpriseUserConf().getManager().getKey() != null) {

attrCond = new AttrCond();
attrCond.setSchema(conf.getEnterpriseUserConf().getManager().getKey());
}
}

if (conf.getExtensionUserConf() != null) {
for (Map.Entry<String, String> entry : conf.getExtensionUserConf().asMap().entrySet()) {
if (schemaEquals(Resource.ExtensionUser, entry.getKey(), schema)) {
attrCond = new AttrCond();
attrCond.setSchema(entry.getValue());
}
}
}
break;

case Group:
if (conf.getGroupConf() != null) {
for (Map.Entry<String, String> entry : conf.getGroupConf().asMap().entrySet()) {
if (schemaEquals(Resource.Group, entry.getKey(), schema)) {
attrCond = new AttrCond();
attrCond.setSchema(entry.getValue());
}
}
}
break;

default:
}

if (attrCond == null) {
throw new IllegalArgumentException("Could not match " + schema + " for " + resource);
}

return attrCond;
}

private SearchCond transform(final String operator, final String left, final String right) {
SearchCond result = null;

Expand Down
Loading

0 comments on commit e716cfb

Please sign in to comment.