From e449a4529fc52987afb8f2170ac5f0c597ec3b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Huet?= Date: Sat, 13 Dec 2014 16:22:56 +0100 Subject: [PATCH] Adds a recursive parameter and allows for more chmod possibilities such as +a --- .../BuiltIn/Filesystem/PermissionsTask.php | 51 +++++++++++++++---- .../PermissionsWritableByApacheTask.php | 6 +-- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/Mage/Task/BuiltIn/Filesystem/PermissionsTask.php b/Mage/Task/BuiltIn/Filesystem/PermissionsTask.php index e04ef91..167f1d2 100644 --- a/Mage/Task/BuiltIn/Filesystem/PermissionsTask.php +++ b/Mage/Task/BuiltIn/Filesystem/PermissionsTask.php @@ -10,9 +10,9 @@ use Mage\Task\SkipException; * * Usage : * pre-deploy: - * - filesystem/permissions: {paths: /var/www/myapp/app/cache:/var/www/myapp/app/cache, checkPathsExist: true, owner: www-data, group: www-data, rights: 775} + * - filesystem/permissions: {paths: /var/www/myapp/app/cache:/var/www/myapp/app/cache, recursive: false, checkPathsExist: true, owner: www-data, group: www-data, rights: 775} * on-deploy: - * - filesystem/permissions: {paths: app/cache:app/logs, checkPathsExist: true, owner: www-data, group: www-data, rights: 775} + * - filesystem/permissions: {paths: app/cache:app/logs, recursive: false, checkPathsExist: true, owner: www-data, group: www-data, rights: 775} * * @author Jérémy Huet */ @@ -57,6 +57,13 @@ class PermissionsTask extends AbstractTask */ private $rights; + /** + * If set to true, will recursively change permissions on given paths. + * + * @var string + */ + private $recursive = false; + /** * Initialize parameters. * @@ -86,6 +93,10 @@ class PermissionsTask extends AbstractTask if (! is_null($this->getParameter('rights'))) { $this->setRights($this->getParameter('rights')); } + + if (! is_null($this->getParameter('recursive'))) { + $this->setRecursive($this->getParameter('recursive')); + } } /** @@ -102,17 +113,18 @@ class PermissionsTask extends AbstractTask public function run() { $command = ''; + $recursive = $this->recursive ? '-R' : ''; if ($this->paths && $this->owner) { - $command .= 'chown -R ' . $this->owner . ' ' . $this->getPathsForCmd() . ';'; + $command .= 'chown '. $recursive .' ' . $this->owner . ' ' . $this->getPathsForCmd() . ';'; } if ($this->paths && $this->group) { - $command .= 'chgrp -R ' . $this->group . ' ' . $this->getPathsForCmd() . ';'; + $command .= 'chgrp '. $recursive .' ' . $this->group . ' ' . $this->getPathsForCmd() . ';'; } if ($this->paths && $this->rights) { - $command .= 'chmod -R ' . $this->rights . ' ' . $this->getPathsForCmd() . ';'; + $command .= 'chmod '. $recursive .' ' . $this->rights . ' ' . $this->getPathsForCmd() . ';'; } $result = $this->runCommand($command); @@ -178,7 +190,7 @@ class PermissionsTask extends AbstractTask */ protected function setCheckPathsExist($checkPathsExist) { - $this->checkPathsExist = $checkPathsExist; + $this->checkPathsExist = (bool) $checkPathsExist; return $this; } @@ -240,17 +252,13 @@ class PermissionsTask extends AbstractTask /** * Set rights. * - * @todo better way to check if $rights is in a correct format. + * @todo check if $rights is in a correct format. * * @param string $rights * @return PermissionsTask */ protected function setRights($rights) { - if (strlen($rights) != 3 || !is_numeric($rights) || $rights > 777) { - throw new SkipException('Make sure the rights "' . $rights . '" are in a correct format.'); - } - $this->rights = $rights; return $this; @@ -263,4 +271,25 @@ class PermissionsTask extends AbstractTask { return $this->rights; } + + /** + * Set recursive. + * + * @param boolean $recursive + * @return PermissionsTask + */ + protected function setRecursive($recursive) + { + $this->recursive = (bool) $recursive; + + return $this; + } + + /** + * @return boolean + */ + protected function getRecursive() + { + return $this->recursive; + } } diff --git a/Mage/Task/BuiltIn/Filesystem/PermissionsWritableByApacheTask.php b/Mage/Task/BuiltIn/Filesystem/PermissionsWritableByApacheTask.php index 5bb3f7c..defd90e 100644 --- a/Mage/Task/BuiltIn/Filesystem/PermissionsWritableByApacheTask.php +++ b/Mage/Task/BuiltIn/Filesystem/PermissionsWritableByApacheTask.php @@ -6,9 +6,9 @@ namespace Mage\Task\BuiltIn\Filesystem; * * Usage : * pre-deploy: - * - filesystem/permissions-writable-by-apache: {paths: /var/www/myapp/app/cache:/var/www/myapp/app/cache, checkPathsExist: true} + * - filesystem/permissions-writable-by-apache: {paths: /var/www/myapp/app/cache:/var/www/myapp/app/cache, recursive: false, checkPathsExist: true} * on-deploy: - * - filesystem/permissions-writable-by-apache: {paths: app/cache:app/logs, checkPathsExist: true} + * - filesystem/permissions-writable-by-apache: {paths: app/cache:app/logs, recursive: false, checkPathsExist: true} * * @author Jérémy Huet */ @@ -19,7 +19,7 @@ class PermissionsWritableByApacheTask extends PermissionsTask parent::init(); $this->setGroup('www-data') - ->setRights('775'); + ->setRights('g+w'); } /**