1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license MIT, http://opensource.org/licenses/MIT |
||
5 | * @copyright Aimeos (aimeos.org), 2015-2016 |
||
6 | * @package laravel |
||
7 | * @subpackage Command |
||
8 | */ |
||
9 | |||
10 | |||
11 | namespace Aimeos\Shop\Command; |
||
12 | |||
13 | use Symfony\Component\Console\Input\InputOption; |
||
14 | use Symfony\Component\Console\Input\InputArgument; |
||
15 | |||
16 | |||
17 | /** |
||
18 | * Creates new accounts or resets their passwords |
||
19 | * @package laravel |
||
20 | * @subpackage Command |
||
21 | */ |
||
22 | class AccountCommand extends AbstractCommand |
||
23 | { |
||
24 | /** |
||
25 | * The name and signature of the console command. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $signature = 'aimeos:account |
||
30 | {email? : E-Mail adress of the (admin) user (will ask for if not given)} |
||
31 | {site=default : Site to create account for} |
||
32 | {--password= : Secret password for the account (will ask for if not given)} |
||
33 | {--super : If account should have super user privileges for all sites} |
||
34 | {--admin : If account should have site administrator privileges} |
||
35 | {--editor : If account should have limited editor privileges} |
||
36 | {--api : If account should be able to access the APIs} |
||
37 | '; |
||
38 | |||
39 | /** |
||
40 | * The console command description. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $description = 'Creates new (admin) accounts'; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Execute the console command. |
||
49 | * |
||
50 | * @return mixed |
||
51 | */ |
||
52 | public function handle() |
||
53 | { |
||
54 | if( ( $code = $this->argument( 'email' ) ) === null ) { |
||
55 | $code = $this->ask( 'E-Mail' ); |
||
56 | } |
||
57 | |||
58 | if( ( $password = $this->option( 'password' ) ) === null ) { |
||
59 | $password = $this->secret( 'Password' ); |
||
60 | } |
||
61 | |||
62 | $context = $this->getLaravel()->make( 'Aimeos\Shop\Base\Context' )->get( false, 'command' ); |
||
63 | $context->setEditor( 'aimeos:account' ); |
||
64 | |||
65 | $localeManager = \Aimeos\MShop::create( $context, 'locale' ); |
||
66 | $localeItem = $localeManager->bootstrap( $this->argument( 'site' ), '', '', false ); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
67 | $context->setLocale( $localeItem ); |
||
68 | |||
69 | $this->addGroups( $context, $this->createCustomerItem( $context, $code, $password ) ); |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Associates the user to the group by their given IDs |
||
75 | * |
||
76 | * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
||
77 | * @param string $userid Unique user ID |
||
78 | * @param string $groupid Unique group ID |
||
79 | */ |
||
80 | protected function addListItem( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupid ) |
||
81 | { |
||
82 | $manager = \Aimeos\MShop::create( $context, 'customer/lists' ); |
||
83 | |||
84 | $search = $manager->createSearch(); |
||
85 | $expr = array( |
||
86 | $search->compare( '==', 'customer.lists.parentid', $userid ), |
||
87 | $search->compare( '==', 'customer.lists.refid', $groupid ), |
||
88 | $search->compare( '==', 'customer.lists.type', 'default' ), |
||
89 | $search->compare( '==', 'customer.lists.domain', 'customer/group' ), |
||
90 | ); |
||
91 | $search->setConditions( $search->combine( '&&', $expr ) ); |
||
92 | $search->setSlice( 0, 1 ); |
||
93 | |||
94 | if( count( $manager->searchItems( $search ) ) === 0 ) |
||
95 | { |
||
96 | $item = $manager->createItem(); |
||
97 | $item->setDomain( 'customer/group' ); |
||
98 | $item->setParentId( $userid ); |
||
99 | $item->setRefId( $groupid ); |
||
100 | $item->setType( 'default' ); |
||
101 | $item->setStatus( 1 ); |
||
102 | |||
103 | $manager->saveItem( $item, false ); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | |||
108 | /** |
||
109 | * Adds the group to the given user |
||
110 | * |
||
111 | * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
||
112 | * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
||
113 | */ |
||
114 | protected function addGroups( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user ) |
||
115 | { |
||
116 | \Illuminate\Foundation\Auth\User::findOrFail( $user->getId() ) |
||
117 | ->forceFill( ['superuser' => ( $this->option( 'super' ) ? 1 : 0 )] ) |
||
118 | ->save(); |
||
119 | |||
120 | if( $this->option( 'admin' ) ) { |
||
121 | $this->addGroup( $context, $user, 'admin' ); |
||
122 | } |
||
123 | |||
124 | if( $this->option( 'editor' ) ) { |
||
125 | $this->addGroup( $context, $user, 'editor' ); |
||
126 | } |
||
127 | |||
128 | if( $this->option( 'api' ) ) { |
||
129 | $this->addGroup( $context, $user, 'api' ); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | |||
134 | /** |
||
135 | * Adds the group to the given user |
||
136 | * |
||
137 | * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
||
138 | * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
||
139 | * @param string $group Unique customer group code |
||
140 | */ |
||
141 | protected function addGroup( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user, $group ) |
||
142 | { |
||
143 | $msg = 'Add "%1$s" group to user "%2$s" for site "%3$s"'; |
||
144 | $this->info( sprintf( $msg, $group, $user->getCode(), $this->argument( 'site' ) ) ); |
||
145 | |||
146 | $groupItem = $this->getGroupItem( $context, $group ); |
||
147 | $this->addListItem( $context, $user->getId(), $groupItem->getId() ); |
||
148 | } |
||
149 | |||
150 | |||
151 | /** |
||
152 | * Returns the customer item for the given e-mail and set its password |
||
153 | * |
||
154 | * If the customer doesn't exist yet, it will be created. |
||
155 | * |
||
156 | * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
||
157 | * @param string $email Unique e-mail address |
||
158 | * @param string $password New user password |
||
159 | * @return \Aimeos\MShop\Customer\Item\Iface Aimeos customer item object |
||
160 | */ |
||
161 | protected function createCustomerItem( \Aimeos\MShop\Context\Item\Iface $context, $email, $password ) |
||
162 | { |
||
163 | $manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
164 | |||
165 | try { |
||
166 | $item = $manager->findItem( $email ); |
||
167 | } catch( \Aimeos\MShop\Exception $e ) { |
||
168 | $item = $manager->createItem(); |
||
169 | } |
||
170 | |||
171 | $item->setCode( $email ); |
||
172 | $item->setLabel( $email ); |
||
173 | $item->getPaymentAddress()->setEmail( $email ); |
||
174 | $item->setPassword( $password ); |
||
175 | $item->setStatus( 1 ); |
||
176 | |||
177 | $manager->saveItem( $item ); |
||
178 | |||
179 | return $item; |
||
180 | } |
||
181 | |||
182 | |||
183 | /** |
||
184 | * Get the console command arguments. |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | protected function getArguments() |
||
189 | { |
||
190 | return array( |
||
191 | array( 'email', InputArgument::REQUIRED, 'E-mail address of the account that should be created' ), |
||
192 | array( 'site', InputArgument::OPTIONAL, 'Site code to create the account for', 'default' ), |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | |||
197 | /** |
||
198 | * Returns the customer group item for the given code |
||
199 | * |
||
200 | * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
||
201 | * @param string $code Unique customer group code |
||
202 | * @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object |
||
203 | */ |
||
204 | protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, $code ) |
||
205 | { |
||
206 | $manager = \Aimeos\MShop::create( $context, 'customer/group' ); |
||
207 | |||
208 | try |
||
209 | { |
||
210 | $item = $manager->findItem( $code ); |
||
211 | } |
||
212 | catch( \Aimeos\MShop\Exception $e ) |
||
213 | { |
||
214 | $item = $manager->createItem(); |
||
215 | $item->setLabel( $code ); |
||
216 | $item->setCode( $code ); |
||
217 | |||
218 | $manager->saveItem( $item ); |
||
219 | } |
||
220 | |||
221 | return $item; |
||
222 | } |
||
223 | |||
224 | |||
225 | /** |
||
226 | * Get the console command options. |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | protected function getOptions() |
||
231 | { |
||
232 | return array( |
||
233 | array( 'password', null, InputOption::VALUE_REQUIRED, 'Optional password for the account (will ask for if not given)' ), |
||
234 | array( 'admin', null, InputOption::VALUE_NONE, 'If account should have administrator privileges' ), |
||
235 | array( 'api', null, InputOption::VALUE_NONE, 'If account should be able to access the APIs' ), |
||
236 | array( 'editor', null, InputOption::VALUE_NONE, 'If account should have limited editor privileges' ), |
||
237 | array( 'viewer', null, InputOption::VALUE_NONE, 'If account should only have view privileges' ), |
||
238 | ); |
||
239 | } |
||
240 | } |
||
241 |