diff --git a/composer.json b/composer.json index 6153bb0..459cf92 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,11 @@ "name": "Fabien Potencier", "email": "fabien.potencier@symfony-project.com", "role": "Creator" + }, + { + "name": "Emanuele Panzeri", + "email": "thepanz@gmail.com", + "role": "Contributor" } ], "require": { diff --git a/lib/task/sfGuardAddPermissionTask.class.php b/lib/task/sfGuardAddPermissionTask.class.php index 932c132..140f0b4 100644 --- a/lib/task/sfGuardAddPermissionTask.class.php +++ b/lib/task/sfGuardAddPermissionTask.class.php @@ -54,6 +54,8 @@ protected function execute($arguments = array(), $options = array()) $databaseManager = new sfDatabaseManager($this->configuration); $model = sfConfig::get('app_sf_guard_user_model', 'sfGuardUser'); + + /** @var sfGuardUser $user */ $user = Doctrine_Core::getTable($model)->findOneByUsername($arguments['username']); if (!$user) { diff --git a/lib/task/sfGuardGroupCreateTask.class.php b/lib/task/sfGuardGroupCreateTask.class.php new file mode 100644 index 0000000..ef39435 --- /dev/null +++ b/lib/task/sfGuardGroupCreateTask.class.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Create a new user. + * + * @package symfony + * @subpackage task + * @author Emanuele Panzeri + */ +class sfGuardGroupCreateTask extends sfBaseTask +{ + /** + * @see sfTask + */ + protected function configure() + { + $this->addArguments(array( + new sfCommandArgument('name', sfCommandArgument::REQUIRED, 'The group name'), + )); + + $this->addOptions(array( + new sfCommandOption('description', null, sfCommandOption::PARAMETER_REQUIRED, 'The Group description', null), + new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null), + new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), + )); + + $this->namespace = 'guard'; + $this->name = 'group:create'; + $this->briefDescription = 'Creates a Group'; + + $this->detailedDescription = <<configuration); + + $table = Doctrine_Core::getTable('sfGuardGroup'); + if ($ret = $table->findOneBy('name', $arguments['name'])) { + $this->logSection('guard', sprintf('Group "%s" already exists!', $arguments['name']), null, 'ERROR'); + return -1; + } + + $group = new sfGuardGroup(); + $group->setName($arguments['name']); + $group->setDescription($options['description']); + $group->save(); + + $this->logSection('guard', sprintf('Created group "%s"', $arguments['name'])); + } +} diff --git a/lib/task/sfGuardGroupDeleteTask.class.php b/lib/task/sfGuardGroupDeleteTask.class.php new file mode 100644 index 0000000..ed086d3 --- /dev/null +++ b/lib/task/sfGuardGroupDeleteTask.class.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Delete a sfGuard Group. + * + * @package symfony + * @subpackage task + * @author Emanuele Panzeri + */ +class sfGuardGroupDeleteTask extends sfBaseTask +{ + /** + * @see sfTask + */ + protected function configure() + { + $this->addArguments(array( + new sfCommandArgument('name', sfCommandArgument::REQUIRED, 'The group name'), + )); + + $this->addOptions(array( + new sfCommandOption('description', null, sfCommandOption::PARAMETER_REQUIRED, 'The Group description', null), + new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null), + new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), + )); + + $this->namespace = 'guard'; + $this->name = 'group:delete'; + $this->briefDescription = 'Deletes a Group'; + + $this->detailedDescription = <<configuration); + + $table = Doctrine_Core::getTable('sfGuardGroup'); + $entity = $table->findOneBy('name', $arguments['name']); + if (!$entity) { + $this->logSection('guard', sprintf('Group "%s" does exists!', $arguments['name']), null, 'ERROR'); + return -1; + } + + if (!$entity->delete()) { + $this->logSection('guard', sprintf('Group "%s" was not deleted', $arguments['name']), 'ERROR'); + return -1; + } + + $this->logSection('guard', sprintf('Group "%s" deleted', $arguments['name'])); + } +} diff --git a/lib/task/sfGuardGroupPermissionAddTask.class.php b/lib/task/sfGuardGroupPermissionAddTask.class.php new file mode 100644 index 0000000..5a3a653 --- /dev/null +++ b/lib/task/sfGuardGroupPermissionAddTask.class.php @@ -0,0 +1,86 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Associates a sfGuard group with a permission. + * + * @package symfony + * @subpackage task + * @author Emanuele Panzeri + */ +class sfGuardGroupPermissionAddTask extends sfBaseTask +{ + /** + * @see sfTask + */ + protected function configure() + { + $this->addArguments(array( + new sfCommandArgument('group', sfCommandArgument::REQUIRED, 'The group name'), + new sfCommandArgument('permission', sfCommandArgument::REQUIRED, 'The permission name'), + )); + + $this->addOptions(array( + new sfCommandOption('description', null, sfCommandOption::PARAMETER_REQUIRED, 'The Group description', null), + new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null), + new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), + )); + + $this->namespace = 'guard'; + $this->name = 'group:permission-add'; + $this->briefDescription = 'Associated a Group with a Permission'; + + $this->detailedDescription = <<configuration); + + /** @var sfGuardGroup $group */ + $group = Doctrine_Core::getTable('sfGuardGroup')->findOneByName($arguments['group']); + if (!$group) { + $this->logSection('guard', sprintf('Group "%s" not found!', $arguments['group']), null, 'ERROR'); + return -1; + } + + /** @var sfGuardPermission $permission */ + $permission = Doctrine_Core::getTable('sfGuardPermission')->findOneByName($arguments['permission']); + if (!$permission) { + $this->logSection('guard', sprintf('Permission "%s" not found!', $arguments['permission']), null, 'ERROR'); + return -1; + } + + /** @var sfGuardGroupPermission $groupPermission */ + $groupPermission = Doctrine_Core::getTable('sfGuardGroupPermission')->find(array( + $group->getPrimaryKey(), + $permission->getPrimaryKey(), + )); + if ($groupPermission) { + $this->logSection('guard', 'Group Permission already exists', null, 'ERROR'); + return -1; + } + + $groupPermission = new sfGuardGroupPermission(); + $groupPermission + ->setGroup($group) + ->setPermission($permission); + $groupPermission->save(); + + $this->logSection('guard', sprintf('Associated group "%s" with permission "%s".', $group->getName(), $permission->getName())); + } +} diff --git a/lib/task/sfGuardGroupPermissionRemoveTask.class.php b/lib/task/sfGuardGroupPermissionRemoveTask.class.php new file mode 100644 index 0000000..565e8ac --- /dev/null +++ b/lib/task/sfGuardGroupPermissionRemoveTask.class.php @@ -0,0 +1,86 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Associates a sfGuard group with a permission. + * + * @package symfony + * @subpackage task + * @author Emanuele Panzeri + */ +class sfGuardGroupPermissionRemoveTask extends sfBaseTask +{ + /** + * @see sfTask + */ + protected function configure() + { + $this->addArguments(array( + new sfCommandArgument('group', sfCommandArgument::REQUIRED, 'The group name'), + new sfCommandArgument('permission', sfCommandArgument::REQUIRED, 'The permission name'), + )); + + $this->addOptions(array( + new sfCommandOption('description', null, sfCommandOption::PARAMETER_REQUIRED, 'The Group description', null), + new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null), + new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), + )); + + $this->namespace = 'guard'; + $this->name = 'group:permission-remove'; + $this->briefDescription = 'Removes a permission from a group'; + + $this->detailedDescription = <<configuration); + + /** @var sfGuardGroup $group */ + $group = Doctrine_Core::getTable('sfGuardGroup')->findOneByName($arguments['group']); + if (!$group) { + $this->logSection('guard', sprintf('Group "%s" not found!', $arguments['group']), null, 'ERROR'); + return -1; + } + + /** @var sfGuardPermission $permission */ + $permission = Doctrine_Core::getTable('sfGuardPermission')->findOneByName($arguments['permission']); + if (!$permission) { + $this->logSection('guard', sprintf('Permission "%s" not found!', $arguments['permission']), null, 'ERROR'); + return -1; + } + + /** @var sfGuardGroupPermission $entity */ + $entity = Doctrine_Core::getTable('sfGuardGroupPermission')->find(array( + $group->getPrimaryKey(), + $permission->getPrimaryKey(), + )); + if (!$entity) { + $this->logSection('guard', 'Group Permission does not exists', null, 'ERROR'); + return -1; + } + + + if (!$entity->delete()) { + $this->logSection('guard', sprintf('Can not remove permission "%s" from group "%s"', $group->getName(), $permission->getName()), 'ERROR'); + return -1; + } + + $this->logSection('guard', sprintf('Permission "%s" removed from group "%s"', $group->getName(), $permission->getName())); + } +} diff --git a/lib/task/sfGuardPermissionCreateTask.class.php b/lib/task/sfGuardPermissionCreateTask.class.php new file mode 100644 index 0000000..64cfb5d --- /dev/null +++ b/lib/task/sfGuardPermissionCreateTask.class.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Create a new sfGuard permission. + * + * @package symfony + * @subpackage task + * @author Emanuele Panzeri + */ +class sfGuardPermissionCreateTask extends sfBaseTask +{ + /** + * @see sfTask + */ + protected function configure() + { + $this->addArguments(array( + new sfCommandArgument('name', sfCommandArgument::REQUIRED, 'The permission name'), + )); + + $this->addOptions(array( + new sfCommandOption('description', null, sfCommandOption::PARAMETER_REQUIRED, 'The Permission description', null), + new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null), + new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), + )); + + $this->namespace = 'guard'; + $this->name = 'permission:create'; + $this->briefDescription = 'Creates a Permission'; + + $this->detailedDescription = <<configuration); + + $table = Doctrine_Core::getTable('sfGuardPermission'); + if ($ret = $table->findOneBy('name', $arguments['name'])) { + $this->logSection('guard', sprintf('Permission "%s" already exists!', $arguments['name']), null, 'ERROR'); + return -1; + } + + $permission = new sfGuardPermission(); + + $permission->setName($arguments['name']); + $permission->setDescription($options['description']); + $permission->save(); + + $this->logSection('guard', sprintf('Created Permission "%s"', $arguments['name'])); + } +} diff --git a/lib/task/sfGuardPermissionDeleteTask.class.php b/lib/task/sfGuardPermissionDeleteTask.class.php new file mode 100644 index 0000000..4799414 --- /dev/null +++ b/lib/task/sfGuardPermissionDeleteTask.class.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Create a new Permission. + * + * @package symfony + * @subpackage task + * @author Emanuele Panzeri + */ +class sfGuardPermissionDeleteTask extends sfBaseTask +{ + /** + * @see sfTask + */ + protected function configure() + { + $this->addArguments(array( + new sfCommandArgument('name', sfCommandArgument::REQUIRED, 'The permission name to delete'), + )); + + $this->addOptions(array( + new sfCommandOption('description', null, sfCommandOption::PARAMETER_REQUIRED, 'The Permission description', null), + new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null), + new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), + )); + + $this->namespace = 'guard'; + $this->name = 'permission:delete'; + $this->briefDescription = 'Delete a Permission'; + + $this->detailedDescription = <<configuration); + + $table = Doctrine_Core::getTable('sfGuardPermission'); + $entity = $table->findOneBy('name', $arguments['name']); + if (!$entity) { + $this->logSection('guard', sprintf('Permission "%s" does not exists!', $arguments['name']), null, 'ERROR'); + return -1; + } + + if (!$entity->delete()) { + $this->logSection('guard', sprintf('Permission "%s" was not deleted', $arguments['name']), 'ERROR'); + return -1; + } + + $this->logSection('guard', sprintf('Permission "%s" deleted', $arguments['name'])); + } +} diff --git a/lib/task/sfGuardUserGroupAddTask.class.php b/lib/task/sfGuardUserGroupAddTask.class.php new file mode 100644 index 0000000..1b367b2 --- /dev/null +++ b/lib/task/sfGuardUserGroupAddTask.class.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Adds a sfGuard group to a user. + * + * @package symfony + * @subpackage task + * @author Emanuele Panzeri + */ +class sfGuardUserGroupAddTask extends sfBaseTask +{ + /** + * @see sfTask + */ + protected function configure() + { + $this->addArguments(array( + new sfCommandArgument('username', sfCommandArgument::REQUIRED, 'The user name'), + new sfCommandArgument('group', sfCommandArgument::REQUIRED, 'The group name'), + )); + + $this->addOptions(array( + new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null), + new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), + )); + + $this->namespace = 'guard'; + $this->name = 'user:group-add'; + $this->briefDescription = 'Adds a group to a user'; + + $this->detailedDescription = <<configuration); + + $model = sfConfig::get('app_sf_guard_user_model', 'sfGuardUser'); + + /** @var sfGuardUser $user */ + $user = Doctrine_Core::getTable($model)->findOneByUsername($arguments['username']); + if (!$user) + { + throw new sfCommandException(sprintf('User "%s" does not exist.', $arguments['username'])); + } + + $user->addGroupByName($arguments['group']); + + $this->logSection('guard', sprintf('Add group %s to user %s', $arguments['group'], $arguments['username'])); + } +} diff --git a/lib/task/sfGuardUserGroupRemoveTask.class.php b/lib/task/sfGuardUserGroupRemoveTask.class.php new file mode 100644 index 0000000..5dc08a1 --- /dev/null +++ b/lib/task/sfGuardUserGroupRemoveTask.class.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Removes a sfGuard group to a user. + * + * @package symfony + * @subpackage task + * @author Emanuele Panzeri + */ +class sfGuardUserGroupRemoveTask extends sfBaseTask +{ + /** + * @see sfTask + */ + protected function configure() + { + $this->addArguments(array( + new sfCommandArgument('username', sfCommandArgument::REQUIRED, 'The user name'), + new sfCommandArgument('group', sfCommandArgument::REQUIRED, 'The group name'), + )); + + $this->addOptions(array( + new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null), + new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), + )); + + $this->namespace = 'guard'; + $this->name = 'user:group-remove'; + $this->briefDescription = 'Removes a group from a user'; + + $this->detailedDescription = <<configuration); + + $model = sfConfig::get('app_sf_guard_user_model', 'sfGuardUser'); + + /** @var sfGuardUser $user */ + $user = Doctrine_Core::getTable($model)->findOneByUsername($arguments['username']); + if (!$user) + { + throw new sfCommandException(sprintf('User "%s" does not exist.', $arguments['username'])); + } + + $user->removeUserGroupByGroupName($arguments['group']); + + $this->logSection('guard', sprintf('Remove group %s from to user %s', $arguments['group'], $arguments['username'])); + } +} diff --git a/lib/task/sfGuardUserUserPermissionAddTask.class.php b/lib/task/sfGuardUserUserPermissionAddTask.class.php new file mode 100644 index 0000000..87cde2b --- /dev/null +++ b/lib/task/sfGuardUserUserPermissionAddTask.class.php @@ -0,0 +1,69 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * Add a permission to a user. + * + * @package symfony + * @subpackage task + * @author Fabien Potencier + * @version SVN: $Id$ + */ +class sfGuardUserPermissionAddTask extends sfBaseTask +{ + /** + * @see sfTask + */ + protected function configure() + { + $this->addArguments(array( + new sfCommandArgument('username', sfCommandArgument::REQUIRED, 'The user name'), + new sfCommandArgument('permission', sfCommandArgument::REQUIRED, 'The permission name'), + )); + + $this->addOptions(array( + new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null), + new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'), + )); + + $this->namespace = 'guard'; + $this->name = 'user:permission-add'; + $this->briefDescription = 'Adds a permission to a user'; + + $this->detailedDescription = <<configuration); + + $model = sfConfig::get('app_sf_guard_user_model', 'sfGuardUser'); + + /** @var sfGuardUser $user */ + $user = Doctrine_Core::getTable($model)->findOneByUsername($arguments['username']); + if (!$user) + { + throw new sfCommandException(sprintf('User "%s" does not exist.', $arguments['username'])); + } + + $user->addPermissionByName($arguments['permission']); + + $this->logSection('guard', sprintf('Add permission %s to user %s', $arguments['permission'], $arguments['username'])); + } +}