Skip to content

Commit 1e1c330

Browse files
committed
refactor: use same approach for config as ORM
1 parent 919a06b commit 1e1c330

24 files changed

+339
-320
lines changed

docs/permissions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Getting all permissions
3636
~~~~~~~~~~~~~~~~~~~~~~~
3737

3838
You can get a list of all permissions with the
39-
``LaravelDoctrine\ACL\Permissions\PermissionManager``
39+
``LaravelDoctrine\ACL\PermissionManager``
4040

4141
.. code:: php
4242

docs/usage.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Use the `PermissionManager` to retrieve all permissions:
7171

7272
.. code-block:: php
7373
74-
$manager = app(LaravelDoctrine\ACL\Permissions\PermissionManager::class);
74+
$manager = app(LaravelDoctrine\ACL\PermissionManager::class);
7575
$manager->getAllPermissions();
7676
7777

src/AclServiceProvider.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88
use Illuminate\Support\ServiceProvider;
99
use LaravelDoctrine\ACL\Contracts\HasPermissions;
1010
use LaravelDoctrine\ACL\Mappings\RegisterMappedEventSubscribers;
11-
use LaravelDoctrine\ACL\Permissions\PermissionManager;
1211
use LaravelDoctrine\ORM\DoctrineManager;
1312

1413
use const DIRECTORY_SEPARATOR;
1514

1615
class AclServiceProvider extends ServiceProvider
1716
{
18-
/**
19-
* Register the service provider.
20-
*/
2117
public function register(): void
2218
{
2319
$this->mergeConfig();
@@ -35,13 +31,13 @@ protected function registerDoctrineMappings(): void
3531

3632
protected function registerPaths(): void
3733
{
38-
$manager = $this->app->make(DoctrineManager::class);
3934
$permissionManager = $this->app->make(PermissionManager::class);
4035

4136
if (! $permissionManager->useDefaultPermissionEntity()) {
4237
return;
4338
}
4439

40+
$manager = $this->app->make(DoctrineManager::class);
4541
$manager->addPaths([
4642
__DIR__ . DIRECTORY_SEPARATOR . 'Permissions',
4743
]);
@@ -60,9 +56,6 @@ protected function registerGatePermissions(): void
6056
});
6157
}
6258

