vendor/bundles/FOS/UserBundle/Command/RoleCommand.php
author cavaliet
Thu, 20 Oct 2011 18:35:34 +0200
changeset 14 fc78844c8a76
parent 3 e54dfe4d0b2b
permissions -rwxr-xr-x
debug addtag for document
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
/*
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 * This file is part of the FOSUserBundle package.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 *
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 *
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * For the full copyright and license information, please view the LICENSE
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * file that was distributed with this source code.
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
namespace FOS\UserBundle\Command;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
use Symfony\Component\Console\Input\InputArgument;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
use Symfony\Component\Console\Input\InputOption;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
use Symfony\Component\Console\Input\InputInterface;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
use Symfony\Component\Console\Output\OutputInterface;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
use FOS\UserBundle\Model\User;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
use FOS\UserBundle\Util\UserManipulator;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
/**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 * @author Lenar Lõhmus <lenar@city.ee>
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
abstract class RoleCommand extends ContainerAwareCommand
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
{
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
     * @see Command
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    protected function configure()
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        $this
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
            ->setDefinition(array(
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
                new InputArgument('username', InputArgument::REQUIRED, 'The username'),
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
                new InputArgument('role', InputArgument::OPTIONAL, 'The role'),
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
                new InputOption('super', null, InputOption::VALUE_NONE, 'Instead specifying role, use this to quickly add the super administrator role'),
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
            ));
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
     * @see Command
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
    protected function execute(InputInterface $input, OutputInterface $output)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        $username = $input->getArgument('username');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        $role = $input->getArgument('role');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        $super = (true === $input->getOption('super'));
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        if (null !== $role && $super) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
            throw new \InvalidArgumentException('You can pass either the role or the --super option (but not both simultaneously).');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        if (null === $role && !$super) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            throw new \RuntimeException('Not enough arguments.');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        $manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        $this->executeRoleCommand($manipulator, $output, $username, $super, $role);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
     * @see Command
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    abstract protected function executeRoleCommand(UserManipulator $manipulator, OutputInterface $output, $username, $super, $role);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    /**
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
     * @see Command
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
     */
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
    protected function interact(InputInterface $input, OutputInterface $output)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
    {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        if (!$input->getArgument('username')) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
            $username = $this->getHelper('dialog')->askAndValidate(
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
                $output,
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
                'Please choose a username:',
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
                function($username)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
                {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
                    if (empty($username)) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
                        throw new \Exception('Username can not be empty');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
                    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
                    return $username;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
                }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
            );
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
            $input->setArgument('username', $username);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
        if ((true !== $input->getOption('super')) && !$input->getArgument('role')) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
            $role = $this->getHelper('dialog')->askAndValidate(
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
                $output,
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
                'Please choose a role:',
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
                function($role)
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
                {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
                    if (empty($role)) {
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
                        throw new \Exception('Role can not be empty');
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
                    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
                    return $role;
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
                }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
            );
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
            $input->setArgument('role', $role);
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
    }
e54dfe4d0b2b add FOSUserBundle
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
}