1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\AccessUrl; |
10
|
|
|
use Chamilo\CoreBundle\Entity\User; |
11
|
|
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
12
|
|
|
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository; |
13
|
|
|
use Chamilo\CoreBundle\Repository\Node\UserRepository; |
14
|
|
|
use Doctrine\DBAL\Schema\Schema; |
15
|
|
|
use Symfony\Component\Uid\Uuid; |
16
|
|
|
|
17
|
|
|
final class Version20201212114910 extends AbstractMigrationChamilo |
18
|
|
|
{ |
19
|
|
|
public function getDescription(): string |
20
|
|
|
{ |
21
|
|
|
return 'Migrate access_url, users'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function up(Schema $schema): void |
25
|
|
|
{ |
26
|
|
|
$container = $this->getContainer(); |
27
|
|
|
$doctrine = $container->get('doctrine'); |
28
|
|
|
$em = $doctrine->getManager(); |
29
|
|
|
|
30
|
|
|
$urlRepo = $container->get(AccessUrlRepository::class); |
31
|
|
|
$userRepo = $container->get(UserRepository::class); |
32
|
|
|
|
33
|
|
|
$userList = []; |
34
|
|
|
// Adding first admin as main creator also adding to the resource node tree. |
35
|
|
|
$admin = $this->getAdmin(); |
36
|
|
|
|
37
|
|
|
$adminId = $admin->getId(); |
38
|
|
|
$userList[$adminId] = $admin; |
39
|
|
|
|
40
|
|
|
$this->write('Adding admin user'); |
41
|
|
|
if (false === $admin->hasResourceNode()) { |
42
|
|
|
$resourceNode = $userRepo->addUserToResourceNode($adminId, $adminId); |
43
|
|
|
$em->persist($resourceNode); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Adding portals (AccessUrl) to the resource node tree. |
47
|
|
|
$urls = $urlRepo->findAll(); |
48
|
|
|
/** @var AccessUrl $url */ |
49
|
|
|
foreach ($urls as $url) { |
50
|
|
|
if (false === $url->hasResourceNode()) { |
51
|
|
|
$urlRepo->createNodeForResourceWithNoParent($url, $admin); |
52
|
|
|
$em->persist($url); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
$em->flush(); |
56
|
|
|
|
57
|
|
|
$sql = 'SELECT DISTINCT(user_id) FROM admin'; |
58
|
|
|
$result = $em->getConnection()->executeQuery($sql); |
59
|
|
|
$results = $result->fetchAllAssociative(); |
60
|
|
|
$adminList = []; |
61
|
|
|
if (!empty($results)) { |
62
|
|
|
$adminList = array_map('intval', array_column($results, 'user_id')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Adding users to the resource node tree. |
66
|
|
|
$batchSize = self::BATCH_SIZE; |
67
|
|
|
$counter = 1; |
68
|
|
|
$q = $em->createQuery('SELECT u FROM Chamilo\CoreBundle\Entity\User u'); |
69
|
|
|
|
70
|
|
|
$this->write('Migrating users'); |
71
|
|
|
/** @var User $userEntity */ |
72
|
|
|
foreach ($q->toIterable() as $userEntity) { |
73
|
|
|
if ($userEntity->hasResourceNode()) { |
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$userId = $userEntity->getId(); |
78
|
|
|
$this->write("Migrating user: #$userId"); |
79
|
|
|
|
80
|
|
|
$userEntity |
81
|
|
|
->setUuid(Uuid::v4()) |
82
|
|
|
->setRoles([]) |
83
|
|
|
->setRoleFromStatus($userEntity->getStatus()) |
84
|
|
|
; |
85
|
|
|
|
86
|
|
|
if (\in_array($userId, $adminList, true)) { |
87
|
|
|
$userEntity->addRole('ROLE_ADMIN'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$creatorId = $userEntity->getCreatorId(); |
91
|
|
|
$creator = null; |
92
|
|
|
if (isset($userList[$adminId])) { |
93
|
|
|
$creator = $userList[$adminId]; |
94
|
|
|
} else { |
95
|
|
|
$creator = $userRepo->find($creatorId); |
96
|
|
|
$userList[$adminId] = $creator; |
97
|
|
|
} |
98
|
|
|
if (null === $creator) { |
99
|
|
|
$creator = $admin; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$resourceNode = $userRepo->addUserToResourceNode($userId, $creator->getId()); |
103
|
|
|
$em->persist($resourceNode); |
104
|
|
|
|
105
|
|
|
if (($counter % $batchSize) === 0) { |
106
|
|
|
$em->flush(); |
107
|
|
|
$em->clear(); // Detaches all objects from Doctrine! |
108
|
|
|
} |
109
|
|
|
$counter++; |
110
|
|
|
} |
111
|
|
|
$em->flush(); |
112
|
|
|
$em->clear(); |
113
|
|
|
|
114
|
|
|
$table = $schema->getTable('user'); |
115
|
|
|
if (false === $table->hasIndex('UNIQ_8D93D649D17F50A6')) { |
116
|
|
|
$this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649D17F50A6 ON user (uuid);'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|