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( ( $email = $this->argument( 'email' ) ) === null ) { |
||
55 | $email = $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 ); |
||
67 | $context->setLocale( $localeItem ); |
||
68 | |||
69 | $manager = \Aimeos\MShop::create( $context, 'customer' ); |
||
70 | |||
71 | try { |
||
72 | $item = $manager->findItem( $email ); |
||
73 | } catch( \Aimeos\MShop\Exception $e ) { |
||
74 | $item = $manager->createItem(); |
||
75 | } |
||
76 | |||
77 | $item = $item->setCode( $email )->setLabel( $email )->setPassword( $password )->setStatus( 1 ); |
||
78 | $item->getPaymentAddress()->setEmail( $email ); |
||
79 | |||
80 | $item = $manager->saveItem( $this->addGroups( $context, $item ) ); |
||
0 ignored issues
–
show
|
|||
81 | |||
82 | \Illuminate\Foundation\Auth\User::findOrFail( $item->getId() ) |
||
83 | ->forceFill( ['superuser' => ( $this->option( 'super' ) ? 1 : 0 )] ) |
||
84 | ->save(); |
||
85 | } |
||
86 | |||
87 | |||
88 | /** |
||
89 | * Adds the group to the given user |
||
90 | * |
||
91 | * @param \Aimeos\MShop\Context\Item\Iface $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\Context\Item\Iface $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 | if( $this->option( 'api' ) ) { |
||
107 | $user = $this->addGroup( $context, $user, 'api' ); |
||
108 | } |
||
109 | |||
110 | return $user; |
||
111 | } |
||
112 | |||
113 | |||
114 | /** |
||
115 | * Adds the group to the given user |
||
116 | * |
||
117 | * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
||
118 | * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
||
119 | * @param string $group Unique customer group code |
||
120 | */ |
||
121 | protected function addGroup( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user, |
||
122 | string $group ) : \Aimeos\MShop\Customer\Item\Iface |
||
123 | { |
||
124 | $msg = 'Add "%1$s" group to user "%2$s" for site "%3$s"'; |
||
125 | $this->info( sprintf( $msg, $group, $user->getCode(), $this->argument( 'site' ) ) ); |
||
126 | |||
127 | $groupId = $this->getGroupItem( $context, $group )->getId(); |
||
128 | return $user->setGroups( array_merge( $user->getGroups(), [$groupId] ) ); |
||
129 | } |
||
130 | |||
131 | |||
132 | /** |
||
133 | * Returns the customer group item for the given code |
||
134 | * |
||
135 | * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
||
136 | * @param string $code Unique customer group code |
||
137 | * @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object |
||
138 | */ |
||
139 | protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, string $code ) : \Aimeos\MShop\Customer\Item\Group\Iface |
||
140 | { |
||
141 | $manager = \Aimeos\MShop::create( $context, 'customer/group' ); |
||
142 | |||
143 | try |
||
144 | { |
||
145 | $item = $manager->findItem( $code ); |
||
146 | } |
||
147 | catch( \Aimeos\MShop\Exception $e ) |
||
148 | { |
||
149 | $item = $manager->createItem(); |
||
150 | $item->setLabel( $code ); |
||
151 | $item->setCode( $code ); |
||
152 | |||
153 | $manager->saveItem( $item ); |
||
154 | } |
||
155 | |||
156 | return $item; |
||
157 | } |
||
158 | } |
||
159 |
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.