-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: new command to move a user's home folder - occ user:move-home #39136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Enhancement: Add console command to move a user's home folder | ||
|
|
||
| occ user:move-home <user_id> <new_location> | ||
|
|
||
| https://github.com/owncloud/core/pull/39136 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| <?php | ||
| /** | ||
| * @author Thomas Müller <thomas.mueller@tmit.eu> | ||
| * | ||
| * @copyright Copyright (c) 2021, ownCloud GmbH | ||
| * @license AGPL-3.0 | ||
| * | ||
| * This code is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License, version 3, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License, version 3, | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/> | ||
| * | ||
| */ | ||
|
|
||
| namespace OC\Core\Command\User; | ||
|
|
||
| use InvalidArgumentException; | ||
| use OC\User\AccountMapper; | ||
| use OCP\IUser; | ||
| use OCP\IUserManager; | ||
| use RuntimeException; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\InputArgument; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
|
|
||
| class MoveHome extends Command { | ||
|
|
||
| /** | ||
| * @var IUserManager | ||
| */ | ||
| private $userManager; | ||
|
|
||
| public function __construct( | ||
| IUserManager $userManager | ||
| ) { | ||
| parent::__construct(); | ||
| $this->userManager = $userManager; | ||
| } | ||
|
|
||
| protected function configure() { | ||
| $this->setName('user:move-home') | ||
| ->setDescription('Move a user\'s home folder to a new location.') | ||
| ->addArgument('user_id', InputArgument::REQUIRED, 'Id of the user whose home folder is to be moved. ') | ||
| ->addArgument('new_location', InputArgument::REQUIRED, 'Absolute path to the parent folder of the new location of the home folder.') | ||
| ; | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output) { | ||
| $user = $this->getUser($input); | ||
| $userId = $user->getUID(); | ||
| $oldHome = $user->getHome(); | ||
|
|
||
| $newLocation = $this->getNewLocationForUser($input, $user); | ||
| $output->writeln("Move $userId from $oldHome to $newLocation"); | ||
|
|
||
| # disable user | ||
| $output->writeln("Disabling user ...."); | ||
| $user->setEnabled(false); | ||
|
|
||
| # copy files | ||
| $output->writeln("Syncing files from $oldHome/ to $newLocation ..."); | ||
| $this->rsync($oldHome, $newLocation); | ||
|
|
||
| # set new home folder | ||
| $output->writeln("Updating user home path ...."); | ||
| $user->setHome($newLocation); | ||
|
|
||
| # enable user | ||
| $output->writeln("Enabling user ...."); | ||
| $user->setEnabled(true); | ||
|
|
||
| # notify admin about necessary LDAP change | ||
| $output->writeln(''); | ||
| $output->writeln("Files for user $userId have been moved to $newLocation"); | ||
| $output->writeln("In case you are using LDAP Home Folder please update the property in your LDAP directory for this user"); | ||
| $output->writeln(''); | ||
|
|
||
| $output->writeln(''); | ||
| $output->writeln("The old home folder can now be deleted (rm -rf $oldHome)"); | ||
| $output->writeln(''); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| protected function getUser(InputInterface $input): IUser { | ||
| $userId = $input->getArgument('user_id'); | ||
| $user = $this->userManager->get($userId); | ||
| if ($user === null) { | ||
| throw new InvalidArgumentException('User is not known.'); | ||
| } | ||
|
|
||
| return $user; | ||
| } | ||
|
|
||
| protected function getNewLocationForUser(InputInterface $input, IUser $user): string { | ||
| $newLocation = $input->getArgument('new_location'); | ||
| if (!is_dir($newLocation)) { | ||
| throw new InvalidArgumentException('New root location has to exist'); | ||
| } | ||
|
|
||
| $oldHome = $user->getHome(); | ||
| if (!is_dir($oldHome)) { | ||
| throw new InvalidArgumentException("Current user home $oldHome does not exist. Not mounted? Non local storage?"); | ||
| } | ||
| $userFolder = basename($oldHome); | ||
| $newLocation .= "/$userFolder"; | ||
|
|
||
| mkdir($newLocation); | ||
| if ($this->is_dir_empty($newLocation) !== true) { | ||
| throw new InvalidArgumentException("New user folder $newLocation is either not readable or not empty"); | ||
| } | ||
|
|
||
| return realpath($newLocation); | ||
| } | ||
|
|
||
| private function is_dir_empty($dir): ?bool { | ||
| if (!is_readable($dir)) { | ||
| return null; | ||
| } | ||
| return (\count(scandir($dir)) === 2); | ||
| } | ||
|
|
||
| private function rsync(string $oldHome, string $newLocation): void { | ||
| exec("rsync -aAX $oldHome/ $newLocation", $output, $return_var); | ||
| if ($return_var !== 0) { | ||
| throw new RuntimeException("Copying files failed: \n $output"); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <?php | ||
| /** | ||
| * @author Thomas Müller <thomas.mueller@tmit.eu> | ||
| * | ||
| * @copyright Copyright (c) 2021, ownCloud GmbH | ||
| * @license AGPL-3.0 | ||
| * | ||
| * This code is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License, version 3, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License, version 3, | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/> | ||
| * | ||
| */ | ||
|
|
||
| namespace Tests\Core\Command\User; | ||
|
|
||
| use OC\Core\Command\User\MoveHome; | ||
| use OC\Files\ObjectStore\ObjectStoreStorage; | ||
| use OCP\IUser; | ||
| use Symfony\Component\Console\Tester\CommandTester; | ||
| use Test\TestCase; | ||
|
|
||
| /** | ||
| * Class MoveHomeTest | ||
| * | ||
| * @group DB | ||
| * @package Tests\Core\Command\User | ||
| */ | ||
| class MoveHomeTest extends TestCase { | ||
| /** | ||
| * @var CommandTester | ||
| */ | ||
| private $commandTester; | ||
| /** | ||
| * @var string | ||
| */ | ||
| private $newLocation; | ||
| /** | ||
| * @var bool|IUser | ||
| */ | ||
| private $user; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| $command = new MoveHome(\OC::$server->getUserManager()); | ||
| $this->commandTester = new CommandTester($command); | ||
|
|
||
| # create a new location in the data folder | ||
| $newLocation = \OC::$server->getSystemConfig()->getValue('datadirectory'); | ||
| $this->newLocation = uniqid($newLocation . '/'); | ||
| mkdir($this->newLocation); | ||
|
|
||
| # test user | ||
| $this->user = \OC::$server->getUserManager()->createUser('test-user', 'test-user'); | ||
| } | ||
|
|
||
| protected function tearDown(): void { | ||
| $this->user->delete(); | ||
| parent::tearDown(); | ||
|
|
||
| rmdir($this->newLocation); | ||
| } | ||
|
|
||
| public function test(): void { | ||
| \OC::$server->getUserSession()->login('test-user', 'test-user'); | ||
| $file = \OC::$server->getUserFolder($this->user->getUID())->newFile('hello.txt'); | ||
| $file->putContent('1234567890'); | ||
|
|
||
| if ($file->getStorage()->instanceOfStorage(ObjectStoreStorage::class)) { | ||
| $this->markTestSkipped('user:move-home is only implemented for posix file systems'); | ||
| } | ||
|
|
||
| $exitCode = $this->commandTester->execute([ | ||
| 'user_id' => 'test-user', | ||
| 'new_location' => $this->newLocation | ||
| ]); | ||
|
|
||
| # assert command output | ||
| self::assertEquals(0, $exitCode, $this->commandTester->getDisplay()); | ||
|
|
||
| # assert that the new location holds the file | ||
| self::assertFileExists("{$this->newLocation}/test-user/files/hello.txt"); | ||
|
|
||
| # assert the user has a new home set | ||
| self::assertEquals("{$this->newLocation}/test-user", $this->user->getHome()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.