Improving CRUD detection for Aggregate Results
This commit is contained in:
1 parent
22f5b75024
commit
13ab94deff
2 files changed
+48
-5
No files matched your search
+16
-3
@@ -480,6 +480,7 @@ public class ApexCRUDViolationRule extends AbstractApexRule {
|
||||
|
||||
private void checkForAccessibility(final ASTSoqlExpression node, Object data) {
|
||||
final boolean isCount = node.getNode().getCanonicalQuery().startsWith("SELECT COUNT()");
|
||||
final String typeFromSOQL = getTypeFromSOQLQuery(node);
|
||||
|
||||
final HashSet<ASTMethodCallExpression> prevCalls = getPreviousMethodCalls(node);
|
||||
for (ASTMethodCallExpression prevCall : prevCalls) {
|
||||
@@ -510,7 +511,7 @@ public class ApexCRUDViolationRule extends AbstractApexRule {
|
||||
.append(":").append(type);
|
||||
|
||||
if (!isGetter) {
|
||||
validateCRUDCheckPresent(node, data, ANY, typeCheck.toString());
|
||||
validateCRUDCheckPresent(node, data, ANY, typeFromSOQL == null ? typeCheck.toString() : typeFromSOQL);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -523,7 +524,7 @@ public class ApexCRUDViolationRule extends AbstractApexRule {
|
||||
if (varToTypeMapping.containsKey(variableWithClass)) {
|
||||
String type = varToTypeMapping.get(variableWithClass);
|
||||
if (!isGetter) {
|
||||
validateCRUDCheckPresent(node, data, ANY, type);
|
||||
validateCRUDCheckPresent(node, data, ANY, typeFromSOQL == null ? type : typeFromSOQL);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -533,11 +534,23 @@ public class ApexCRUDViolationRule extends AbstractApexRule {
|
||||
final ASTReturnStatement returnStatement = node.getFirstParentOfType(ASTReturnStatement.class);
|
||||
if (returnStatement != null) {
|
||||
if (!isGetter) {
|
||||
validateCRUDCheckPresent(node, data, ANY, returnType == null ? "" : returnType);
|
||||
String retType = typeFromSOQL == null ? returnType : typeFromSOQL;
|
||||
validateCRUDCheckPresent(node, data, ANY, retType == null ? "" : retType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getTypeFromSOQLQuery(final ASTSoqlExpression node) {
|
||||
final String canonQuery = node.getNode().getCanonicalQuery();
|
||||
|
||||
Matcher m = Pattern.compile("^[\\S|\\s]+?FROM[\\s]+?(\\S+)", Pattern.CASE_INSENSITIVE).matcher(canonQuery);
|
||||
while (m.find()) {
|
||||
return new StringBuffer().append(node.getNode().getDefiningType().getApexName()).append(":")
|
||||
.append(m.group(1)).toString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getReturnType(final ASTMethod method) {
|
||||
return new StringBuilder().append(method.getNode().getDefiningType().getApexName()).append(":")
|
||||
.append(method.getNode().getMethodInfo().getEmitSignature().getReturnType().getApexName()).toString();
|
||||
|
||||
+32
-2
@@ -2,6 +2,37 @@
|
||||
|
||||
<test-data>
|
||||
|
||||
<test-code>
|
||||
<description>Proper CRUD checks for Aggregate Result</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public void justGiveMeFoo() {
|
||||
if (Opportunity.sObjectType.getDescribe().isAccessible()) {
|
||||
return;
|
||||
}
|
||||
AggregateResult[] test = [SELECT Id FROM Opportunity];
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
|
||||
<test-code>
|
||||
<description>Proper CRUD checks for Aggregate Result return</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public AggregateResult[] justGiveMeFoo() {
|
||||
if (Opportunity.sObjectType.getDescribe().isAccessible()) {
|
||||
return null;
|
||||
}
|
||||
return [SELECT Id FROM Opportunity];
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>Not a getter</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
@@ -688,7 +719,7 @@ public class Foo {
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
|
||||
<test-code>
|
||||
<description>Field detection</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
@@ -703,5 +734,4 @@ public class MyProfilePageController {
|
||||
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
</test-data>
|
||||
Reference in new issue
Block a user