generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
RbacCycleInit.php
152 lines (130 loc) · 5.65 KB
/
RbacCycleInit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
declare(strict_types=1);
namespace Yiisoft\Rbac\Cycle\Command;
use Cycle\Database\DatabaseProviderInterface;
use Cycle\Database\ForeignKeyInterface;
use Cycle\Database\Table;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Yiisoft\Rbac\Item;
final class RbacCycleInit extends Command
{
protected static $defaultName = 'rbac/cycle/init';
public function __construct(
/**
* @psalm-var array{
* itemsTable: non-empty-string,
* assignmentsTable: non-empty-string,
* itemsChildrenTable?: non-empty-string
* }
*/
private array $config,
private DatabaseProviderInterface $dbal
) {
parent::__construct();
}
protected function configure(): void
{
$this
->setDescription('Create RBAC schemas')
->setHelp('This command creates schemas for RBAC using Cycle DBAL')
->addOption('force', 'f', InputOption::VALUE_OPTIONAL, 'Force re-create schemas if exists', false);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var non-empty-string $itemsChildrenTable */
$itemsChildrenTable = $this->config['itemsChildrenTable'] ?? $this->config['itemsTable'] . '_child';
$reCreate = $input->getOption('force') !== false;
/** @var Table $table */
if ($reCreate && $this->dbal->database()->hasTable($itemsChildrenTable) === true) {
$this->dropTable($itemsChildrenTable);
}
if ($reCreate && $this->dbal->database()->hasTable($this->config['assignmentsTable']) === true) {
$this->dropTable($this->config['assignmentsTable']);
}
if ($reCreate && $this->dbal->database()->hasTable($this->config['itemsTable']) === true) {
$this->dropTable($this->config['itemsTable']);
}
if ($this->dbal->database()->hasTable($this->config['itemsTable']) === false) {
$output->writeln('<fg=blue>Creating `' . $this->config['itemsTable'] . '` table...</>');
$this->createItemsTable();
$output->writeln('<bg=green>Table `' . $this->config['itemsTable'] . '` created successfully</>');
}
if ($this->dbal->database()->hasTable($itemsChildrenTable) === false) {
$output->writeln('<fg=blue>Creating `' . $itemsChildrenTable . '` table...</>');
$this->createItemsChildrenTable($itemsChildrenTable);
$output->writeln('<bg=green>Table `' . $itemsChildrenTable . '` created successfully</>');
}
if ($this->dbal->database()->hasTable($this->config['assignmentsTable']) === false) {
$output->writeln('<fg=blue>Creating `' . $this->config['assignmentsTable'] . '` table...</>');
$this->createAssignmentsTable();
$output->writeln('<bg=green>Table `' . $this->config['assignmentsTable'] . '` created successfully</>');
}
$output->writeln('<fg=green>DONE</>');
return 0;
}
private function createItemsTable(): void
{
/** @var Table $table */
$table = $this->dbal->database()->table($this->config['itemsTable']);
$schema = $table->getSchema();
$schema->string('name', 128);
$schema->enum('type', [Item::TYPE_ROLE, Item::TYPE_PERMISSION])->nullable(false);
$schema->string('description', 191)->nullable();
$schema->string('ruleName', 64)->nullable();
$schema->integer('createdAt')->nullable(false);
$schema->integer('updatedAt')->nullable(false);
$schema->index(['type']);
$schema->setPrimaryKeys(['name']);
$schema->save();
}
/**
* @param non-empty-string $itemsChildrenTable
*/
private function createItemsChildrenTable(string $itemsChildrenTable): void
{
/** @var Table $table */
$table = $this->dbal->database()->table($itemsChildrenTable);
$schema = $table->getSchema();
$schema->string('parent', 128)->nullable(false);
$schema->string('child', 128)->nullable(false);
$schema->setPrimaryKeys(['parent', 'child']);
$schema->foreignKey(['parent'])
->references($this->config['itemsTable'], ['name'])
->onDelete(ForeignKeyInterface::CASCADE)
->onUpdate(ForeignKeyInterface::CASCADE);
$schema->foreignKey(['child'])
->references($this->config['itemsTable'], ['name'])
->onDelete(ForeignKeyInterface::CASCADE)
->onUpdate(ForeignKeyInterface::CASCADE);
$schema->save();
}
private function createAssignmentsTable(): void
{
/** @var Table $table */
$table = $this->dbal->database()->table($this->config['assignmentsTable']);
$schema = $table->getSchema();
$schema->string('itemName', 128)->nullable(false);
$schema->string('userId', 128)->nullable(false);
$schema->setPrimaryKeys(['itemName', 'userId']);
$schema->integer('createdAt')->nullable(false);
$schema->foreignKey(['itemName'])
->references($this->config['itemsTable'], ['name'])
->onUpdate(ForeignKeyInterface::CASCADE)
->onDelete(ForeignKeyInterface::CASCADE);
$schema->save();
}
/**
* @param non-empty-string $tableName
*/
private function dropTable(string $tableName): void
{
/** @var Table $table */
$table = $this->dbal->database()->table($tableName);
$schema = $table->getSchema();
$schema->declareDropped();
$schema->save();
}
}