Skip to content

Commit 1d57543

Browse files
committed
Added permission & added command
1 parent 5514af6 commit 1d57543

16 files changed

+223
-41
lines changed

config/services.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
ggbb_user_permission.author.email: 'devggbb@gmail.com'
3+
4+
services:
5+
Ggbb\SymfonyUserPermission\Command\CreateDefaultUserRoleCommand:
6+
autowire: true
7+
tags:
8+
- { name: console.command }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Command;
4+
5+
use Doctrine\ORM\EntityManagerInterface;
6+
use Ggbb\SymfonyUserPermission\Entity\Interface\UserRoleInterface;
7+
use Ggbb\SymfonyUserPermission\GgbbUserPermissionBundle;
8+
use Symfony\Component\Console\Attribute\AsCommand;
9+
use Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
13+
14+
#[AsCommand(
15+
name: 'permission:create-default-user-role',
16+
description: '',
17+
)]
18+
class CreateDefaultUserRoleCommand extends Command
19+
{
20+
const USER_ROLES = [
21+
'ROLE_ADMIN',
22+
'ROLE_USER',
23+
];
24+
25+
public function __construct(
26+
private EntityManagerInterface $entityManager,
27+
private ContainerBagInterface $container,
28+
string $name = null)
29+
{
30+
parent::__construct($name);
31+
}
32+
33+
protected function execute(InputInterface $input, OutputInterface $output): int
34+
{
35+
if (!$this->container->has(GgbbUserPermissionBundle::CONFIG_USER_ROLE_REPOSITORY)) {
36+
@trigger_error('Config user_role_repository not found');
37+
}
38+
39+
$userRoleName = $this->container->get(GgbbUserPermissionBundle::CONFIG_USER_ROLE_REPOSITORY);
40+
foreach (self::USER_ROLES as $role) {
41+
/** @var UserRoleInterface $userRole */
42+
$userRole = new $userRoleName();
43+
$userRole->setRole($role);
44+
$this->entityManager->persist($userRole);
45+
}
46+
47+
try {
48+
$this->entityManager->flush();
49+
} catch (\Exception $exception) {
50+
throw new \Exception('Error: '.$exception->getMessage());
51+
52+
return Command::INVALID;
53+
}
54+
55+
return Command::SUCCESS;
56+
}
57+
}

src/DependencyInjection/UserPermissionBundleExtension.php

-21
This file was deleted.

src/Entity/Interface/UserPermissionInterface.php

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Entity\Interface;
4+
5+
interface UserRoleFieldInterface
6+
{
7+
public function getUserRole(): ?UserRoleInterface;
8+
public function setUserRole(?UserRoleInterface $userRole): static;
9+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Entity\Interface;
4+
5+
use Ggbb\SymfonyUserPermission\Permission\Interface\PermissionInterface;
6+
7+
interface UserRoleInterface
8+
{
9+
public function getPermissions(): ?array;
10+
11+
public function setPermissions(array $permissions): static;
12+
13+
public function getRole(): string;
14+
15+
public function setRole(string $role): static;
16+
}

src/Entity/Trait/RoleFieldTrait.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Entity\Trait;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
trait RoleFieldTrait
8+
{
9+
#[ORM\Column(length: 255)]
10+
private ?string $role = null;
11+
12+
public function getRole(): string
13+
{
14+
return $this->role;
15+
}
16+
17+
public function setRole(string $role): static
18+
{
19+
$this->role = $role;
20+
21+
return $this;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Entity\Trait;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Doctrine\DBAL\Types\Types;
7+
8+
trait RolePermissionFieldTrait
9+
{
10+
#[ORM\Column(type: Types::JSON, nullable: true)]
11+
private ?array $permissions = null;
12+
13+
public function getPermissions(): ?array
14+
{
15+
return $this->permissions;
16+
}
17+
18+
public function setPermissions(array $permissions): static
19+
{
20+
$this->permissions = $permissions;
21+
22+
return $this;
23+
}
24+
}

src/Entity/UserRole.php

-8
This file was deleted.

src/GgbbUserPermissionBundle.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,33 @@
22

33
namespace Ggbb\SymfonyUserPermission;
44

5+
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
58
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
69

710
class GgbbUserPermissionBundle extends AbstractBundle
811
{
9-
public function getPath(): string
12+
const CONFIG_USER_ROLE_REPOSITORY = 'ggbb_user_permission.entity.user_role';
13+
14+
public function configure(DefinitionConfigurator $definition): void
15+
{
16+
$definition->rootNode()
17+
->children()
18+
->arrayNode('entity')
19+
->children()
20+
->scalarNode('user_role')
21+
->end()
22+
->end()
23+
->end();
24+
}
25+
26+
public function loadExtension(array $config, ContainerConfigurator $containerConfigurator, ContainerBuilder $containerBuilder): void
1027
{
11-
return \dirname(__DIR__);
28+
$containerConfigurator->import(__DIR__ . '/../config/services.yaml');
29+
30+
31+
$containerConfigurator->parameters()->set(self::CONFIG_USER_ROLE_REPOSITORY, $config['entity']['user_role']);
32+
// dd($config['user_role_repository']);
1233
}
1334
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Permission\Interface;
4+
5+
interface PermissionGroupInterface
6+
{
7+
public function getName(): string;
8+
9+
/**
10+
* @return array|PermissionInterface[]
11+
*/
12+
public function getPermissions(): array;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Permission\Interface;
4+
5+
interface PermissionInterface
6+
{
7+
static public function getTitle(): string;
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Permission\Permission;
4+
5+
use Ggbb\SymfonyUserPermission\Permission\Interface\PermissionInterface;
6+
7+
class TestPermission implements PermissionInterface
8+
{
9+
static public function getTitle(): string
10+
{
11+
return 'Test permission';
12+
}
13+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Permission;
4+
5+
use Ggbb\SymfonyUserPermission\Permission\Interface\PermissionGroupInterface;
6+
use Ggbb\SymfonyUserPermission\Permission\Permission\TestPermission;
7+
8+
class TestPermissionGroup implements PermissionGroupInterface
9+
{
10+
public function getName(): string
11+
{
12+
return 'TestGroupName';
13+
}
14+
15+
public function getPermissions(): array
16+
{
17+
return [
18+
TestPermission::class
19+
];
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Ggbb\SymfonyUserPermission\Repository\interface;
4+
5+
interface UserRoleRepositoryInterface
6+
{
7+
8+
}

src/Resources/config/services.yaml

-2
This file was deleted.

0 commit comments

Comments
 (0)