| @@ 10-81 (lines=72) @@ | ||
| 7 | use Symfony\Component\Console\Input\InputInterface; |
|
| 8 | use Symfony\Component\Console\Output\OutputInterface; |
|
| 9 | ||
| 10 | class UserGroupCommand extends ContainerAwareCommand { |
|
| 11 | ||
| 12 | protected function configure() { |
|
| 13 | $this |
|
| 14 | ->setName('auth:user:roles') |
|
| 15 | ->setDescription('Add/Remove a user from a role') |
|
| 16 | ->addArgument('role', InputArgument::REQUIRED, 'Code of the role') |
|
| 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 | $rolecode = $input->getArgument('role'); |
|
| 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 role |
|
| 34 | $role = $this->getContainer()->get('doctrine') |
|
| 35 | ->getRepository('VivaitAuthBundle:Group') |
|
| 36 | ->findOneBy(array('role' => $rolecode)); |
|
| 37 | ||
| 38 | if(!$role) { |
|
| 39 | $output->writeln(sprintf("Error: Could not find role with code: %s", $role)); |
|
| 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 role list |
|
| 56 | if($user->getGroups()->contains($role)) { |
|
| 57 | $output->writeln(sprintf("Error: %s is already associated with tenant: %s", $user->getFullname(),$role->getName())); |
|
| 58 | return null; |
|
| 59 | } |
|
| 60 | $user->addGroup($role); |
|
| 61 | } else { |
|
| 62 | #check to see if already removed from role list |
|
| 63 | if(!$user->getGroups()->contains($role)) { |
|
| 64 | $output->writeln(sprintf("Error: %s not currently associated with tenant: %s", $user->getFullname(),$role->getName())); |
|
| 65 | return null; |
|
| 66 | } |
|
| 67 | $user->removeGroup($role); |
|
| 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 role %s", |
|
| 75 | $user->getFullname(), |
|
| 76 | strtolower($action) == 'add' ? 'added' : 'removed', |
|
| 77 | strtolower($action) == 'add' ? 'to' : 'from', |
|
| 78 | $role->getName() |
|
| 79 | )); |
|
| 80 | } |
|
| 81 | } |
|
| @@ 10-81 (lines=72) @@ | ||
| 7 | use Symfony\Component\Console\Input\InputInterface; |
|
| 8 | use Symfony\Component\Console\Output\OutputInterface; |
|
| 9 | ||
| 10 | 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 | } |
|