63-
/**
64-
* Merge config.
65-
*/
6659
protected function mergeConfig(): void
6760
{
6861
$this->mergeConfigFrom(
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LaravelDoctrine\ACL\Configurations;
6+
7+
use Illuminate\Contracts\Config\Repository;
8+
use LaravelDoctrine\ACL\Permissions\Driver\Config;
9+
use LaravelDoctrine\ACL\Permissions\Driver\PermissionDriver;
10+
11+
class ConfigPermissionsProvider implements PermissionsProvider
12+
{
13+
public function __construct(protected Repository $config)
14+
{
15+
}
16+
17+
/** @param mixed[] $settings */
18+
public function resolve(array $settings = []): PermissionDriver
19+
{
20+
return new Config($this->config->get('acl.permissions.list', []));
21+
}
22+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LaravelDoctrine\ACL\Configurations;
6+
7+
use Doctrine\ORM\EntityManagerInterface;
8+
use Doctrine\ORM\EntityRepository;
9+
use Doctrine\Persistence\ManagerRegistry;
10+
use Illuminate\Contracts\Config\Repository;
11+
use LaravelDoctrine\ACL\Permissions\Driver\Doctrine;
12+
use LaravelDoctrine\ACL\Permissions\Driver\PermissionDriver;
13+
use RunTimeException;
14+
15+
class DoctrinePermissionsProvider implements PermissionsProvider
16+
{
17+
public function __construct(protected ManagerRegistry $registry, protected Repository $config)
18+
{
19+
}
20+
21+
/** @param mixed[] $settings */
22+
public function resolve(array $settings = []): PermissionDriver
23+
{
24+
$em = $this->getEntityManager();
25+
$metadata = $em->getClassMetadata($this->getPermissionClass());
26+
27+
return new Doctrine(new EntityRepository($em, $metadata));
28+
}
29+
30+
protected function getPermissionClass(): string
31+
{
32+
$class = $this->config->get('acl.permissions.entity');
33+
34+
if (! $class) {
35+
throw new RunTimeException(
36+
'Failed to configure doctrine permissions. No entity class provided.',
37+
);
38+
}
39+
40+
return $class;
41+
}
42+
43+
protected function getEntityManager(): EntityManagerInterface
44+
{
45+
$em = $this->registry->getManagerForClass($this->getPermissionClass());
46+
47+
if (! $em) {
48+
throw new RunTimeException(
49+
'Failed to configure doctrine permissions.'
50+
. ' No entity manager found for entity: ' . $this->getPermissionClass() . '.',
51+
);
52+
}
53+
54+
return $em;
55+
}
56+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LaravelDoctrine\ACL\Configurations;
6+
7+
use LaravelDoctrine\ACL\Permissions\Driver\PermissionDriver;
8+
use LaravelDoctrine\ORM\Configuration\Driver;
9+
10+
interface PermissionsProvider extends Driver
11+
{
12+
/** @param mixed[] $settings */
13+
public function resolve(array $settings = []): PermissionDriver;
14+
}

src/Organisations/BelongsToOrganisation.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@
22

33
declare(strict_types=1);
44

5-
/**
6-
* Time: 10:29 AM
7-
*/
8-
95
namespace LaravelDoctrine\ACL\Organisations;
106

117
use LaravelDoctrine\ACL\Contracts\BelongsToOrganisation as BelongsToOrganisationContract;
12-
use LaravelDoctrine\ACL\Contracts\BelongsToOrganisations as BelongsToOrganisationsContract;
13-
use LaravelDoctrine\ACL\Contracts\Organisation as OrganisationContract;
8+
use LaravelDoctrine\ACL\Contracts\BelongsToOrganisations;
9+
use LaravelDoctrine\ACL\Contracts\Organisation;
1410

1511
use function is_array;
1612

1713
trait BelongsToOrganisation
1814
{
19-
public function belongsToOrganisation(OrganisationContract|string|array $org, bool $requireAll = false): bool
15+
public function belongsToOrganisation(Organisation|string|array $org, bool $requireAll = false): bool
2016
{
2117
if (is_array($org)) {
2218
foreach ($org as $o) {
@@ -40,7 +36,7 @@ public function belongsToOrganisation(OrganisationContract|string|array $org, bo
4036
}
4137
}
4238

43-
if ($this instanceof BelongsToOrganisationsContract) {
39+
if ($this instanceof BelongsToOrganisations) {
4440
foreach ($this->getOrganisations() as $o) {
4541
if ($this->getOrganisationName($org) === $o->getName()) {
4642
return true;
@@ -51,8 +47,8 @@ public function belongsToOrganisation(OrganisationContract|string|array $org, bo
5147
return false;
5248
}
5349

54-
protected function getOrganisationName(OrganisationContract|string $org): string
50+
protected function getOrganisationName(Organisation|string $org): string
5551
{
56-
return $org instanceof OrganisationContract ? $org->getName() : $org;
52+
return $org instanceof Organisation ? $org->getName() : $org;
5753
}
5854
}

src/Permissions/PermissionManager.php renamed to src/PermissionManager.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
declare(strict_types=1);
44

5-
namespace LaravelDoctrine\ACL\Permissions;
5+
namespace LaravelDoctrine\ACL;
66

77
use Illuminate\Support\Arr;
88
use Illuminate\Support\Collection;
9+
use LaravelDoctrine\ACL\Permissions\Permission;
10+
use LaravelDoctrine\ORM\Configuration\Manager;
911

1012
use function is_array;
1113
use function is_numeric;
@@ -55,12 +57,12 @@ public function getDefaultDriver(): string
5557

5658
public function getNamespace(): string
5759
{
58-
return __NAMESPACE__;
60+
return __NAMESPACE__ . '\\Configurations';
5961
}
6062

6163
public function getClassSuffix(): string
6264
{
63-
return 'PermissionDriver';
65+
return 'PermissionsProvider';
6466
}
6567

6668
public function useDefaultPermissionEntity(): bool

src/Permissions/ConfigPermissionDriver.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Permissions/DoctrinePermissionDriver.php

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/Permissions/Driver/Config.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LaravelDoctrine\ACL\Permissions\Driver;
6+
7+
use Illuminate\Support\Collection;
8+
9+
class Config implements PermissionDriver
10+
{
11+
protected Collection $collection;
12+
13+
/** @var array<string> */
14+
public function __construct(protected array $permissions)
15+
{
16+
$this->collection = new Collection($this->permissions);
17+
}
18+
19+
public function getAllPermissions(): Collection
20+
{
21+
return $this->collection;
22+
}
23+
}

src/Permissions/Driver/Doctrine.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LaravelDoctrine\ACL\Permissions\Driver;
6+
7+
use Doctrine\ORM\EntityRepository;
8+
use Illuminate\Support\Collection;
9+
use LaravelDoctrine\ACL\Contracts\Permission;
10+
11+
use function array_map;
12+
13+
class Doctrine implements PermissionDriver
14+
{
15+
public function __construct(protected EntityRepository $repository)
16+
{
17+
}
18+
19+
public function getAllPermissions(): Collection
20+
{
21+
// TODO: We can improve performance by fetching only permission names.
22+
$permissions = $this->repository->findAll();
23+
$permissions = array_map(static fn (Permission $permission) => $permission->getName(), $permissions);
24+
25+
return new Collection($permissions);
26+
}
27+
}

src/Permissions/PermissionDriver.php renamed to src/Permissions/Driver/PermissionDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace LaravelDoctrine\ACL\Permissions;
5+
namespace LaravelDoctrine\ACL\Permissions\Driver;
66

77
use Illuminate\Support\Collection;
88

0 commit comments

Comments
 (0)