1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license MIT, http://opensource.org/licenses/MIT |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2016 |
6
|
|
|
* @package symfony |
7
|
|
|
* @subpackage Command |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\ShopBundle\Command; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symfony\Component\Console\Question\Question; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Creates new accounts or resets their passwords |
22
|
|
|
* @package symfony |
23
|
|
|
* @subpackage Command |
24
|
|
|
*/ |
25
|
|
|
class AccountCommand extends Command |
26
|
|
|
{ |
27
|
|
|
protected static $defaultName = 'aimeos:account'; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Configures the command name and description. |
32
|
|
|
*/ |
33
|
|
|
protected function configure() |
34
|
|
|
{ |
35
|
|
|
$this->setName( self::$defaultName ); |
36
|
|
|
$this->setDescription( 'Creates new (admin) accounts' ); |
37
|
|
|
$this->addArgument( 'email', InputArgument::REQUIRED, 'E-mail address of the account that should be created' ); |
38
|
|
|
$this->addArgument( 'site', InputArgument::OPTIONAL, 'Site codes to create accounts for like "default"', 'default' ); |
39
|
|
|
$this->addOption( 'password', null, InputOption::VALUE_REQUIRED, 'Optional password for the account (will ask for if not given)' ); |
40
|
|
|
$this->addOption( 'super', null, InputOption::VALUE_NONE, 'If account should have super user privileges' ); |
41
|
|
|
$this->addOption( 'admin', null, InputOption::VALUE_NONE, 'If account should have administrator privileges' ); |
42
|
|
|
$this->addOption( 'editor', null, InputOption::VALUE_NONE, 'If account should have limited editor privileges' ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Execute the console command. |
48
|
|
|
* |
49
|
|
|
* @param InputInterface $input Input object |
50
|
|
|
* @param OutputInterface $output Output object |
51
|
|
|
*/ |
52
|
|
|
protected function execute( InputInterface $input, OutputInterface $output ) |
53
|
|
|
{ |
54
|
|
|
$email = $input->getArgument( 'email' ); |
55
|
|
|
|
56
|
|
|
if( ( $password = $input->getOption( 'password' ) ) === null ) |
57
|
|
|
{ |
58
|
|
|
$helper = $this->getHelper( 'question' ); |
59
|
|
|
$question = new Question( 'Password' ); |
60
|
|
|
$question->setHidden( true ); |
61
|
|
|
|
62
|
|
|
$password = $helper->ask( $input, $output, $question ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$context = $this->getContainer()->get( 'aimeos.context' )->get( false, 'command' ); |
66
|
|
|
$context->setEditor( 'aimeos:account' ); |
67
|
|
|
|
68
|
|
|
$localeManager = \Aimeos\MShop::create( $context, 'locale' ); |
69
|
|
|
$localeItem = $localeManager->bootstrap( $input->getArgument( 'site' ) ?: 'default', '', '', false ); |
|
|
|
|
70
|
|
|
$context->setLocale( $localeItem ); |
71
|
|
|
|
72
|
|
|
$manager = \Aimeos\MShop::create( $context, 'customer' ); |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$item = $manager->findItem( $email ); |
|
|
|
|
76
|
|
|
} catch( \Aimeos\MShop\Exception $e ) { |
77
|
|
|
$item = $manager->createItem(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$item = $item->setCode( $email )->setLabel( $email )->setPassword( $password )->setStatus( 1 ); |
81
|
|
|
$item->getPaymentAddress()->setEmail( $email ); |
82
|
|
|
|
83
|
|
|
$manager->saveItem( $this->addGroups( $input, $output, $context, $item ), false ); |
84
|
|
|
$this->addRoles( $input, $email ); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Adds the group to the given user |
90
|
|
|
* |
91
|
|
|
* @param InputInterface $input Input object |
92
|
|
|
* @param OutputInterface $output Output object |
93
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
94
|
|
|
* @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
95
|
|
|
*/ |
96
|
|
|
protected function addGroups( InputInterface $input, OutputInterface $output, |
97
|
|
|
\Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user ) |
98
|
|
|
{ |
99
|
|
|
if( $input->getOption( 'admin' ) ) { |
100
|
|
|
$this->addGroup( $input, $output, $context, $user, 'admin' ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if( $input->getOption( 'editor' ) ) { |
104
|
|
|
$this->addGroup( $input, $output, $context, $user, 'editor' ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $user; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Adds the group to the given user |
113
|
|
|
* |
114
|
|
|
* @param InputInterface $input Input object |
115
|
|
|
* @param OutputInterface $output Output object |
116
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
117
|
|
|
* @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
118
|
|
|
* @param string $group Unique customer group code |
119
|
|
|
*/ |
120
|
|
|
protected function addGroup( InputInterface $input, OutputInterface $output, |
121
|
|
|
\Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user, $group ) |
122
|
|
|
{ |
123
|
|
|
$output->writeln( sprintf( 'Add "%1$s" group to user "%2$s" for sites', $group, $user->getCode() ) ); |
124
|
|
|
$output->writeln( '- ' . $context->getLocale()->getSite()->getCode() ); |
125
|
|
|
|
126
|
|
|
$groupId = $this->getGroupItem( $context, $group )->getId(); |
127
|
|
|
return $user->setGroups( array_merge( $user->getGroups(), [$groupId] ) ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Adds required roles to user identified by his e-mail |
133
|
|
|
* |
134
|
|
|
* @param InputInterface $input Input object |
135
|
|
|
* @param string $email Unique e-mail address |
136
|
|
|
*/ |
137
|
|
|
protected function addRoles( InputInterface $input, $email ) |
138
|
|
|
{ |
139
|
|
|
if( $this->getContainer()->has( 'fos_user.user_manager' ) ) |
140
|
|
|
{ |
141
|
|
|
$userManager = $this->getContainer()->get( 'fos_user.user_manager' ); |
142
|
|
|
|
143
|
|
|
if( ( $fosUser = $userManager->findUserByUsername( $email ) ) === null ) { |
144
|
|
|
throw new \RuntimeException( 'No user created' ); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$fosUser->setSuperAdmin( false ); |
148
|
|
|
|
149
|
|
|
if( $input->getOption( 'super' ) ) { |
150
|
|
|
$fosUser->setSuperAdmin( true ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if( $input->getOption( 'admin' ) || $input->getOption( 'editor' ) ) { |
154
|
|
|
$fosUser->addRole( 'ROLE_ADMIN' ); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$userManager->updateUser( $fosUser ); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Returns the customer group item for the given code |
164
|
|
|
* |
165
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
166
|
|
|
* @param string $code Unique customer group code |
167
|
|
|
* @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object |
168
|
|
|
*/ |
169
|
|
|
protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, $code ) |
170
|
|
|
{ |
171
|
|
|
$manager = \Aimeos\MShop::create( $context, 'customer/group' ); |
172
|
|
|
|
173
|
|
|
try |
174
|
|
|
{ |
175
|
|
|
$item = $manager->findItem( $code ); |
176
|
|
|
} |
177
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
178
|
|
|
{ |
179
|
|
|
$item = $manager->createItem(); |
180
|
|
|
$item->setLabel( $code ); |
|
|
|
|
181
|
|
|
$item->setCode( $code ); |
|
|
|
|
182
|
|
|
|
183
|
|
|
$manager->saveItem( $item ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $item; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|