Passed
Push — master ( 6c6334...d4aa98 )
by Aimeos
06:24
created

src/Aimeos/Shop/Command/AccountCommand.php (1 issue)

Labels
Severity
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
The method bootstrap() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of Aimeos\MShop\Common\Manager\Iface such as Aimeos\MShop\Locale\Manager\Iface or Aimeos\MShop\Common\Manager\Decorator\Base or Aimeos\MShop\Service\Manager\Lists\Type\Standard or Aimeos\MShop\Price\Manager\Standard or Aimeos\MShop\Attribute\Manager\Type\Standard or Aimeos\MShop\Price\Manager\Lists\Type\Standard or Aimeos\MShop\Media\Manager\Type\Standard or Aimeos\MShop\Coupon\Manager\Code\Standard or Aimeos\MShop\Order\Manager\Base\Coupon\Standard or Aimeos\MShop\Product\Manager\Standard or Aimeos\MShop\Index\Manager\Standard or Aimeos\MShop\Index\Manager\Attribute\Standard or Aimeos\MShop\Index\Manager\Text\Standard or Aimeos\MShop\Index\Manager\Supplier\Standard or Aimeos\MShop\Index\Manager\Catalog\Standard or Aimeos\MShop\Index\Manager\Price\Standard or Aimeos\MShop\Supplier\Manager\Standard or Aimeos\MShop\Customer\Manager\Property\Standard or Aimeos\MShop\Order\Manager\Base\Service\Standard or Aimeos\MShop\Order\Manager\Base\Standard or Aimeos\MShop\Price\Manager\Lists\Standard or Aimeos\MShop\Supplier\Manager\Lists\Type\Standard or Aimeos\MShop\Order\Manag...vice\Attribute\Standard or Aimeos\MShop\Service\Manager\Lists\Standard or Aimeos\MShop\Tag\Manager\Type\Standard or Aimeos\MShop\Text\Manager\Lists\Standard or Aimeos\MShop\Price\Manager\Type\Standard or Aimeos\MShop\Locale\Manager\Currency\Standard or Aimeos\MShop\Order\Manag...duct\Attribute\Standard or Aimeos\MShop\Media\Manager\Lists\Type\Standard or Aimeos\MShop\Catalog\Manager\Lists\Standard or Aimeos\MShop\Tag\Manager\Standard or Aimeos\MShop\Coupon\Manager\Standard or Aimeos\MShop\Attribute\Manager\Standard or Aimeos\MShop\Attribute\M...\Property\Type\Standard or Aimeos\MShop\Service\Manager\Type\Standard or Aimeos\MShop\Product\Manager\Lists\Standard or Aimeos\MShop\Customer\Ma...\Property\Type\Standard or Aimeos\MShop\Order\Manager\Standard or Aimeos\MShop\Customer\Manager\Standard or Aimeos\MShop\Media\Manager\Standard or Aimeos\MShop\Customer\Manager\Lists\Type\Standard or Aimeos\MShop\Attribute\Manager\Lists\Standard or Aimeos\MShop\Product\Man...\Property\Type\Standard or Aimeos\MShop\Media\Manager\Lists\Standard or Aimeos\MShop\Plugin\Manager\Standard or Aimeos\MShop\Order\Manager\Base\Address\Standard or Aimeos\MShop\Catalog\Manager\Standard or Aimeos\MShop\Locale\Manager\Site\Standard or Aimeos\MShop\Product\Manager\Type\Standard or Aimeos\MShop\Supplier\Manager\Lists\Standard or Aimeos\MShop\Stock\Manager\Type\Standard or Aimeos\MShop\Text\Manager\Standard or Aimeos\MAdmin\Job\Manager\Standard or Aimeos\MShop\Customer\Manager\Group\Standard or Aimeos\MShop\Product\Manager\Lists\Type\Standard or Aimeos\MShop\Text\Manager\Lists\Type\Standard or Aimeos\MShop\Text\Manager\Type\Standard or Aimeos\MShop\Order\Manager\Status\Standard or Aimeos\MShop\Supplier\Manager\Address\Standard or Aimeos\MShop\Customer\Manager\Address\Standard or Aimeos\MShop\Plugin\Manager\Type\Standard or Aimeos\MShop\Stock\Manager\Standard or Aimeos\MShop\Stock\Manager\Nolimit or Aimeos\MShop\Attribute\Manager\Property\Standard or Aimeos\MShop\Subscription\Manager\Standard or Aimeos\MShop\Media\Manager\Property\Type\Standard or Aimeos\MShop\Product\Manager\Property\Standard or Aimeos\MShop\Locale\Manager\Language\Standard or Aimeos\MShop\Media\Manager\Property\Standard or Aimeos\MShop\Service\Manager\Standard or Aimeos\MShop\Attribute\Manager\Lists\Type\Standard or Aimeos\MAdmin\Log\Manager\Standard or Aimeos\MAdmin\Cache\Manager\Standard or Aimeos\MAdmin\Cache\Manager\None or Aimeos\MShop\Order\Manager\Base\Product\Standard or Aimeos\MShop\Customer\Manager\Lists\Standard or Aimeos\MShop\Catalog\Manager\Lists\Type\Standard. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
		/** @scrutinizer ignore-call */ 
67
  $localeItem = $localeManager->bootstrap( $this->argument( 'site' ), '', '', false );
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