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
2 changes: 2 additions & 0 deletions .coderabbit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
reviews:
request_changes_workflow: true
12 changes: 7 additions & 5 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ jobs:
operating-system:
- ubuntu-latest
php-version:
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
- '8.4'
- '8.5'
Comment thread
coderabbitai[bot] marked this conversation as resolved.
steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Setup PHP ${{ matrix.php-version }}
uses: shivammathur/setup-php@v2
Expand All @@ -33,10 +35,10 @@ jobs:

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
],
"license": "MIT",
"require": {
"php": "^7.3 || ^8.0",
"php": "^8.0",
"ray/di": "~2.0",
"laminas/laminas-permissions-acl": "~2.5",
"koriym/attributes": "^1.0"
"laminas/laminas-permissions-acl": "~2.5"
},
"require-dev": {
"phpunit/phpunit": "^8.5"
Expand Down
12 changes: 1 addition & 11 deletions src/Annotation/RequiresRoles.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,10 @@

use Attribute;

/**
* @Annotation
* @Target({"CLASS", "METHOD"})
*/
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)]
final class RequiresRoles
{
/**
* @var array
*/
public $value;

public function __construct(array $value)
public function __construct(public array $value)
{
$this->value = $value;
}
}
25 changes: 11 additions & 14 deletions src/RequiredRolesInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
namespace Ray\RoleModule;

use Doctrine\Common\Annotations\Reader;
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;
use Ray\RoleModule\Annotation\RequiresRoles;
Expand All @@ -16,18 +15,12 @@

class RequiredRolesInterceptor implements MethodInterceptor
{
private $reader;
private AclInterface $acl;

/**
* @var AclInterface
*/
private $acl;

private $roleProvider;
private RoleProviderInterface $roleProvider;

public function __construct(Reader $reader, AclInterface $acl, RoleProviderInterface $roleProvider)
public function __construct(AclInterface $acl, RoleProviderInterface $roleProvider)
{
$this->reader = $reader;
$this->acl = $acl;
$this->roleProvider = $roleProvider;
}
Expand All @@ -37,12 +30,16 @@ public function __construct(Reader $reader, AclInterface $acl, RoleProviderInter
*/
public function invoke(MethodInvocation $invocation)
{
/** @var $annotation RequiresRoles */
$annotation = $this->reader->getMethodAnnotation($invocation->getMethod(), RequiresRoles::class);
if (! $annotation) {
$attrs = $invocation->getMethod()->getAttributes(RequiresRoles::class);
if (! $attrs) {
$class = new \ReflectionClass($invocation->getThis());
$annotation = $this->reader->getClassAnnotation($class, RequiresRoles::class);
$attrs = $class->getAttributes(RequiresRoles::class);
}
if (! $attrs) {
return $invocation->proceed();
}

$annotation = $attrs[0]->newInstance();
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
$target = get_class($invocation->getThis());
$this->acl->addResource(new GenericResource($target));
foreach ($annotation->value as $role) {
Expand Down
10 changes: 0 additions & 10 deletions src/ZendAclModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
*/
namespace Ray\RoleModule;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\Reader;
use Koriym\Attributes\AttributeReader;
use Koriym\Attributes\DualReader;
use Ray\RoleModule\Annotation\RequiresRoles;
use Ray\Di\AbstractModule;
use Ray\Di\Scope;
Expand Down Expand Up @@ -42,12 +38,6 @@ public function __construct(AclInterface $acl, $roleProviderClass)
*/
protected function configure()
{
$this->bind(Reader::class)->toConstructor(DualReader::class, [
'annotationReader' => 'annotation',
'attributeReader' => 'attribute',
]);
$this->bind(Reader::class)->annotatedWith('annotation')->to(AnnotationReader::class);
$this->bind(Reader::class)->annotatedWith('attribute')->to(AttributeReader::class);
$this->bind(AclInterface::class)->toInstance($this->acl);
$this->bind(RoleProviderInterface::class)->to($this->roleProvider)->in(Scope::SINGLETON);
// method
Expand Down
3 changes: 0 additions & 3 deletions tests/Fake/FakeClassResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@

use Ray\RoleModule\Annotation\RequiresRoles;

/**
* @RequiresRoles({"admin"})
*/
#[RequiresRoles(value: ['admin'])]
class FakeClassResource
{
Expand Down
3 changes: 0 additions & 3 deletions tests/Fake/FakeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

class FakeResource
{
/**
* @RequiresRoles({"admin"})
*/
#[RequiresRoles(value: ['admin'])]
public function createUser()
{
Expand Down
5 changes: 1 addition & 4 deletions tests/RequiredRoleInterceptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Ray\RoleModule;

use Doctrine\Common\Annotations\AnnotationReader;
use Koriym\Attributes\AttributeReader;
use Koriym\Attributes\DualReader;
use PHPUnit\Framework\TestCase;
use Ray\Aop\ReflectiveMethodInvocation;
use Ray\RoleModule\Exception\RequiredRolesException;
Expand All @@ -18,7 +15,7 @@ private function factory($obj, $method, AclInterface $acl, RoleProviderInterface
{
$invocation = new ReflectiveMethodInvocation(
$obj, $method, $args, [
new RequiredRolesInterceptor(new DualReader(new AnnotationReader(),new AttributeReader()), $acl, $roleProvider)
new RequiredRolesInterceptor($acl, $roleProvider)
]
);
return $invocation;
Expand Down
7 changes: 0 additions & 7 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<?php
declare(strict_types=1);

use Koriym\Attributes\AttributeReader;
use Ray\ServiceLocator\ServiceLocator;

require_once dirname(__DIR__) . '/vendor/autoload.php';

if (PHP_MAJOR_VERSION >= 8) {
ServiceLocator::setReader(new AttributeReader());
}