Skip to content

Commit db63554

Browse files
committed
Merge pull request #10 from maartenJacobs/unwanted-profile-creation
Unwanted profile creation
2 parents 6e28315 + 5035ba5 commit db63554

23 files changed

Lines changed: 678 additions & 104 deletions

Command/MigrateUsersCommand.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the CCDNUser ProfileBundle
5+
*
6+
* (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
7+
*
8+
* Available on github <http://www.github.com/codeconsortium/>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace CCDNUser\ProfileBundle\Command;
15+
16+
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
use CCDNUser\ProfileBundle\Entity\Factory\ProfileFactoryInterface;
20+
use CCDNUser\ProfileBundle\Model\FrontModel\UserModel;
21+
use CCDNUser\ProfileBundle\Model\FrontModel\ProfileModel;
22+
23+
/**
24+
* Locates users without an attached profile, and attaches a default profile to them.
25+
*
26+
* @category CCDNUser
27+
* @package ProfileBundle
28+
*
29+
* @author Maarten Jacobs <maarten.j.jacobs@gmail.com>
30+
* @license http://opensource.org/licenses/MIT MIT
31+
* @version Release: 2.0
32+
* @link https://github.com/codeconsortium/CCDNUserProfileBundle
33+
*
34+
*/
35+
class MigrateUsersCommand extends Command
36+
{
37+
/**
38+
* @var UserModel
39+
*/
40+
private $userModel;
41+
42+
/**
43+
* @var ProfileModel
44+
*/
45+
private $profileModel;
46+
47+
/**
48+
* @var ProfileFactoryInterface
49+
*/
50+
private $profileFactory;
51+
52+
public function __construct(
53+
UserModel $userModel,
54+
ProfileModel $profileModel,
55+
ProfileFactoryInterface $profileFactory
56+
) {
57+
parent::__construct();
58+
59+
$this->userModel = $userModel;
60+
$this->profileModel = $profileModel;
61+
$this->profileFactory = $profileFactory;
62+
}
63+
64+
protected function configure()
65+
{
66+
parent::configure();
67+
68+
$this->setName('ccdn-user-profile:migrate-users')
69+
->setDescription('Creates a default profile for users without a profile.');
70+
}
71+
72+
/**
73+
* @param InputInterface $input
74+
* @param OutputInterface $output
75+
* @return null
76+
*/
77+
public function execute(InputInterface $input, OutputInterface $output)
78+
{
79+
// Locate users without a profile.
80+
$profilelessUsers = $this->userModel->findAllUsersWithoutProfiles();
81+
$output->writeln(sprintf('Migrating %d users without profiles.', count($profilelessUsers)));
82+
83+
// Attach a profile to each of them.
84+
foreach ($profilelessUsers as $user) {
85+
$profile = $this->profileFactory->createDefaultProfile();
86+
$profile->setUser($user);
87+
$user->setProfile($profile);
88+
$this->userModel->saveUser($user);
89+
$this->profileModel->saveProfile($profile);
90+
}
91+
92+
// Notify the user we're done.
93+
$output->writeln(sprintf('Migrated %d users without profiles.', count($profilelessUsers)));
94+
}
95+
}

DependencyInjection/CCDNUserProfileExtension.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public function load(array $configs, ContainerBuilder $container)
5656
$config = $this->processConfiguration($configuration, $configs);
5757

5858
// Class file namespaces.
59+
$this->getFactorySection($config, $container);
60+
$this->getEventListenerSection($config, $container);
5961
$this->getEntitySection($config, $container);
6062
$this->getGatewaySection($config, $container);
6163
$this->getModelSection($config, $container);
@@ -75,6 +77,9 @@ public function load(array $configs, ContainerBuilder $container)
7577
// Load Service definitions.
7678
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
7779
$loader->load('services.yml');
80+
$loader->load('services/factory.yml');
81+
$loader->load('services/event-listener.yml');
82+
$loader->load('services/command.yml');
7883
$loader->load('services/components.yml');
7984
$loader->load('services/forms-profile.yml');
8085
$loader->load('services/model-gateway.yml');
@@ -83,6 +88,34 @@ public function load(array $configs, ContainerBuilder $container)
8388
$loader->load('services/model.yml');
8489
}
8590

