Commit fa495b9f authored by Roman Vašek's avatar Roman Vašek

initial commit

parent 99e37ee2
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Command;
use FOS\UserBundle\Util\UserManipulator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
/**
* @author Antoine Hérault <antoine.herault@gmail.com>
*/
class ActivateUserCommand extends Command
{
protected static $defaultName = 'fos:user:activate';
private $userManipulator;
public function __construct(UserManipulator $userManipulator)
{
parent::__construct();
$this->userManipulator = $userManipulator;
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('fos:user:activate')
->setDescription('Activate a user')
->setDefinition(array(
new InputArgument('username', InputArgument::REQUIRED, 'The username'),
))
->setHelp(<<<'EOT'
The <info>fos:user:activate</info> command activates a user (so they will be able to log in):
<info>php %command.full_name% matthieu</info>
EOT
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$username = $input->getArgument('username');
$this->userManipulator->activate($username);
$output->writeln(sprintf('User "%s" has been activated.', $username));
return 0;
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('username')) {
$question = new Question('Please choose a username:');
$question->setValidator(function ($username) {
if (empty($username)) {
throw new \Exception('Username can not be empty');
}
return $username;
});
$answer = $this->getHelper('question')->ask($input, $output, $question);
$input->setArgument('username', $answer);
}
}
}
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Command;
use FOS\UserBundle\Util\UserManipulator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
class ChangePasswordCommand extends Command
{
protected static $defaultName = 'fos:user:change-password';
private $userManipulator;
public function __construct(UserManipulator $userManipulator)
{
parent::__construct();
$this->userManipulator = $userManipulator;
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('fos:user:change-password')
->setDescription('Change the password of a user.')
->setDefinition(array(
new InputArgument('username', InputArgument::REQUIRED, 'The username'),
new InputArgument('password', InputArgument::REQUIRED, 'The password'),
))
->setHelp(<<<'EOT'
The <info>fos:user:change-password</info> command changes the password of a user:
<info>php %command.full_name% matthieu</info>
This interactive shell will first ask you for a password.
You can alternatively specify the password as a second argument:
<info>php %command.full_name% matthieu mypassword</info>
EOT
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$username = $input->getArgument('username');
$password = $input->getArgument('password');
$this->userManipulator->changePassword($username, $password);
$output->writeln(sprintf('Changed password for user <comment>%s</comment>', $username));
return 0;
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$questions = array();
if (!$input->getArgument('username')) {
$question = new Question('Please give the username:');
$question->setValidator(function ($username) {
if (empty($username)) {
throw new \Exception('Username can not be empty');
}
return $username;
});
$questions['username'] = $question;
}
if (!$input->getArgument('password')) {
$question = new Question('Please enter the new password:');
$question->setValidator(function ($password) {
if (empty($password)) {
throw new \Exception('Password can not be empty');
}
return $password;
});
$question->setHidden(true);
$questions['password'] = $question;
}
foreach ($questions as $name => $question) {
$answer = $this->getHelper('question')->ask($input, $output, $question);
$input->setArgument($name, $answer);
}
}
}
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Command;
use FOS\UserBundle\Util\UserManipulator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
/**
* @author Matthieu Bontemps <matthieu@knplabs.com>
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
* @author Luis Cordova <cordoval@gmail.com>
*/
class CreateUserCommand extends Command
{
protected static $defaultName = 'fos:user:create';
private $userManipulator;
public function __construct(UserManipulator $userManipulator)
{
parent::__construct();
$this->userManipulator = $userManipulator;
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('fos:user:create')
->setDescription('Create a user.')
->setDefinition(array(
new InputArgument('username', InputArgument::REQUIRED, 'The username'),
new InputArgument('email', InputArgument::REQUIRED, 'The email'),
new InputArgument('password', InputArgument::REQUIRED, 'The password'),
new InputOption('super-admin', null, InputOption::VALUE_NONE, 'Set the user as super admin'),
new InputOption('inactive', null, InputOption::VALUE_NONE, 'Set the user as inactive'),
))
->setHelp(<<<'EOT'
The <info>fos:user:create</info> command creates a user:
<info>php %command.full_name% matthieu</info>
This interactive shell will ask you for an email and then a password.
You can alternatively specify the email and password as the second and third arguments:
<info>php %command.full_name% matthieu matthieu@example.com mypassword</info>
You can create a super admin via the super-admin flag:
<info>php %command.full_name% admin --super-admin</info>
You can create an inactive user (will not be able to log in):
<info>php %command.full_name% thibault --inactive</info>
EOT
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$username = $input->getArgument('username');
$email = $input->getArgument('email');
$password = $input->getArgument('password');
$inactive = $input->getOption('inactive');
$superadmin = $input->getOption('super-admin');
$this->userManipulator->create($username, $password, $email, !$inactive, $superadmin);
$output->writeln(sprintf('Created user <comment>%s</comment>', $username));
return 0;
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$questions = array();
if (!$input->getArgument('username')) {
$question = new Question('Please choose a username:');
$question->setValidator(function ($username) {
if (empty($username)) {
throw new \Exception('Username can not be empty');
}
return $username;
});
$questions['username'] = $question;
}
if (!$input->getArgument('email')) {
$question = new Question('Please choose an email:');
$question->setValidator(function ($email) {
if (empty($email)) {
throw new \Exception('Email can not be empty');
}
return $email;
});
$questions['email'] = $question;
}
if (!$input->getArgument('password')) {
$question = new Question('Please choose a password:');
$question->setValidator(function ($password) {
if (empty($password)) {
throw new \Exception('Password can not be empty');
}
return $password;
});
$question->setHidden(true);
$questions['password'] = $question;
}
foreach ($questions as $name => $question) {
$answer = $this->getHelper('question')->ask($input, $output, $question);
$input->setArgument($name, $answer);
}
}
}
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Command;
use FOS\UserBundle\Util\UserManipulator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
/**
* @author Antoine Hérault <antoine.herault@gmail.com>
*/
class DeactivateUserCommand extends Command
{
protected static $defaultName = 'fos:user:deactivate';
private $userManipulator;
public function __construct(UserManipulator $userManipulator)
{
parent::__construct();
$this->userManipulator = $userManipulator;
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('fos:user:deactivate')
->setDescription('Deactivate a user')
->setDefinition(array(
new InputArgument('username', InputArgument::REQUIRED, 'The username'),
))
->setHelp(<<<'EOT'
The <info>fos:user:deactivate</info> command deactivates a user (will not be able to log in)
<info>php %command.full_name% matthieu</info>
EOT
);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$username = $input->getArgument('username');
$this->userManipulator->deactivate($username);
$output->writeln(sprintf('User "%s" has been deactivated.', $username));
return 0;
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getArgument('username')) {
$question = new Question('Please choose a username:');
$question->setValidator(function ($username) {
if (empty($username)) {
throw new \Exception('Username can not be empty');
}
return $username;
});
$answer = $this->getHelper('question')->ask($input, $output, $question);
$input->setArgument('username', $answer);
}
}
}
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Command;
use FOS\UserBundle\Util\UserManipulator;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @author Antoine Hérault <antoine.herault@gmail.com>
* @author Lenar Lõhmus <lenar@city.ee>
*/
class DemoteUserCommand extends RoleCommand
{
protected static $defaultName = 'fos:user:demote';
/**
* {@inheritdoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('fos:user:demote')
->setDescription('Demote a user by removing a role')
->setHelp(<<<'EOT'
The <info>fos:user:demote</info> command demotes a user by removing a role
<info>php %command.full_name% matthieu ROLE_CUSTOM</info>
<info>php %command.full_name% --super matthieu</info>
EOT
);
}
/**
* {@inheritdoc}
*/
protected function executeRoleCommand(UserManipulator $manipulator, OutputInterface $output, $username, $super, $role)
{
if ($super) {
$manipulator->demote($username);
$output->writeln(sprintf('User "%s" has been demoted as a simple user. This change will not apply until the user logs out and back in again.', $username));
} else {
if ($manipulator->removeRole($username, $role)) {
$output->writeln(sprintf('Role "%s" has been removed from user "%s". This change will not apply until the user logs out and back in again.', $role, $username));
} else {
$output->writeln(sprintf('User "%s" didn\'t have "%s" role.', $username, $role));
}
}
}
}
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Command;
use FOS\UserBundle\Util\UserManipulator;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @author Matthieu Bontemps <matthieu@knplabs.com>
* @author Thibault Duplessis <thibault.duplessis@gmail.com>
* @author Luis Cordova <cordoval@gmail.com>
* @author Lenar Lõhmus <lenar@city.ee>
*/
class PromoteUserCommand extends RoleCommand
{
protected static $defaultName = 'fos:user:promote';
/**
* {@inheritdoc}
*/
protected function configure()
{
parent::configure();
$this
->setName('fos:user:promote')
->setDescription('Promotes a user by adding a role')
->setHelp(<<<'EOT'
The <info>fos:user:promote</info> command promotes a user by adding a role
<info>php %command.full_name% matthieu ROLE_CUSTOM</info>
<info>php %command.full_name% --super matthieu</info>
EOT
);
}
/**
* {@inheritdoc}
*/
protected function executeRoleCommand(UserManipulator $manipulator, OutputInterface $output, $username, $super, $role)
{
if ($super) {
$manipulator->promote($username);
$output->writeln(sprintf('User "%s" has been promoted as a super administrator. This change will not apply until the user logs out and back in again.', $username));
} else {
if ($manipulator->addRole($username, $role)) {
$output->writeln(sprintf('Role "%s" has been added to user "%s". This change will not apply until the user logs out and back in again.', $role, $username));
} else {
$output->writeln(sprintf('User "%s" did already have "%s" role.', $username, $role));
}
}
}
}
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Command;
use FOS\UserBundle\Util\UserManipulator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
/**
* @author Lenar Lõhmus <lenar@city.ee>
*/
abstract class RoleCommand extends Command
{
private $userManipulator;
public function __construct(UserManipulator $userManipulator)
{
parent::__construct();
$this->userManipulator = $userManipulator;
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setDefinition(array(
new InputArgument('username', InputArgument::REQUIRED, 'The username'),
new InputArgument('role', InputArgument::OPTIONAL, 'The role'),
new InputOption('super', null, InputOption::VALUE_NONE, 'Instead specifying role, use this to quickly add the super administrator role'),
));
}