Skip to content

Commit 37deabf

Browse files
committed
Added user repository
1 parent 1d57543 commit 37deabf

9 files changed

+153
-16
lines changed

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,34 @@
22

33
```console
44
composer require ggbb/symfony-user-permission
5+
```
6+
7+
```
8+
// ggbb_user_role.yaml
9+
ggbb_user_permission:
10+
entity:
11+
user: App\Entity\User
12+
user_role: App\Entity\UserRole
13+
```
14+
15+
```
16+
// security.yaml
17+
security:
18+
providers:
19+
users:
20+
id: Ggbb\SymfonyUserPermission\Security\UserProvider
21+
```
22+
```
23+
// UserRepository.php
24+
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface, UserLoaderInterface
25+
{
26+
use UserRoleUserLoaderTrait;
27+
...
28+
}
29+
```
30+
```
31+
// User.php
32+
class User implements UserInterface, UserRoleFieldInterface
33+
{
34+
use GetRolesMethodTrait;
535
```

config/services.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ services:
66
autowire: true
77
tags:
88
- { name: console.command }
9+
10+
Ggbb\SymfonyUserPermission\Security\UserProvider:
11+
autowire: true # Automatically injects dependencies in your services.
12+
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

src/Command/CreateDefaultUserRoleCommand.php

+38-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
namespace Ggbb\SymfonyUserPermission\Command;
44

55
use Doctrine\ORM\EntityManagerInterface;
6+
use Ggbb\SymfonyUserPermission\Entity\Interface\UserRoleFieldInterface;
67
use Ggbb\SymfonyUserPermission\Entity\Interface\UserRoleInterface;
78
use Ggbb\SymfonyUserPermission\GgbbUserPermissionBundle;
89
use Symfony\Component\Console\Attribute\AsCommand;
910
use Symfony\Component\Console\Command\Command;
1011
use Symfony\Component\Console\Input\InputInterface;
1112
use Symfony\Component\Console\Output\OutputInterface;
13+
use Symfony\Component\Console\Style\SymfonyStyle;
1214
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
15+
use Symfony\Component\Security\Core\User\UserInterface;
1316

