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 | {--api : If account should be able to access the APIs} |
||
33 | '; |
||
34 | |||
35 | /** |
||
36 | * The console command description. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $description = 'Creates new (admin) accounts'; |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Execute the console command. |
||
45 | * |
||
46 | * @return mixed |
||
47 | */ |
||
48 | public function handle() |
||
49 | { |
||
50 | $site = $this->argument( 'site' ) ?: config( 'shop.mshop.locale.site', 'default' ); |
||
51 | |||
52 | if( ( $email = $this->argument( 'email' ) ) === null ) { |
||
53 | $email = $this->ask( 'E-Mail' ); |
||
54 | } |
||
55 | |||
56 | if( ( $password = $this->option( 'password' ) ) === null ) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
57 | $password = $this->secret( 'Password' ); |
||
58 | } |
||
59 | |||
60 | $context = $this->getLaravel()->make( 'aimeos.context' )->get( false, 'command' ); |
||
61 | $context->setEditor( 'aimeos:account' ); |
||
62 | |||
63 | $localeManager = \Aimeos\MShop::create( $context, 'locale' ); |
||
64 | $localeItem = $localeManager->bootstrap( $site, '', '', false, null, true ); |
||
65 | $context->setLocale( $localeItem ); |
||
66 | |||
67 | $manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
68 | |||
69 | try { |
||
70 | $item = $manager->find( $email ); |
||
71 | } catch( \Aimeos\MShop\Exception $e ) { |
||
72 | $item = $manager->create(); |
||
73 | } |
||
74 | |||
75 | $item = $item->setCode( $email )->setLabel( $email )->setPassword( $password )->setStatus( 1 ); |
||
76 | $item->getPaymentAddress()->setEmail( $email ); |
||
77 | |||
78 | $item = $manager->save( $this->addGroups( $context, $item ) ); |
||
79 | |||
80 | \Illuminate\Foundation\Auth\User::findOrFail( $item->getId() ) |
||
81 | ->forceFill( [ |
||
82 | 'siteid' => $this->option( 'super' ) ? '' : $item->getSiteId(), |
||
83 | 'superuser' => ( $this->option( 'super' ) ? 1 : 0 ), |
||
84 | 'email_verified_at' => now(), |
||
85 | ] )->save(); |
||
86 | } |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Adds the group to the given user |
||
91 | * |
||
92 | * @param \Aimeos\MShop\ContextIface $context Aimeos context object |
||
93 | * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
||
94 | * @return \Aimeos\MShop\Customer\Item\Iface Updated customer object |
||
95 | */ |
||
96 | protected function addGroups( \Aimeos\MShop\ContextIface $context, |
||
97 | \Aimeos\MShop\Customer\Item\Iface $user ) : \Aimeos\MShop\Customer\Item\Iface |
||
98 | { |
||
99 | if( $this->option( 'admin' ) ) { |
||
100 | $user = $this->addGroup( $context, $user, 'admin' ); |
||
101 | } |
||
102 | |||
103 | if( $this->option( 'editor' ) ) { |
||
104 | $user = $this->addGroup( $context, $user, 'editor' ); |
||
105 | } |
||
106 | |||
107 | if( $this->option( 'api' ) ) { |
||
108 | $user = $this->addGroup( $context, $user, 'api' ); |
||
109 | } |
||
110 | |||
111 | return $user; |
||
112 | } |
||
113 | |||
114 | |||
115 | /** |
||
116 | * Adds the group to the given user |
||
117 | * |
||
118 | * @param \Aimeos\MShop\ContextIface $context Aimeos context object |
||
119 | * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
||
120 | * @param string $group Unique customer group code |
||
121 | */ |
||
122 | protected function addGroup( \Aimeos\MShop\ContextIface $context, \Aimeos\MShop\Customer\Item\Iface $user, |
||
123 | string $group ) : \Aimeos\MShop\Customer\Item\Iface |
||
124 | { |
||
125 | $msg = 'Add "%1$s" group to user "%2$s" for site "%3$s"'; |
||
126 | $site = $this->argument( 'site' ) ?: config( 'shop.mshop.locale.site', 'default' ); |
||
127 | $this->info( sprintf( $msg, $group, $user->getCode(), $site ) ); |
||
128 | |||
129 | $item = $this->getGroupItem( $context, $group ); |
||
130 | return $user->setGroups( $user->getGroups() + [$item->getId() => $item->getCode()] ); |
||
131 | } |
||
132 | |||
133 | |||
134 | /** |
||
135 | * Returns the customer group item for the given code |
||
136 | * |
||
137 | * @param \Aimeos\MShop\ContextIface $context Aimeos context object |
||
138 | * @param string $code Unique customer group code |
||
139 | * @return \Aimeos\MShop\Group\Item\Iface Aimeos customer group item object |
||
140 | */ |
||
141 | protected function getGroupItem( \Aimeos\MShop\ContextIface $context, string $code ) : \Aimeos\MShop\Group\Item\Iface |
||
142 | { |
||
143 | $manager = \Aimeos\MShop::create( $context, 'group' ); |
||
144 | |||
145 | try |
||
146 | { |
||
147 | $item = $manager->find( $code ); |
||
148 | } |
||
149 | catch( \Aimeos\MShop\Exception $e ) |
||
150 | { |
||
151 | $item = $manager->create(); |
||
152 | $item->setLabel( $code ); |
||
153 | $item->setCode( $code ); |
||
154 | |||
155 | $manager->save( $item ); |
||
156 | } |
||
157 | |||
158 | return $item; |
||
159 | } |
||
160 | } |
||
161 |