1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Tenant; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\User; |
6
|
|
|
use AppBundle\EventListener\User\IdentityListener; |
7
|
|
|
use AppBundle\Service\UserService; |
8
|
|
|
use Ds\Component\Tenant\Loader\Loader; |
9
|
|
|
use Symfony\Component\Yaml\Yaml; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Users |
13
|
|
|
*/ |
14
|
|
|
class Users implements Loader |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \AppBundle\Service\UserService |
18
|
|
|
*/ |
19
|
|
|
protected $userService; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor |
23
|
|
|
* |
24
|
|
|
* @param \AppBundle\Service\UserService $userService |
25
|
|
|
*/ |
26
|
|
|
public function __construct(UserService $userService) |
27
|
|
|
{ |
28
|
|
|
$this->userService = $userService; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function load(array $data) |
35
|
|
|
{ |
36
|
|
|
$metadata = $this->userService->getManager()->getClassMetadata(User::class); |
37
|
|
|
|
38
|
|
|
foreach ($metadata->entityListeners as $event => $listeners) { |
39
|
|
|
foreach ($listeners as $key => $listener) { |
40
|
|
|
if (IdentityListener::class === $listener['class']) { |
41
|
|
|
unset($metadata->entityListeners[$event][$key]); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$yml = file_get_contents('/srv/api-platform/src/AppBundle/Resources/tenant/users.yml'); |
47
|
|
|
|
48
|
|
|
// @todo Figure out how symfony does parameter binding and use the same technique |
49
|
|
|
$yml = strtr($yml, [ |
50
|
|
|
'%user.system.uuid%' => $data['user']['system']['uuid'], |
51
|
|
|
'%user.system.password%' => $data['user']['system']['password'], |
52
|
|
|
'%user.admin.uuid%' => $data['user']['admin']['uuid'], |
53
|
|
|
'%user.admin.password%' => $data['user']['admin']['password'], |
54
|
|
|
'%business_unit.administration.uuid%' => $data['business_unit']['administration']['uuid'], |
55
|
|
|
'%tenant.uuid%' => $data['tenant']['uuid'] |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
$users = Yaml::parse($yml, YAML::PARSE_OBJECT_FOR_MAP); |
59
|
|
|
$manager = $this->userService->getCustomManager(); |
60
|
|
|
|
61
|
|
|
foreach ($users->objects as $object) { |
62
|
|
|
$object = (object) array_merge((array) $users->prototype, (array) $object); |
63
|
|
|
$user = $manager->createUser(); |
64
|
|
|
$user |
65
|
|
|
->setUsername($object->username) |
66
|
|
|
->setEmail($object->email) |
67
|
|
|
->setPlainPassword($object->password) |
68
|
|
|
->setRoles($object->roles) |
69
|
|
|
->setOwner($object->owner) |
|
|
|
|
70
|
|
|
->setOwnerUuid($object->owner_uuid) |
71
|
|
|
->setIdentity($object->identity) |
72
|
|
|
->setIdentityUuid($object->identity_uuid) |
73
|
|
|
->setEnabled($object->enabled) |
74
|
|
|
->setTenant($object->tenant); |
75
|
|
|
$manager->updateUser($user); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|