Issues (67)

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

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 ) {
0 ignored issues
show
The condition $password = $this->option('password') === null is always false.
Loading history...
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->find( $email );
73
		} catch( \Aimeos\MShop\Exception $e ) {
74
			$item = $manager->create();
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 ) );
81
82
		\Illuminate\Foundation\Auth\User::findOrFail( $item->getId() )
83
			->forceFill( [
84
				'superuser' => ( $this->option( 'super' ) ? 1 : 0 ),
85
				'email_verified_at' => now(),
86
			] )->save();
87
	}
88
89
90
	/**
91
	 * Adds the group to the given user
92
	 *
93
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
94
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
95
	 * @return \Aimeos\MShop\Customer\Item\Iface Updated customer object
96
	 */
97
	protected function addGroups( \Aimeos\MShop\Context\Item\Iface $context,
98
		\Aimeos\MShop\Customer\Item\Iface $user ) : \Aimeos\MShop\Customer\Item\Iface
99
	{
100
		if( $this->option( 'admin' ) ) {
101
			$user = $this->addGroup( $context, $user, 'admin' );
102
		}
103
104
		if( $this->option( 'editor' ) ) {
105
			$user = $this->addGroup( $context, $user, 'editor' );
106
		}
107
108
		if( $this->option( 'api' ) ) {
109
			$user = $this->addGroup( $context, $user, 'api' );
110
		}
111
112
		return $user;
113
	}
114
115
116
	/**
117
	 * Adds the group to the given user
118
	 *
119
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
120
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
121
	 * @param string $group Unique customer group code
122
	 */
123
	protected function addGroup( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user,
124
		string $group ) : \Aimeos\MShop\Customer\Item\Iface
125
	{
126
		$msg = 'Add "%1$s" group to user "%2$s" for site "%3$s"';
127
		$this->info( sprintf( $msg, $group, $user->getCode(), $this->argument( 'site' ) ) );
128
129
		$groupId = $this->getGroupItem( $context, $group )->getId();
130
		return $user->setGroups( array_merge( $user->getGroups(), [$groupId] ) );
131
	}
132
133
134
	/**
135
	 * Returns the customer group item for the given code
136
	 *
137
	 * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object
138
	 * @param string $code Unique customer group code
139
	 * @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object
140
	 */
141
	protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, string $code ) : \Aimeos\MShop\Customer\Item\Group\Iface
142
	{
143
		$manager = \Aimeos\MShop::create( $context, 'customer/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->saveItem( $item );
156
		}
157
158
		return $item;
159
	}
160
}
161