Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package fr.adrienbrault.idea.symfony2plugin.security;

import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import de.espend.idea.php.annotation.extension.PhpAnnotationReferenceProvider;
import de.espend.idea.php.annotation.extension.parameter.AnnotationPropertyParameter;
import de.espend.idea.php.annotation.extension.parameter.PhpAnnotationReferenceProviderParameter;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.security.utils.VoterUtil;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Support: @IsGranted("VOTER_ATTRIBUTE")
*/
public class IsGrantedAnnotationReferences implements PhpAnnotationReferenceProvider {
private static String IS_GRANTED_CLASS = "Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\IsGranted";

@Nullable
@Override
public PsiReference[] getPropertyReferences(AnnotationPropertyParameter annotationPropertyParameter, PhpAnnotationReferenceProviderParameter phpAnnotationReferenceProviderParameter) {
Project project = annotationPropertyParameter.getProject();
if(!Symfony2ProjectComponent.isEnabled(project) || !(annotationPropertyParameter.getElement() instanceof StringLiteralExpression) || !PhpElementsUtil.isEqualClassName(annotationPropertyParameter.getPhpClass(), IS_GRANTED_CLASS)) {
return new PsiReference[0];
}

if(annotationPropertyParameter.getType() != AnnotationPropertyParameter.Type.DEFAULT) {
return new PsiReference[0];
}

return new PsiReference[] {
new VoterReference(((StringLiteralExpression) annotationPropertyParameter.getElement()))
};
}

private static class VoterReference extends PsiPolyVariantReferenceBase<PsiElement> {
@NotNull
private final StringLiteralExpression element;

VoterReference(@NotNull StringLiteralExpression element) {
super(element);
this.element = element;
}

@NotNull
@Override
public Object[] getVariants() {
VoterUtil.LookupElementPairConsumer consumer = new VoterUtil.LookupElementPairConsumer();
VoterUtil.visitAttribute(this.myElement.getProject(), consumer);
return consumer.getLookupElements().toArray();
}

@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
VoterUtil.TargetPairConsumer voterConsumer = new VoterUtil.TargetPairConsumer(element.getContents());
VoterUtil.visitAttribute(this.myElement.getProject(), voterConsumer);
return PsiElementResolveResult.createResults(voterConsumer.getValues());
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,9 @@
<PhpAnnotationReferenceProvider implementation="fr.adrienbrault.idea.symfony2plugin.doctrine.DoctrineAnnotationReferencedColumnReferences"/>
<PhpAnnotationDocTagAnnotator implementation="fr.adrienbrault.idea.symfony2plugin.twig.annotation.TemplateAnnotationAnnotator"/>
<PhpAnnotationDocTagGotoHandler implementation="fr.adrienbrault.idea.symfony2plugin.twig.annotation.TemplateAnnotationGotoHandler"/>

<!-- @IsGranted -->
<PhpAnnotationReferenceProvider implementation="fr.adrienbrault.idea.symfony2plugin.security.IsGrantedAnnotationReferences"/>
</extensions>

<extensions defaultExtensionNs="de.espend.idea.php.toolbox.extension">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fr.adrienbrault.idea.symfony2plugin.tests.security;

import com.intellij.patterns.PlatformPatterns;
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;

public class IsGrantedAnnotationReferencesTest extends SymfonyLightCodeInsightFixtureTestCase {
public void setUp() throws Exception {
super.setUp();

myFixture.copyFileToProject("security.yml");
myFixture.copyFileToProject("classes.php");
}

protected String getTestDataPath() {
return "src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/security/fixtures";
}

public void testThatIsGrantedAnnotationProvidesCompletion() {
assertCompletionContains(
"test.php",
"<?php\n" +
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\IsGranted;\n" +
"" +
"/**\n" +
"* @IsGranted(\"<caret>\")\n" +
"*/\n" +
"function test() {};\n" +
"",
"YAML_ROLE_USER_FOOBAR"
);
}

public void testThatIsGrantedAnnotationProvidesRoleNavigation() {
assertReferenceMatchOnParent(
"test.php",
"<?php\n" +
"use Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\IsGranted;\n" +
"/**\n" +
"* @IsGranted(\"YAML_ROLE<caret>_USER_FOOBAR\")\n" +
"*/\n" +
"function test() {};\n" +
"",
PlatformPatterns.psiElement()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ public function isGranted($attributes, $object = null);
}
}

/**
* @Annotation
*/
namespace Sensio\Bundle\FrameworkExtraBundle\Configuration
{
/**
* @Annotation
*/
class Security
{
}
}

/**
* @Annotation
*/
class IsGranted
{
}
}