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->find( $email ); |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
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 ) ); |
||||||
0 ignored issues
–
show
The method
saveItem() does not exist on Aimeos\MShop\Common\Manager\Iface . Did you maybe mean saveItems() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||||||
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 ) : \Aimeos\MShop\Customer\Item\Iface |
||||||
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, \Aimeos\MShop\Context\Item\Iface $context, |
||||||
121 | \Aimeos\MShop\Customer\Item\Iface $user, string $group ) : \Aimeos\MShop\Customer\Item\Iface |
||||||
122 | { |
||||||
123 | $site = $context->getLocale()->getSiteItem()->getCode(); |
||||||
124 | $output->writeln( sprintf( 'Add "%1$s" group to user "%2$s" for site "%3$s"', $group, $user->getCode(), $site ) ); |
||||||
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, string $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, |
||||||
170 | string $code ) : \Aimeos\MShop\Customer\Item\Group\Iface |
||||||
171 | { |
||||||
172 | $manager = \Aimeos\MShop::create( $context, 'customer/group' ); |
||||||
173 | |||||||
174 | try |
||||||
175 | { |
||||||
176 | $item = $manager->find( $code ); |
||||||
177 | } |
||||||
178 | catch( \Aimeos\MShop\Exception $e ) |
||||||
179 | { |
||||||
180 | $item = $manager->createItem(); |
||||||
181 | $item->setLabel( $code ); |
||||||
182 | $item->setCode( $code ); |
||||||
183 | |||||||
184 | $manager->saveItem( $item ); |
||||||
185 | } |
||||||
186 | |||||||
187 | return $item; |
||||||
188 | } |
||||||
189 | } |
||||||
190 |