1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license MIT, http://opensource.org/licenses/MIT |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2023 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Shop\Command; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Creates new accounts or resets their passwords |
17
|
|
|
*/ |
18
|
|
|
class AccountCommand extends AbstractCommand |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The name and signature of the console command. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $signature = 'aimeos:account |
26
|
|
|
{email? : E-Mail adress of the (admin) user (will ask for if not given)} |
27
|
|
|
{site? : Site to create account for (will use default value if not given} |
28
|
|
|
{--password= : Secret password for the account (will ask for if not given)} |
29
|
|
|
{--super : If account should have super user privileges for all sites} |
30
|
|
|
{--admin : If account should have site administrator privileges} |
31
|
|
|
{--editor : If account should have limited editor privileges} |
32
|
|
|
'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The console command description. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $description = 'Creates new (admin) accounts'; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Execute the console command. |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
public function handle() |
48
|
|
|
{ |
49
|
|
|
$site = $this->argument( 'site' ) ?: config( 'shop.mshop.locale.site', 'default' ); |
|
|
|
|
50
|
|
|
|
51
|
|
|
if( ( $email = $this->argument( 'email' ) ) === null ) { |
52
|
|
|
$email = $this->ask( 'E-Mail' ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if( ( $password = $this->option( 'password' ) ) === null ) { |
|
|
|
|
56
|
|
|
$password = $this->secret( 'Password' ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$context = $this->getLaravel()->make( 'aimeos.context' )->get( false, 'command' ); |
60
|
|
|
$context->setEditor( 'aimeos:account' ); |
61
|
|
|
|
62
|
|
|
$localeManager = \Aimeos\MShop::create( $context, 'locale' ); |
63
|
|
|
$localeItem = $localeManager->bootstrap( $site, '', '', false, null, true ); |
|
|
|
|
64
|
|
|
$context->setLocale( $localeItem ); |
65
|
|
|
|
66
|
|
|
$manager = \Aimeos\MShop::create( $context, 'customer' ); |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$item = $manager->find( $email ); |
|
|
|
|
70
|
|
|
} catch( \Aimeos\MShop\Exception $e ) { |
71
|
|
|
$item = $manager->create(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$item = $item->setCode( $email )->setLabel( $email )->setPassword( $password )->setStatus( 1 ); |
75
|
|
|
$item->getPaymentAddress()->setEmail( $email ); |
76
|
|
|
|
77
|
|
|
$item = $manager->save( $this->addGroups( $context, $item ) ); |
78
|
|
|
|
79
|
|
|
\Illuminate\Foundation\Auth\User::findOrFail( $item->getId() ) |
80
|
|
|
->forceFill( [ |
81
|
|
|
'siteid' => $this->option( 'super' ) ? '' : $item->getSiteId(), |
82
|
|
|
'superuser' => ( $this->option( 'super' ) ? 1 : 0 ), |
83
|
|
|
'email_verified_at' => now(), |
84
|
|
|
] )->save(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Adds the group to the given user |
90
|
|
|
* |
91
|
|
|
* @param \Aimeos\MShop\ContextIface $context Aimeos context object |
92
|
|
|
* @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
93
|
|
|
* @return \Aimeos\MShop\Customer\Item\Iface Updated customer object |
94
|
|
|
*/ |
95
|
|
|
protected function addGroups( \Aimeos\MShop\ContextIface $context, |
96
|
|
|
\Aimeos\MShop\Customer\Item\Iface $user ) : \Aimeos\MShop\Customer\Item\Iface |
97
|
|
|
{ |
98
|
|
|
if( $this->option( 'admin' ) ) { |
99
|
|
|
$user = $this->addGroup( $context, $user, 'admin' ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if( $this->option( 'editor' ) ) { |
103
|
|
|
$user = $this->addGroup( $context, $user, 'editor' ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $user; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Adds the group to the given user |
112
|
|
|
* |
113
|
|
|
* @param \Aimeos\MShop\ContextIface $context Aimeos context object |
114
|
|
|
* @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
115
|
|
|
* @param string $group Unique customer group code |
116
|
|
|
*/ |
117
|
|
|
protected function addGroup( \Aimeos\MShop\ContextIface $context, \Aimeos\MShop\Customer\Item\Iface $user, |
118
|
|
|
string $group ) : \Aimeos\MShop\Customer\Item\Iface |
119
|
|
|
{ |
120
|
|
|
$msg = 'Add "%1$s" group to user "%2$s" for site "%3$s"'; |
121
|
|
|
$site = $this->argument( 'site' ) ?: config( 'shop.mshop.locale.site', 'default' ); |
|
|
|
|
122
|
|
|
$this->info( sprintf( $msg, $group, $user->getCode(), $site ) ); |
123
|
|
|
|
124
|
|
|
$item = $this->getGroupItem( $context, $group ); |
125
|
|
|
return $user->setGroups( array_merge( $user->getGroups(), [$item->getId()] ) ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the customer group item for the given code |
131
|
|
|
* |
132
|
|
|
* @param \Aimeos\MShop\ContextIface $context Aimeos context object |
133
|
|
|
* @param string $code Unique customer group code |
134
|
|
|
* @return \Aimeos\MShop\Group\Item\Iface Aimeos customer group item object |
135
|
|
|
*/ |
136
|
|
|
protected function getGroupItem( \Aimeos\MShop\ContextIface $context, string $code ) : \Aimeos\MShop\Group\Item\Iface |
137
|
|
|
{ |
138
|
|
|
$manager = \Aimeos\MShop::create( $context, 'group' ); |
139
|
|
|
|
140
|
|
|
try |
141
|
|
|
{ |
142
|
|
|
$item = $manager->find( $code ); |
143
|
|
|
} |
144
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
145
|
|
|
{ |
146
|
|
|
$item = $manager->create(); |
147
|
|
|
$item->setLabel( $code ); |
148
|
|
|
$item->setCode( $code ); |
149
|
|
|
|
150
|
|
|
$manager->save( $item ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $item; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|