91+
/**
92+
*
93+
* @access private
94+
* @param array $config
95+
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
96+
* @return \CCDNUser\ProfileBundle\DependencyInjection\CCDNUserProfileExtension
97+
*/
98+
private function getEventListenerSection(array $config, ContainerBuilder $container)
99+
{
100+
$container->setParameter('ccdn_user_profile.listener.user_creation.class', $config['listener']['user_creation']['class']);
101+
102+
return $this;
103+
}
104+
105+
/**
106+
*
107+
* @access private
108+
* @param array $config
109+
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
110+
* @return \CCDNUser\ProfileBundle\DependencyInjection\CCDNUserProfileExtension
111+
*/
112+
private function getFactorySection(array $config, ContainerBuilder $container)
113+
{
114+
$container->setParameter('ccdn_user_profile.factory.profile.class', $config['factory']['profile']['class']);
115+
116+
return $this;
117+
}
118+
86119
/**
87120
*
88121
* @access private

DependencyInjection/Configuration.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public function getConfigTreeBuilder()
9494
$this->addManagerSection($rootNode);
9595
$this->addFormSection($rootNode);
9696
$this->addComponentSection($rootNode);
97+
$this->addFactorySection($rootNode);
98+
$this->addEventListenerSection($rootNode);
9799

98100
// Configuration stuff.
99101
$this->addSEOSection($rootNode);
@@ -103,6 +105,60 @@ public function getConfigTreeBuilder()
103105
return $treeBuilder;
104106
}
105107

108+
/**
109+
*
110+
* @access private
111+
* @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $node
112+
* @return \CCDNUser\ProfileBundle\DependencyInjection\Configuration
113+
*/
114+
private function addEventListenerSection(ArrayNodeDefinition $node)
115+
{
116+
$node
117+
->addDefaultsIfNotSet()
118+
->children()
119+
->arrayNode('listener')
120+
->addDefaultsIfNotSet()
121+
->children()
122+
->arrayNode('user_creation')
123+
->addDefaultsIfNotSet()
124+
->canBeUnset()
125+
->children()
126+
->scalarNode('class')->defaultValue('CCDNUser\ProfileBundle\EventListener\ProfileUserCreationListener')->end()
127+
->end()
128+
->end()
129+
->end()
130+
->end()
131+
->end()
132+
;
133+
}
134+
135+
/**
136+
*
137+
* @access private
138+
* @param \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition $node
139+
* @return \CCDNUser\ProfileBundle\DependencyInjection\Configuration
140+
*/
141+
private function addFactorySection(ArrayNodeDefinition $node)
142+
{
143+
$node
144+
->addDefaultsIfNotSet()
145+
->children()
146+
->arrayNode('factory')
147+
->addDefaultsIfNotSet()
148+
->children()
149+
->arrayNode('profile')
150+
->addDefaultsIfNotSet()
151+
->canBeUnset()
152+
->children()
153+
->scalarNode('class')->defaultValue('CCDNUser\ProfileBundle\Entity\Factory\ProfileFactory')->end()
154+
->end()
155+
->end()
156+
->end()
157+
->end()
158+
->end()
159+
;
160+
}
161+
106162
/**
107163
*
108164
* @access private

Entity/Factory/ProfileFactory.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the CCDNUser ProfileBundle
5+
*
6+
* (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
7+
*
8+
* Available on github <http://www.github.com/codeconsortium/>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace CCDNUser\ProfileBundle\Entity\Factory;
15+
16+
use CCDNUser\ProfileBundle\Entity\Profile;
17+
18+
/**
19+
*
20+
* @category CCDNUser
21+
* @package ProfileBundle
22+
*
23+
* @author Maarten Jacobs <maarten.j.jacobs@gmail.com>
24+
* @license http://opensource.org/licenses/MIT MIT
25+
* @version Release: 2.0
26+
* @link https://github.com/codeconsortium/CCDNUserProfileBundle
27+
*
28+
*/
29+
class ProfileFactory implements ProfileFactoryInterface
30+
{
31+
/**
32+
* @var string
33+
*/
34+
private $profileClass;
35+
36+
/**
37+
* @param string $profileClass
38+
*/
39+
public function __construct($profileClass)
40+
{
41+
$this->profileClass = $profileClass;
42+
}
43+
44+
/**
45+
* Creates a default profile for users.
46+
*
47+
* @return Profile
48+
*/
49+
public function createDefaultProfile()
50+
{
51+
return new $this->profileClass();
52+
}
53+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the CCDNUser ProfileBundle
5+
*
6+
* (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
7+
*
8+
* Available on github <http://www.github.com/codeconsortium/>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace CCDNUser\ProfileBundle\Entity\Factory;
15+
16+
use CCDNUser\ProfileBundle\Entity\Profile;
17+
18+
/**
19+
*
20+
* @category CCDNUser
21+
* @package ProfileBundle
22+
*
23+
* @author Maarten Jacobs <maarten.j.jacobs@gmail.com>
24+
* @license http://opensource.org/licenses/MIT MIT
25+
* @version Release: 2.0
26+
* @link https://github.com/codeconsortium/CCDNUserProfileBundle
27+
*
28+
*/
29+
interface ProfileFactoryInterface
30+
{
31+
/**
32+
* Creates a default profile for users.
33+
*
34+
* @return Profile
35+
*/
36+
public function createDefaultProfile();
37+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the CCDNUser ProfileBundle
5+
*
6+
* (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
7+
*
8+
* Available on github <http://www.github.com/codeconsortium/>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace CCDNUser\ProfileBundle\EventListener;
15+
16+
use CCDNUser\ProfileBundle\Entity\Factory\ProfileFactoryInterface;
17+
use Symfony\Component\Security\Core\User\UserInterface;
18+
use Doctrine\ORM\Event\LifecycleEventArgs;
19+
20+
/**
21+
* Listens to the creation of Symfony users and attaches a default profile.
22+
*
23+
* @category CCDNUser
24+
* @package ProfileBundle
25+
*
26+
* @author Maarten Jacobs <maarten.j.jacobs@gmail.com>
27+
* @license http://opensource.org/licenses/MIT MIT
28+
* @version Release: 2.0
29+
* @link https://github.com/codeconsortium/CCDNUserProfileBundle
30+
*
31+
* @todo relying on the Symfony UserInterface in this case is incorrect, as we are expecting an object that has a
32+
* `setProfile` method
33+
*/
34+
class ProfileUserCreationListener
35+
{
36+
/**
37+
* @var ProfileFactoryInterface
38+
*/
39+
private $profileFactory;
40+
41+
/**
42+
* @param ProfileFactoryInterface $profileFactory
43+
*/
44+
public function __construct(ProfileFactoryInterface $profileFactory)
45+
{
46+
$this->profileFactory = $profileFactory;
47+
}
48+
49+
/**
50+
* Listens to the prePersist event for Symfony users and attaches a default profile.
51+
*
52+
* @param LifecycleEventArgs $args
53+
*/
54+
public function prePersist(LifecycleEventArgs $args)
55+
{
56+
$entity = $args->getEntity();
57+
if ($entity instanceof UserInterface) {
58+
$profile = $this->profileFactory->createDefaultProfile();
59+
$profile->setUser($entity);
60+
$entity->setProfile($profile);
61+
}
62+
}
63+
}
64+

Model/Component/Gateway/UserGateway.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,15 @@ public function deleteUser(UserInterface $user)
136136

137137
return $this;
138138
}
139+
140+
/**
141+
*
142+
* @access public
143+
* @param QueryBuilder $qb
144+
* @return \Doctrine\Common\Collections\ArrayCollection
145+
*/
146+
public function findAll(QueryBuilder $qb)
147+
{
148+
return $this->all($qb);
149+
}
139150
}

0 commit comments

Comments
 (0)