Issues (34)

src/Command/AccountCommand.php (2 issues)

Labels
Severity
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package symfony
7
 * @subpackage Command
8
 */
9
10
11
namespace Aimeos\ShopBundle\Command;
12
13
use Symfony\Component\Console\Attribute\AsCommand;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\Question;
19
use Symfony\Component\DependencyInjection\Container;
20
21
22
/**
23
 * Creates new accounts or resets their passwords
24
 * @package symfony
25
 * @subpackage Command
26
 */
27
#[AsCommand(name: 'aimeos:account', description: 'Creates new (admin) accounts')]
28
class AccountCommand extends Command
29
{
30
	private $container;
31
	protected static $defaultName = 'aimeos:account';
32
33
34
	public function __construct( Container $container )
35
	{
36
		parent::__construct();
37
		$this->container = $container;
38
	}
39
40
41
	/**
42
	 * Configures the command name and description.
43
	 */
44
	protected function configure()
45
	{
46
		$this->setName( self::$defaultName );
47
		$this->setDescription( 'Creates new (admin) accounts' );
48
		$this->addArgument( 'email', InputArgument::REQUIRED, 'E-mail address of the account that should be created' );
49
		$this->addArgument( 'site', InputArgument::OPTIONAL, 'Site codes to create accounts for like "default"', 'default' );
50
		$this->addOption( 'password', null, InputOption::VALUE_REQUIRED, 'Optional password for the account (will ask for if not given)' );
51
		$this->addOption( 'super', null, InputOption::VALUE_NONE, 'If account should have super user privileges' );
52
		$this->addOption( 'admin', null, InputOption::VALUE_NONE, 'If account should have administrator privileges' );
53
		$this->addOption( 'editor', null, InputOption::VALUE_NONE, 'If account should have limited editor privileges' );
54
	}
55
56
57
	/**
58
	 * Execute the console command.
59
	 *
60
	 * @param InputInterface $input Input object
61
	 * @param OutputInterface $output Output object
62
	 */
63
	protected function execute( InputInterface $input, OutputInterface $output )
64
	{
65
		$email = $input->getArgument( 'email' );
66
67
		if( ( $password = $input->getOption( 'password' ) ) === null )
68
		{
69
			$helper = $this->getHelper( 'question' );
70
			$question = new Question( 'Password' );
71
			$question->setHidden( true );
72
73
			$password = $helper->ask( $input, $output, $question );
74
		}
75
76
		$context = $this->container->get( 'aimeos.context' )->get( false, 'command' );
77
		$context->setEditor( 'aimeos:account' );
78
79
		$localeManager = \Aimeos\MShop::create( $context, 'locale' );
80
		$localeItem = $localeManager->bootstrap( $input->getArgument( 'site' ) ?: 'default', '', '', 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 said class. However, the method does not exist in Aimeos\MShop\Common\Manager\Decorator\Iface or Aimeos\MShop\Order\Manag...rvice\Transaction\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Order\Manager\Service\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Coupon\Manager\Code\Iface or Aimeos\MShop\Product\Manager\Iface or Aimeos\MShop\Supplier\Manager\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Locale\Manager\Currency\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Iface or Aimeos\MShop\Coupon\Manager\Iface or Aimeos\MShop\Attribute\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Address\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Product\Attribute\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Customer\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Order\Manager\Coupon\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Group\Manager\Iface or Aimeos\MShop\Catalog\Manager\Iface or Aimeos\MShop\Locale\Manager\Site\Iface or Aimeos\MShop\Order\Manager\Service\Attribute\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MShop\Common\Manager\Type\Iface or Aimeos\MAdmin\Job\Manager\Iface or Aimeos\MShop\Order\Manager\Status\Iface or Aimeos\MShop\Rule\Manager\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Stock\Manager\Iface or Aimeos\MShop\Attribute\Manager\Property\Iface or Aimeos\MShop\Subscription\Manager\Iface or Aimeos\MShop\Product\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Language\Iface or Aimeos\MShop\Media\Manager\Property\Iface or Aimeos\MShop\Service\Manager\Iface or Aimeos\MAdmin\Log\Manager\Iface or Aimeos\MAdmin\Cache\Manager\Iface or Aimeos\MShop\Order\Manager\Basket\Iface or Aimeos\MShop\Order\Manager\Product\Iface or Aimeos\MShop\Price\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Lists\Iface or Aimeos\MShop\Service\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Lists\Type\Iface or Aimeos\MShop\Media\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Property\Type\Iface or Aimeos\MShop\Supplier\Manager\Lists\Type\Iface or Aimeos\MShop\Tag\Manager\Type\Iface or Aimeos\MShop\Price\Manager\Type\Iface or Aimeos\MShop\Media\Manager\Lists\Type\Iface or Aimeos\MShop\Attribute\Manager\Property\Type\Iface or Aimeos\MShop\Service\Manager\Type\Iface or Aimeos\MShop\Customer\Manager\Property\Type\Iface or Aimeos\MShop\Rule\Manager\Type\Iface or Aimeos\MShop\Customer\Manager\Lists\Type\Iface or Aimeos\MShop\Product\Manager\Property\Type\Iface or Aimeos\MShop\Product\Manager\Type\Iface or Aimeos\MShop\Stock\Manager\Type\Iface or Aimeos\MShop\Product\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Lists\Type\Iface or Aimeos\MShop\Text\Manager\Type\Iface or Aimeos\MShop\Plugin\Manager\Type\Iface or Aimeos\MShop\Media\Manager\Property\Type\Iface or Aimeos\MShop\Attribute\Manager\Lists\Type\Iface or Aimeos\MShop\Catalog\Manager\Lists\Type\Iface or Aimeos\MShop\Supplier\Manager\Address\Iface or Aimeos\MShop\Customer\Manager\Address\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

80
		/** @scrutinizer ignore-call */ 
81
  $localeItem = $localeManager->bootstrap( $input->getArgument( 'site' ) ?: 'default', '', '', false );
Loading history...
81
		$context->setLocale( $localeItem );
82
83
		$manager = \Aimeos\MShop::create( $context, 'customer' );
84
85
		try {
86
			$item = $manager->find( $email, ['group'] );
0 ignored issues
show
The method find() does not exist on Aimeos\MShop\Common\Manager\Iface. It seems like you code against a sub-type of said class. However, the method does not exist in Aimeos\MShop\Common\Manager\Decorator\Iface or Aimeos\MShop\Order\Manag...rvice\Transaction\Iface or Aimeos\MShop\Price\Manager\Iface or Aimeos\MShop\Order\Manager\Service\Iface or Aimeos\MShop\Review\Manager\Iface or Aimeos\MShop\Common\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Property\Iface or Aimeos\MShop\Price\Manager\Lists\Iface or Aimeos\MShop\Service\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Lists\Iface or Aimeos\MShop\Locale\Manager\Currency\Iface or Aimeos\MShop\Catalog\Manager\Lists\Iface or Aimeos\MShop\Tag\Manager\Iface or Aimeos\MShop\Coupon\Manager\Iface or Aimeos\MShop\Common\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Address\Iface or Aimeos\MShop\Product\Manager\Lists\Iface or Aimeos\MShop\Order\Manager\Product\Attribute\Iface or Aimeos\MShop\Order\Manager\Iface or Aimeos\MShop\Media\Manager\Iface or Aimeos\MShop\Order\Manager\Coupon\Iface or Aimeos\MShop\Attribute\Manager\Lists\Iface or Aimeos\MShop\Media\Manager\Lists\Iface or Aimeos\MShop\Plugin\Manager\Iface or Aimeos\MShop\Order\Manager\Service\Attribute\Iface or Aimeos\MShop\Supplier\Manager\Lists\Iface or Aimeos\MShop\Text\Manager\Iface or Aimeos\MAdmin\Job\Manager\Iface or Aimeos\MShop\Order\Manager\Status\Iface or Aimeos\MShop\Rule\Manager\Iface or Aimeos\MShop\Common\Manager\Address\Iface or Aimeos\MShop\Stock\Manager\Iface or Aimeos\MShop\Attribute\Manager\Property\Iface or Aimeos\MShop\Subscription\Manager\Iface or Aimeos\MShop\Product\Manager\Property\Iface or Aimeos\MShop\Locale\Manager\Language\Iface or Aimeos\MShop\Media\Manager\Property\Iface or Aimeos\MAdmin\Log\Manager\Iface or Aimeos\MShop\Locale\Manager\Iface or Aimeos\MAdmin\Cache\Manager\Iface or Aimeos\MShop\Order\Manager\Basket\Iface or Aimeos\MShop\Order\Manager\Product\Iface or Aimeos\MShop\Price\Manager\Property\Iface or Aimeos\MShop\Customer\Manager\Lists\Iface or Aimeos\MShop\Supplier\Manager\Address\Iface or Aimeos\MShop\Customer\Manager\Address\Iface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

86
			/** @scrutinizer ignore-call */ 
87
   $item = $manager->find( $email, ['group'] );
Loading history...
87
		} catch( \Aimeos\MShop\Exception $e ) {
88
			$item = $manager->create();
89
		}
90
91
		$item = $item->setCode( $email )->setLabel( $email )->setPassword( $password )->setStatus( 1 );
92
		$item->getPaymentAddress()->setEmail( $email );
93
94
		$manager->save( $this->addGroups( $input, $output, $context, $item ) );
95
		$this->addRoles( $input, $email );
96
97
		return 0;
98
	}
99
100
101
	/**
102
	 * Adds the group to the given user
103
	 *
104
	 * @param InputInterface $input Input object
105
	 * @param OutputInterface $output Output object
106
	 * @param \Aimeos\MShop\ContextIface $context Aimeos context object
107
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
108
	 */
109
	protected function addGroups( InputInterface $input, OutputInterface $output,
110
		\Aimeos\MShop\ContextIface $context, \Aimeos\MShop\Customer\Item\Iface $user ) : \Aimeos\MShop\Customer\Item\Iface
111
	{
112
		if( $input->getOption( 'admin' ) ) {
113
			$this->addGroup( $input, $output, $context, $user, 'admin' );
114
		}
115
116
		if( $input->getOption( 'editor' ) ) {
117
			$this->addGroup( $input, $output, $context, $user, 'editor' );
118
		}
119
120
		return $user;
121
	}
122
123
124
	/**
125
	 * Adds the group to the given user
126
	 *
127
	 * @param InputInterface $input Input object
128
	 * @param OutputInterface $output Output object
129
	 * @param \Aimeos\MShop\ContextIface $context Aimeos context object
130
	 * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object
131
	 * @param string $group Unique customer group code
132
	 */
133
	protected function addGroup( InputInterface $input, OutputInterface $output, \Aimeos\MShop\ContextIface $context,
134
		\Aimeos\MShop\Customer\Item\Iface $user, string $group ) : \Aimeos\MShop\Customer\Item\Iface
135
	{
136
		$site = $context->locale()->getSiteItem()->getCode();
137
		$output->writeln( sprintf( 'Add "%1$s" group to user "%2$s" for site "%3$s"', $group, $user->getCode(), $site ) );
138
139
		$item = $this->getGroupItem( $context, $group );
140
		return $user->setGroups( $user->getGroups() + [$item->getId() => $item->getCode()] );
141
	}
142
143
144
	/**
145
	 * Adds required roles to user identified by his e-mail
146
	 *
147
	 * @param InputInterface $input Input object
148
	 * @param string $email Unique e-mail address
149
	 */
150
	protected function addRoles( InputInterface $input, string $email )
151
	{
152
		if( $this->container->has( 'fos_user.user_manager' ) )
153
		{
154
			$userManager = $this->container->get( 'fos_user.user_manager' );
155
156
			if( ( $fosUser = $userManager->findUserByUsername( $email ) ) === null ) {
157
				throw new \RuntimeException( 'No user created' );
158
			}
159
160
			$fosUser->setSuperAdmin( false );
161
162
			if( $input->getOption( 'super' ) ) {
163
				$fosUser->setSuperAdmin( true );
164
			}
165
166
			if( $input->getOption( 'admin' ) || $input->getOption( 'editor' ) ) {
167
				$fosUser->addRole( 'ROLE_ADMIN' );
168
			}
169
170
			$userManager->updateUser( $fosUser );
171
		}
172
	}
173
174
175
	/**
176
	 * Returns the customer group item for the given code
177
	 *
178
	 * @param \Aimeos\MShop\ContextIface $context Aimeos context object
179
	 * @param string $code Unique customer group code
180
	 * @return \Aimeos\MShop\Group\Item\Iface Aimeos customer group item object
181
	 */
182
	protected function getGroupItem( \Aimeos\MShop\ContextIface $context, string $code ) : \Aimeos\MShop\Group\Item\Iface
183
	{
184
		$manager = \Aimeos\MShop::create( $context, 'group' );
185
186
		try
187
		{
188
			$item = $manager->find( $code );
189
		}
190
		catch( \Aimeos\MShop\Exception $e )
191
		{
192
			$item = $manager->create();
193
			$item->setLabel( $code );
194
			$item->setCode( $code );
195
196
			$manager->save( $item );
197
		}
198
199
		return $item;
200
	}
201
}
202