|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vivait\AuthBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
|
|
10
|
|
View Code Duplication |
class UserTenantCommand extends ContainerAwareCommand { |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
protected function configure() { |
|
13
|
|
|
$this |
|
14
|
|
|
->setName('auth:user:tenants') |
|
15
|
|
|
->setDescription('Add/Remove a user from a tenant') |
|
16
|
|
|
->addArgument('tenant', InputArgument::REQUIRED, 'Code of the Tenant') |
|
17
|
|
|
->addArgument('action', InputArgument::REQUIRED, '[Add|Remove]') |
|
18
|
|
|
->addArgument('username', InputArgument::REQUIRED, 'Username of the User?'); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
|
23
|
|
|
$tenantcode = $input->getArgument('tenant'); |
|
24
|
|
|
$action = $input->getArgument('action'); |
|
25
|
|
|
$username = $input->getArgument('username'); |
|
26
|
|
|
|
|
27
|
|
|
#check modifer |
|
28
|
|
|
if(!strtolower($action) == "add" && !strtolower($action) == "remove") { |
|
29
|
|
|
$output->writeln(sprintf("Error: Invalid action: %s, should be [Add|Remove]", $action)); |
|
30
|
|
|
return; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
#find tenant |
|
34
|
|
|
$tenant = $this->getContainer()->get('doctrine') |
|
35
|
|
|
->getRepository('VivaitAuthBundle:Tenant') |
|
36
|
|
|
->findOneBy(array('code' => $tenantcode)); |
|
37
|
|
|
|
|
38
|
|
|
if(!$tenant) { |
|
39
|
|
|
$output->writeln(sprintf("Error: Could not find tenant with code: %s", $tenantcode)); |
|
40
|
|
|
return null; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
#find user |
|
44
|
|
|
$user = $this->getContainer()->get('doctrine') |
|
45
|
|
|
->getRepository('VivaitAuthBundle:User') |
|
46
|
|
|
->findOneBy(array('username' => $username)); |
|
47
|
|
|
|
|
48
|
|
|
if(!$user) { |
|
49
|
|
|
$output->writeln(sprintf("Error: Could not find user with username: %s", $username)); |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
#should have now valid parameters |
|
54
|
|
|
if(strtolower($action) == 'add') { |
|
55
|
|
|
#check to see if already in tenant list |
|
56
|
|
|
if($user->getTenants()->contains($tenant)) { |
|
57
|
|
|
$output->writeln(sprintf("Error: %s is already associated with tenant: %s", $user->getFullname(),$tenant->getTenant())); |
|
58
|
|
|
return null; |
|
59
|
|
|
} |
|
60
|
|
|
$user->addTenant($tenant); |
|
61
|
|
|
} else { |
|
62
|
|
|
#check to see if already removed from tenant list |
|
63
|
|
|
if(!$user->getTenants()->contains($tenant)) { |
|
64
|
|
|
$output->writeln(sprintf("Error: %s not currently associated with tenant: %s", $user->getFullname(),$tenant->getTenant())); |
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
$user->removeTenant($tenant); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$em = $this->getContainer()->get('doctrine')->getManager(); |
|
71
|
|
|
$em->persist($user); |
|
72
|
|
|
$em->flush(); |
|
73
|
|
|
|
|
74
|
|
|
$output->writeln(sprintf("Success: %s has been %s %s %s", |
|
75
|
|
|
$user->getFullname(), |
|
76
|
|
|
strtolower($action) == 'add' ? 'associated' : 'removed', |
|
77
|
|
|
strtolower($action) == 'add' ? 'to' : 'from', |
|
78
|
|
|
$tenant->getTenant() |
|
79
|
|
|
)); |
|
80
|
|
|
} |
|
81
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.