1417
#[AsCommand(
1518
name: 'permission:create-default-user-role',
@@ -18,8 +21,8 @@
1821
class CreateDefaultUserRoleCommand extends Command
1922
{
2023
const USER_ROLES = [
21-
'ROLE_ADMIN',
2224
'ROLE_USER',
25+
'ROLE_ADMIN',
2326
];
2427

2528
public function __construct(
@@ -32,16 +35,44 @@ public function __construct(
3235

3336
protected function execute(InputInterface $input, OutputInterface $output): int
3437
{
35-
if (!$this->container->has(GgbbUserPermissionBundle::CONFIG_USER_ROLE_REPOSITORY)) {
36-
@trigger_error('Config user_role_repository not found');
38+
$io = new SymfonyStyle($input, $output);
39+
if (!$this->container->has(GgbbUserPermissionBundle::CONFIG_USER)) {
40+
@trigger_error('Config user not found');
41+
}
42+
if (!$this->container->has(GgbbUserPermissionBundle::CONFIG_USER_ROLE)) {
43+
@trigger_error('Config user_role not found');
3744
}
3845

39-
$userRoleName = $this->container->get(GgbbUserPermissionBundle::CONFIG_USER_ROLE_REPOSITORY);
46+
$defaultUserRole = null;
47+
$userRoleName = $this->container->get(GgbbUserPermissionBundle::CONFIG_USER_ROLE);
4048
foreach (self::USER_ROLES as $role) {
4149
/** @var UserRoleInterface $userRole */
4250
$userRole = new $userRoleName();
4351
$userRole->setRole($role);
4452
$this->entityManager->persist($userRole);
53+
$io->info([
54+
"Add role {$role}",
55+
]);
56+
57+
if ($userRole->getRole() === self::USER_ROLES[0]) {
58+
$defaultUserRole = $userRole;
59+
}
60+
}
61+
62+
if (!$defaultUserRole) {
63+
@trigger_error('Default user_role not found');
64+
}
65+
66+
$userName = $this->container->get(GgbbUserPermissionBundle::CONFIG_USER);
67+
$userRepository = $this->entityManager->getRepository($userName);
68+
$users = $userRepository->findBy(['userRole' => null]);
69+
/** @var UserRoleFieldInterface|UserInterface $user */
70+
foreach ($users as $user) {
71+
$user->setUserRole($defaultUserRole);
72+
$this->entityManager->persist($user);
73+
$io->info([
74+
"Add role to user {$user->getUserIdentifier()}",
75+
]);
4576
}
4677

4778
try {
@@ -52,6 +83,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5283
return Command::INVALID;
5384
}
5485

86+
$io->success([
87+
"It's ok",
88+
]);
5589
return Command::SUCCESS;
5690
}
5791
}

src/Entity/Interface/UserRoleFieldInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ interface UserRoleFieldInterface
66
{
77
public function getUserRole(): ?UserRoleInterface;
88
public function setUserRole(?UserRoleInterface $userRole): static;
9+
public function getRoles(): array;
910
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Entity\Trait;
4+
5+
trait GetRolesMethodTrait
6+
{
7+
public function getRoles(): array
8+
{
9+
if (!$this->userRole || !$this->userRole->getRole()) {
10+
return ['ROLE_USER'];
11+
}
12+
13+
return [$this->userRole->getRole()];
14+
}
15+
}

src/GgbbUserPermissionBundle.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99

1010
class GgbbUserPermissionBundle extends AbstractBundle
1111
{
12-
const CONFIG_USER_ROLE_REPOSITORY = 'ggbb_user_permission.entity.user_role';
12+
const CONFIG_USER = 'ggbb_user_permission.entity.user';
13+
const CONFIG_USER_ROLE = 'ggbb_user_permission.entity.user_role';
1314

1415
public function configure(DefinitionConfigurator $definition): void
1516
{
1617
$definition->rootNode()
1718
->children()
1819
->arrayNode('entity')
1920
->children()
20-
->scalarNode('user_role')
21+
->scalarNode('user')->end()
22+
->scalarNode('user_role')->end()
2123
->end()
2224
->end()
2325
->end();
@@ -27,8 +29,8 @@ public function loadExtension(array $config, ContainerConfigurator $containerCon
2729
{
2830
$containerConfigurator->import(__DIR__ . '/../config/services.yaml');
2931

30-
31-
$containerConfigurator->parameters()->set(self::CONFIG_USER_ROLE_REPOSITORY, $config['entity']['user_role']);
32+
$containerConfigurator->parameters()->set(self::CONFIG_USER, $config['entity']['user']);
33+
$containerConfigurator->parameters()->set(self::CONFIG_USER_ROLE, $config['entity']['user_role']);
3234
// dd($config['user_role_repository']);
3335
}
3436
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Repository\Trait;
4+
5+
use Symfony\Component\Security\Core\User\UserInterface;
6+
7+
trait UserRoleUserLoaderTrait
8+
{
9+
public function loadUserByIdentifier(string $identifier): ?UserInterface
10+
{
11+
return $this
12+
->createQueryBuilder('u')
13+
->select('u, ur')
14+
->leftJoin('u.userRole', 'ur')
15+
->where('u.phone = :phone')
16+
->setParameter('phone', $identifier)
17+
->getQuery()
18+
->getOneOrNullResult();
19+
}
20+
}

src/Repository/interface/UserRoleRepositoryInterface.php

-8
This file was deleted.

src/Security/UserProvider.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Security;
4+
5+
use Doctrine\ORM\EntityManagerInterface;
6+
use Ggbb\SymfonyUserPermission\GgbbUserPermissionBundle;
7+
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
8+
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
9+
use Symfony\Component\Security\Core\User\UserInterface;
10+
use Symfony\Component\Security\Core\User\UserProviderInterface;
11+
12+
class UserProvider implements UserProviderInterface
13+
{
14+
public function __construct(
15+
private EntityManagerInterface $entityManager,
16+
private ContainerBagInterface $container,
17+
)
18+
{
19+
}
20+
21+
public function loadUserByIdentifier(string $identifier): UserInterface
22+
{
23+
$userName = $this->container->get(GgbbUserPermissionBundle::CONFIG_USER);
24+
/** @var UserLoaderInterface $userRepository */
25+
$userRepository = $this->entityManager->getRepository($userName);
26+
27+
return $userRepository->loadUserByIdentifier($identifier);
28+
}
29+
30+
public function refreshUser(UserInterface $user): UserInterface
31+
{
32+
throw new \Exception('TODO: fill in refreshUser() inside ' . __FILE__);
33+
}
34+
35+
public function supportsClass(string $class): bool
36+
{
37+
return User::class === $class || is_subclass_of($class, User::class);
38+
}
39+
}

0 commit comments

Comments
 (0)