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